Skip to content
This repository has been archived by the owner on Jan 31, 2025. It is now read-only.

Commit

Permalink
Merge pull request #26 from josx/add_type_in_urlGetter
Browse files Browse the repository at this point in the history
Adding action type to customUrlFn
  • Loading branch information
JiriChara authored Nov 5, 2018
2 parents 9ad8471 + 6f4e967 commit 25f6dfd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ import createCrudModule from 'vuex-crud';

export default createCrudModule({
resource: 'pages',
customUrlFn(id, bookId) {
customUrlFn(id, type, bookId) {
// id will only be available when doing request to single resource, otherwise null
// type is the actions you are dispatching: FETCH_LIST, FETCH_SINGLE, CREATE, UPDATE, REPLACE, DESTROY
const rootUrl = `/api/books/${bookId}`;
return id ? `rootUrl/${id}` : rootUrl;
}
Expand Down
62 changes: 47 additions & 15 deletions src/vuex-crud/createActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@ const createActions = ({
parseSingle,
parseError
}) => {
const [
FETCH_LIST,
FETCH_SINGLE,
CREATE,
UPDATE,
REPLACE,
DESTROY
] = ['FETCH_LIST', 'FETCH_SINGLE', 'CREATE', 'UPDATE', 'REPLACE', 'DESTROY'];
const crudActions = {};
const isUsingCustomUrlGetter = typeof rootUrl === 'function';

const urlGetter = ({ customUrl, customUrlFnArgs, id }) => {
const urlGetter = ({
customUrl,
customUrlFnArgs,
id,
type
}) => {
if (typeof customUrl === 'string') {
return customUrl;
} else if (isUsingCustomUrlGetter) {
const argsArray = Array.isArray(customUrlFnArgs) ? customUrlFnArgs : [customUrlFnArgs];
const args = [id || null].concat(argsArray);

const args = [id || null, type || null].concat(argsArray);
return rootUrl(...args);
}

return id ? `${rootUrl}/${id}` : rootUrl;
};

if (only.includes('FETCH_LIST')) {
if (only.includes(FETCH_LIST)) {
Object.assign(crudActions, {
/**
* GET /api/<resourceName>
Expand All @@ -33,7 +45,7 @@ const createActions = ({
fetchList({ commit }, { config, customUrl, customUrlFnArgs = [] } = {}) {
commit('fetchListStart');

return client.get(urlGetter({ customUrl, customUrlFnArgs }), config)
return client.get(urlGetter({ customUrl, customUrlFnArgs, type: FETCH_LIST }), config)
.then((res) => {
const parsedResponse = parseList(res);

Expand All @@ -52,7 +64,7 @@ const createActions = ({
});
}

if (only.includes('FETCH_SINGLE')) {
if (only.includes(FETCH_SINGLE)) {
Object.assign(crudActions, {
/**
* GET /api/<resourceName>/:id
Expand All @@ -67,7 +79,12 @@ const createActions = ({
} = {}) {
commit('fetchSingleStart');

return client.get(urlGetter({ id, customUrl, customUrlFnArgs }), config)
return client.get(urlGetter({
id,
customUrl,
customUrlFnArgs,
type: FETCH_SINGLE
}), config)
.then((res) => {
const parsedResponse = parseSingle(res);

Expand All @@ -86,7 +103,7 @@ const createActions = ({
});
}

if (only.includes('CREATE')) {
if (only.includes(CREATE)) {
Object.assign(crudActions, {
/**
* POST /api/<resourceName>
Expand All @@ -101,7 +118,7 @@ const createActions = ({
} = {}) {
commit('createStart');

return client.post(urlGetter({ customUrl, customUrlFnArgs }), data, config)
return client.post(urlGetter({ customUrl, customUrlFnArgs, type: CREATE }), data, config)
.then((res) => {
const parsedResponse = parseSingle(res);

Expand All @@ -120,7 +137,7 @@ const createActions = ({
});
}

if (only.includes('UPDATE')) {
if (only.includes(UPDATE)) {
Object.assign(crudActions, {
/**
* PATCH /api/<resouceName>/:id
Expand All @@ -136,7 +153,12 @@ const createActions = ({
} = {}) {
commit('updateStart');

return client.patch(urlGetter({ id, customUrl, customUrlFnArgs }), data, config)
return client.patch(urlGetter({
id,
customUrl,
customUrlFnArgs,
type: UPDATE
}), data, config)
.then((res) => {
const parsedResponse = parseSingle(res);

Expand All @@ -155,7 +177,7 @@ const createActions = ({
});
}

if (only.includes('REPLACE')) {
if (only.includes(REPLACE)) {
Object.assign(crudActions, {
/**
* PUT /api/<resouceName>/:id
Expand All @@ -171,7 +193,12 @@ const createActions = ({
} = {}) {
commit('replaceStart');

return client.put(urlGetter({ id, customUrl, customUrlFnArgs }), data, config)
return client.put(urlGetter({
id,
customUrl,
customUrlFnArgs,
type: REPLACE
}), data, config)
.then((res) => {
const parsedResponse = parseSingle(res);

Expand All @@ -190,7 +217,7 @@ const createActions = ({
});
}

if (only.includes('DESTROY')) {
if (only.includes(DESTROY)) {
Object.assign(crudActions, {
/**
* DELETE /api/<resouceName>/:id
Expand All @@ -205,7 +232,12 @@ const createActions = ({
} = {}) {
commit('destroyStart');

return client.delete(urlGetter({ id, customUrl, customUrlFnArgs }), config)
return client.delete(urlGetter({
id,
customUrl,
customUrlFnArgs,
type: DESTROY
}), config)
.then((res) => {
const parsedResponse = parseSingle(res);

Expand Down
24 changes: 12 additions & 12 deletions test/unit/vuex-crud/createActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test('fetch list supports customUrl', (t) => {

test('fetch list supports customUrlFnArgs', (t) => {
const { fetchList } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles`; },
only: ['FETCH_LIST'],
client,
parseList: res => res,
Expand All @@ -161,7 +161,7 @@ test('fetch list supports customUrlFnArgs', (t) => {

test('fetch list supports customUrlFnArgs as array', (t) => {
const { fetchList } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles`; },
only: ['FETCH_LIST'],
client,
parseList: res => res,
Expand Down Expand Up @@ -311,7 +311,7 @@ test('fetch single supports customUrl', (t) => {

test('fetch single supports customUrlFnArgs', (t) => {
const { fetchSingle } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['FETCH_SINGLE'],
client,
parseList: res => res,
Expand All @@ -334,7 +334,7 @@ test('fetch single supports customUrlFnArgs', (t) => {

test('fetch single supports customUrlFnArgs as array', (t) => {
const { fetchSingle } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['FETCH_SINGLE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -487,7 +487,7 @@ test('create supports customUrl', (t) => {

test('create supports customUrlFnArgs', (t) => {
const { create } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles`; },
only: ['CREATE'],
client,
parseList: res => res,
Expand All @@ -510,7 +510,7 @@ test('create supports customUrlFnArgs', (t) => {

test('create supports customUrlFnArgs as array', (t) => {
const { create } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles`; },
only: ['CREATE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -663,7 +663,7 @@ test('update supports customUrl', (t) => {

test('update supports customUrlFnArgs', (t) => {
const { update } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['UPDATE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -692,7 +692,7 @@ test('update supports customUrlFnArgs', (t) => {

test('update supports customUrlFnArgs as array', (t) => {
const { update } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['UPDATE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -851,7 +851,7 @@ test('replace supports customUrl', (t) => {

test('replace supports customUrlFnArgs', (t) => {
const { replace } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['REPLACE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -880,7 +880,7 @@ test('replace supports customUrlFnArgs', (t) => {

test('replace supports customUrlFnArgs as array', (t) => {
const { replace } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['REPLACE'],
client,
parseList: res => res,
Expand Down Expand Up @@ -1039,7 +1039,7 @@ test('destroy supports customUrl', (t) => {

test('destroy supports customUrlFnArgs', (t) => {
const { destroy } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['DESTROY'],
client,
parseList: res => res,
Expand All @@ -1066,7 +1066,7 @@ test('destroy supports customUrlFnArgs', (t) => {

test('destroy supports customUrlFnArgs as array', (t) => {
const { destroy } = createActions({
rootUrl(id, parentId) { return `/users/${parentId}/articles/${id}`; },
rootUrl(id, type, parentId) { return `/users/${parentId}/articles/${id}`; },
only: ['DESTROY'],
client,
parseList: res => res,
Expand Down

0 comments on commit 25f6dfd

Please # to comment.