Skip to content

Commit 7ef3e2a

Browse files
authored
enhance: Transform updateParams to 'update' function (#854)
1 parent 2436f3b commit 7ef3e2a

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed

packages/core/src/react-integration/__tests__/__snapshots__/hooks-endpoint.web.tsx.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ Object {
131131
"schema": [Function],
132132
"throttle": false,
133133
"type": "mutate",
134-
"updaters": Object {
135-
"GET http://test.com/article/": [Function],
136-
"GET http://test.com/article/some-list-url": [Function],
137-
},
134+
"update": [Function],
138135
},
139136
"payload": [Function],
140137
"type": "rest-hooks/fetch",
@@ -166,9 +163,7 @@ Object {
166163
"schema": [Function],
167164
"throttle": false,
168165
"type": "mutate",
169-
"updaters": Object {
170-
"GET http://test.com/article-cooler/": [Function],
171-
},
166+
"update": [Function],
172167
},
173168
"payload": [Function],
174169
"type": "rest-hooks/fetch",

packages/core/src/react-integration/__tests__/__snapshots__/hooks.web.tsx.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ Object {
119119
"schema": [Function],
120120
"throttle": false,
121121
"type": "mutate",
122-
"updaters": Object {
123-
"GET http://test.com/article/": [Function],
124-
"http://test.com/article/some-list-url": [Function],
125-
},
122+
"update": [Function],
126123
},
127124
"payload": [Function],
128125
"type": "rest-hooks/fetch",
@@ -154,9 +151,7 @@ Object {
154151
"schema": [Function],
155152
"throttle": false,
156153
"type": "mutate",
157-
"updaters": Object {
158-
"GET http://test.com/article-cooler/": [Function],
159-
},
154+
"update": [Function],
160155
},
161156
"payload": [Function],
162157
"type": "rest-hooks/fetch",

packages/core/src/state/__tests__/networkManager.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,9 @@ describe('NetworkManager', () => {
163163
type: RECEIVE_TYPE,
164164
payload: data,
165165
meta: {
166-
updaters: {
167-
[ArticleResource.listShape().getFetchKey({})]: expect.any(Function),
168-
},
166+
updaters: undefined,
169167
args: fetchRpcWithUpdatersAction.meta.args,
170-
update: fetchRpcWithUpdatersAction.meta.update,
168+
update: expect.any(Function),
171169
schema: fetchRpcWithUpdatersAction.meta.schema,
172170
key: fetchRpcWithUpdatersAction.meta.key,
173171
date: expect.any(Number),
@@ -190,11 +188,9 @@ describe('NetworkManager', () => {
190188
type: RECEIVE_TYPE,
191189
payload: data,
192190
meta: {
193-
updaters: {
194-
[ArticleResource.listShape().getFetchKey({})]: expect.any(Function),
195-
},
191+
updaters: undefined,
196192
args: fetchRpcWithUpdatersAndOptimisticAction.meta.args,
197-
update: fetchRpcWithUpdatersAndOptimisticAction.meta.update,
193+
update: expect.any(Function),
198194
schema: fetchRpcWithUpdatersAndOptimisticAction.meta.schema,
199195
key: fetchRpcWithUpdatersAndOptimisticAction.meta.key,
200196
date: expect.any(Number),

packages/core/src/state/actions/createFetch.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,22 @@ export default function createFetch<
6868
: /* istanbul ignore next */ new Date(),
6969
};
7070

71-
if (updateParams) {
72-
meta.updaters = updateParams.reduce(
73-
(accumulator: object, [toShape, toParams, updateFn]) => ({
74-
[toShape.getFetchKey(toParams)]: updateFn,
75-
...accumulator,
76-
}),
77-
{},
78-
);
79-
}
80-
8171
if (fetchShape.update) {
8272
meta.update = fetchShape.update;
8373
}
8474

75+
// for simplicity we simply override if updateParams are defined - usage together is silly to support as we are migrating
76+
if (updateParams) {
77+
meta.update = (newresult: any): Record<string, (...args: any) => any> => {
78+
const updateMap: any = {};
79+
updateParams.forEach(([toShape, toParams, updateFn]) => {
80+
updateMap[toShape.getFetchKey(toParams)] = (existing: any) =>
81+
updateFn(newresult, existing);
82+
});
83+
return updateMap;
84+
};
85+
}
86+
8587
if (options && options.optimisticUpdate) {
8688
meta.optimisticResponse = options.optimisticUpdate(params, body);
8789
}

0 commit comments

Comments
 (0)