Skip to content

Commit fad67de

Browse files
committed
doc: update context example
1 parent 87e39c8 commit fad67de

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

plugins/typescript/README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ The `{filenamePrefix}Context.ts` can be tweak to inject any props in the generat
5757

5858
```ts
5959
// `BadassContext.ts`
60+
import type { QueryKey, UseQueryOptions } from "react-query";
6061

6162
export type BadassContext = {
6263
fetcherOptions: {
6364
/**
6465
* Headers to inject in the fetcher
6566
*/
66-
headers?: {
67-
authorization?: string;
68-
};
67+
headers?: {};
6968
/**
7069
* Query params to inject in the fetcher
7170
*/
@@ -78,24 +77,42 @@ export type BadassContext = {
7877
*/
7978
enabled?: boolean;
8079
};
80+
/**
81+
* Query key middleware.
82+
*/
83+
queryKeyFn: (queryKey: QueryKey) => QueryKey;
8184
};
8285

8386
/**
8487
* Context injected into every react-query hook wrappers
88+
*
89+
* @param queryOptions options from the useQuery wrapper
8590
*/
86-
export const useBadassContext = (): BadassContext => {
91+
export function useBadassContext<
92+
TQueryFnData = unknown,
93+
TError = unknown,
94+
TData = TQueryFnData,
95+
TQueryKey extends QueryKey = QueryKey
96+
>(
97+
queryOptions?: Omit<
98+
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
99+
"queryKey" | "queryFn"
100+
>
101+
): BadassContext {
87102
const token = window.localStorage.getItem("token");
103+
88104
return {
89105
fetcherOptions: {
90106
headers: {
91107
authorization: token ? `Bearer ${token}` : undefined,
92108
},
93109
},
94110
queryOptions: {
95-
enabled: Boolean(token),
111+
enabled: Boolean(token) && (queryOptions?.enabled ?? true),
96112
},
113+
queryKeyFn: (queryKey) => queryKey,
97114
};
98-
};
115+
}
99116
```
100117

101118
You can also tweak `{filenamePrefix}Fetcher.ts`, especially the error management part, so everything fit the expected `ErrorType`.

0 commit comments

Comments
 (0)