Skip to content

Commit a62b5df

Browse files
committed
fix missing hooks in useApi
1 parent e107cb7 commit a62b5df

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

docs/docs/getting-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface ApiConfig {
4343
}
4444

4545
interface Variables {
46-
path?: Record<string, string | number>
46+
path?: Record<string | number, string>
4747
search?: any
4848
body?: any
4949
}
@@ -55,13 +55,13 @@ export async function fetcher (apiConfig: ApiConfig, variables: Variables) {
5555
const url = new URL(path, baseUrl)
5656

5757
if ('path' in variables) {
58-
for (const [key, value] of Object.entries(variables.body)) {
58+
for (const [key, value] of Object.entries(variables.path)) {
5959
url.pathname = url.pathname.replace(`:${key}`, value)
6060
}
6161
}
6262

6363
if ('search' in variables) {
64-
url.searchParams = new URLSearchParams(variables.search)
64+
url.search = new URLSearchParams(variables.search).toString()
6565
}
6666

6767
const res = await fetch(url.toString(), {

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.5.0",
3+
"version": "0.5.1",
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export interface UseLazyApiOptions<
171171
cached?: boolean
172172
refetchOnFocus?: boolean
173173
variables?: TVariables
174-
onFetch?: () => Promise<any>
174+
onFetch?: () => any
175175
onCompleted?: (params: OnCompletedParams<T, TData, TError, TVariables, K>) => any
176176
}
177177

@@ -334,6 +334,7 @@ function createUseApi<
334334
TApiVariables extends TVariables[K]
335335
>(key: K, opts: UseApiOptions<T, TData, TError, TVariables, K> = {}) {
336336
const { cache } = useContext(ctx)
337+
const optsRef = useValueRef(opts)
337338
const {
338339
skip = false,
339340
force = false,
@@ -350,9 +351,11 @@ function createUseApi<
350351
const onFetch = useCallback(async () => {
351352
calledRef.current = true
352353
loadingRef.current = true
354+
if (optsRef.current.onFetch) await optsRef.current.onFetch()
353355
}, [])
354-
const onCompleted = useCallback(async () => {
356+
const onCompleted = useCallback(async (params: OnCompletedParams<T, TData, TError, TVariables, K>) => {
355357
loadingRef.current = false
358+
if (optsRef.current.onCompleted) await optsRef.current.onCompleted(params)
356359
}, [])
357360
const [fetch, result] = useLazyApi<K, TApiData, TApiError, TApiVariables>(key, {
358361
...lazyOpts,

0 commit comments

Comments
 (0)