Skip to content

enhance: Transform updateParams to 'update' function #854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ Object {
"schema": [Function],
"throttle": false,
"type": "mutate",
"updaters": Object {
"GET http://test.com/article/": [Function],
"GET http://test.com/article/some-list-url": [Function],
},
"update": [Function],
},
"payload": [Function],
"type": "rest-hooks/fetch",
Expand Down Expand Up @@ -166,9 +163,7 @@ Object {
"schema": [Function],
"throttle": false,
"type": "mutate",
"updaters": Object {
"GET http://test.com/article-cooler/": [Function],
},
"update": [Function],
},
"payload": [Function],
"type": "rest-hooks/fetch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ Object {
"schema": [Function],
"throttle": false,
"type": "mutate",
"updaters": Object {
"GET http://test.com/article/": [Function],
"http://test.com/article/some-list-url": [Function],
},
"update": [Function],
},
"payload": [Function],
"type": "rest-hooks/fetch",
Expand Down Expand Up @@ -154,9 +151,7 @@ Object {
"schema": [Function],
"throttle": false,
"type": "mutate",
"updaters": Object {
"GET http://test.com/article-cooler/": [Function],
},
"update": [Function],
},
"payload": [Function],
"type": "rest-hooks/fetch",
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/state/__tests__/networkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,9 @@ describe('NetworkManager', () => {
type: RECEIVE_TYPE,
payload: data,
meta: {
updaters: {
[ArticleResource.listShape().getFetchKey({})]: expect.any(Function),
},
updaters: undefined,
args: fetchRpcWithUpdatersAction.meta.args,
update: fetchRpcWithUpdatersAction.meta.update,
update: expect.any(Function),
schema: fetchRpcWithUpdatersAction.meta.schema,
key: fetchRpcWithUpdatersAction.meta.key,
date: expect.any(Number),
Expand All @@ -190,11 +188,9 @@ describe('NetworkManager', () => {
type: RECEIVE_TYPE,
payload: data,
meta: {
updaters: {
[ArticleResource.listShape().getFetchKey({})]: expect.any(Function),
},
updaters: undefined,
args: fetchRpcWithUpdatersAndOptimisticAction.meta.args,
update: fetchRpcWithUpdatersAndOptimisticAction.meta.update,
update: expect.any(Function),
schema: fetchRpcWithUpdatersAndOptimisticAction.meta.schema,
key: fetchRpcWithUpdatersAndOptimisticAction.meta.key,
date: expect.any(Number),
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/state/actions/createFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,22 @@ export default function createFetch<
: /* istanbul ignore next */ new Date(),
};

if (updateParams) {
meta.updaters = updateParams.reduce(
(accumulator: object, [toShape, toParams, updateFn]) => ({
[toShape.getFetchKey(toParams)]: updateFn,
...accumulator,
}),
{},
);
}

if (fetchShape.update) {
meta.update = fetchShape.update;
}

// for simplicity we simply override if updateParams are defined - usage together is silly to support as we are migrating
if (updateParams) {
meta.update = (newresult: any): Record<string, (...args: any) => any> => {
const updateMap: any = {};
updateParams.forEach(([toShape, toParams, updateFn]) => {
updateMap[toShape.getFetchKey(toParams)] = (existing: any) =>
updateFn(newresult, existing);
});
return updateMap;
};
}

if (options && options.optimisticUpdate) {
meta.optimisticResponse = options.optimisticUpdate(params, body);
}
Expand Down