Skip to content

Commit 936306b

Browse files
committed
Add links to swr documentation
1 parent 2af9169 commit 936306b

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ const prefix = "some-api";
8383
export const useQuery = createQueryHook(client, prefix);
8484
export const useImmutable = createImmutableHook(client, prefix);
8585
export const useInfinite = createInfiniteHook(client, prefix);
86-
export const useMutate = createMutateHook(client, prefix, _.isMatch);
86+
export const useMutate = createMutateHook(
87+
client,
88+
prefix,
89+
_.isMatch, // Or any comparision function
90+
);
8791
```
8892

8993
### Parameters
@@ -106,6 +110,8 @@ Each builder hook accepts the same initial parameters:
106110

107111
## `useQuery`
108112

113+
This hook is a typed wrapper over [`useSWR`][swr-api].
114+
109115
```ts
110116
const useQuery = createQueryHook(/* ... */);
111117

@@ -161,6 +167,8 @@ function useQuery(path, ...[init, config]) {
161167

162168
## `useImmutable`
163169

170+
This hook has the same contracts as `useQuery`. However, instead of wrapping [`useSWR`][swr-api], it wraps `useSWRImmutable`. This immutable hook [disables automatic revalidations][swr-disable-auto-revalidate] but is otherwise identical to `useSWR`.
171+
164172
```ts
165173
const useImmutable = createImmutableHook(/* ... */);
166174

@@ -171,8 +179,6 @@ const { data, error, isLoading, isValidating, mutate } = useImmutable(
171179
);
172180
```
173181

174-
This hook has the same contracts as `useQuery`. However, instead of wrapping [`useSWR`][swr-api], it wraps `useSWRImmutable`. This immutable hook [disables automatic revalidations][swr-disable-auto-revalidate] but is otherwise identical to `useSWR`.
175-
176182
### Parameters
177183

178184
Identical to `useQuery` [parameters](#parameters-1).
@@ -183,6 +189,8 @@ Identical to `useQuery` [returns](#returns-1).
183189

184190
## `useInfinite`
185191

192+
This hook is a typed wrapper over [`useSWRInfinite`][swr-infinite].
193+
186194
```ts
187195
const useInfinite = createInfiniteHook(/* ... */);
188196

@@ -241,9 +249,10 @@ This function is similar to the [`getKey`][swr-infinite-options] parameter accep
241249
- `undefined` (if on the first page).
242250
- The fetched response for the last page retrieved.
243251

244-
#### Should Return
252+
#### Returns
245253

246254
- [Fetch options][oai-fetch-options] for the next page to load.
255+
- `null` if no more pages should be loaded.
247256

248257
#### Examples
249258

@@ -319,18 +328,18 @@ useInfinite("/something", (pageIndex, previousPageData) => {
319328

320329
## `useMutate`
321330

322-
```ts
323-
const mutate = useMutate();
324-
325-
await mutate([path, init], data, options);
326-
```
327-
328331
`useMutate` is a wrapper around SWR's [global mutate][swr-global-mutate] function. It provides a type-safe mechanism for updating and revalidating SWR's client-side cache for specific endpoints.
329332

330333
Like global mutate, this mutate wrapper accepts three parameters: `key`, `data`, and `options`. The latter two parameters are identical to those in _bound mutate_. `key` can be either a path alone, or a path with fetch options.
331334

332335
The level of specificity used when defining the key will determine which cached requests are updated. If only a path is provided, any cached request using that path will be updated. If fetch options are included in the key, the [`compare`](#compare) function will determine if a cached request's fetch options match the key's fetch options.
333336

337+
```ts
338+
const mutate = useMutate();
339+
340+
await mutate([path, init], data, options);
341+
```
342+
334343
<details>
335344
<summary>How <code>useMutate</code> works</summary>
336345

@@ -468,6 +477,5 @@ useQuery("/path", {
468477
[swr-infinite-return]: https://swr.vercel.app/docs/pagination#return-values
469478
[swr-infinite-options]: https://swr.vercel.app/docs/pagination#parameters
470479
[swr-global-mutate]: https://swr.vercel.app/docs/mutation#global-mutate
471-
[swr-bound-mutate]: https://swr.vercel.app/docs/mutation#bound-mutate
472480
[swr-mutate-params]: https://swr.vercel.app/docs/mutation#parameters
473481
[lodash-is-match]: https://lodash.com/docs/4.17.15#isMatch

src/immutable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import useSWRImmutable from "swr/immutable";
22
import { configureBaseQueryHook } from "./query-base.js";
33

44
/**
5+
* Produces a typed wrapper for [`useSWRImmutable`](https://swr.vercel.app/docs/revalidation.en-US#disable-automatic-revalidations).
6+
*
57
* ```ts
8+
* import createClient from "openapi-fetch";
69
* const client = createClient();
710
*
811
* const useImmutable = createImmutableHook(client, "<unique-key>");

src/infinite.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import type { TypesForGetRequest } from "./types.js";
99
import { useCallback, useDebugValue } from "react";
1010

1111
/**
12+
* Produces a typed wrapper for [`useSWRInfinite`](https://swr.vercel.app/docs/pagination#useswrinfinite).
13+
*
1214
* ```ts
15+
* import createClient from "openapi-fetch";
1316
* const client = createClient();
1417
*
1518
* const useInfinite = createInfiniteHook(client, "<unique-key>");

src/mutate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type { TypesForGetRequest } from "./types.js";
1010
export type CompareFn = (init: any, partialInit: any) => boolean;
1111

1212
/**
13+
* Produces a typed wrapper for [`useSWRConfig#mutate`](https://swr.vercel.app/docs/mutation).
14+
*
1315
* ```ts
16+
* import createClient from "openapi-fetch";
1417
* import { isMatch } from "lodash";
1518
*
1619
* const client = createClient();

src/query.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import useSWR from "swr";
22
import { configureBaseQueryHook } from "./query-base.js";
33

44
/**
5+
* Produces a typed wrapper for [`useSWR`](https://swr.vercel.app/docs/api).
6+
*
57
* ```ts
8+
* import createClient from "openapi-fetch";
9+
*
610
* const client = createClient();
711
*
812
* const useQuery = createQueryHook(client, "<unique-key>");

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type TryKey<T, K extends PropertyKey> = T extends { [Key in K]?: unknown }
1313
? T[K]
1414
: undefined;
1515

16+
/**
17+
* Provides specific types used within a given request
18+
*/
1619
export type TypesForRequest<
1720
Paths extends {},
1821
Method extends Extract<HttpMethod, keyof Paths[keyof Paths]>,
@@ -40,6 +43,11 @@ export type TypesForRequest<
4043
SWRResponse: SWRResponse<Data, Error, SWRConfig>;
4144
};
4245

46+
/**
47+
* Provides specific types for GET requests
48+
*
49+
* Uses {@link TypesForRequest}
50+
*/
4351
export type TypesForGetRequest<
4452
Paths extends {},
4553
Path extends PathsWithMethod<Paths, Extract<"get", keyof Paths[keyof Paths]>>,

0 commit comments

Comments
 (0)