Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
fix(executeFetch): revert forced methods (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcur committed Oct 22, 2020
1 parent 6927525 commit d4206b7
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default connectAsync({ loadDataAsProps })(Articles);

### Method Opts

Some REST APIs support bulk updates as opposed to individual resource updates. There are many thoughts on this matter but they generally suggest using a POST or a PUT for this type of CRUD action. For iguazu-rest, we take the opinion that by default, updating a collection should be a POST but provide the option to override the method. However, other CRUD actions' methods cannot be overriden (e.g. `loadCollection`).
Some REST APIs support bulk updates as opposed to individual resource updates. There are many thoughts on this matter but they generally suggest using a POST or a PUT for this type of CRUD action. For iguazu-rest, we take the opinion that by default, updating a collection should be a POST but provide the option to override the method.

```jsx
// Articles.jsx
Expand Down
86 changes: 0 additions & 86 deletions __tests__/actions/executeFetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,92 +130,6 @@ describe('executeFetch', () => {
const data = await thunk(dispatch, getState);
expect(data).toEqual([{ id: '123', name: 'joe' }]);
});

it('should use specified REST method for UPDATE_COLLECTION in options if provided', async () => {
Object.assign(config, {
defaultOpts: { default: 'opt' },
resources: {
users: {
fetch: () => ({
url: 'http://api.domain.com/users/:id',
opts: {
resource: 'opt',
},
}),
},
},
});
fetch.mockResponseOnce(
JSON.stringify([{ id: '123', name: 'joe' }, { id: '456', name: 'josephine' }]),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
const thunk = executeFetch({
resource, id, opts: { ...opts, method: 'PUT' }, actionType: 'UPDATE_COLLECTION',
});
const data = await thunk(dispatch, getState);
expect(data).toEqual([{ id: '123', name: 'joe' }, { id: '456', name: 'josephine' }]);
expect(fetch).toHaveBeenCalledWith(
'http://api.domain.com/users/123',
{
default: 'opt', method: 'PUT', resource: 'opt', some: 'opt',
}
);
const promise = Promise.resolve(data);
expect(dispatch).toHaveBeenCalledWith({
type: types.UPDATE_COLLECTION_STARTED,
resource,
id,
opts: {
...opts,
method: 'PUT',
},
promise,
});
expect(dispatch).toHaveBeenCalledWith('waitAndDispatchFinishedThunk');
});

it('should not use specified REST method for non overrideable actions in options if provided', async () => {
Object.assign(config, {
defaultOpts: { default: 'opt' },
resources: {
users: {
fetch: () => ({
url: 'http://api.domain.com/users/:id',
opts: {
resource: 'opt',
},
}),
},
},
});
fetch.mockResponseOnce(
JSON.stringify([{ id: '123', name: 'joe' }, { id: '456', name: 'josephine' }]),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
const thunk = executeFetch({
resource, id, opts: { ...opts, method: 'POST' }, actionType: 'LOAD',
});
const data = await thunk(dispatch, getState);
expect(data).toEqual([{ id: '123', name: 'joe' }, { id: '456', name: 'josephine' }]);
expect(fetch).toHaveBeenCalledWith(
'http://api.domain.com/users/123',
{
default: 'opt', method: 'GET', resource: 'opt', some: 'opt',
}
);
const promise = Promise.resolve(data);
expect(dispatch).toHaveBeenCalledWith({
type: types.LOAD_STARTED,
resource,
id,
opts: {
...opts,
method: 'POST',
},
promise,
});
expect(dispatch).toHaveBeenCalledWith('waitAndDispatchFinishedThunk');
});
});
describe('fetchClient', () => {
// Helpers and Mocks
Expand Down
3 changes: 0 additions & 3 deletions src/actions/executeFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const actionTypeMethodMap = {
PATCH: 'PATCH',
};

const overridableActionMethods = new Set(['UPDATE_COLLECTION']);

async function getAsyncData({
resource, id, opts, actionType, state, fetchClient,
}) {
Expand All @@ -64,7 +62,6 @@ async function getAsyncData({
defaultOpts || {},
resourceOpts || {},
opts || {},
{ ...!overridableActionMethods.has(actionType) && { method: actionTypeMethodMap[actionType] } },
]);
const fetchUrl = buildFetchUrl({ url, id, opts: fetchOpts });

Expand Down

0 comments on commit d4206b7

Please sign in to comment.