@geekmidas/client 4.0.3 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/auth-fetcher.cjs +1 -1
- package/dist/auth-fetcher.d.cts +1 -1
- package/dist/auth-fetcher.d.mts +1 -1
- package/dist/auth-fetcher.mjs +1 -1
- package/dist/endpoint-hooks.cjs +44 -1
- package/dist/endpoint-hooks.cjs.map +1 -1
- package/dist/endpoint-hooks.d.cts +24 -3
- package/dist/endpoint-hooks.d.cts.map +1 -1
- package/dist/endpoint-hooks.d.mts +24 -3
- package/dist/endpoint-hooks.d.mts.map +1 -1
- package/dist/endpoint-hooks.mjs +45 -2
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/{fetcher-C7rErA7M.mjs → fetcher-CakjC9C7.mjs} +5 -6
- package/dist/fetcher-CakjC9C7.mjs.map +1 -0
- package/dist/{fetcher-DtsaihL6.cjs → fetcher-CwnSqc4D.cjs} +5 -6
- package/dist/fetcher-CwnSqc4D.cjs.map +1 -0
- package/dist/fetcher.cjs +1 -1
- package/dist/fetcher.d.cts +6 -5
- package/dist/fetcher.d.cts.map +1 -1
- package/dist/fetcher.d.mts +6 -5
- package/dist/fetcher.d.mts.map +1 -1
- package/dist/fetcher.mjs +1 -1
- package/dist/openapi-hooks.cjs +1 -1
- package/dist/openapi-hooks.d.cts +4 -4
- package/dist/openapi-hooks.d.mts +1 -1
- package/dist/openapi-hooks.mjs +1 -1
- package/dist/react-query.cjs +1 -1
- package/dist/react-query.d.cts +8 -8
- package/dist/react-query.d.mts +1 -1
- package/dist/react-query.mjs +1 -1
- package/dist/{types-AMWu2wmL.d.mts → types-D8Jrl4o3.d.mts} +6 -5
- package/dist/{types-AMWu2wmL.d.mts.map → types-D8Jrl4o3.d.mts.map} +1 -1
- package/dist/{types-OSOkr8AA.d.cts → types-DMQMHePI.d.cts} +6 -5
- package/dist/{types-OSOkr8AA.d.cts.map → types-DMQMHePI.d.cts.map} +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/package.json +69 -29
- package/src/__tests__/fetcher.spec.ts +58 -0
- package/src/endpoint-hooks.ts +144 -2
- package/src/fetcher.ts +13 -6
- package/src/types.ts +8 -4
- package/dist/fetcher-C7rErA7M.mjs.map +0 -1
- package/dist/fetcher-DtsaihL6.cjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @geekmidas/client
|
|
2
2
|
|
|
3
|
+
## 4.0.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 🐛 [`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix `package.json` exports so TypeScript declarations resolve correctly under NodeNext/Bundler module resolution. Each subpath export now nests `types` inside its `import`/`require` condition, pointing at the `.d.mts` and `.d.cts` files that `tsdown` actually emits (previously the exports referenced non-existent `.d.ts` files, causing type-resolution failures for consumers). Both ESM (`.mjs`) and CJS (`.cjs`) runtime entry points are preserved. Additionally, `@geekmidas/ui` had `import` paths pointing at `.js` files that were never emitted — those are corrected to `.mjs`.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651)]:
|
|
10
|
+
- @geekmidas/constructs@3.0.12
|
|
11
|
+
- @geekmidas/schema@1.0.2
|
|
12
|
+
|
|
13
|
+
## 4.0.4
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- ✨ [`6f8f28a`](https://github.com/geekmidas/toolbox/commit/6f8f28a1317c0b519fd2067a2cd39b73c0585755) Thanks [@geekmidas](https://github.com/geekmidas)! - Add method preservation on the client and add query helpers to hooks
|
|
18
|
+
|
|
3
19
|
## 4.0.3
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/auth-fetcher.cjs
CHANGED
package/dist/auth-fetcher.d.cts
CHANGED
package/dist/auth-fetcher.d.mts
CHANGED
package/dist/auth-fetcher.mjs
CHANGED
package/dist/endpoint-hooks.cjs
CHANGED
|
@@ -27,7 +27,7 @@ function buildQueryKey(endpoint, config) {
|
|
|
27
27
|
* await mutation.mutateAsync({ body: { name: 'John' } });
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
function createEndpointHooks(fetcher,
|
|
30
|
+
function createEndpointHooks(fetcher, options = {}) {
|
|
31
31
|
return {
|
|
32
32
|
useQuery: (endpoint, ...args) => {
|
|
33
33
|
const [config, queryOptions] = args;
|
|
@@ -56,6 +56,49 @@ function createEndpointHooks(fetcher, _options = {}) {
|
|
|
56
56
|
]);
|
|
57
57
|
return (0, __tanstack_react_query.useMutation)(memoizedOptions);
|
|
58
58
|
},
|
|
59
|
+
useInfiniteQuery: (endpoint, options$1, config) => {
|
|
60
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
61
|
+
const memoizedOptions = (0, react.useMemo)(() => ({
|
|
62
|
+
queryKey,
|
|
63
|
+
queryFn: ({ pageParam }) => {
|
|
64
|
+
let mergedConfig = config;
|
|
65
|
+
if (pageParam !== void 0 && config) {
|
|
66
|
+
const pageQuery = typeof pageParam === "object" ? pageParam : { page: pageParam };
|
|
67
|
+
mergedConfig = {
|
|
68
|
+
...config,
|
|
69
|
+
query: {
|
|
70
|
+
...config.query,
|
|
71
|
+
...pageQuery
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
} else if (pageParam !== void 0 && !config) {
|
|
75
|
+
const pageQuery = typeof pageParam === "object" ? pageParam : { page: pageParam };
|
|
76
|
+
mergedConfig = { query: pageQuery };
|
|
77
|
+
}
|
|
78
|
+
return fetcher(endpoint, mergedConfig);
|
|
79
|
+
},
|
|
80
|
+
...options$1
|
|
81
|
+
}), [
|
|
82
|
+
endpoint,
|
|
83
|
+
config,
|
|
84
|
+
fetcher,
|
|
85
|
+
queryKey,
|
|
86
|
+
options$1
|
|
87
|
+
]);
|
|
88
|
+
return (0, __tanstack_react_query.useInfiniteQuery)(memoizedOptions);
|
|
89
|
+
},
|
|
90
|
+
invalidateQueries: (endpoint, config) => {
|
|
91
|
+
if (!options.queryClient) throw new Error("queryClient is required for invalidateQueries. Pass it to createEndpointHooks options.");
|
|
92
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
93
|
+
return options.queryClient.invalidateQueries({
|
|
94
|
+
queryKey,
|
|
95
|
+
exact: !!config
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
invalidateAllQueries: () => {
|
|
99
|
+
if (!options.queryClient) throw new Error("queryClient is required for invalidateAllQueries. Pass it to createEndpointHooks options.");
|
|
100
|
+
return options.queryClient.invalidateQueries();
|
|
101
|
+
},
|
|
59
102
|
buildQueryKey
|
|
60
103
|
};
|
|
61
104
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-hooks.cjs","names":["endpoint: T","config?: FilteredRequestConfig<Paths, T>","key: unknown[]","fetcher: TypedApiFunction<Paths>","_options: CreateEndpointHooksOptions","mutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>","config: FilteredRequestConfig<Paths, T>"],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":["import type {\n\tQueryClient,\n\tUseMutationOptions,\n\tUseMutationResult,\n\tUseQueryOptions,\n\tUseQueryResult,\n} from '@tanstack/react-query';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport { useMemo } from 'react';\nimport type {\n\tExtractEndpointResponse,\n\tFilteredRequestConfig,\n\tIsConfigRequired,\n\tMutationEndpoint,\n\tQueryEndpoint,\n\tTypedApiFunction,\n} from './types';\n\n/**\n * Build query key from endpoint and config\n */\nfunction buildQueryKey<Paths, T extends QueryEndpoint<Paths>>(\n\tendpoint: T,\n\tconfig?: FilteredRequestConfig<Paths, T>,\n): unknown[] {\n\tconst key: unknown[] = [endpoint];\n\n\tif (config && 'params' in config && config.params) {\n\t\tkey.push({ params: config.params });\n\t}\n\n\tif (config && 'query' in config && config.query) {\n\t\tkey.push({ query: config.query });\n\t}\n\n\treturn key;\n}\n\n/**\n * Options for creating endpoint-based hooks\n */\nexport interface CreateEndpointHooksOptions {\n\tqueryClient?: QueryClient;\n}\n\n/**\n * Hook options type that conditionally requires config\n */\ntype UseQueryArgs<Paths, T extends QueryEndpoint<Paths>> = IsConfigRequired<\n\tPaths,\n\tT\n> extends true\n\t? [\n\t\t\tconfig: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t]\n\t: [\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t];\n\n/**\n * Endpoint-based React Query hooks\n */\nexport interface EndpointHooks<Paths> {\n\t/**\n\t * Use query hook for GET endpoints.\n\t * Config is required when endpoint has path params.\n\t */\n\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\t...args: UseQueryArgs<Paths, T>\n\t) => UseQueryResult<ExtractEndpointResponse<Paths, T>, Error>;\n\n\t/**\n\t * Use mutation hook for POST, PUT, PATCH, DELETE endpoints.\n\t * Config with params/body is passed to mutate().\n\t */\n\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\toptions?: Omit<\n\t\t\tUseMutationOptions<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>,\n\t\t\t'mutationFn'\n\t\t>,\n\t) => UseMutationResult<\n\t\tExtractEndpointResponse<Paths, T>,\n\t\tError,\n\t\tFilteredRequestConfig<Paths, T>\n\t>;\n\n\t/**\n\t * Build a query key for manual cache operations\n\t */\n\tbuildQueryKey: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => unknown[];\n}\n\n/**\n * Create endpoint-based React Query hooks from a typed fetcher.\n *\n * @example\n * ```typescript\n * const fetcher = createAuthAwareFetcher<paths>({ ... });\n * const hooks = createEndpointHooks<paths>(fetcher);\n *\n * // In a component\n * const { data } = hooks.useQuery('GET /users/{id}', { params: { id: '123' } });\n *\n * const mutation = hooks.useMutation('POST /users');\n * await mutation.mutateAsync({ body: { name: 'John' } });\n * ```\n */\nexport function createEndpointHooks<Paths>(\n\tfetcher: TypedApiFunction<Paths>,\n\t_options: CreateEndpointHooksOptions = {},\n): EndpointHooks<Paths> {\n\treturn {\n\t\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\t...args: UseQueryArgs<Paths, T>\n\t\t) => {\n\t\t\t// Parse args - config is first, options is second\n\t\t\tconst [config, queryOptions] = args as [\n\t\t\t\tFilteredRequestConfig<Paths, T> | undefined,\n\t\t\t\t(\n\t\t\t\t\t| Omit<\n\t\t\t\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t\t\t >\n\t\t\t\t\t| undefined\n\t\t\t\t),\n\t\t\t];\n\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: () =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...queryOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, queryOptions],\n\t\t\t);\n\n\t\t\treturn useQuery<ExtractEndpointResponse<Paths, T>, Error>(\n\t\t\t\tmemoizedOptions,\n\t\t\t);\n\t\t},\n\n\t\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tmutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tmutationFn: (config: FilteredRequestConfig<Paths, T>) =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...mutationOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, fetcher, mutationOptions],\n\t\t\t);\n\n\t\t\treturn useMutation<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tbuildQueryKey,\n\t};\n}\n"],"mappings":";;;;;;;;AAqBA,SAAS,cACRA,UACAC,QACY;CACZ,MAAMC,MAAiB,CAAC,QAAS;AAEjC,KAAI,UAAU,YAAY,UAAU,OAAO,OAC1C,KAAI,KAAK,EAAE,QAAQ,OAAO,OAAQ,EAAC;AAGpC,KAAI,UAAU,WAAW,UAAU,OAAO,MACzC,KAAI,KAAK,EAAE,OAAO,OAAO,MAAO,EAAC;AAGlC,QAAO;AACP;;;;;;;;;;;;;;;;AAwFD,SAAgB,oBACfC,SACAC,WAAuC,CAAE,GAClB;AACvB,QAAO;EACN,UAAU,CACTJ,UACA,GAAG,SACC;GAEJ,MAAM,CAAC,QAAQ,aAAa,GAAG;GAW/B,MAAM,WAAW,cAAc,UAAU,OAAO;GAEhD,MAAM,kBAAkB,mBACvB,OAAO;IACN;IACA,SAAS,MAER,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAQ;IAAS;IAAU;GAAa,EACnD;AAED,UAAO,qCACN,gBACA;EACD;EAED,aAAa,CACZA,UACAK,oBAQI;GACJ,MAAM,kBAAkB,mBACvB,OAAO;IACN,YAAY,CAACC,WAEZ,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAS;GAAgB,EACpC;AAED,UAAO,wCAIL,gBAAgB;EAClB;EAED;CACA;AACD"}
|
|
1
|
+
{"version":3,"file":"endpoint-hooks.cjs","names":["endpoint: T","config?: FilteredRequestConfig<Paths, T>","key: unknown[]","fetcher: TypedApiFunction<Paths>","options: CreateEndpointHooksOptions","mutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>","config: FilteredRequestConfig<Paths, T>","options: Omit<\n\t\t\t\tUseInfiniteQueryOptions<\n\t\t\t\t\tTPageData,\n\t\t\t\t\tError,\n\t\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\t\tunknown[],\n\t\t\t\t\tTPageParam\n\t\t\t\t>,\n\t\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t\t> & {\n\t\t\t\tgetNextPageParam: (\n\t\t\t\t\tlastPage: TPageData,\n\t\t\t\t\tallPages: TPageData[],\n\t\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\t\tallPageParams: TPageParam[],\n\t\t\t\t) => TPageParam | undefined;\n\t\t\t\tinitialPageParam: TPageParam;\n\t\t\t}","options"],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":["import type {\n\tQueryClient,\n\tQueryFunctionContext,\n\tUseInfiniteQueryOptions,\n\tUseInfiniteQueryResult,\n\tUseMutationOptions,\n\tUseMutationResult,\n\tUseQueryOptions,\n\tUseQueryResult,\n} from '@tanstack/react-query';\nimport { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';\nimport { useMemo } from 'react';\nimport type {\n\tExtractEndpointResponse,\n\tFilteredRequestConfig,\n\tIsConfigRequired,\n\tMutationEndpoint,\n\tQueryEndpoint,\n\tTypedApiFunction,\n} from './types';\n\n/**\n * Build query key from endpoint and config\n */\nfunction buildQueryKey<Paths, T extends QueryEndpoint<Paths>>(\n\tendpoint: T,\n\tconfig?: FilteredRequestConfig<Paths, T>,\n): unknown[] {\n\tconst key: unknown[] = [endpoint];\n\n\tif (config && 'params' in config && config.params) {\n\t\tkey.push({ params: config.params });\n\t}\n\n\tif (config && 'query' in config && config.query) {\n\t\tkey.push({ query: config.query });\n\t}\n\n\treturn key;\n}\n\n/**\n * Options for creating endpoint-based hooks\n */\nexport interface CreateEndpointHooksOptions {\n\tqueryClient?: QueryClient;\n}\n\n/**\n * Hook options type that conditionally requires config\n */\ntype UseQueryArgs<Paths, T extends QueryEndpoint<Paths>> = IsConfigRequired<\n\tPaths,\n\tT\n> extends true\n\t? [\n\t\t\tconfig: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t]\n\t: [\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t];\n\n/**\n * Endpoint-based React Query hooks\n */\nexport interface EndpointHooks<Paths> {\n\t/**\n\t * Use query hook for GET endpoints.\n\t * Config is required when endpoint has path params.\n\t */\n\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\t...args: UseQueryArgs<Paths, T>\n\t) => UseQueryResult<ExtractEndpointResponse<Paths, T>, Error>;\n\n\t/**\n\t * Use mutation hook for POST, PUT, PATCH, DELETE endpoints.\n\t * Config with params/body is passed to mutate().\n\t */\n\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\toptions?: Omit<\n\t\t\tUseMutationOptions<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>,\n\t\t\t'mutationFn'\n\t\t>,\n\t) => UseMutationResult<\n\t\tExtractEndpointResponse<Paths, T>,\n\t\tError,\n\t\tFilteredRequestConfig<Paths, T>\n\t>;\n\n\t/**\n\t * Use infinite query hook for paginated GET endpoints.\n\t */\n\tuseInfiniteQuery: <\n\t\tT extends QueryEndpoint<Paths>,\n\t\tTPageData = ExtractEndpointResponse<Paths, T>,\n\t\tTPageParam = unknown,\n\t>(\n\t\tendpoint: T,\n\t\toptions: Omit<\n\t\t\tUseInfiniteQueryOptions<\n\t\t\t\tTPageData,\n\t\t\t\tError,\n\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\tunknown[],\n\t\t\t\tTPageParam\n\t\t\t>,\n\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t> & {\n\t\t\tgetNextPageParam: (\n\t\t\t\tlastPage: TPageData,\n\t\t\t\tallPages: TPageData[],\n\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\tallPageParams: TPageParam[],\n\t\t\t) => TPageParam | undefined;\n\t\t\tinitialPageParam: TPageParam;\n\t\t},\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => UseInfiniteQueryResult<\n\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\tError\n\t>;\n\n\t/**\n\t * Invalidate queries for a specific endpoint.\n\t */\n\tinvalidateQueries: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => Promise<void>;\n\n\t/**\n\t * Invalidate all queries in the cache.\n\t */\n\tinvalidateAllQueries: () => Promise<void>;\n\n\t/**\n\t * Build a query key for manual cache operations\n\t */\n\tbuildQueryKey: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => unknown[];\n}\n\n/**\n * Create endpoint-based React Query hooks from a typed fetcher.\n *\n * @example\n * ```typescript\n * const fetcher = createAuthAwareFetcher<paths>({ ... });\n * const hooks = createEndpointHooks<paths>(fetcher);\n *\n * // In a component\n * const { data } = hooks.useQuery('GET /users/{id}', { params: { id: '123' } });\n *\n * const mutation = hooks.useMutation('POST /users');\n * await mutation.mutateAsync({ body: { name: 'John' } });\n * ```\n */\nexport function createEndpointHooks<Paths>(\n\tfetcher: TypedApiFunction<Paths>,\n\toptions: CreateEndpointHooksOptions = {},\n): EndpointHooks<Paths> {\n\treturn {\n\t\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\t...args: UseQueryArgs<Paths, T>\n\t\t) => {\n\t\t\t// Parse args - config is first, options is second\n\t\t\tconst [config, queryOptions] = args as [\n\t\t\t\tFilteredRequestConfig<Paths, T> | undefined,\n\t\t\t\t(\n\t\t\t\t\t| Omit<\n\t\t\t\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t\t\t >\n\t\t\t\t\t| undefined\n\t\t\t\t),\n\t\t\t];\n\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: () =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...queryOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, queryOptions],\n\t\t\t);\n\n\t\t\treturn useQuery<ExtractEndpointResponse<Paths, T>, Error>(\n\t\t\t\tmemoizedOptions,\n\t\t\t);\n\t\t},\n\n\t\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tmutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tmutationFn: (config: FilteredRequestConfig<Paths, T>) =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...mutationOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, fetcher, mutationOptions],\n\t\t\t);\n\n\t\t\treturn useMutation<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tuseInfiniteQuery: <\n\t\t\tT extends QueryEndpoint<Paths>,\n\t\t\tTPageData = ExtractEndpointResponse<Paths, T>,\n\t\t\tTPageParam = unknown,\n\t\t>(\n\t\t\tendpoint: T,\n\t\t\toptions: Omit<\n\t\t\t\tUseInfiniteQueryOptions<\n\t\t\t\t\tTPageData,\n\t\t\t\t\tError,\n\t\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\t\tunknown[],\n\t\t\t\t\tTPageParam\n\t\t\t\t>,\n\t\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t\t> & {\n\t\t\t\tgetNextPageParam: (\n\t\t\t\t\tlastPage: TPageData,\n\t\t\t\t\tallPages: TPageData[],\n\t\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\t\tallPageParams: TPageParam[],\n\t\t\t\t) => TPageParam | undefined;\n\t\t\t\tinitialPageParam: TPageParam;\n\t\t\t},\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t) => {\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: ({\n\t\t\t\t\t\tpageParam,\n\t\t\t\t\t}: QueryFunctionContext<unknown[], TPageParam>) => {\n\t\t\t\t\t\tlet mergedConfig = config;\n\t\t\t\t\t\tif (pageParam !== undefined && config) {\n\t\t\t\t\t\t\tconst pageQuery =\n\t\t\t\t\t\t\t\ttypeof pageParam === 'object' ? pageParam : { page: pageParam };\n\t\t\t\t\t\t\tmergedConfig = {\n\t\t\t\t\t\t\t\t...config,\n\t\t\t\t\t\t\t\tquery: { ...(config as any).query, ...pageQuery },\n\t\t\t\t\t\t\t} as any;\n\t\t\t\t\t\t} else if (pageParam !== undefined && !config) {\n\t\t\t\t\t\t\tconst pageQuery =\n\t\t\t\t\t\t\t\ttypeof pageParam === 'object' ? pageParam : { page: pageParam };\n\t\t\t\t\t\t\tmergedConfig = { query: pageQuery } as any;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, mergedConfig) as Promise<TPageData>;\n\t\t\t\t\t},\n\t\t\t\t\t...options,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, options],\n\t\t\t);\n\n\t\t\treturn useInfiniteQuery<\n\t\t\t\tTPageData,\n\t\t\t\tError,\n\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\tunknown[],\n\t\t\t\tTPageParam\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tinvalidateQueries: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t) => {\n\t\t\tif (!options.queryClient) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'queryClient is required for invalidateQueries. Pass it to createEndpointHooks options.',\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\t\t\treturn options.queryClient.invalidateQueries({\n\t\t\t\tqueryKey,\n\t\t\t\texact: !!config,\n\t\t\t});\n\t\t},\n\n\t\tinvalidateAllQueries: () => {\n\t\t\tif (!options.queryClient) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'queryClient is required for invalidateAllQueries. Pass it to createEndpointHooks options.',\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn options.queryClient.invalidateQueries();\n\t\t},\n\n\t\tbuildQueryKey,\n\t};\n}\n"],"mappings":";;;;;;;;AAwBA,SAAS,cACRA,UACAC,QACY;CACZ,MAAMC,MAAiB,CAAC,QAAS;AAEjC,KAAI,UAAU,YAAY,UAAU,OAAO,OAC1C,KAAI,KAAK,EAAE,QAAQ,OAAO,OAAQ,EAAC;AAGpC,KAAI,UAAU,WAAW,UAAU,OAAO,MACzC,KAAI,KAAK,EAAE,OAAO,OAAO,MAAO,EAAC;AAGlC,QAAO;AACP;;;;;;;;;;;;;;;;AAsID,SAAgB,oBACfC,SACAC,UAAsC,CAAE,GACjB;AACvB,QAAO;EACN,UAAU,CACTJ,UACA,GAAG,SACC;GAEJ,MAAM,CAAC,QAAQ,aAAa,GAAG;GAW/B,MAAM,WAAW,cAAc,UAAU,OAAO;GAEhD,MAAM,kBAAkB,mBACvB,OAAO;IACN;IACA,SAAS,MAER,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAQ;IAAS;IAAU;GAAa,EACnD;AAED,UAAO,qCACN,gBACA;EACD;EAED,aAAa,CACZA,UACAK,oBAQI;GACJ,MAAM,kBAAkB,mBACvB,OAAO;IACN,YAAY,CAACC,WAEZ,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAS;GAAgB,EACpC;AAED,UAAO,wCAIL,gBAAgB;EAClB;EAED,kBAAkB,CAKjBN,UACAO,WAkBAN,WACI;GACJ,MAAM,WAAW,cAAc,UAAU,OAAO;GAEhD,MAAM,kBAAkB,mBACvB,OAAO;IACN;IACA,SAAS,CAAC,EACT,WAC6C,KAAK;KAClD,IAAI,eAAe;AACnB,SAAI,wBAA2B,QAAQ;MACtC,MAAM,mBACE,cAAc,WAAW,YAAY,EAAE,MAAM,UAAW;AAChE,qBAAe;OACd,GAAG;OACH,OAAO;QAAE,GAAI,OAAe;QAAO,GAAG;OAAW;MACjD;KACD,WAAU,yBAA4B,QAAQ;MAC9C,MAAM,mBACE,cAAc,WAAW,YAAY,EAAE,MAAM,UAAW;AAChE,qBAAe,EAAE,OAAO,UAAW;KACnC;AACD,YAAO,AACN,QAIC,UAAU,aAAa;IACzB;IACD,GAAGO;GACH,IACD;IAAC;IAAU;IAAQ;IAAS;IAAUA;GAAQ,EAC9C;AAED,UAAO,6CAML,gBAAgB;EAClB;EAED,mBAAmB,CAClBR,UACAC,WACI;AACJ,QAAK,QAAQ,YACZ,OAAM,IAAI,MACT;GAGF,MAAM,WAAW,cAAc,UAAU,OAAO;AAChD,UAAO,QAAQ,YAAY,kBAAkB;IAC5C;IACA,SAAS;GACT,EAAC;EACF;EAED,sBAAsB,MAAM;AAC3B,QAAK,QAAQ,YACZ,OAAM,IAAI,MACT;AAGF,UAAO,QAAQ,YAAY,mBAAmB;EAC9C;EAED;CACA;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, QueryEndpoint, TypedApiFunction } from "./types-
|
|
2
|
-
import { QueryClient, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
1
|
+
import { ExtractEndpointResponse, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, QueryEndpoint, TypedApiFunction } from "./types-DMQMHePI.cjs";
|
|
2
|
+
import { QueryClient, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
3
3
|
|
|
4
4
|
//#region src/endpoint-hooks.d.ts
|
|
5
5
|
|
|
@@ -27,6 +27,27 @@ interface EndpointHooks<Paths> {
|
|
|
27
27
|
* Config with params/body is passed to mutate().
|
|
28
28
|
*/
|
|
29
29
|
useMutation: <T extends MutationEndpoint<Paths>>(endpoint: T, options?: Omit<UseMutationOptions<ExtractEndpointResponse<Paths, T>, Error, FilteredRequestConfig<Paths, T>>, 'mutationFn'>) => UseMutationResult<ExtractEndpointResponse<Paths, T>, Error, FilteredRequestConfig<Paths, T>>;
|
|
30
|
+
/**
|
|
31
|
+
* Use infinite query hook for paginated GET endpoints.
|
|
32
|
+
*/
|
|
33
|
+
useInfiniteQuery: <T extends QueryEndpoint<Paths>, TPageData = ExtractEndpointResponse<Paths, T>, TPageParam = unknown>(endpoint: T, options: Omit<UseInfiniteQueryOptions<TPageData, Error, {
|
|
34
|
+
pages: TPageData[];
|
|
35
|
+
pageParams: TPageParam[];
|
|
36
|
+
}, unknown[], TPageParam>, 'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'> & {
|
|
37
|
+
getNextPageParam: (lastPage: TPageData, allPages: TPageData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined;
|
|
38
|
+
initialPageParam: TPageParam;
|
|
39
|
+
}, config?: FilteredRequestConfig<Paths, T>) => UseInfiniteQueryResult<{
|
|
40
|
+
pages: TPageData[];
|
|
41
|
+
pageParams: TPageParam[];
|
|
42
|
+
}, Error>;
|
|
43
|
+
/**
|
|
44
|
+
* Invalidate queries for a specific endpoint.
|
|
45
|
+
*/
|
|
46
|
+
invalidateQueries: <T extends QueryEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Invalidate all queries in the cache.
|
|
49
|
+
*/
|
|
50
|
+
invalidateAllQueries: () => Promise<void>;
|
|
30
51
|
/**
|
|
31
52
|
* Build a query key for manual cache operations
|
|
32
53
|
*/
|
|
@@ -47,7 +68,7 @@ interface EndpointHooks<Paths> {
|
|
|
47
68
|
* await mutation.mutateAsync({ body: { name: 'John' } });
|
|
48
69
|
* ```
|
|
49
70
|
*/
|
|
50
|
-
declare function createEndpointHooks<Paths>(fetcher: TypedApiFunction<Paths>,
|
|
71
|
+
declare function createEndpointHooks<Paths>(fetcher: TypedApiFunction<Paths>, options?: CreateEndpointHooksOptions): EndpointHooks<Paths>;
|
|
51
72
|
//#endregion
|
|
52
73
|
export { CreateEndpointHooksOptions, EndpointHooks, createEndpointHooks };
|
|
53
74
|
//# sourceMappingURL=endpoint-hooks.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-hooks.d.cts","names":[],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"endpoint-hooks.d.cts","names":[],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;AA4CA;AAOK,UAPY,0BAAA,CAOA;EAAA,WAAA,CAAA,EANF,WAME;;;;;KAAZ,YAAsD,CAAA,KAAA,EAAA,UAAxB,aAAwB,CAAV,KAAU,CAAA,CAAA,GAAA,gBAAA,CAC1D,KAD0D,EAE1D,CAF0D,CAAA,SAAA,IAAA,GAAA,CAAgB,MAK1C,EAAtB,qBAAsB,CAAA,KAAA,EAAO,CAAP,CAAA,EAAK,OAAE,GAC3B,IAD2B,CAEpC,eAFoC,CAEpB,uBAFoB,CAEI,KAFJ,EAEW,CAFX,CAAA,EAEe,KAFf,CAAA,EAAA,UAAA,GAAA,SAAA,CAAA,CAAC,GAAA,CAAT,MAEY,GAKhC,qBALgC,CAKV,KALU,EAKH,CALG,CAAA,EAAK,OAAE,GAMtC,IANsC,CAO/C,eAP+C,CAO/B,uBAP+B,CAOP,KAPO,EAOA,CAPA,CAAA,EAOI,KAPJ,CAAA,EAAA,UAAA,GAAA,SAAA,CAAA,CAAC;;;;AAKlB,UAUjB,aAViB,CAAA,KAAA,CAAA,CAAA;EAAK;;;;EAEa,QAAhC,EAAA,CAAA,UAaE,aAbF,CAagB,KAbhB,CAAA,CAAA,CAAA,QAAA,EAcR,CAdQ,EAAA,GAAA,IAAA,EAeT,YAfS,CAeI,KAfJ,EAeW,CAfX,CAAA,EAAA,GAgBd,cAhBc,CAgBC,uBAhB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iDmB,CAjDnB,CAAA,EAAA,GAkDf,sBAlDe,CAAA;IAAmC,KAAA,EAmD7C,SAnD6C,EAAA;IAAlD,UAAA,EAmD8B,UAnD9B,EAAA;EAAc,CAAA,EAoDlB,KA9CwC,CAAA;EAAK;;;EAId,iBAAE,EAAA,CAAA,UAgDJ,aAhDI,CAgDU,KAhDV,CAAA,CAAA,CAAA,QAAA,EAiDvB,CAjDuB,EAAA,MAAA,CAAA,EAkDxB,qBAlDwB,CAkDF,KAlDE,EAkDK,CAlDL,CAAA,EAAA,GAmD7B,OAnD6B,CAAA,IAAA,CAAA;EAAC;;;EAEL,oBAAE,EAAA,GAAA,GAsDJ,OAtDI,CAAA,IAAA,CAAA;EAAC;;;EAJlB,aASU,EAAA,CAAA,UAsDC,aAtDD,CAsDe,KAtDf,CAAA,CAAA,CAAA,QAAA,EAuDd,CAvDc,EAAA,MAAA,CAAA,EAwDf,qBAxDe,CAwDO,KAxDP,EAwDc,CAxDd,CAAA,EAAA,GAAA,OAAA,EAAA;;;;;;;;;;;;;;;;;AAkBY,iBAyDtB,mBAzDsB,CAAA,KAAA,CAAA,CAAA,OAAA,EA0D5B,gBA1D4B,CA0DX,KA1DW,CAAA,EAAA,OAAA,CAAA,EA2D5B,0BA3D4B,CAAA,EA4DnC,aA5DmC,CA4DrB,KA5DqB,CAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, QueryEndpoint, TypedApiFunction } from "./types-
|
|
2
|
-
import { QueryClient, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
1
|
+
import { ExtractEndpointResponse, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, QueryEndpoint, TypedApiFunction } from "./types-D8Jrl4o3.mjs";
|
|
2
|
+
import { QueryClient, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
3
3
|
|
|
4
4
|
//#region src/endpoint-hooks.d.ts
|
|
5
5
|
|
|
@@ -27,6 +27,27 @@ interface EndpointHooks<Paths> {
|
|
|
27
27
|
* Config with params/body is passed to mutate().
|
|
28
28
|
*/
|
|
29
29
|
useMutation: <T extends MutationEndpoint<Paths>>(endpoint: T, options?: Omit<UseMutationOptions<ExtractEndpointResponse<Paths, T>, Error, FilteredRequestConfig<Paths, T>>, 'mutationFn'>) => UseMutationResult<ExtractEndpointResponse<Paths, T>, Error, FilteredRequestConfig<Paths, T>>;
|
|
30
|
+
/**
|
|
31
|
+
* Use infinite query hook for paginated GET endpoints.
|
|
32
|
+
*/
|
|
33
|
+
useInfiniteQuery: <T extends QueryEndpoint<Paths>, TPageData = ExtractEndpointResponse<Paths, T>, TPageParam = unknown>(endpoint: T, options: Omit<UseInfiniteQueryOptions<TPageData, Error, {
|
|
34
|
+
pages: TPageData[];
|
|
35
|
+
pageParams: TPageParam[];
|
|
36
|
+
}, unknown[], TPageParam>, 'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'> & {
|
|
37
|
+
getNextPageParam: (lastPage: TPageData, allPages: TPageData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined;
|
|
38
|
+
initialPageParam: TPageParam;
|
|
39
|
+
}, config?: FilteredRequestConfig<Paths, T>) => UseInfiniteQueryResult<{
|
|
40
|
+
pages: TPageData[];
|
|
41
|
+
pageParams: TPageParam[];
|
|
42
|
+
}, Error>;
|
|
43
|
+
/**
|
|
44
|
+
* Invalidate queries for a specific endpoint.
|
|
45
|
+
*/
|
|
46
|
+
invalidateQueries: <T extends QueryEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Invalidate all queries in the cache.
|
|
49
|
+
*/
|
|
50
|
+
invalidateAllQueries: () => Promise<void>;
|
|
30
51
|
/**
|
|
31
52
|
* Build a query key for manual cache operations
|
|
32
53
|
*/
|
|
@@ -47,7 +68,7 @@ interface EndpointHooks<Paths> {
|
|
|
47
68
|
* await mutation.mutateAsync({ body: { name: 'John' } });
|
|
48
69
|
* ```
|
|
49
70
|
*/
|
|
50
|
-
declare function createEndpointHooks<Paths>(fetcher: TypedApiFunction<Paths>,
|
|
71
|
+
declare function createEndpointHooks<Paths>(fetcher: TypedApiFunction<Paths>, options?: CreateEndpointHooksOptions): EndpointHooks<Paths>;
|
|
51
72
|
//#endregion
|
|
52
73
|
export { CreateEndpointHooksOptions, EndpointHooks, createEndpointHooks };
|
|
53
74
|
//# sourceMappingURL=endpoint-hooks.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-hooks.d.mts","names":[],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"endpoint-hooks.d.mts","names":[],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":[],"mappings":";;;;;;;AA4CA;AAOK,UAPY,0BAAA,CAOA;EAAA,WAAA,CAAA,EANF,WAME;;;;;KAAZ,YAAsD,CAAA,KAAA,EAAA,UAAxB,aAAwB,CAAV,KAAU,CAAA,CAAA,GAAA,gBAAA,CAC1D,KAD0D,EAE1D,CAF0D,CAAA,SAAA,IAAA,GAAA,CAAgB,MAK1C,EAAtB,qBAAsB,CAAA,KAAA,EAAO,CAAP,CAAA,EAAK,OAAE,GAC3B,IAD2B,CAEpC,eAFoC,CAEpB,uBAFoB,CAEI,KAFJ,EAEW,CAFX,CAAA,EAEe,KAFf,CAAA,EAAA,UAAA,GAAA,SAAA,CAAA,CAAC,GAAA,CAAT,MAEY,GAKhC,qBALgC,CAKV,KALU,EAKH,CALG,CAAA,EAAK,OAAE,GAMtC,IANsC,CAO/C,eAP+C,CAO/B,uBAP+B,CAOP,KAPO,EAOA,CAPA,CAAA,EAOI,KAPJ,CAAA,EAAA,UAAA,GAAA,SAAA,CAAA,CAAC;;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import { useMemo } from "react";
|
|
3
3
|
|
|
4
4
|
//#region src/endpoint-hooks.ts
|
|
@@ -26,7 +26,7 @@ function buildQueryKey(endpoint, config) {
|
|
|
26
26
|
* await mutation.mutateAsync({ body: { name: 'John' } });
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
function createEndpointHooks(fetcher,
|
|
29
|
+
function createEndpointHooks(fetcher, options = {}) {
|
|
30
30
|
return {
|
|
31
31
|
useQuery: (endpoint, ...args) => {
|
|
32
32
|
const [config, queryOptions] = args;
|
|
@@ -55,6 +55,49 @@ function createEndpointHooks(fetcher, _options = {}) {
|
|
|
55
55
|
]);
|
|
56
56
|
return useMutation(memoizedOptions);
|
|
57
57
|
},
|
|
58
|
+
useInfiniteQuery: (endpoint, options$1, config) => {
|
|
59
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
60
|
+
const memoizedOptions = useMemo(() => ({
|
|
61
|
+
queryKey,
|
|
62
|
+
queryFn: ({ pageParam }) => {
|
|
63
|
+
let mergedConfig = config;
|
|
64
|
+
if (pageParam !== void 0 && config) {
|
|
65
|
+
const pageQuery = typeof pageParam === "object" ? pageParam : { page: pageParam };
|
|
66
|
+
mergedConfig = {
|
|
67
|
+
...config,
|
|
68
|
+
query: {
|
|
69
|
+
...config.query,
|
|
70
|
+
...pageQuery
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
} else if (pageParam !== void 0 && !config) {
|
|
74
|
+
const pageQuery = typeof pageParam === "object" ? pageParam : { page: pageParam };
|
|
75
|
+
mergedConfig = { query: pageQuery };
|
|
76
|
+
}
|
|
77
|
+
return fetcher(endpoint, mergedConfig);
|
|
78
|
+
},
|
|
79
|
+
...options$1
|
|
80
|
+
}), [
|
|
81
|
+
endpoint,
|
|
82
|
+
config,
|
|
83
|
+
fetcher,
|
|
84
|
+
queryKey,
|
|
85
|
+
options$1
|
|
86
|
+
]);
|
|
87
|
+
return useInfiniteQuery(memoizedOptions);
|
|
88
|
+
},
|
|
89
|
+
invalidateQueries: (endpoint, config) => {
|
|
90
|
+
if (!options.queryClient) throw new Error("queryClient is required for invalidateQueries. Pass it to createEndpointHooks options.");
|
|
91
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
92
|
+
return options.queryClient.invalidateQueries({
|
|
93
|
+
queryKey,
|
|
94
|
+
exact: !!config
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
invalidateAllQueries: () => {
|
|
98
|
+
if (!options.queryClient) throw new Error("queryClient is required for invalidateAllQueries. Pass it to createEndpointHooks options.");
|
|
99
|
+
return options.queryClient.invalidateQueries();
|
|
100
|
+
},
|
|
58
101
|
buildQueryKey
|
|
59
102
|
};
|
|
60
103
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-hooks.mjs","names":["endpoint: T","config?: FilteredRequestConfig<Paths, T>","key: unknown[]","fetcher: TypedApiFunction<Paths>","_options: CreateEndpointHooksOptions","mutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>","config: FilteredRequestConfig<Paths, T>"],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":["import type {\n\tQueryClient,\n\tUseMutationOptions,\n\tUseMutationResult,\n\tUseQueryOptions,\n\tUseQueryResult,\n} from '@tanstack/react-query';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport { useMemo } from 'react';\nimport type {\n\tExtractEndpointResponse,\n\tFilteredRequestConfig,\n\tIsConfigRequired,\n\tMutationEndpoint,\n\tQueryEndpoint,\n\tTypedApiFunction,\n} from './types';\n\n/**\n * Build query key from endpoint and config\n */\nfunction buildQueryKey<Paths, T extends QueryEndpoint<Paths>>(\n\tendpoint: T,\n\tconfig?: FilteredRequestConfig<Paths, T>,\n): unknown[] {\n\tconst key: unknown[] = [endpoint];\n\n\tif (config && 'params' in config && config.params) {\n\t\tkey.push({ params: config.params });\n\t}\n\n\tif (config && 'query' in config && config.query) {\n\t\tkey.push({ query: config.query });\n\t}\n\n\treturn key;\n}\n\n/**\n * Options for creating endpoint-based hooks\n */\nexport interface CreateEndpointHooksOptions {\n\tqueryClient?: QueryClient;\n}\n\n/**\n * Hook options type that conditionally requires config\n */\ntype UseQueryArgs<Paths, T extends QueryEndpoint<Paths>> = IsConfigRequired<\n\tPaths,\n\tT\n> extends true\n\t? [\n\t\t\tconfig: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t]\n\t: [\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t];\n\n/**\n * Endpoint-based React Query hooks\n */\nexport interface EndpointHooks<Paths> {\n\t/**\n\t * Use query hook for GET endpoints.\n\t * Config is required when endpoint has path params.\n\t */\n\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\t...args: UseQueryArgs<Paths, T>\n\t) => UseQueryResult<ExtractEndpointResponse<Paths, T>, Error>;\n\n\t/**\n\t * Use mutation hook for POST, PUT, PATCH, DELETE endpoints.\n\t * Config with params/body is passed to mutate().\n\t */\n\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\toptions?: Omit<\n\t\t\tUseMutationOptions<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>,\n\t\t\t'mutationFn'\n\t\t>,\n\t) => UseMutationResult<\n\t\tExtractEndpointResponse<Paths, T>,\n\t\tError,\n\t\tFilteredRequestConfig<Paths, T>\n\t>;\n\n\t/**\n\t * Build a query key for manual cache operations\n\t */\n\tbuildQueryKey: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => unknown[];\n}\n\n/**\n * Create endpoint-based React Query hooks from a typed fetcher.\n *\n * @example\n * ```typescript\n * const fetcher = createAuthAwareFetcher<paths>({ ... });\n * const hooks = createEndpointHooks<paths>(fetcher);\n *\n * // In a component\n * const { data } = hooks.useQuery('GET /users/{id}', { params: { id: '123' } });\n *\n * const mutation = hooks.useMutation('POST /users');\n * await mutation.mutateAsync({ body: { name: 'John' } });\n * ```\n */\nexport function createEndpointHooks<Paths>(\n\tfetcher: TypedApiFunction<Paths>,\n\t_options: CreateEndpointHooksOptions = {},\n): EndpointHooks<Paths> {\n\treturn {\n\t\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\t...args: UseQueryArgs<Paths, T>\n\t\t) => {\n\t\t\t// Parse args - config is first, options is second\n\t\t\tconst [config, queryOptions] = args as [\n\t\t\t\tFilteredRequestConfig<Paths, T> | undefined,\n\t\t\t\t(\n\t\t\t\t\t| Omit<\n\t\t\t\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t\t\t >\n\t\t\t\t\t| undefined\n\t\t\t\t),\n\t\t\t];\n\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: () =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...queryOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, queryOptions],\n\t\t\t);\n\n\t\t\treturn useQuery<ExtractEndpointResponse<Paths, T>, Error>(\n\t\t\t\tmemoizedOptions,\n\t\t\t);\n\t\t},\n\n\t\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tmutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tmutationFn: (config: FilteredRequestConfig<Paths, T>) =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...mutationOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, fetcher, mutationOptions],\n\t\t\t);\n\n\t\t\treturn useMutation<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tbuildQueryKey,\n\t};\n}\n"],"mappings":";;;;;;;AAqBA,SAAS,cACRA,UACAC,QACY;CACZ,MAAMC,MAAiB,CAAC,QAAS;AAEjC,KAAI,UAAU,YAAY,UAAU,OAAO,OAC1C,KAAI,KAAK,EAAE,QAAQ,OAAO,OAAQ,EAAC;AAGpC,KAAI,UAAU,WAAW,UAAU,OAAO,MACzC,KAAI,KAAK,EAAE,OAAO,OAAO,MAAO,EAAC;AAGlC,QAAO;AACP;;;;;;;;;;;;;;;;AAwFD,SAAgB,oBACfC,SACAC,WAAuC,CAAE,GAClB;AACvB,QAAO;EACN,UAAU,CACTJ,UACA,GAAG,SACC;GAEJ,MAAM,CAAC,QAAQ,aAAa,GAAG;GAW/B,MAAM,WAAW,cAAc,UAAU,OAAO;GAEhD,MAAM,kBAAkB,QACvB,OAAO;IACN;IACA,SAAS,MAER,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAQ;IAAS;IAAU;GAAa,EACnD;AAED,UAAO,SACN,gBACA;EACD;EAED,aAAa,CACZA,UACAK,oBAQI;GACJ,MAAM,kBAAkB,QACvB,OAAO;IACN,YAAY,CAACC,WAEZ,AACC,QAIC,UAAU,OAAO;IACpB,GAAG;GACH,IACD;IAAC;IAAU;IAAS;GAAgB,EACpC;AAED,UAAO,YAIL,gBAAgB;EAClB;EAED;CACA;AACD"}
|
|
1
|
+
{"version":3,"file":"endpoint-hooks.mjs","names":["endpoint: T","config?: FilteredRequestConfig<Paths, T>","key: unknown[]","fetcher: TypedApiFunction<Paths>","options: CreateEndpointHooksOptions","mutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>","config: FilteredRequestConfig<Paths, T>","options: Omit<\n\t\t\t\tUseInfiniteQueryOptions<\n\t\t\t\t\tTPageData,\n\t\t\t\t\tError,\n\t\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\t\tunknown[],\n\t\t\t\t\tTPageParam\n\t\t\t\t>,\n\t\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t\t> & {\n\t\t\t\tgetNextPageParam: (\n\t\t\t\t\tlastPage: TPageData,\n\t\t\t\t\tallPages: TPageData[],\n\t\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\t\tallPageParams: TPageParam[],\n\t\t\t\t) => TPageParam | undefined;\n\t\t\t\tinitialPageParam: TPageParam;\n\t\t\t}","options"],"sources":["../src/endpoint-hooks.ts"],"sourcesContent":["import type {\n\tQueryClient,\n\tQueryFunctionContext,\n\tUseInfiniteQueryOptions,\n\tUseInfiniteQueryResult,\n\tUseMutationOptions,\n\tUseMutationResult,\n\tUseQueryOptions,\n\tUseQueryResult,\n} from '@tanstack/react-query';\nimport { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';\nimport { useMemo } from 'react';\nimport type {\n\tExtractEndpointResponse,\n\tFilteredRequestConfig,\n\tIsConfigRequired,\n\tMutationEndpoint,\n\tQueryEndpoint,\n\tTypedApiFunction,\n} from './types';\n\n/**\n * Build query key from endpoint and config\n */\nfunction buildQueryKey<Paths, T extends QueryEndpoint<Paths>>(\n\tendpoint: T,\n\tconfig?: FilteredRequestConfig<Paths, T>,\n): unknown[] {\n\tconst key: unknown[] = [endpoint];\n\n\tif (config && 'params' in config && config.params) {\n\t\tkey.push({ params: config.params });\n\t}\n\n\tif (config && 'query' in config && config.query) {\n\t\tkey.push({ query: config.query });\n\t}\n\n\treturn key;\n}\n\n/**\n * Options for creating endpoint-based hooks\n */\nexport interface CreateEndpointHooksOptions {\n\tqueryClient?: QueryClient;\n}\n\n/**\n * Hook options type that conditionally requires config\n */\ntype UseQueryArgs<Paths, T extends QueryEndpoint<Paths>> = IsConfigRequired<\n\tPaths,\n\tT\n> extends true\n\t? [\n\t\t\tconfig: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t]\n\t: [\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t];\n\n/**\n * Endpoint-based React Query hooks\n */\nexport interface EndpointHooks<Paths> {\n\t/**\n\t * Use query hook for GET endpoints.\n\t * Config is required when endpoint has path params.\n\t */\n\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\t...args: UseQueryArgs<Paths, T>\n\t) => UseQueryResult<ExtractEndpointResponse<Paths, T>, Error>;\n\n\t/**\n\t * Use mutation hook for POST, PUT, PATCH, DELETE endpoints.\n\t * Config with params/body is passed to mutate().\n\t */\n\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\toptions?: Omit<\n\t\t\tUseMutationOptions<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>,\n\t\t\t'mutationFn'\n\t\t>,\n\t) => UseMutationResult<\n\t\tExtractEndpointResponse<Paths, T>,\n\t\tError,\n\t\tFilteredRequestConfig<Paths, T>\n\t>;\n\n\t/**\n\t * Use infinite query hook for paginated GET endpoints.\n\t */\n\tuseInfiniteQuery: <\n\t\tT extends QueryEndpoint<Paths>,\n\t\tTPageData = ExtractEndpointResponse<Paths, T>,\n\t\tTPageParam = unknown,\n\t>(\n\t\tendpoint: T,\n\t\toptions: Omit<\n\t\t\tUseInfiniteQueryOptions<\n\t\t\t\tTPageData,\n\t\t\t\tError,\n\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\tunknown[],\n\t\t\t\tTPageParam\n\t\t\t>,\n\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t> & {\n\t\t\tgetNextPageParam: (\n\t\t\t\tlastPage: TPageData,\n\t\t\t\tallPages: TPageData[],\n\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\tallPageParams: TPageParam[],\n\t\t\t) => TPageParam | undefined;\n\t\t\tinitialPageParam: TPageParam;\n\t\t},\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => UseInfiniteQueryResult<\n\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\tError\n\t>;\n\n\t/**\n\t * Invalidate queries for a specific endpoint.\n\t */\n\tinvalidateQueries: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => Promise<void>;\n\n\t/**\n\t * Invalidate all queries in the cache.\n\t */\n\tinvalidateAllQueries: () => Promise<void>;\n\n\t/**\n\t * Build a query key for manual cache operations\n\t */\n\tbuildQueryKey: <T extends QueryEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => unknown[];\n}\n\n/**\n * Create endpoint-based React Query hooks from a typed fetcher.\n *\n * @example\n * ```typescript\n * const fetcher = createAuthAwareFetcher<paths>({ ... });\n * const hooks = createEndpointHooks<paths>(fetcher);\n *\n * // In a component\n * const { data } = hooks.useQuery('GET /users/{id}', { params: { id: '123' } });\n *\n * const mutation = hooks.useMutation('POST /users');\n * await mutation.mutateAsync({ body: { name: 'John' } });\n * ```\n */\nexport function createEndpointHooks<Paths>(\n\tfetcher: TypedApiFunction<Paths>,\n\toptions: CreateEndpointHooksOptions = {},\n): EndpointHooks<Paths> {\n\treturn {\n\t\tuseQuery: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\t...args: UseQueryArgs<Paths, T>\n\t\t) => {\n\t\t\t// Parse args - config is first, options is second\n\t\t\tconst [config, queryOptions] = args as [\n\t\t\t\tFilteredRequestConfig<Paths, T> | undefined,\n\t\t\t\t(\n\t\t\t\t\t| Omit<\n\t\t\t\t\t\t\tUseQueryOptions<ExtractEndpointResponse<Paths, T>, Error>,\n\t\t\t\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t\t\t >\n\t\t\t\t\t| undefined\n\t\t\t\t),\n\t\t\t];\n\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: () =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...queryOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, queryOptions],\n\t\t\t);\n\n\t\t\treturn useQuery<ExtractEndpointResponse<Paths, T>, Error>(\n\t\t\t\tmemoizedOptions,\n\t\t\t);\n\t\t},\n\n\t\tuseMutation: <T extends MutationEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tmutationOptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\t\tError,\n\t\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tmutationFn: (config: FilteredRequestConfig<Paths, T>) =>\n\t\t\t\t\t\t// Type assertion needed due to complex conditional types\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, config),\n\t\t\t\t\t...mutationOptions,\n\t\t\t\t}),\n\t\t\t\t[endpoint, fetcher, mutationOptions],\n\t\t\t);\n\n\t\t\treturn useMutation<\n\t\t\t\tExtractEndpointResponse<Paths, T>,\n\t\t\t\tError,\n\t\t\t\tFilteredRequestConfig<Paths, T>\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tuseInfiniteQuery: <\n\t\t\tT extends QueryEndpoint<Paths>,\n\t\t\tTPageData = ExtractEndpointResponse<Paths, T>,\n\t\t\tTPageParam = unknown,\n\t\t>(\n\t\t\tendpoint: T,\n\t\t\toptions: Omit<\n\t\t\t\tUseInfiniteQueryOptions<\n\t\t\t\t\tTPageData,\n\t\t\t\t\tError,\n\t\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\t\tunknown[],\n\t\t\t\t\tTPageParam\n\t\t\t\t>,\n\t\t\t\t'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'\n\t\t\t> & {\n\t\t\t\tgetNextPageParam: (\n\t\t\t\t\tlastPage: TPageData,\n\t\t\t\t\tallPages: TPageData[],\n\t\t\t\t\tlastPageParam: TPageParam,\n\t\t\t\t\tallPageParams: TPageParam[],\n\t\t\t\t) => TPageParam | undefined;\n\t\t\t\tinitialPageParam: TPageParam;\n\t\t\t},\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t) => {\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\n\t\t\tconst memoizedOptions = useMemo(\n\t\t\t\t() => ({\n\t\t\t\t\tqueryKey,\n\t\t\t\t\tqueryFn: ({\n\t\t\t\t\t\tpageParam,\n\t\t\t\t\t}: QueryFunctionContext<unknown[], TPageParam>) => {\n\t\t\t\t\t\tlet mergedConfig = config;\n\t\t\t\t\t\tif (pageParam !== undefined && config) {\n\t\t\t\t\t\t\tconst pageQuery =\n\t\t\t\t\t\t\t\ttypeof pageParam === 'object' ? pageParam : { page: pageParam };\n\t\t\t\t\t\t\tmergedConfig = {\n\t\t\t\t\t\t\t\t...config,\n\t\t\t\t\t\t\t\tquery: { ...(config as any).query, ...pageQuery },\n\t\t\t\t\t\t\t} as any;\n\t\t\t\t\t\t} else if (pageParam !== undefined && !config) {\n\t\t\t\t\t\t\tconst pageQuery =\n\t\t\t\t\t\t\t\ttypeof pageParam === 'object' ? pageParam : { page: pageParam };\n\t\t\t\t\t\t\tmergedConfig = { query: pageQuery } as any;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\tfetcher as (\n\t\t\t\t\t\t\t\tendpoint: T,\n\t\t\t\t\t\t\t\tconfig?: unknown,\n\t\t\t\t\t\t\t) => Promise<ExtractEndpointResponse<Paths, T>>\n\t\t\t\t\t\t)(endpoint, mergedConfig) as Promise<TPageData>;\n\t\t\t\t\t},\n\t\t\t\t\t...options,\n\t\t\t\t}),\n\t\t\t\t[endpoint, config, fetcher, queryKey, options],\n\t\t\t);\n\n\t\t\treturn useInfiniteQuery<\n\t\t\t\tTPageData,\n\t\t\t\tError,\n\t\t\t\t{ pages: TPageData[]; pageParams: TPageParam[] },\n\t\t\t\tunknown[],\n\t\t\t\tTPageParam\n\t\t\t>(memoizedOptions);\n\t\t},\n\n\t\tinvalidateQueries: <T extends QueryEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t) => {\n\t\t\tif (!options.queryClient) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'queryClient is required for invalidateQueries. Pass it to createEndpointHooks options.',\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst queryKey = buildQueryKey(endpoint, config);\n\t\t\treturn options.queryClient.invalidateQueries({\n\t\t\t\tqueryKey,\n\t\t\t\texact: !!config,\n\t\t\t});\n\t\t},\n\n\t\tinvalidateAllQueries: () => {\n\t\t\tif (!options.queryClient) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'queryClient is required for invalidateAllQueries. Pass it to createEndpointHooks options.',\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn options.queryClient.invalidateQueries();\n\t\t},\n\n\t\tbuildQueryKey,\n\t};\n}\n"],"mappings":";;;;;;;AAwBA,SAAS,cACRA,UACAC,QACY;CACZ,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C,UAAU,aAAa;IACzB;IACD,GAAGO;GACH,IACD;IAAC;IAAU;IAAQ;IAAS;IAAUA;GAAQ,EAC9C;AAED,UAAO,iBAML,gBAAgB;EAClB;EAED,mBAAmB,CAClBR,UACAC,WACI;AACJ,QAAK,QAAQ,YACZ,OAAM,IAAI,MACT;GAGF,MAAM,WAAW,cAAc,UAAU,OAAO;AAChD,UAAO,QAAQ,YAAY,kBAAkB;IAC5C;IACA,SAAS;GACT,EAAC;EACF;EAED,sBAAsB,MAAM;AAC3B,QAAK,QAAQ,YACZ,OAAM,IAAI,MACT;AAGF,UAAO,QAAQ,YAAY,mBAAmB;EAC9C;EAED;CACA;AACD"}
|
|
@@ -59,13 +59,13 @@ var TypedFetcher = class TypedFetcher {
|
|
|
59
59
|
throw error;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
-
wrap() {
|
|
62
|
+
wrap(onError) {
|
|
63
63
|
return (endpoint, config) => this.request(endpoint, config).then((data) => ({
|
|
64
64
|
ok: true,
|
|
65
65
|
data
|
|
66
|
-
}), (error) => ({
|
|
66
|
+
}), async (error) => ({
|
|
67
67
|
ok: false,
|
|
68
|
-
error
|
|
68
|
+
error: onError ? await onError(error) : error
|
|
69
69
|
}));
|
|
70
70
|
}
|
|
71
71
|
parseEndpoint(endpoint) {
|
|
@@ -80,10 +80,9 @@ var TypedFetcher = class TypedFetcher {
|
|
|
80
80
|
function createTypedFetcher(options) {
|
|
81
81
|
const fetcher = new TypedFetcher(options);
|
|
82
82
|
const fn = (endpoint, config) => fetcher.request(endpoint, config);
|
|
83
|
-
fn
|
|
84
|
-
return fn;
|
|
83
|
+
return Object.assign(fn, { wrap: (onError) => Object.assign(fetcher.wrap(onError), fn, { wrap: void 0 }) });
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
//#endregion
|
|
88
87
|
export { TypedFetcher, createTypedFetcher };
|
|
89
|
-
//# sourceMappingURL=fetcher-
|
|
88
|
+
//# sourceMappingURL=fetcher-CakjC9C7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetcher-CakjC9C7.mjs","names":["fn?: FetchFn","options: FetcherOptions","endpoint: T","config?: FilteredRequestConfig<Paths, T>","requestConfig: RequestInit","onError?: ErrorTransformer<E>","options?: FetcherOptions"],"sources":["../src/fetcher.ts"],"sourcesContent":["import qs from 'qs';\nimport type {\n\tEndpointString,\n\tErrorTransformer,\n\tExtractEndpointResponse,\n\tFetcherOptions,\n\tFilteredRequestConfig,\n\tParseEndpoint,\n\tTypedEndpoint,\n\tWrappedResult,\n} from './types';\n\nexport type { FetcherOptions, WrappedResult } from './types';\n\nexport class TypedFetcher<Paths> {\n\tprivate baseURL: string;\n\tprivate defaultHeaders: Record<string, string>;\n\tprivate options: FetcherOptions;\n\tprivate fetchFn: FetchFn;\n\n\tstatic getFetchFn(fn?: FetchFn): FetchFn {\n\t\tif (fn) {\n\t\t\treturn fn;\n\t\t}\n\n\t\tif (typeof window !== 'undefined' && typeof window.fetch === 'function') {\n\t\t\treturn window.fetch.bind(window);\n\t\t}\n\n\t\tif (\n\t\t\ttypeof globalThis !== 'undefined' &&\n\t\t\ttypeof globalThis.fetch === 'function'\n\t\t) {\n\t\t\treturn globalThis.fetch.bind(globalThis);\n\t\t}\n\n\t\tthrow new Error('No fetch implementation found');\n\t}\n\n\tconstructor(options: FetcherOptions = {}) {\n\t\tthis.baseURL = options.baseURL || '';\n\t\tthis.defaultHeaders = options.headers || {};\n\t\tthis.options = options;\n\t\tthis.fetchFn = TypedFetcher.getFetchFn(options.fetch);\n\t}\n\n\tasync request<T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t): Promise<ExtractEndpointResponse<Paths, T>> {\n\t\tconst { method, route } = this.parseEndpoint(endpoint);\n\n\t\t// Replace path parameters\n\t\tlet url = route;\n\t\tif (config && 'params' in config && config.params) {\n\t\t\tObject.entries(config.params as Record<string, unknown>).forEach(\n\t\t\t\t([key, value]) => {\n\t\t\t\t\turl = url.replace(`{${key}}`, encodeURIComponent(String(value)));\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\t// Add query parameters\n\t\tif (config && 'query' in config && config.query) {\n\t\t\tconst queryString = qs.stringify(config.query, {\n\t\t\t\tencode: true,\n\t\t\t\tarrayFormat: 'brackets',\n\t\t\t\tskipNulls: true,\n\t\t\t});\n\t\t\tif (queryString) {\n\t\t\t\turl += `?${queryString}`;\n\t\t\t}\n\t\t}\n\n\t\t// Build request configuration\n\t\tlet requestConfig: RequestInit = {\n\t\t\tmethod: method.toUpperCase(),\n\t\t\theaders: {\n\t\t\t\t...this.defaultHeaders,\n\t\t\t\t...((config && 'headers' in config && config.headers) || {}),\n\t\t\t},\n\t\t};\n\n\t\t// Add body if present\n\t\tif (config && 'body' in config && config.body) {\n\t\t\trequestConfig.body = JSON.stringify(config.body);\n\t\t\trequestConfig.headers = {\n\t\t\t\t...requestConfig.headers,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t};\n\t\t}\n\n\t\t// Apply request interceptor\n\t\tif (this.options.onRequest) {\n\t\t\trequestConfig = await this.options.onRequest(requestConfig);\n\t\t}\n\n\t\ttry {\n\t\t\t// Make the request\n\t\t\tlet response = await this.fetchFn(`${this.baseURL}${url}`, requestConfig);\n\n\t\t\t// Apply response interceptor\n\t\t\tif (this.options.onResponse) {\n\t\t\t\tresponse = await this.options.onResponse(response);\n\t\t\t}\n\n\t\t\t// Handle errors\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow response;\n\t\t\t}\n\n\t\t\t// Handle empty responses (204 No Content, etc.)\n\t\t\tif (\n\t\t\t\tresponse.status === 204 ||\n\t\t\t\tresponse.headers.get('content-length') === '0'\n\t\t\t) {\n\t\t\t\treturn undefined as ExtractEndpointResponse<Paths, T>;\n\t\t\t}\n\n\t\t\t// Parse JSON response\n\t\t\tconst data = await response.json();\n\t\t\treturn data as ExtractEndpointResponse<Paths, T>;\n\t\t} catch (error) {\n\t\t\t// Apply error handler\n\t\t\tif (this.options.onError) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tawait this.options.onError(error);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\twrap<E = unknown>(onError?: ErrorTransformer<E>) {\n\t\treturn <T extends TypedEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t): Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>> =>\n\t\t\tthis.request(endpoint, config).then(\n\t\t\t\t(data) => ({ ok: true as const, data }),\n\t\t\t\tasync (error) => ({\n\t\t\t\t\tok: false as const,\n\t\t\t\t\terror: onError ? await onError(error) : (error as E),\n\t\t\t\t}),\n\t\t\t);\n\t}\n\n\tprivate parseEndpoint<T extends EndpointString>(\n\t\tendpoint: T,\n\t): ParseEndpoint<T> {\n\t\tconst [method, ...routeParts] = endpoint.split(' ');\n\t\tconst route = routeParts.join(' ');\n\t\treturn { method: method?.toLowerCase() ?? '', route } as ParseEndpoint<T>;\n\t}\n}\n\nexport function createTypedFetcher<Paths>(options?: FetcherOptions) {\n\tconst fetcher = new TypedFetcher<Paths>(options);\n\tconst fn = <T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => fetcher.request(endpoint, config);\n\n\treturn Object.assign(fn, {\n\t\twrap: <E = unknown>(onError?: ErrorTransformer<E>) =>\n\t\t\tObject.assign(fetcher.wrap(onError), fn, {\n\t\t\t\twrap: undefined,\n\t\t\t}),\n\t});\n}\n\nexport type FetchFn = typeof fetch;\n"],"mappings":";;;AAcA,IAAa,eAAb,MAAa,aAAoB;CAChC,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,OAAO,WAAWA,IAAuB;AACxC,MAAI,GACH,QAAO;AAGR,aAAW,WAAW,sBAAsB,OAAO,UAAU,WAC5D,QAAO,OAAO,MAAM,KAAK,OAAO;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AAW,MAAM,KAAK,QAAQ,WAAW,SAAS;AAInD,QAAK,SAAS,GACb,OAAM;AAIP,OACC,SAAS,WAAW,OACpB,SAAS,QAAQ,IAAI,iBAAiB,KAAK,IAE3C;GAID,MAAM,OAAO,MAAM,SAAS,MAAM;AAClC,UAAO;EACP,SAAQ,OAAO;AAEf,OAAI,KAAK,QAAQ,QAEhB,OAAM,KAAK,QAAQ,QAAQ,MAAM;AAElC,SAAM;EACN;CACD;CAED,KAAkBC,SAA+B;AAChD,SAAO,CACNH,UACAC,WAEA,KAAK,QAAQ,UAAU,OAAO,CAAC,KAC9B,CAAC,UAAU;GAAE,IAAI;GAAe;EAAM,IACtC,OAAO,WAAW;GACjB,IAAI;GACJ,OAAO,UAAU,MAAM,QAAQ,MAAM,GAAI;EACzC,GACD;CACF;CAED,AAAQ,cACPD,UACmB;EACnB,MAAM,CAAC,QAAQ,GAAG,WAAW,GAAG,SAAS,MAAM,IAAI;EACnD,MAAM,QAAQ,WAAW,KAAK,IAAI;AAClC,SAAO;GAAE,QAAQ,QAAQ,aAAa,IAAI;GAAI;EAAO;CACrD;AACD;AAED,SAAgB,mBAA0BI,SAA0B;CACnE,MAAM,UAAU,IAAI,aAAoB;CACxC,MAAM,KAAK,CACVJ,UACAC,WACI,QAAQ,QAAQ,UAAU,OAAO;AAEtC,QAAO,OAAO,OAAO,IAAI,EACxB,MAAM,CAAcE,YACnB,OAAO,OAAO,QAAQ,KAAK,QAAQ,EAAE,IAAI,EACxC,aACA,EAAC,CACH,EAAC;AACF"}
|
|
@@ -60,13 +60,13 @@ var TypedFetcher = class TypedFetcher {
|
|
|
60
60
|
throw error;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
wrap() {
|
|
63
|
+
wrap(onError) {
|
|
64
64
|
return (endpoint, config) => this.request(endpoint, config).then((data) => ({
|
|
65
65
|
ok: true,
|
|
66
66
|
data
|
|
67
|
-
}), (error) => ({
|
|
67
|
+
}), async (error) => ({
|
|
68
68
|
ok: false,
|
|
69
|
-
error
|
|
69
|
+
error: onError ? await onError(error) : error
|
|
70
70
|
}));
|
|
71
71
|
}
|
|
72
72
|
parseEndpoint(endpoint) {
|
|
@@ -81,8 +81,7 @@ var TypedFetcher = class TypedFetcher {
|
|
|
81
81
|
function createTypedFetcher(options) {
|
|
82
82
|
const fetcher = new TypedFetcher(options);
|
|
83
83
|
const fn = (endpoint, config) => fetcher.request(endpoint, config);
|
|
84
|
-
fn
|
|
85
|
-
return fn;
|
|
84
|
+
return Object.assign(fn, { wrap: (onError) => Object.assign(fetcher.wrap(onError), fn, { wrap: void 0 }) });
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
//#endregion
|
|
@@ -98,4 +97,4 @@ Object.defineProperty(exports, 'createTypedFetcher', {
|
|
|
98
97
|
return createTypedFetcher;
|
|
99
98
|
}
|
|
100
99
|
});
|
|
101
|
-
//# sourceMappingURL=fetcher-
|
|
100
|
+
//# sourceMappingURL=fetcher-CwnSqc4D.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetcher-CwnSqc4D.cjs","names":["fn?: FetchFn","options: FetcherOptions","endpoint: T","config?: FilteredRequestConfig<Paths, T>","requestConfig: RequestInit","onError?: ErrorTransformer<E>","options?: FetcherOptions"],"sources":["../src/fetcher.ts"],"sourcesContent":["import qs from 'qs';\nimport type {\n\tEndpointString,\n\tErrorTransformer,\n\tExtractEndpointResponse,\n\tFetcherOptions,\n\tFilteredRequestConfig,\n\tParseEndpoint,\n\tTypedEndpoint,\n\tWrappedResult,\n} from './types';\n\nexport type { FetcherOptions, WrappedResult } from './types';\n\nexport class TypedFetcher<Paths> {\n\tprivate baseURL: string;\n\tprivate defaultHeaders: Record<string, string>;\n\tprivate options: FetcherOptions;\n\tprivate fetchFn: FetchFn;\n\n\tstatic getFetchFn(fn?: FetchFn): FetchFn {\n\t\tif (fn) {\n\t\t\treturn fn;\n\t\t}\n\n\t\tif (typeof window !== 'undefined' && typeof window.fetch === 'function') {\n\t\t\treturn window.fetch.bind(window);\n\t\t}\n\n\t\tif (\n\t\t\ttypeof globalThis !== 'undefined' &&\n\t\t\ttypeof globalThis.fetch === 'function'\n\t\t) {\n\t\t\treturn globalThis.fetch.bind(globalThis);\n\t\t}\n\n\t\tthrow new Error('No fetch implementation found');\n\t}\n\n\tconstructor(options: FetcherOptions = {}) {\n\t\tthis.baseURL = options.baseURL || '';\n\t\tthis.defaultHeaders = options.headers || {};\n\t\tthis.options = options;\n\t\tthis.fetchFn = TypedFetcher.getFetchFn(options.fetch);\n\t}\n\n\tasync request<T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t): Promise<ExtractEndpointResponse<Paths, T>> {\n\t\tconst { method, route } = this.parseEndpoint(endpoint);\n\n\t\t// Replace path parameters\n\t\tlet url = route;\n\t\tif (config && 'params' in config && config.params) {\n\t\t\tObject.entries(config.params as Record<string, unknown>).forEach(\n\t\t\t\t([key, value]) => {\n\t\t\t\t\turl = url.replace(`{${key}}`, encodeURIComponent(String(value)));\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\t// Add query parameters\n\t\tif (config && 'query' in config && config.query) {\n\t\t\tconst queryString = qs.stringify(config.query, {\n\t\t\t\tencode: true,\n\t\t\t\tarrayFormat: 'brackets',\n\t\t\t\tskipNulls: true,\n\t\t\t});\n\t\t\tif (queryString) {\n\t\t\t\turl += `?${queryString}`;\n\t\t\t}\n\t\t}\n\n\t\t// Build request configuration\n\t\tlet requestConfig: RequestInit = {\n\t\t\tmethod: method.toUpperCase(),\n\t\t\theaders: {\n\t\t\t\t...this.defaultHeaders,\n\t\t\t\t...((config && 'headers' in config && config.headers) || {}),\n\t\t\t},\n\t\t};\n\n\t\t// Add body if present\n\t\tif (config && 'body' in config && config.body) {\n\t\t\trequestConfig.body = JSON.stringify(config.body);\n\t\t\trequestConfig.headers = {\n\t\t\t\t...requestConfig.headers,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t};\n\t\t}\n\n\t\t// Apply request interceptor\n\t\tif (this.options.onRequest) {\n\t\t\trequestConfig = await this.options.onRequest(requestConfig);\n\t\t}\n\n\t\ttry {\n\t\t\t// Make the request\n\t\t\tlet response = await this.fetchFn(`${this.baseURL}${url}`, requestConfig);\n\n\t\t\t// Apply response interceptor\n\t\t\tif (this.options.onResponse) {\n\t\t\t\tresponse = await this.options.onResponse(response);\n\t\t\t}\n\n\t\t\t// Handle errors\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow response;\n\t\t\t}\n\n\t\t\t// Handle empty responses (204 No Content, etc.)\n\t\t\tif (\n\t\t\t\tresponse.status === 204 ||\n\t\t\t\tresponse.headers.get('content-length') === '0'\n\t\t\t) {\n\t\t\t\treturn undefined as ExtractEndpointResponse<Paths, T>;\n\t\t\t}\n\n\t\t\t// Parse JSON response\n\t\t\tconst data = await response.json();\n\t\t\treturn data as ExtractEndpointResponse<Paths, T>;\n\t\t} catch (error) {\n\t\t\t// Apply error handler\n\t\t\tif (this.options.onError) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tawait this.options.onError(error);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\twrap<E = unknown>(onError?: ErrorTransformer<E>) {\n\t\treturn <T extends TypedEndpoint<Paths>>(\n\t\t\tendpoint: T,\n\t\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t\t): Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>> =>\n\t\t\tthis.request(endpoint, config).then(\n\t\t\t\t(data) => ({ ok: true as const, data }),\n\t\t\t\tasync (error) => ({\n\t\t\t\t\tok: false as const,\n\t\t\t\t\terror: onError ? await onError(error) : (error as E),\n\t\t\t\t}),\n\t\t\t);\n\t}\n\n\tprivate parseEndpoint<T extends EndpointString>(\n\t\tendpoint: T,\n\t): ParseEndpoint<T> {\n\t\tconst [method, ...routeParts] = endpoint.split(' ');\n\t\tconst route = routeParts.join(' ');\n\t\treturn { method: method?.toLowerCase() ?? '', route } as ParseEndpoint<T>;\n\t}\n}\n\nexport function createTypedFetcher<Paths>(options?: FetcherOptions) {\n\tconst fetcher = new TypedFetcher<Paths>(options);\n\tconst fn = <T extends TypedEndpoint<Paths>>(\n\t\tendpoint: T,\n\t\tconfig?: FilteredRequestConfig<Paths, T>,\n\t) => fetcher.request(endpoint, config);\n\n\treturn Object.assign(fn, {\n\t\twrap: <E = unknown>(onError?: ErrorTransformer<E>) =>\n\t\t\tObject.assign(fetcher.wrap(onError), fn, {\n\t\t\t\twrap: undefined,\n\t\t\t}),\n\t});\n}\n\nexport type FetchFn = typeof fetch;\n"],"mappings":";;;;AAcA,IAAa,eAAb,MAAa,aAAoB;CAChC,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,OAAO,WAAWA,IAAuB;AACxC,MAAI,GACH,QAAO;AAGR,aAAW,WAAW,sBAAsB,OAAO,UAAU,WAC5D,QAAO,OAAO,MAAM,KAAK,OAAO;AAGjC,aACQ,eAAe,sBACf,WAAW,UAAU,WAE5B,QAAO,WAAW,MAAM,KAAK,WAAW;AAGzC,QAAM,IAAI,MAAM;CAChB;CAED,YAAYC,UAA0B,CAAE,GAAE;AACzC,OAAK,UAAU,QAAQ,WAAW;AAClC,OAAK,iBAAiB,QAAQ,WAAW,CAAE;AAC3C,OAAK,UAAU;AACf,OAAK,UAAU,aAAa,WAAW,QAAQ,MAAM;CACrD;CAED,MAAM,QACLC,UACAC,QAC6C;EAC7C,MAAM,EAAE,QAAQ,OAAO,GAAG,KAAK,cAAc,SAAS;EAGtD,IAAI,MAAM;AACV,MAAI,UAAU,YAAY,UAAU,OAAO,OAC1C,QAAO,QAAQ,OAAO,OAAkC,CAAC,QACxD,CAAC,CAAC,KAAK,MAAM,KAAK;AACjB,SAAM,IAAI,SAAS,GAAG,IAAI,IAAI,mBAAmB,OAAO,MAAM,CAAC,CAAC;EAChE,EACD;AAIF,MAAI,UAAU,WAAW,UAAU,OAAO,OAAO;GA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;AAClC,SAAO;GAAE,QAAQ,QAAQ,aAAa,IAAI;GAAI;EAAO;CACrD;AACD;AAED,SAAgB,mBAA0BI,SAA0B;CACnE,MAAM,UAAU,IAAI,aAAoB;CACxC,MAAM,KAAK,CACVJ,UACAC,WACI,QAAQ,QAAQ,UAAU,OAAO;AAEtC,QAAO,OAAO,OAAO,IAAI,EACxB,MAAM,CAAcE,YACnB,OAAO,OAAO,QAAQ,KAAK,QAAQ,EAAE,IAAI,EACxC,aACA,EAAC,CACH,EAAC;AACF"}
|
package/dist/fetcher.cjs
CHANGED
package/dist/fetcher.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint, WrappedResult } from "./types-
|
|
1
|
+
import { ErrorTransformer, ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint, WrappedResult } from "./types-DMQMHePI.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/fetcher.d.ts
|
|
4
4
|
declare class TypedFetcher<Paths> {
|
|
@@ -9,12 +9,13 @@ declare class TypedFetcher<Paths> {
|
|
|
9
9
|
static getFetchFn(fn?: FetchFn): FetchFn;
|
|
10
10
|
constructor(options?: FetcherOptions);
|
|
11
11
|
request<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T>>;
|
|
12
|
-
wrap(): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T
|
|
12
|
+
wrap<E = unknown>(onError?: ErrorTransformer<E>): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>;
|
|
13
13
|
private parseEndpoint;
|
|
14
14
|
}
|
|
15
|
-
declare function createTypedFetcher<Paths>(options?: FetcherOptions): {
|
|
16
|
-
<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T
|
|
17
|
-
|
|
15
|
+
declare function createTypedFetcher<Paths>(options?: FetcherOptions): (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>) & {
|
|
16
|
+
wrap: <E = unknown>(onError?: ErrorTransformer<E>) => (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T> | undefined) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>) & (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>) & {
|
|
17
|
+
wrap: undefined;
|
|
18
|
+
};
|
|
18
19
|
};
|
|
19
20
|
type FetchFn = typeof fetch;
|
|
20
21
|
//# sourceMappingURL=fetcher.d.ts.map
|
package/dist/fetcher.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.cts","names":[],"sources":["../src/fetcher.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"fetcher.d.cts","names":[],"sources":["../src/fetcher.ts"],"sourcesContent":[],"mappings":";;;cAca;EAAA,QAAA,OAAY;EAAA,QAAA,cAAA;EAAA,QAMD,OAAA;EAAO,QAAG,OAAA;EAAO,OAmBnB,UAAA,CAAA,EAAA,CAAA,EAnBE,OAmBF,CAAA,EAnBY,OAmBZ;EAAmB,WAOF,CAAA,OAAA,CAAA,EAPjB,cAOiB;EAAK,OAAnB,CAAA,UAAA,aAAA,CAAc,KAAd,CAAA,CAAA,CAAA,QAAA,EACb,CADa,EAAA,MAAA,CAAA,EAEd,qBAFc,CAEQ,KAFR,EAEe,CAFf,CAAA,CAAA,EAGrB,OAHqB,CAGb,uBAHa,CAGW,KAHX,EAGkB,CAHlB,CAAA,CAAA;EAAa,I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AA,CAAA,EA8GzB,qBA9GyB,CA8GH,KA9GG,EA8GI,CA9GJ,CAAA,EAAA,GA8GM,OA9GN,CA8GM,uBA9GN,CA8GM,KA9GN,EA8GM,CA9GN,CAAA,CAAA,CAAA,GAAA;IAAO,IAAA,EAAA,SAAA;EAAC,CAAA;CAAT;AAmFW,KAsClC,OAAA,GAtCkC,OAsCjB,KAtCiB"}
|
package/dist/fetcher.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint, WrappedResult } from "./types-
|
|
1
|
+
import { ErrorTransformer, ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint, WrappedResult } from "./types-D8Jrl4o3.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/fetcher.d.ts
|
|
4
4
|
declare class TypedFetcher<Paths> {
|
|
@@ -9,12 +9,13 @@ declare class TypedFetcher<Paths> {
|
|
|
9
9
|
static getFetchFn(fn?: FetchFn): FetchFn;
|
|
10
10
|
constructor(options?: FetcherOptions);
|
|
11
11
|
request<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T>>;
|
|
12
|
-
wrap(): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T
|
|
12
|
+
wrap<E = unknown>(onError?: ErrorTransformer<E>): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>;
|
|
13
13
|
private parseEndpoint;
|
|
14
14
|
}
|
|
15
|
-
declare function createTypedFetcher<Paths>(options?: FetcherOptions): {
|
|
16
|
-
<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T
|
|
17
|
-
|
|
15
|
+
declare function createTypedFetcher<Paths>(options?: FetcherOptions): (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>) & {
|
|
16
|
+
wrap: <E = unknown>(onError?: ErrorTransformer<E>) => (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T> | undefined) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>) & (<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>) & {
|
|
17
|
+
wrap: undefined;
|
|
18
|
+
};
|
|
18
19
|
};
|
|
19
20
|
type FetchFn = typeof fetch;
|
|
20
21
|
//# sourceMappingURL=fetcher.d.ts.map
|
package/dist/fetcher.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.mts","names":[],"sources":["../src/fetcher.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"fetcher.d.mts","names":[],"sources":["../src/fetcher.ts"],"sourcesContent":[],"mappings":";;;cAca;EAAA,QAAA,OAAY;EAAA,QAAA,cAAA;EAAA,QAMD,OAAA;EAAO,QAAG,OAAA;EAAO,OAmBnB,UAAA,CAAA,EAAA,CAAA,EAnBE,OAmBF,CAAA,EAnBY,OAmBZ;EAAmB,WAOF,CAAA,OAAA,CAAA,EAPjB,cAOiB;EAAK,OAAnB,CAAA,UAAA,aAAA,CAAc,KAAd,CAAA,CAAA,CAAA,QAAA,EACb,CADa,EAAA,MAAA,CAAA,EAEd,qBAFc,CAEQ,KAFR,EAEe,CAFf,CAAA,CAAA,EAGrB,OAHqB,CAGb,uBAHa,CAGW,KAHX,EAGkB,CAHlB,CAAA,CAAA;EAAa,IAC1B,CAAA,IAAA,OAAA,CAAA,CAAA,OAAA,CAAA,EAqFiB,gBArFjB,CAqFkC,CArFlC,CAAA,CAAA,EAAA,CAAA,U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,KA9GN,EA8GM,CA9GN,CAAA,CAAA,CAAA,GAAA;IAAO,IAAA,EAAA,SAAA;EAAC,CAAA;CAAT;AAmFW,KAsClC,OAAA,GAtCkC,OAsCjB,KAtCiB"}
|
package/dist/fetcher.mjs
CHANGED
package/dist/openapi-hooks.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_fetcher = require('./fetcher-
|
|
2
|
+
const require_fetcher = require('./fetcher-CwnSqc4D.cjs');
|
|
3
3
|
const __tanstack_react_query = require_chunk.__toESM(require("@tanstack/react-query"));
|
|
4
4
|
|
|
5
5
|
//#region src/openapi-hooks.ts
|