Skip to content

Commit 99bbf52

Browse files
committed
enhance: Provide args to update method
1 parent 4b504c8 commit 99bbf52

File tree

7 files changed

+91
-19
lines changed

7 files changed

+91
-19
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,45 @@ describe('reducer', () => {
454454
},
455455
);
456456
});
457+
458+
it('should account for args', () => {
459+
const iniState: any = {
460+
...initialState,
461+
entities: {
462+
[PaginatedArticle.key]: {
463+
'10': PaginatedArticle.fromJS({ id: 10 }),
464+
},
465+
},
466+
results: {
467+
[PaginatedArticle.list().key({ admin: true })]: { results: ['10'] },
468+
},
469+
};
470+
const newState = reducer(
471+
iniState,
472+
createReceive(
473+
{ results: [{ id: 11 }, { id: 12 }] },
474+
{
475+
...endpoint,
476+
key: endpoint.key({ cursor: 2, admin: true }),
477+
args: [{ cursor: 2, admin: true }],
478+
update: (nextpage: { results: string[] }, { admin }) => ({
479+
[PaginatedArticle.list().key({ admin })]: (
480+
existing: { results: string[] } = { results: [] },
481+
) => ({
482+
...existing,
483+
results: [...nextpage.results, ...existing.results],
484+
}),
485+
}),
486+
dataExpiryLength: 600000,
487+
},
488+
),
489+
);
490+
expect(
491+
newState.results[PaginatedArticle.list().key({ admin: true })],
492+
).toStrictEqual({
493+
results: ['11', '12', '10'],
494+
});
495+
});
457496
});
458497
});
459498

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface Options<
1515
S extends Schema | undefined = any,
1616
> extends Pick<
1717
FetchAction<Payload, S>['meta'],
18-
'schema' | 'key' | 'type' | 'updaters' | 'update'
18+
'schema' | 'key' | 'type' | 'updaters' | 'update' | 'args'
1919
> {
2020
dataExpiryLength: NonNullable<FetchOptions['dataExpiryLength']>;
2121
}
@@ -34,7 +34,14 @@ export default function createReceive<
3434
S extends Schema | undefined = any,
3535
>(
3636
data: Payload,
37-
{ schema, key, updaters, update, dataExpiryLength }: Options<Payload, S>,
37+
{
38+
schema,
39+
key,
40+
args,
41+
updaters,
42+
update,
43+
dataExpiryLength,
44+
}: Options<Payload, S>,
3845
): ReceiveAction<Payload, S> {
3946
/* istanbul ignore next */
4047
if (process.env.NODE_ENV === 'development' && dataExpiryLength < 0) {
@@ -44,6 +51,7 @@ export default function createReceive<
4451
const meta: ReceiveAction['meta'] = {
4552
schema,
4653
key,
54+
args,
4755
date: now,
4856
expiresAt: now + dataExpiryLength,
4957
};

packages/core/src/state/reducer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export default function reducer(
7070
};
7171
results = applyUpdatersToResults(results, result, action.meta.updaters);
7272
if (action.meta.update) {
73-
const updaters = action.meta.update(result);
73+
const updaters = action.meta.update(
74+
result,
75+
...(action.meta.args || []),
76+
);
7477
Object.keys(updaters).forEach(key => {
7578
results[key] = updaters[key](results[key]);
7679
});

packages/core/src/types.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { NormalizedIndex } from '@rest-hooks/normalizr';
22
import { Middleware } from '@rest-hooks/use-enhanced-reducer';
33
import { FSAWithPayloadAndMeta, FSAWithMeta, FSA } from 'flux-standard-action';
4+
import type {
5+
UpdateFunction,
6+
AbstractInstanceType,
7+
Schema,
8+
FetchFunction,
9+
EndpointExtraOptions,
10+
} from '@rest-hooks/endpoint';
411

512
import { ErrorableFSAWithPayloadAndMeta } from './fsa';
613
import { FetchShape } from './endpoint';
@@ -13,14 +20,6 @@ import {
1320
INVALIDATE_TYPE,
1421
} from './actionTypes';
1522

16-
import type {
17-
UpdateFunction,
18-
AbstractInstanceType,
19-
Schema,
20-
FetchFunction,
21-
EndpointExtraOptions,
22-
} from '@rest-hooks/endpoint';
23-
2423
export type { AbstractInstanceType, UpdateFunction };
2524

2625
export type ReceiveTypes = typeof RECEIVE_TYPE;
@@ -66,8 +65,9 @@ export type FetchOptions<F extends FetchFunction = FetchFunction> =
6665
export interface ReceiveMeta<S extends Schema | undefined> {
6766
schema: S;
6867
key: string;
68+
args?: readonly any[];
6969
updaters?: Record<string, UpdateFunction<S, any>>;
70-
update?: (result: any) => Record<string, (...args: any) => any>;
70+
update?: (result: any, ...args: any) => Record<string, (...args: any) => any>;
7171
date: number;
7272
expiresAt: number;
7373
}
@@ -98,8 +98,9 @@ interface FetchMeta<
9898
type: FetchShape<any, any>['type'];
9999
schema: S;
100100
key: string;
101+
args?: readonly any[];
101102
updaters?: Record<string, UpdateFunction<S, any>>;
102-
update?: (result: any) => Record<string, (...args: any) => any>;
103+
update?: (result: any, ...args: any) => Record<string, (...args: any) => any>;
103104
options?: FetchOptions;
104105
throttle: boolean;
105106
resolve: (value?: any | PromiseLike<any>) => void;

packages/experimental/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
</div>
1515

16-
## useFetcher()
16+
## Motivation
17+
18+
Field application of designs help smooth edges of a theoretical design. New designs can be iterated on here, breaking freely without worry of legacy support plans.
19+
20+
## Usage
21+
22+
### useFetcher()
1723

1824
```tsx
1925
const fetch = useFetcher();
@@ -25,7 +31,7 @@ return (
2531
);
2632
```
2733

28-
## Endpoint.update
34+
### Endpoint.update
2935

3036
<details open><summary><b>Simple</b></summary>
3137

@@ -65,7 +71,7 @@ const createUser = new Endpoint(postToUserFunction, {
6571
</details>
6672
6773
68-
## Entity, EntityRecord, Resource, BaseResource
74+
### Entity, EntityRecord, Resource, BaseResource
6975
7076
- Normalizes to pojo
7177
- Faster

packages/experimental/src/createFetch.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { actionTypes } from '@rest-hooks/core';
2-
3-
import { UpdateFunction } from './types';
4-
52
import type { FetchAction } from '@rest-hooks/core';
63
import type { EndpointInterface } from '@rest-hooks/endpoint';
74

5+
import { UpdateFunction } from './types';
6+
87
/** Requesting a fetch to begin
98
*
109
* @param endpoint
@@ -25,6 +24,7 @@ export default function createFetch<
2524
const meta: FetchAction['meta'] = {
2625
schema: endpoint.schema,
2726
type: endpoint.sideEffect ? ('mutate' as const) : ('read' as const),
27+
args,
2828
key,
2929
throttle,
3030
options: endpoint,
@@ -51,3 +51,17 @@ export default function createFetch<
5151
meta,
5252
};
5353
}
54+
55+
/** Future action shape
56+
{
57+
type: actionTypes.FETCH_TYPE,
58+
endpoint,
59+
meta: {
60+
args,
61+
throttle,
62+
createdAt,
63+
promise,
64+
resolve,
65+
reject,
66+
}
67+
} */

website/siteConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const siteConfig = {
5757
{
5858
href: 'https://github.com/Rest-Hooks/todo-example',
5959
label: '🎮 Demo',
60+
external: true,
6061
},
6162
//{ page: 'help', label: 'Help' },
6263
{ search: true },

0 commit comments

Comments
 (0)