-
Notifications
You must be signed in to change notification settings - Fork 0
/
createRESTfulSWR.ts
107 lines (99 loc) · 3.17 KB
/
createRESTfulSWR.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { useCallback, useMemo } from 'react';
import useSWR, { useSWRConfig } from 'swr';
import { ResourceCacher } from './ResourceCacher';
import {
CreateRESTfulSWRParams,
RESTfulSWR,
UseRESTfulSWRCollection,
UseRESTfulSWRResource,
} from './types';
const assertId: (id: string | undefined) => asserts id is string = id => {
if (!id) throw new Error(`id is undefined`);
};
/**
* Create RESTful SWR hooks for managing a collection & resource.
*
* This is the core entrypoint of the package. The function returns two hooks,
* `useSWRResource` & `useSWRCollection`, that work with SWR to keep the
* resources in sync between the individual resources & the root collection.
*
* @template RT Resource type
* @template CT Collection type
* @template LP List params
* @template CP Create params
* @template UP Update params
* @returns RESTful SWR hooks
*/
export const createRESTfulSWR = <RT, CT, LP, CP, UP>({
adapter,
useRestClient,
}: CreateRESTfulSWRParams<RT, CT, LP, CP, UP>): RESTfulSWR<
RT,
CT,
LP,
CP,
UP
> => {
const useSWRCollection: UseRESTfulSWRCollection<RT, CT, LP, CP, UP> = (
params?: LP,
) => {
const client = useRestClient();
const { mutate } = useSWRConfig();
const cacher = useMemo(() => new ResourceCacher(client, adapter, mutate), [
client,
mutate,
]);
const response = useSWR(adapter.collectionKey(params), {
fetcher: useCallback(() => client.list(params), [client, params]),
});
const api = useMemo(
() => ({
list: async () => cacher.listResources(params),
create: async (params: CP) => cacher.createResource(params),
view: async (id: string) => cacher.viewResource(id),
update: async (id: string, params: UP) =>
cacher.updateResource(id, params),
partial: async (id: string, params: Partial<UP>) =>
cacher.partialResource(id, params),
remove: async (id: string) => cacher.removeResource(id),
}),
[cacher, params],
);
return useMemo(() => ({ response, api }), [response, api]);
};
const useSWRResource: UseRESTfulSWRResource<RT, CP, UP> = (id?: string) => {
const client = useRestClient();
const { mutate } = useSWRConfig();
const cacher = useMemo(() => new ResourceCacher(client, adapter, mutate), [
client,
mutate,
]);
const response = useSWR(id ? adapter.resourceKey(id) : null, {
fetcher: useCallback(() => client.view(id!), [client, id]),
});
const api = useMemo(
() => ({
create: async (params: CP) => cacher.createResource(params),
view: async () => {
assertId(id);
return cacher.viewResource(id);
},
update: async (params: UP) => {
assertId(id);
return cacher.updateResource(id, params);
},
partial: async (params: Partial<UP>) => {
assertId(id);
return cacher.partialResource(id, params);
},
remove: async () => {
assertId(id);
return cacher.removeResource(id);
},
}),
[cacher, id],
);
return useMemo(() => ({ response, api }), [response, api]);
};
return { useSWRCollection, useSWRResource };
};