Skip to content

Commit 2f67b71

Browse files
committed
v0.5.0 mutate cache
1 parent 98a482a commit 2f67b71

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-api-fetching",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Make fetching API easier with React's hooks + context",
55
"source": "src/index.tsx",
66
"main": "dist/main.js",

src/index.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ function createUseRevalidate<
112112
}
113113
}
114114

115+
function createUseMutate<
116+
T,
117+
TData extends Record<keyof T, any>,
118+
TVariables extends Record<keyof T, any>
119+
>(ctx: React.Context<ApiContext<T>>) {
120+
return function useMutate() {
121+
const { cache } = useContext(ctx)
122+
123+
const mutate = useCallback(function<
124+
K extends keyof T,
125+
TApiData extends TData[K],
126+
TApiVariables extends TVariables[K]
127+
>(key:K, variables: TApiVariables, callback: (prevData: TApiData) => TApiData) {
128+
const cacheKey = generateCacheKey(key, variables)
129+
const cacheData = cache.get(cacheKey) as TApiData
130+
const data = callback(cacheData)
131+
cache.set(cacheKey, data)
132+
return data
133+
}, [cache])
134+
135+
return mutate
136+
}
137+
}
138+
115139
interface OnCompletedParams<
116140
T,
117141
TData extends Record<keyof T, any>,
@@ -130,6 +154,11 @@ interface OnCompletedParams<
130154
data: TApiData | null;
131155
error: TApiError | null;
132156
}>
157+
mutate: <
158+
X extends keyof T,
159+
TApiData extends TData[X],
160+
TApiVariables extends TVariables[X]
161+
>(key: X, variables: TApiVariables, callback: (prevData: TApiData) => TApiData) => TApiData
133162
}
134163

135164
export interface UseLazyApiOptions<
@@ -153,13 +182,16 @@ function createUseLazyApi<
153182
TVariables extends Record<keyof T, any>
154183
>(ctx: React.Context<ApiContext<T>>, apis: T) {
155184
const useRevalidate = createUseRevalidate<T, TData, TError, TVariables>(ctx, apis)
185+
const useMutate = createUseMutate<T, TData, TVariables>(ctx)
186+
156187
return function useLazyApi<
157188
K extends keyof T,
158189
TApiData extends TData[K],
159190
TApiError extends TError[K],
160191
TApiVariables extends TVariables[K],
161192
>(key: K, defaultOpts: UseLazyApiOptions<T, TData, TError, TVariables, K> = {}) {
162193
const revalidate = useRevalidate()
194+
const mutate = useMutate()
163195
const fetchedRef = useRef(false)
164196
const defaultOptsRef = useValueRef(defaultOpts)
165197
const {
@@ -211,7 +243,7 @@ function createUseLazyApi<
211243
error = err as TApiError
212244
}
213245

214-
if (onCompleted) await onCompleted({ data, error, revalidate })
246+
if (onCompleted) await onCompleted({ data, error, revalidate, mutate })
215247

216248
fetchedRef.current = true
217249
prevResultRef.current = { data, error }
@@ -232,7 +264,7 @@ function createUseLazyApi<
232264
}
233265

234266
return { data, error }
235-
}, [key, cache, fetcher, setVariables, setLocalResult, revalidate])
267+
}, [key, cache, fetcher, setVariables, setLocalResult, revalidate, mutate])
236268

237269
const refetch = useCallback(() => fetch({ variables: prevVariablesRef.current }), [fetch])
238270

0 commit comments

Comments
 (0)