@geekmidas/client 4.0.3 → 4.0.4
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 +6 -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 +1 -1
- 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 +1 -1
- 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 +3 -3
- 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,11 @@
|
|
|
1
1
|
# @geekmidas/client
|
|
2
2
|
|
|
3
|
+
## 4.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ✨ [`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
|
|
8
|
+
|
|
3
9
|
## 4.0.3
|
|
4
10
|
|
|
5
11
|
### 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,OAA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ilteredRequestConfig, 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,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;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,0BA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J,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"}
|
package/dist/endpoint-hooks.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
1
|
+
import { useInfiniteQuery, useMutation, useQuery } from "@tanstack/react-query";
|
|
2
2
|
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,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,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,kBAAkB,CAKjBN,UACAO,WAkBAN,WACI;GACJ,MAAM,WAAW,cAAc,UAAU,OAAO;GAEhD,MAAM,kBAAkB,QACvB,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,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;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;GAChD,MAAM,cAAc,GAAG,UAAU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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,W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InD,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"}
|
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,IAC1B,CAAA,IAAA,OAAA,CAAA,CAAA,OAAA,CAAA,EAqFiB,gBArFjB,CAqFkC,CArFlC,CAAA,CAAA,EAAA,CAAA,UAsFQ,aAtFR,CAsFsB,KAtFtB,CAAA,CAAA,CAAA,QAAA,EAuFC,CAvFD,EAAA,MAAA,CAAA,EAwFA,qBAxFA,CAwFsB,KAxFtB,EAwF6B,CAxF7B,CAAA,EAAA,GAyFP,OAzFO,CAyFC,aAzFD,CAyFe,uBAzFf,CAyFuC,KAzFvC,EAyF8C,CAzF9C,CAAA,EAyFkD,CAzFlD,CAAA,CAAA;EAAC,QACoB,aAAA;;AAAtB,iBA2GK,kBA3GL,CAAA,KAAA,CAAA,CAAA,OAAA,CAAA,EA2GyC,cA3GzC,CAAA,EAAA,CAAA,CAAA,UA6GY,aA7GZ,CA6G0B,KA7G1B,CAAA,CAAA,CAAA,QAAA,EA8GC,CA9GD,EAAA,MAAA,CAAA,EA+GA,qBA/GA,CA+GsB,KA/GtB,EA+G6B,CA/G7B,CAAA,EAAA,GA+G+B,OA/G/B,CA+G+B,uBA/G/B,CA+G+B,KA/G/B,EA+G+B,CA/G/B,CAAA,CAAA,CAAA,GAAA;EAAqB,IACI,EAAA,CAAA,IAAA,OAAA,CAAA,CAAA,OAAA,CAAA,EAkHJ,gBAlHI,CAkHa,CAlHb,CAAA,EAAA,GAAA,CAAA,CAAA,UAoFzB,aApFyB,CAoFzB,KApFyB,CAAA,CAAA,CAAA,QAAA,EAoFzB,CApFyB,EAAA,MAAA,CAAA,EAoFzB,qBApFyB,CAoFzB,KApFyB,EAoFzB,CApFyB,CAAA,GAAA,SAAA,EAAA,GAoFzB,OApFyB,CAoFzB,aApFyB,CAoFzB,uBApFyB,CAoFzB,KApFyB,EAoFzB,CApFyB,CAAA,EAoFzB,CApFyB,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,UA4Gb,aA5Ga,CA4GC,KA5GD,CAAA,CAAA,CAAA,QAAA,EA6GxB,CA7GwB,EAAA,MAAA,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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,IAAA,OAAA,CAAA,CAAA,OAAA,CAAA,EAkHJ,gBAlHI,CAkHa,CAlHb,CAAA,EAAA,GAAA,CAAA,CAAA,UAoFzB,aApFyB,CAoFzB,KApFyB,CAAA,CAAA,CAAA,QAAA,EAoFzB,CApFyB,EAAA,MAAA,CAAA,EAoFzB,qBApFyB,CAoFzB,KApFyB,EAoFzB,CApFyB,CAAA,GAAA,SAAA,EAAA,GAoFzB,OApFyB,CAoFzB,aApFyB,CAoFzB,uBApFyB,CAoFzB,KApFyB,EAoFzB,CApFyB,CAAA,EAoFzB,CApFyB,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,UA4Gb,aA5Ga,CA4GC,KA5GD,CAAA,CAAA,CAAA,QAAA,EA6GxB,CA7GwB,EAAA,MAAA,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.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
|
package/dist/openapi-hooks.d.cts
CHANGED
package/dist/openapi-hooks.d.mts
CHANGED
package/dist/openapi-hooks.mjs
CHANGED
package/dist/react-query.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
|
const react = require_chunk.__toESM(require("react"));
|
|
5
5
|
|
package/dist/react-query.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, MutationEndpoint, QueryEndpoint, TypedEndpoint } from "./types-
|
|
1
|
+
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, MutationEndpoint, QueryEndpoint, TypedEndpoint } from "./types-DMQMHePI.cjs";
|
|
2
2
|
import * as _tanstack_react_query3 from "@tanstack/react-query";
|
|
3
3
|
import { QueryClient, UseInfiniteQueryOptions, UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
4
|
|
package/dist/react-query.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, MutationEndpoint, QueryEndpoint, TypedEndpoint } from "./types-
|
|
1
|
+
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, MutationEndpoint, QueryEndpoint, TypedEndpoint } from "./types-D8Jrl4o3.mjs";
|
|
2
2
|
import * as _tanstack_react_query3 from "@tanstack/react-query";
|
|
3
3
|
import { QueryClient, UseInfiniteQueryOptions, UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
4
|
|
package/dist/react-query.mjs
CHANGED
|
@@ -96,14 +96,15 @@ interface FetcherOptions {
|
|
|
96
96
|
onError?: (error: Error) => void | Promise<void>;
|
|
97
97
|
fetch?: typeof fetch;
|
|
98
98
|
}
|
|
99
|
-
type WrappedResult<T> = {
|
|
99
|
+
type WrappedResult<T, E = unknown> = {
|
|
100
100
|
ok: true;
|
|
101
101
|
data: T;
|
|
102
102
|
} | {
|
|
103
103
|
ok: false;
|
|
104
|
-
error:
|
|
104
|
+
error: E;
|
|
105
105
|
};
|
|
106
|
-
type
|
|
106
|
+
type ErrorTransformer<E> = (error: unknown) => E | Promise<E>;
|
|
107
|
+
type WrappedApiFunction<Paths, E = unknown> = <T extends TypedEndpoint<Paths>>(endpoint: T, ...args: IsConfigRequired<Paths, T> extends true ? [config: FilteredRequestConfig<Paths, T>] : [config?: FilteredRequestConfig<Paths, T>]) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>;
|
|
107
108
|
//#endregion
|
|
108
|
-
export { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
109
|
-
//# sourceMappingURL=types-
|
|
109
|
+
export { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
110
|
+
//# sourceMappingURL=types-D8Jrl4o3.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types-
|
|
1
|
+
{"version":3,"file":"types-D8Jrl4o3.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";KAAY,6BAA6B;AAAzC,KAEK,UAAA,GAFO,KAAa,GAAA,MAAgB,GAAA,KAAK,GAAA,OAAA,GAAA,QAAA,GAAA,MAAA,GAAA,SAAA;AAEzC,KASO,aATG,CAAA,KAAA,EAAA,cASgC,aAThC,CAS8C,KAT9C,CAAA,CAAA,GASwD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oI6B,CApI7B,CAAA,SAAA,IAAA,GAAA,CAAA,MAAA,EAqIQ,qBArIR,CAqI8B,KArI9B,EAqIqC,CArIrC,CAAA,CAAA,GAAA,CAAA,MAAA,GAsIS,qBAtIT,CAsI+B,KAtI/B,EAsIsC,CAtItC,CAAA,CAAA,EAAA,GAuIA,OAvIA,CAuIQ,aAvIR,CAuIsB,uBAvItB,CAuI8C,KAvI9C,EAuIqD,CAvIrD,CAAA,EAuIyD,CAvIzD,CAAA,CAAA"}
|
|
@@ -96,14 +96,15 @@ interface FetcherOptions {
|
|
|
96
96
|
onError?: (error: Error) => void | Promise<void>;
|
|
97
97
|
fetch?: typeof fetch;
|
|
98
98
|
}
|
|
99
|
-
type WrappedResult<T> = {
|
|
99
|
+
type WrappedResult<T, E = unknown> = {
|
|
100
100
|
ok: true;
|
|
101
101
|
data: T;
|
|
102
102
|
} | {
|
|
103
103
|
ok: false;
|
|
104
|
-
error:
|
|
104
|
+
error: E;
|
|
105
105
|
};
|
|
106
|
-
type
|
|
106
|
+
type ErrorTransformer<E> = (error: unknown) => E | Promise<E>;
|
|
107
|
+
type WrappedApiFunction<Paths, E = unknown> = <T extends TypedEndpoint<Paths>>(endpoint: T, ...args: IsConfigRequired<Paths, T> extends true ? [config: FilteredRequestConfig<Paths, T>] : [config?: FilteredRequestConfig<Paths, T>]) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>;
|
|
107
108
|
//#endregion
|
|
108
|
-
export { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
109
|
-
//# sourceMappingURL=types-
|
|
109
|
+
export { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
110
|
+
//# sourceMappingURL=types-DMQMHePI.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types-
|
|
1
|
+
{"version":3,"file":"types-DMQMHePI.d.cts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";KAAY,6BAA6B;AAAzC,KAEK,UAAA,GAFO,KAAa,GAAA,MAAgB,GAAA,KAAK,GAAA,OAAA,GAAA,QAAA,GAAA,MAAA,GAAA,SAAA;AAEzC,KASO,aATG,CAAA,KAAA,EAAA,cASgC,aAThC,CAS8C,KAT9C,CAAA,CAAA,GASwD,OATxD,CAAA,MAUR,KAVQ,CAUF,KAVE,CAAA,EAWd,UAXc,CAAA;AASH,KAKA,iBALa,CAAA,KAAA,EAAA,cAOV,aAPU,CAOI,KAPJ,CAAA,CAAA,GAQrB,KARqB,CAQf,KARe,CAAA,SAAA;EAAA,UAAA,CAAA,EAAA;IAAoC,IAAA,CAAA,EAAA,KAAA,EAAA;EAAK,CAAA;CAAN,GAWzD,CAXyD,GACrD,KAAA;AAAM,KAaD,kBAbC,CAAA,KAAA,EAAA,cAeE,aAfF,CAegB,KAfhB,CAAA,EAAA,eAgBG,aAhBH,CAgBiB,KAhBjB,EAgBwB,KAhBxB,CAAA,CAAA,GAiBT,KAjBS,CAiBH,KAjBG,CAAA,CAiBI,MAjBJ,CAAA,SAAA;EAAK,UACjB,CAAA,EAAA;IAFsE,KAAA,CAAA,EAAA,KAAA,EAAA;EAAO,CAAA;AAK9E,CAAA,GAcG,CAdS,GAAA,KAAA;AAAiB,KAiBjB,kBAjBiB,CAAA,KAAA,EAAA,cAmBd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A,IAAA,GA8HmC,OA9HnC,CAAA,IAAA,CAAA;EAAa,KAA2B,CAAA,EAAA,OA+HzB,KA/HyB;;AAAoB,KAkIjD,aAlIiD,CAAA,CAAA,EAAA,IAAA,OAAA,CAAA,GAAA;EAGjD,EAAA,EAAA,IAAA;EAAsB,IAAA,EAgIb,CAhIa;CAAA,GAAA;EAGX,EAAA,EAAnB,KAAA;EAAa,KACd,EA6HoB,CA7HpB;CAAC;AACA,KA8HQ,gBA9HR,CAAA,CAAA,CAAA,GAAA,CAAA,KAAA,EAAA,OAAA,EAAA,GA8HkD,CA9HlD,GA8HsD,OA9HtD,CA8H8D,CA9H9D,CAAA;AAAC,KAgIO,kBAhIP,CAAA,KAAA,EAAA,IAAA,OAAA,CAAA,GAAA,CAAA,UAiIM,aAjIN,CAiIoB,KAjIpB,CAAA,CAAA,CAAA,QAAA,EAmIM,CAnIN,EAAA,GAAA,IAAA,EAoIK,gBApIL,CAoIsB,KApItB,EAoI6B,CApI7B,CAAA,SAAA,IAAA,GAAA,CAAA,MAAA,EAqIQ,qBArIR,CAqI8B,KArI9B,EAqIqC,CArIrC,CAAA,CAAA,GAAA,CAAA,MAAA,GAsIS,qBAtIT,CAsI+B,KAtI/B,EAsIsC,CAtItC,CAAA,CAAA,EAAA,GAuIA,OAvIA,CAuIQ,aAvIR,CAuIsB,uBAvItB,CAuI8C,KAvI9C,EAuIqD,CAvIrD,CAAA,EAuIyD,CAvIzD,CAAA,CAAA"}
|
package/dist/types.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult } from "./types-
|
|
2
|
-
export { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
1
|
+
import { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult } from "./types-DMQMHePI.cjs";
|
|
2
|
+
export { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
package/dist/types.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult } from "./types-
|
|
2
|
-
export { EndpointString, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
|
1
|
+
import { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult } from "./types-D8Jrl4o3.mjs";
|
|
2
|
+
export { EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, QueryEndpoint, RequestConfig, TypedApiFunction, TypedEndpoint, ValidEndpoint, WrappedApiFunction, WrappedResult };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/client",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
"@types/react-dom": "~19.1.6",
|
|
69
69
|
"jsdom": "~26.1.0",
|
|
70
70
|
"msw": "~2.10.3",
|
|
71
|
-
"@geekmidas/
|
|
72
|
-
"@geekmidas/
|
|
71
|
+
"@geekmidas/constructs": "^3.0.3",
|
|
72
|
+
"@geekmidas/schema": "^1.0.0"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"@tanstack/react-query": ">=5.0.0",
|
|
@@ -477,6 +477,64 @@ describe('TypedFetcher', () => {
|
|
|
477
477
|
expect('data' in result).toBe(false);
|
|
478
478
|
expect(onError).toHaveBeenCalledWith(expect.any(Response));
|
|
479
479
|
});
|
|
480
|
+
|
|
481
|
+
it('should transform error with callback', async () => {
|
|
482
|
+
const client = createTypedFetcher<paths>({
|
|
483
|
+
baseURL: 'https://api.example.com',
|
|
484
|
+
});
|
|
485
|
+
const wrappedClient = client.wrap(async (error) => {
|
|
486
|
+
const response = error as Response;
|
|
487
|
+
const body = await response.json();
|
|
488
|
+
return { status: response.status, message: body.message };
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
const result = await wrappedClient('GET /users/{id}', {
|
|
492
|
+
params: { id: '404' },
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
expect(result.ok).toBe(false);
|
|
496
|
+
if (!result.ok) {
|
|
497
|
+
expect(result.error).toEqual({
|
|
498
|
+
status: 404,
|
|
499
|
+
message: 'User not found',
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it('should infer error type from transformer', async () => {
|
|
505
|
+
const client = createTypedFetcher<paths>({
|
|
506
|
+
baseURL: 'https://api.example.com',
|
|
507
|
+
});
|
|
508
|
+
const wrappedClient = client.wrap(async (error) => {
|
|
509
|
+
const response = error as Response;
|
|
510
|
+
return { status: response.status, message: 'something went wrong' };
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
const result = await wrappedClient('GET /error');
|
|
514
|
+
|
|
515
|
+
expect(result.ok).toBe(false);
|
|
516
|
+
if (!result.ok) {
|
|
517
|
+
expect(result.error.status).toBe(500);
|
|
518
|
+
expect(result.error.message).toBe('something went wrong');
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it('should preserve extra properties on wrapped function', async () => {
|
|
523
|
+
const client = createTypedFetcher<paths>({
|
|
524
|
+
baseURL: 'https://api.example.com',
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
// Simulate attaching hooks (like useQuery/useMutation)
|
|
528
|
+
const mockUseQuery = vi.fn();
|
|
529
|
+
const mockUseMutation = vi.fn();
|
|
530
|
+
(client as any).useQuery = mockUseQuery;
|
|
531
|
+
(client as any).useMutation = mockUseMutation;
|
|
532
|
+
|
|
533
|
+
const wrappedClient = client.wrap();
|
|
534
|
+
|
|
535
|
+
expect((wrappedClient as any).useQuery).toBe(mockUseQuery);
|
|
536
|
+
expect((wrappedClient as any).useMutation).toBe(mockUseMutation);
|
|
537
|
+
});
|
|
480
538
|
});
|
|
481
539
|
|
|
482
540
|
it('should URL encode special characters in path parameters', async () => {
|
package/src/endpoint-hooks.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
QueryClient,
|
|
3
|
+
QueryFunctionContext,
|
|
4
|
+
UseInfiniteQueryOptions,
|
|
5
|
+
UseInfiniteQueryResult,
|
|
3
6
|
UseMutationOptions,
|
|
4
7
|
UseMutationResult,
|
|
5
8
|
UseQueryOptions,
|
|
6
9
|
UseQueryResult,
|
|
7
10
|
} from '@tanstack/react-query';
|
|
8
|
-
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
11
|
+
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
|
|
9
12
|
import { useMemo } from 'react';
|
|
10
13
|
import type {
|
|
11
14
|
ExtractEndpointResponse,
|
|
@@ -98,6 +101,52 @@ export interface EndpointHooks<Paths> {
|
|
|
98
101
|
FilteredRequestConfig<Paths, T>
|
|
99
102
|
>;
|
|
100
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Use infinite query hook for paginated GET endpoints.
|
|
106
|
+
*/
|
|
107
|
+
useInfiniteQuery: <
|
|
108
|
+
T extends QueryEndpoint<Paths>,
|
|
109
|
+
TPageData = ExtractEndpointResponse<Paths, T>,
|
|
110
|
+
TPageParam = unknown,
|
|
111
|
+
>(
|
|
112
|
+
endpoint: T,
|
|
113
|
+
options: Omit<
|
|
114
|
+
UseInfiniteQueryOptions<
|
|
115
|
+
TPageData,
|
|
116
|
+
Error,
|
|
117
|
+
{ pages: TPageData[]; pageParams: TPageParam[] },
|
|
118
|
+
unknown[],
|
|
119
|
+
TPageParam
|
|
120
|
+
>,
|
|
121
|
+
'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'
|
|
122
|
+
> & {
|
|
123
|
+
getNextPageParam: (
|
|
124
|
+
lastPage: TPageData,
|
|
125
|
+
allPages: TPageData[],
|
|
126
|
+
lastPageParam: TPageParam,
|
|
127
|
+
allPageParams: TPageParam[],
|
|
128
|
+
) => TPageParam | undefined;
|
|
129
|
+
initialPageParam: TPageParam;
|
|
130
|
+
},
|
|
131
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
132
|
+
) => UseInfiniteQueryResult<
|
|
133
|
+
{ pages: TPageData[]; pageParams: TPageParam[] },
|
|
134
|
+
Error
|
|
135
|
+
>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Invalidate queries for a specific endpoint.
|
|
139
|
+
*/
|
|
140
|
+
invalidateQueries: <T extends QueryEndpoint<Paths>>(
|
|
141
|
+
endpoint: T,
|
|
142
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
143
|
+
) => Promise<void>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Invalidate all queries in the cache.
|
|
147
|
+
*/
|
|
148
|
+
invalidateAllQueries: () => Promise<void>;
|
|
149
|
+
|
|
101
150
|
/**
|
|
102
151
|
* Build a query key for manual cache operations
|
|
103
152
|
*/
|
|
@@ -124,7 +173,7 @@ export interface EndpointHooks<Paths> {
|
|
|
124
173
|
*/
|
|
125
174
|
export function createEndpointHooks<Paths>(
|
|
126
175
|
fetcher: TypedApiFunction<Paths>,
|
|
127
|
-
|
|
176
|
+
options: CreateEndpointHooksOptions = {},
|
|
128
177
|
): EndpointHooks<Paths> {
|
|
129
178
|
return {
|
|
130
179
|
useQuery: <T extends QueryEndpoint<Paths>>(
|
|
@@ -199,6 +248,99 @@ export function createEndpointHooks<Paths>(
|
|
|
199
248
|
>(memoizedOptions);
|
|
200
249
|
},
|
|
201
250
|
|
|
251
|
+
useInfiniteQuery: <
|
|
252
|
+
T extends QueryEndpoint<Paths>,
|
|
253
|
+
TPageData = ExtractEndpointResponse<Paths, T>,
|
|
254
|
+
TPageParam = unknown,
|
|
255
|
+
>(
|
|
256
|
+
endpoint: T,
|
|
257
|
+
options: Omit<
|
|
258
|
+
UseInfiniteQueryOptions<
|
|
259
|
+
TPageData,
|
|
260
|
+
Error,
|
|
261
|
+
{ pages: TPageData[]; pageParams: TPageParam[] },
|
|
262
|
+
unknown[],
|
|
263
|
+
TPageParam
|
|
264
|
+
>,
|
|
265
|
+
'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam'
|
|
266
|
+
> & {
|
|
267
|
+
getNextPageParam: (
|
|
268
|
+
lastPage: TPageData,
|
|
269
|
+
allPages: TPageData[],
|
|
270
|
+
lastPageParam: TPageParam,
|
|
271
|
+
allPageParams: TPageParam[],
|
|
272
|
+
) => TPageParam | undefined;
|
|
273
|
+
initialPageParam: TPageParam;
|
|
274
|
+
},
|
|
275
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
276
|
+
) => {
|
|
277
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
278
|
+
|
|
279
|
+
const memoizedOptions = useMemo(
|
|
280
|
+
() => ({
|
|
281
|
+
queryKey,
|
|
282
|
+
queryFn: ({
|
|
283
|
+
pageParam,
|
|
284
|
+
}: QueryFunctionContext<unknown[], TPageParam>) => {
|
|
285
|
+
let mergedConfig = config;
|
|
286
|
+
if (pageParam !== undefined && config) {
|
|
287
|
+
const pageQuery =
|
|
288
|
+
typeof pageParam === 'object' ? pageParam : { page: pageParam };
|
|
289
|
+
mergedConfig = {
|
|
290
|
+
...config,
|
|
291
|
+
query: { ...(config as any).query, ...pageQuery },
|
|
292
|
+
} as any;
|
|
293
|
+
} else if (pageParam !== undefined && !config) {
|
|
294
|
+
const pageQuery =
|
|
295
|
+
typeof pageParam === 'object' ? pageParam : { page: pageParam };
|
|
296
|
+
mergedConfig = { query: pageQuery } as any;
|
|
297
|
+
}
|
|
298
|
+
return (
|
|
299
|
+
fetcher as (
|
|
300
|
+
endpoint: T,
|
|
301
|
+
config?: unknown,
|
|
302
|
+
) => Promise<ExtractEndpointResponse<Paths, T>>
|
|
303
|
+
)(endpoint, mergedConfig) as Promise<TPageData>;
|
|
304
|
+
},
|
|
305
|
+
...options,
|
|
306
|
+
}),
|
|
307
|
+
[endpoint, config, fetcher, queryKey, options],
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
return useInfiniteQuery<
|
|
311
|
+
TPageData,
|
|
312
|
+
Error,
|
|
313
|
+
{ pages: TPageData[]; pageParams: TPageParam[] },
|
|
314
|
+
unknown[],
|
|
315
|
+
TPageParam
|
|
316
|
+
>(memoizedOptions);
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
invalidateQueries: <T extends QueryEndpoint<Paths>>(
|
|
320
|
+
endpoint: T,
|
|
321
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
322
|
+
) => {
|
|
323
|
+
if (!options.queryClient) {
|
|
324
|
+
throw new Error(
|
|
325
|
+
'queryClient is required for invalidateQueries. Pass it to createEndpointHooks options.',
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
const queryKey = buildQueryKey(endpoint, config);
|
|
329
|
+
return options.queryClient.invalidateQueries({
|
|
330
|
+
queryKey,
|
|
331
|
+
exact: !!config,
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
invalidateAllQueries: () => {
|
|
336
|
+
if (!options.queryClient) {
|
|
337
|
+
throw new Error(
|
|
338
|
+
'queryClient is required for invalidateAllQueries. Pass it to createEndpointHooks options.',
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return options.queryClient.invalidateQueries();
|
|
342
|
+
},
|
|
343
|
+
|
|
202
344
|
buildQueryKey,
|
|
203
345
|
};
|
|
204
346
|
}
|
package/src/fetcher.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import qs from 'qs';
|
|
2
2
|
import type {
|
|
3
3
|
EndpointString,
|
|
4
|
+
ErrorTransformer,
|
|
4
5
|
ExtractEndpointResponse,
|
|
5
6
|
FetcherOptions,
|
|
6
7
|
FilteredRequestConfig,
|
|
@@ -129,14 +130,17 @@ export class TypedFetcher<Paths> {
|
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
132
|
|
|
132
|
-
wrap() {
|
|
133
|
+
wrap<E = unknown>(onError?: ErrorTransformer<E>) {
|
|
133
134
|
return <T extends TypedEndpoint<Paths>>(
|
|
134
135
|
endpoint: T,
|
|
135
136
|
config?: FilteredRequestConfig<Paths, T>,
|
|
136
|
-
): Promise<WrappedResult<ExtractEndpointResponse<Paths, T
|
|
137
|
+
): Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>> =>
|
|
137
138
|
this.request(endpoint, config).then(
|
|
138
139
|
(data) => ({ ok: true as const, data }),
|
|
139
|
-
(error) => ({
|
|
140
|
+
async (error) => ({
|
|
141
|
+
ok: false as const,
|
|
142
|
+
error: onError ? await onError(error) : (error as E),
|
|
143
|
+
}),
|
|
140
144
|
);
|
|
141
145
|
}
|
|
142
146
|
|
|
@@ -156,9 +160,12 @@ export function createTypedFetcher<Paths>(options?: FetcherOptions) {
|
|
|
156
160
|
config?: FilteredRequestConfig<Paths, T>,
|
|
157
161
|
) => fetcher.request(endpoint, config);
|
|
158
162
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
163
|
+
return Object.assign(fn, {
|
|
164
|
+
wrap: <E = unknown>(onError?: ErrorTransformer<E>) =>
|
|
165
|
+
Object.assign(fetcher.wrap(onError), fn, {
|
|
166
|
+
wrap: undefined,
|
|
167
|
+
}),
|
|
168
|
+
});
|
|
162
169
|
}
|
|
163
170
|
|
|
164
171
|
export type FetchFn = typeof fetch;
|
package/src/types.ts
CHANGED
|
@@ -210,13 +210,17 @@ export interface FetcherOptions {
|
|
|
210
210
|
fetch?: typeof fetch;
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
export type WrappedResult<T> =
|
|
213
|
+
export type WrappedResult<T, E = unknown> =
|
|
214
214
|
| { ok: true; data: T }
|
|
215
|
-
| { ok: false; error:
|
|
215
|
+
| { ok: false; error: E };
|
|
216
216
|
|
|
217
|
-
export type
|
|
217
|
+
export type ErrorTransformer<E> = (error: unknown) => E | Promise<E>;
|
|
218
|
+
|
|
219
|
+
export type WrappedApiFunction<Paths, E = unknown> = <
|
|
220
|
+
T extends TypedEndpoint<Paths>,
|
|
221
|
+
>(
|
|
218
222
|
endpoint: T,
|
|
219
223
|
...args: IsConfigRequired<Paths, T> extends true
|
|
220
224
|
? [config: FilteredRequestConfig<Paths, T>]
|
|
221
225
|
: [config?: FilteredRequestConfig<Paths, T>]
|
|
222
|
-
) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T
|
|
226
|
+
) => Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher-C7rErA7M.mjs","names":["fn?: FetchFn","options: FetcherOptions","endpoint: T","config?: FilteredRequestConfig<Paths, T>","requestConfig: RequestInit","options?: FetcherOptions"],"sources":["../src/fetcher.ts"],"sourcesContent":["import qs from 'qs';\nimport type {\n\tEndpointString,\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() {\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>>> =>\n\t\t\tthis.request(endpoint, config).then(\n\t\t\t\t(data) => ({ ok: true as const, data }),\n\t\t\t\t(error) => ({ ok: false as const, error }),\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\tfn.wrap = () => fetcher.wrap();\n\n\treturn fn;\n}\n\nexport type FetchFn = typeof fetch;\n"],"mappings":";;;AAaA,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;GAChD,MAAM,cAAc,GAAG,UAAU,OAAO,OAAO;IAC9C,QAAQ;IACR,aAAa;IACb,WAAW;GACX,EAAC;AACF,OAAI,YACH,SAAQ,GAAG,YAAY;EAExB;EAGD,IAAIC,gBAA6B;GAChC,QAAQ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AO,MAAM,QAAQ,MAAM;AAE9B,QAAO;AACP"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher-DtsaihL6.cjs","names":["fn?: FetchFn","options: FetcherOptions","endpoint: T","config?: FilteredRequestConfig<Paths, T>","requestConfig: RequestInit","options?: FetcherOptions"],"sources":["../src/fetcher.ts"],"sourcesContent":["import qs from 'qs';\nimport type {\n\tEndpointString,\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() {\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>>> =>\n\t\t\tthis.request(endpoint, config).then(\n\t\t\t\t(data) => ({ ok: true as const, data }),\n\t\t\t\t(error) => ({ ok: false as const, error }),\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\tfn.wrap = () => fetcher.wrap();\n\n\treturn fn;\n}\n\nexport type FetchFn = typeof fetch;\n"],"mappings":";;;;AAaA,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,iB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