@geekmidas/client 0.0.1
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/README.md +518 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/fetcher-DLDD_7Sa.mjs +86 -0
- package/dist/fetcher-DLDD_7Sa.mjs.map +1 -0
- package/dist/fetcher-KdwHgdAl.cjs +98 -0
- package/dist/fetcher-KdwHgdAl.cjs.map +1 -0
- package/dist/fetcher.cjs +4 -0
- package/dist/fetcher.d.cts +18 -0
- package/dist/fetcher.d.mts +18 -0
- package/dist/fetcher.mjs +3 -0
- package/dist/openapi-hooks.cjs +44 -0
- package/dist/openapi-hooks.cjs.map +1 -0
- package/dist/openapi-hooks.d.cts +99 -0
- package/dist/openapi-hooks.d.mts +99 -0
- package/dist/openapi-hooks.mjs +43 -0
- package/dist/openapi-hooks.mjs.map +1 -0
- package/dist/openapi-types.d.cjs +0 -0
- package/dist/openapi-types.d.cts +443 -0
- package/dist/openapi-types.d.mts +443 -0
- package/dist/openapi.cjs +526 -0
- package/dist/openapi.cjs.map +1 -0
- package/dist/openapi.mjs +501 -0
- package/dist/openapi.mjs.map +1 -0
- package/dist/react-query.cjs +147 -0
- package/dist/react-query.cjs.map +1 -0
- package/dist/react-query.d.cts +77 -0
- package/dist/react-query.d.mts +77 -0
- package/dist/react-query.mjs +141 -0
- package/dist/react-query.mjs.map +1 -0
- package/dist/types-csACmD6U.d.cts +68 -0
- package/dist/types-tMp85Lt_.d.mts +68 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +0 -0
- package/package.json +59 -0
- package/src/__tests__/fetcher.spec.ts +409 -0
- package/src/__tests__/method-restrictions.spec.tsx +227 -0
- package/src/__tests__/openapi-hooks.spec.tsx +558 -0
- package/src/__tests__/react-query-infinite.spec.tsx +1003 -0
- package/src/__tests__/react-query-invalidation.spec.tsx +238 -0
- package/src/__tests__/react-query.spec.tsx +582 -0
- package/src/__tests__/setup.ts +281 -0
- package/src/__tests__/types.spec.ts +134 -0
- package/src/__tests__/url-parsing.spec.ts +196 -0
- package/src/fetcher.ts +173 -0
- package/src/openapi-hooks.ts +193 -0
- package/src/openapi-types.d.ts +440 -0
- package/src/openapi.json +595 -0
- package/src/react-query.ts +341 -0
- package/src/types.ts +149 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/fetcher.ts
|
|
3
|
+
var TypedFetcher = class TypedFetcher {
|
|
4
|
+
baseURL;
|
|
5
|
+
defaultHeaders;
|
|
6
|
+
options;
|
|
7
|
+
fetchFn;
|
|
8
|
+
static getFetchFn(fn) {
|
|
9
|
+
if (fn) return fn;
|
|
10
|
+
if (typeof window !== "undefined" && typeof window.fetch === "function") return window.fetch.bind(window);
|
|
11
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.fetch === "function") return globalThis.fetch.bind(globalThis);
|
|
12
|
+
throw new Error("No fetch implementation found");
|
|
13
|
+
}
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.baseURL = options.baseURL || "";
|
|
16
|
+
this.defaultHeaders = options.headers || {};
|
|
17
|
+
this.options = options;
|
|
18
|
+
this.fetchFn = TypedFetcher.getFetchFn(options.fetch);
|
|
19
|
+
}
|
|
20
|
+
async request(endpoint, config) {
|
|
21
|
+
const { method, route } = this.parseEndpoint(endpoint);
|
|
22
|
+
let url = route;
|
|
23
|
+
if (config && "params" in config && config.params) Object.entries(config.params).forEach(([key, value]) => {
|
|
24
|
+
url = url.replace(`{${key}}`, encodeURIComponent(String(value)));
|
|
25
|
+
});
|
|
26
|
+
if (config && "query" in config && config.query) {
|
|
27
|
+
const queryParams = new URLSearchParams();
|
|
28
|
+
const appendQueryParam = (prefix, value) => {
|
|
29
|
+
if (value === void 0 || value === null) return;
|
|
30
|
+
if (Array.isArray(value)) value.forEach((item) => {
|
|
31
|
+
queryParams.append(prefix, String(item));
|
|
32
|
+
});
|
|
33
|
+
else if (typeof value === "object") Object.entries(value).forEach(([subKey, subValue]) => {
|
|
34
|
+
appendQueryParam(`${prefix}.${subKey}`, subValue);
|
|
35
|
+
});
|
|
36
|
+
else queryParams.append(prefix, String(value));
|
|
37
|
+
};
|
|
38
|
+
Object.entries(config.query).forEach(([key, value]) => {
|
|
39
|
+
appendQueryParam(key, value);
|
|
40
|
+
});
|
|
41
|
+
const queryString = queryParams.toString();
|
|
42
|
+
if (queryString) url += `?${queryString}`;
|
|
43
|
+
}
|
|
44
|
+
let requestConfig = {
|
|
45
|
+
method: method.toUpperCase(),
|
|
46
|
+
headers: {
|
|
47
|
+
...this.defaultHeaders,
|
|
48
|
+
...config && "headers" in config && config.headers || {}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
if (config && "body" in config && config.body) {
|
|
52
|
+
requestConfig.body = JSON.stringify(config.body);
|
|
53
|
+
requestConfig.headers = {
|
|
54
|
+
...requestConfig.headers,
|
|
55
|
+
"Content-Type": "application/json"
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (this.options.onRequest) requestConfig = await this.options.onRequest(requestConfig);
|
|
59
|
+
try {
|
|
60
|
+
let response = await this.fetchFn(`${this.baseURL}${url}`, requestConfig);
|
|
61
|
+
if (this.options.onResponse) response = await this.options.onResponse(response);
|
|
62
|
+
if (!response.ok) throw response;
|
|
63
|
+
if (response.status === 204 || response.headers.get("content-length") === "0") return void 0;
|
|
64
|
+
const data = await response.json();
|
|
65
|
+
return data;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (this.options.onError) await this.options.onError(error);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
parseEndpoint(endpoint) {
|
|
72
|
+
const [method, ...routeParts] = endpoint.split(" ");
|
|
73
|
+
const route = routeParts.join(" ");
|
|
74
|
+
return {
|
|
75
|
+
method: method.toLowerCase(),
|
|
76
|
+
route
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function createTypedFetcher(options) {
|
|
81
|
+
const fetcher = new TypedFetcher(options);
|
|
82
|
+
return (endpoint, config) => fetcher.request(endpoint, config);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
Object.defineProperty(exports, 'TypedFetcher', {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
get: function () {
|
|
89
|
+
return TypedFetcher;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(exports, 'createTypedFetcher', {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
get: function () {
|
|
95
|
+
return createTypedFetcher;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
//# sourceMappingURL=fetcher-KdwHgdAl.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetcher-KdwHgdAl.cjs","names":["fn?: FetchFn","options: FetcherOptions","endpoint: T","config?: FilteredRequestConfig<Paths, T>","prefix: string","value: unknown","requestConfig: RequestInit","options?: FetcherOptions"],"sources":["../src/fetcher.ts"],"sourcesContent":["import type {\n EndpointString,\n ExtractEndpointResponse,\n FetcherOptions,\n FilteredRequestConfig,\n ParseEndpoint,\n TypedEndpoint,\n} from './types';\n\nexport class TypedFetcher<Paths> {\n private baseURL: string;\n private defaultHeaders: Record<string, string>;\n private options: FetcherOptions;\n private fetchFn: FetchFn;\n\n static getFetchFn(fn?: FetchFn): FetchFn {\n if (fn) {\n return fn;\n }\n\n if (typeof window !== 'undefined' && typeof window.fetch === 'function') {\n return window.fetch.bind(window);\n }\n\n if (\n typeof globalThis !== 'undefined' &&\n typeof globalThis.fetch === 'function'\n ) {\n return globalThis.fetch.bind(globalThis);\n }\n\n throw new Error('No fetch implementation found');\n }\n\n constructor(options: FetcherOptions = {}) {\n this.baseURL = options.baseURL || '';\n this.defaultHeaders = options.headers || {};\n this.options = options;\n this.fetchFn = TypedFetcher.getFetchFn(options.fetch);\n }\n\n async request<T extends TypedEndpoint<Paths>>(\n endpoint: T,\n config?: FilteredRequestConfig<Paths, T>,\n ): Promise<ExtractEndpointResponse<Paths, T>> {\n const { method, route } = this.parseEndpoint(endpoint);\n\n // Replace path parameters\n let url = route;\n if (config && 'params' in config && config.params) {\n Object.entries(config.params as Record<string, unknown>).forEach(\n ([key, value]) => {\n url = url.replace(`{${key}}`, encodeURIComponent(String(value)));\n },\n );\n }\n\n // Add query parameters\n if (config && 'query' in config && config.query) {\n const queryParams = new URLSearchParams();\n\n // Recursive function to handle nested objects and arrays\n const appendQueryParam = (prefix: string, value: unknown) => {\n if (value === undefined || value === null) {\n return;\n }\n\n if (Array.isArray(value)) {\n // Handle arrays by appending multiple values with the same key\n value.forEach((item) => {\n queryParams.append(prefix, String(item));\n });\n } else if (typeof value === 'object') {\n // For objects, recursively flatten into dot notation\n Object.entries(value as Record<string, unknown>).forEach(\n ([subKey, subValue]) => {\n appendQueryParam(`${prefix}.${subKey}`, subValue);\n },\n );\n } else {\n queryParams.append(prefix, String(value));\n }\n };\n\n // Process all query parameters\n Object.entries(config.query as Record<string, unknown>).forEach(\n ([key, value]) => {\n appendQueryParam(key, value);\n },\n );\n\n const queryString = queryParams.toString();\n if (queryString) {\n url += `?${queryString}`;\n }\n }\n\n // Build request configuration\n let requestConfig: RequestInit = {\n method: method.toUpperCase(),\n headers: {\n ...this.defaultHeaders,\n ...((config && 'headers' in config && config.headers) || {}),\n },\n };\n\n // Add body if present\n if (config && 'body' in config && config.body) {\n requestConfig.body = JSON.stringify(config.body);\n requestConfig.headers = {\n ...requestConfig.headers,\n 'Content-Type': 'application/json',\n };\n }\n\n // Apply request interceptor\n if (this.options.onRequest) {\n requestConfig = await this.options.onRequest(requestConfig);\n }\n\n try {\n // Make the request\n let response = await this.fetchFn(`${this.baseURL}${url}`, requestConfig);\n\n // Apply response interceptor\n if (this.options.onResponse) {\n response = await this.options.onResponse(response);\n }\n\n // Handle errors\n if (!response.ok) {\n throw response;\n }\n\n // Handle empty responses (204 No Content, etc.)\n if (\n response.status === 204 ||\n response.headers.get('content-length') === '0'\n ) {\n return undefined as ExtractEndpointResponse<Paths, T>;\n }\n\n // Parse JSON response\n const data = await response.json();\n return data as ExtractEndpointResponse<Paths, T>;\n } catch (error) {\n // Apply error handler\n if (this.options.onError) {\n // @ts-ignore\n await this.options.onError(error);\n }\n throw error;\n }\n }\n\n private parseEndpoint<T extends EndpointString>(\n endpoint: T,\n ): ParseEndpoint<T> {\n const [method, ...routeParts] = endpoint.split(' ');\n const route = routeParts.join(' ');\n return { method: method.toLowerCase(), route } as ParseEndpoint<T>;\n }\n}\n\nexport function createTypedFetcher<Paths>(options?: FetcherOptions) {\n const fetcher = new TypedFetcher<Paths>(options);\n return <T extends TypedEndpoint<Paths>>(\n endpoint: T,\n config?: FilteredRequestConfig<Paths, T>,\n ) => fetcher.request(endpoint, config);\n}\n\nexport type FetchFn = typeof fetch;\n"],"mappings":"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"}
|
package/dist/fetcher.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint } from "./types-csACmD6U.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/fetcher.d.ts
|
|
4
|
+
declare class TypedFetcher<Paths> {
|
|
5
|
+
private baseURL;
|
|
6
|
+
private defaultHeaders;
|
|
7
|
+
private options;
|
|
8
|
+
private fetchFn;
|
|
9
|
+
static getFetchFn(fn?: FetchFn): FetchFn;
|
|
10
|
+
constructor(options?: FetcherOptions);
|
|
11
|
+
request<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T>>;
|
|
12
|
+
private parseEndpoint;
|
|
13
|
+
}
|
|
14
|
+
declare function createTypedFetcher<Paths>(options?: FetcherOptions): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>;
|
|
15
|
+
type FetchFn = typeof fetch;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { FetchFn, TypedFetcher, createTypedFetcher };
|
|
18
|
+
//# sourceMappingURL=fetcher.d.cts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ExtractEndpointResponse, FetcherOptions, FilteredRequestConfig, TypedEndpoint } from "./types-tMp85Lt_.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/fetcher.d.ts
|
|
4
|
+
declare class TypedFetcher<Paths> {
|
|
5
|
+
private baseURL;
|
|
6
|
+
private defaultHeaders;
|
|
7
|
+
private options;
|
|
8
|
+
private fetchFn;
|
|
9
|
+
static getFetchFn(fn?: FetchFn): FetchFn;
|
|
10
|
+
constructor(options?: FetcherOptions);
|
|
11
|
+
request<T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>): Promise<ExtractEndpointResponse<Paths, T>>;
|
|
12
|
+
private parseEndpoint;
|
|
13
|
+
}
|
|
14
|
+
declare function createTypedFetcher<Paths>(options?: FetcherOptions): <T extends TypedEndpoint<Paths>>(endpoint: T, config?: FilteredRequestConfig<Paths, T>) => Promise<ExtractEndpointResponse<Paths, T>>;
|
|
15
|
+
type FetchFn = typeof fetch;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { FetchFn, TypedFetcher, createTypedFetcher };
|
|
18
|
+
//# sourceMappingURL=fetcher.d.mts.map
|
package/dist/fetcher.mjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const require_fetcher = require('./fetcher-KdwHgdAl.cjs');
|
|
3
|
+
const __tanstack_react_query = require_chunk.__toESM(require("@tanstack/react-query"));
|
|
4
|
+
|
|
5
|
+
//#region src/openapi-hooks.ts
|
|
6
|
+
function createOpenAPIHooks(options = {}) {
|
|
7
|
+
const { operations,...fetcherOptions } = options;
|
|
8
|
+
const fetcher = require_fetcher.createTypedFetcher(fetcherOptions);
|
|
9
|
+
function buildEndpoint(operationId) {
|
|
10
|
+
if (operations && operations[operationId]) {
|
|
11
|
+
const op = operations[operationId];
|
|
12
|
+
return `${op.method.toUpperCase()} ${op.path}`;
|
|
13
|
+
}
|
|
14
|
+
return operationId;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
useQuery: (operationId, config, options$1) => {
|
|
18
|
+
const endpoint = buildEndpoint(operationId);
|
|
19
|
+
const queryKey = [operationId, ...config ? [config] : []];
|
|
20
|
+
return (0, __tanstack_react_query.useQuery)({
|
|
21
|
+
queryKey,
|
|
22
|
+
queryFn: async () => {
|
|
23
|
+
const response = await fetcher(endpoint, config);
|
|
24
|
+
return response;
|
|
25
|
+
},
|
|
26
|
+
...options$1
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
useMutation: (operationId, options$1) => {
|
|
30
|
+
const endpoint = buildEndpoint(operationId);
|
|
31
|
+
return (0, __tanstack_react_query.useMutation)({
|
|
32
|
+
mutationFn: async (variables) => {
|
|
33
|
+
const response = await fetcher(endpoint, variables);
|
|
34
|
+
return response;
|
|
35
|
+
},
|
|
36
|
+
...options$1
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
exports.createOpenAPIHooks = createOpenAPIHooks;
|
|
44
|
+
//# sourceMappingURL=openapi-hooks.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi-hooks.cjs","names":["options: FetcherOptions & { operations?: OperationRegistry }","operationId: OpId","config?: RemoveNever<OperationParams<Paths, OpId>>","options?: Omit<\n UseQueryOptions<OperationResponse<Paths, OpId>, Error>,\n 'queryKey' | 'queryFn'\n >","options","options?: Omit<\n UseMutationOptions<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >,\n 'mutationFn'\n >"],"sources":["../src/openapi-hooks.ts"],"sourcesContent":["import type {\n UseMutationOptions,\n UseQueryOptions,\n} from '@tanstack/react-query';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport { createTypedFetcher } from './fetcher';\nimport type { FetcherOptions } from './types';\n\n// Simplified type helpers for OpenAPI paths\ntype HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\n// Extract all operations from paths\ntype AllOperations<Paths> = {\n [Path in keyof Paths]: {\n [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {\n operationId: infer OpId;\n }\n ? OpId extends string\n ? {\n operationId: OpId;\n path: Path;\n method: Method;\n spec: Paths[Path][Method] & {\n _pathParams?: Paths[Path] extends {\n parameters: { path: infer P };\n }\n ? P\n : never;\n };\n }\n : never\n : never;\n }[keyof Paths[Path] & HttpMethods];\n}[keyof Paths];\n\n// Create operation map\ntype OperationMap<Paths> = {\n [Op in AllOperations<Paths> as Op extends {\n operationId: infer Id extends string;\n }\n ? Id\n : never]: Op;\n};\n\n// Get operation IDs\ntype OperationId<Paths> = keyof OperationMap<Paths>;\n\n// Get operations by method\ntype OperationsByMethod<Paths, Method extends HttpMethods> = {\n [K in OperationId<Paths>]: OperationMap<Paths>[K] extends { method: Method }\n ? K\n : never;\n}[OperationId<Paths>];\n\n// Extract parameter types\ntype OperationParams<\n Paths,\n OpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends {\n path: infer Path extends keyof Paths;\n spec: infer Spec;\n}\n ? {\n params?: Paths[Path] extends { parameters: { path: infer P } }\n ? P\n : Spec extends { parameters: { path: infer P } }\n ? P\n : never;\n query?: Spec extends { parameters: { query?: infer Q } } ? Q : never;\n body?: Spec extends {\n requestBody: { content: { 'application/json': infer Body } };\n }\n ? Body\n : Spec extends {\n requestBody: {\n required: true;\n content: { 'application/json': infer Body };\n };\n }\n ? Body\n : never;\n }\n : never;\n\n// Extract response type\ntype OperationResponse<\n Paths,\n OpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends { spec: infer Spec }\n ? Spec extends {\n responses: { 200: { content: { 'application/json': infer R } } };\n }\n ? R\n : Spec extends {\n responses: { 201: { content: { 'application/json': infer R } } };\n }\n ? R\n : Spec extends { responses: { 204: any } }\n ? void\n : unknown\n : never;\n\n// Remove never properties\ntype RemoveNever<T> = Pick<\n T,\n {\n [K in keyof T]: T[K] extends never ? never : K;\n }[keyof T]\n>;\n\n// Check if type is empty\ntype IsEmpty<T> = keyof T extends never ? true : false;\n\n// Runtime operation registry (would be generated)\ninterface OperationRegistry {\n [operationId: string]: {\n path: string;\n method: string;\n };\n}\n\nexport function createOpenAPIHooks<Paths>(\n options: FetcherOptions & { operations?: OperationRegistry } = {},\n) {\n const { operations, ...fetcherOptions } = options;\n const fetcher = createTypedFetcher<Paths>(fetcherOptions);\n\n function buildEndpoint<OpId extends OperationId<Paths>>(\n operationId: OpId,\n ): string {\n // Runtime lookup from registry\n if (operations && operations[operationId as string]) {\n const op = operations[operationId as string];\n return `${op.method.toUpperCase()} ${op.path}`;\n }\n // Fallback for compile-time only usage\n return operationId as string;\n }\n\n return {\n useQuery: <OpId extends OperationsByMethod<Paths, 'get'>>(\n operationId: OpId,\n config?: RemoveNever<OperationParams<Paths, OpId>>,\n options?: Omit<\n UseQueryOptions<OperationResponse<Paths, OpId>, Error>,\n 'queryKey' | 'queryFn'\n >,\n ) => {\n const endpoint = buildEndpoint(operationId);\n const queryKey = [operationId, ...(config ? [config] : [])];\n\n return useQuery<OperationResponse<Paths, OpId>, Error>({\n queryKey,\n queryFn: async () => {\n const response = await fetcher(endpoint as any, config as any);\n return response as OperationResponse<Paths, OpId>;\n },\n ...options,\n });\n },\n\n useMutation: <\n OpId extends Exclude<\n OperationId<Paths>,\n OperationsByMethod<Paths, 'get'>\n >,\n >(\n operationId: OpId,\n options?: Omit<\n UseMutationOptions<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >,\n 'mutationFn'\n >,\n ) => {\n const endpoint = buildEndpoint(operationId);\n\n return useMutation<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >({\n mutationFn: async (variables) => {\n const response = await fetcher(endpoint as any, variables as any);\n return response as OperationResponse<Paths, OpId>;\n },\n ...options,\n });\n },\n };\n}\n"],"mappings":";;;;;AAyHA,SAAgB,mBACdA,UAA+D,CAAE,GACjE;CACA,MAAM,EAAE,WAAY,GAAG,gBAAgB,GAAG;CAC1C,MAAM,UAAU,mCAA0B,eAAe;CAEzD,SAAS,cACPC,aACQ;AAER,MAAI,cAAc,WAAW,cAAwB;GACnD,MAAM,KAAK,WAAW;AACtB,WAAQ,EAAE,GAAG,OAAO,aAAa,CAAC,GAAG,GAAG,KAAK;EAC9C;AAED,SAAO;CACR;AAED,QAAO;EACL,UAAU,CACRA,aACAC,QACAC,cAIG;GACH,MAAM,WAAW,cAAc,YAAY;GAC3C,MAAM,WAAW,CAAC,aAAa,GAAI,SAAS,CAAC,MAAO,IAAG,CAAE,CAAE;AAE3D,UAAO,qCAAgD;IACrD;IACA,SAAS,YAAY;KACnB,MAAM,WAAW,MAAM,QAAQ,UAAiB,OAAc;AAC9D,YAAO;IACR;IACD,GAAGC;GACJ,EAAC;EACH;EAED,aAAa,CAMXH,aACAI,cAQG;GACH,MAAM,WAAW,cAAc,YAAY;AAE3C,UAAO,wCAIL;IACA,YAAY,OAAO,cAAc;KAC/B,MAAM,WAAW,MAAM,QAAQ,UAAiB,UAAiB;AACjE,YAAO;IACR;IACD,GAAGD;GACJ,EAAC;EACH;CACF;AACF"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { FetcherOptions } from "./types-csACmD6U.cjs";
|
|
2
|
+
import * as _tanstack_react_query8 from "@tanstack/react-query";
|
|
3
|
+
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
|
+
|
|
5
|
+
//#region src/openapi-hooks.d.ts
|
|
6
|
+
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
7
|
+
type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
8
|
+
operationId: infer OpId;
|
|
9
|
+
} ? OpId extends string ? {
|
|
10
|
+
operationId: OpId;
|
|
11
|
+
path: Path;
|
|
12
|
+
method: Method;
|
|
13
|
+
spec: Paths[Path][Method] & {
|
|
14
|
+
_pathParams?: Paths[Path] extends {
|
|
15
|
+
parameters: {
|
|
16
|
+
path: infer P;
|
|
17
|
+
};
|
|
18
|
+
} ? P : never;
|
|
19
|
+
};
|
|
20
|
+
} : never : never }[keyof Paths[Path] & HttpMethods] }[keyof Paths];
|
|
21
|
+
type OperationMap<Paths> = { [Op in AllOperations<Paths> as Op extends {
|
|
22
|
+
operationId: infer Id extends string;
|
|
23
|
+
} ? Id : never]: Op };
|
|
24
|
+
type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
25
|
+
type OperationsByMethod<Paths, Method extends HttpMethods> = { [K in OperationId<Paths>]: OperationMap<Paths>[K] extends {
|
|
26
|
+
method: Method;
|
|
27
|
+
} ? K : never }[OperationId<Paths>];
|
|
28
|
+
type OperationParams<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
29
|
+
path: infer Path extends keyof Paths;
|
|
30
|
+
spec: infer Spec;
|
|
31
|
+
} ? {
|
|
32
|
+
params?: Paths[Path] extends {
|
|
33
|
+
parameters: {
|
|
34
|
+
path: infer P;
|
|
35
|
+
};
|
|
36
|
+
} ? P : Spec extends {
|
|
37
|
+
parameters: {
|
|
38
|
+
path: infer P;
|
|
39
|
+
};
|
|
40
|
+
} ? P : never;
|
|
41
|
+
query?: Spec extends {
|
|
42
|
+
parameters: {
|
|
43
|
+
query?: infer Q;
|
|
44
|
+
};
|
|
45
|
+
} ? Q : never;
|
|
46
|
+
body?: Spec extends {
|
|
47
|
+
requestBody: {
|
|
48
|
+
content: {
|
|
49
|
+
'application/json': infer Body;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
} ? Body : Spec extends {
|
|
53
|
+
requestBody: {
|
|
54
|
+
required: true;
|
|
55
|
+
content: {
|
|
56
|
+
'application/json': infer Body;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
} ? Body : never;
|
|
60
|
+
} : never;
|
|
61
|
+
type OperationResponse<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
62
|
+
spec: infer Spec;
|
|
63
|
+
} ? Spec extends {
|
|
64
|
+
responses: {
|
|
65
|
+
200: {
|
|
66
|
+
content: {
|
|
67
|
+
'application/json': infer R;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
} ? R : Spec extends {
|
|
72
|
+
responses: {
|
|
73
|
+
201: {
|
|
74
|
+
content: {
|
|
75
|
+
'application/json': infer R;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
} ? R : Spec extends {
|
|
80
|
+
responses: {
|
|
81
|
+
204: any;
|
|
82
|
+
};
|
|
83
|
+
} ? void : unknown : never;
|
|
84
|
+
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>;
|
|
85
|
+
interface OperationRegistry {
|
|
86
|
+
[operationId: string]: {
|
|
87
|
+
path: string;
|
|
88
|
+
method: string;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
92
|
+
operations?: OperationRegistry;
|
|
93
|
+
}): {
|
|
94
|
+
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) => _tanstack_react_query8.UseQueryResult<_tanstack_react_query8.NoInfer<OperationResponse<Paths, OpId>>, Error>;
|
|
95
|
+
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) => _tanstack_react_query8.UseMutationResult<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>, unknown>;
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
export { createOpenAPIHooks };
|
|
99
|
+
//# sourceMappingURL=openapi-hooks.d.cts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { FetcherOptions } from "./types-tMp85Lt_.mjs";
|
|
2
|
+
import * as _tanstack_react_query0 from "@tanstack/react-query";
|
|
3
|
+
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
|
+
|
|
5
|
+
//#region src/openapi-hooks.d.ts
|
|
6
|
+
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
7
|
+
type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
8
|
+
operationId: infer OpId;
|
|
9
|
+
} ? OpId extends string ? {
|
|
10
|
+
operationId: OpId;
|
|
11
|
+
path: Path;
|
|
12
|
+
method: Method;
|
|
13
|
+
spec: Paths[Path][Method] & {
|
|
14
|
+
_pathParams?: Paths[Path] extends {
|
|
15
|
+
parameters: {
|
|
16
|
+
path: infer P;
|
|
17
|
+
};
|
|
18
|
+
} ? P : never;
|
|
19
|
+
};
|
|
20
|
+
} : never : never }[keyof Paths[Path] & HttpMethods] }[keyof Paths];
|
|
21
|
+
type OperationMap<Paths> = { [Op in AllOperations<Paths> as Op extends {
|
|
22
|
+
operationId: infer Id extends string;
|
|
23
|
+
} ? Id : never]: Op };
|
|
24
|
+
type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
25
|
+
type OperationsByMethod<Paths, Method extends HttpMethods> = { [K in OperationId<Paths>]: OperationMap<Paths>[K] extends {
|
|
26
|
+
method: Method;
|
|
27
|
+
} ? K : never }[OperationId<Paths>];
|
|
28
|
+
type OperationParams<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
29
|
+
path: infer Path extends keyof Paths;
|
|
30
|
+
spec: infer Spec;
|
|
31
|
+
} ? {
|
|
32
|
+
params?: Paths[Path] extends {
|
|
33
|
+
parameters: {
|
|
34
|
+
path: infer P;
|
|
35
|
+
};
|
|
36
|
+
} ? P : Spec extends {
|
|
37
|
+
parameters: {
|
|
38
|
+
path: infer P;
|
|
39
|
+
};
|
|
40
|
+
} ? P : never;
|
|
41
|
+
query?: Spec extends {
|
|
42
|
+
parameters: {
|
|
43
|
+
query?: infer Q;
|
|
44
|
+
};
|
|
45
|
+
} ? Q : never;
|
|
46
|
+
body?: Spec extends {
|
|
47
|
+
requestBody: {
|
|
48
|
+
content: {
|
|
49
|
+
'application/json': infer Body;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
} ? Body : Spec extends {
|
|
53
|
+
requestBody: {
|
|
54
|
+
required: true;
|
|
55
|
+
content: {
|
|
56
|
+
'application/json': infer Body;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
} ? Body : never;
|
|
60
|
+
} : never;
|
|
61
|
+
type OperationResponse<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
62
|
+
spec: infer Spec;
|
|
63
|
+
} ? Spec extends {
|
|
64
|
+
responses: {
|
|
65
|
+
200: {
|
|
66
|
+
content: {
|
|
67
|
+
'application/json': infer R;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
} ? R : Spec extends {
|
|
72
|
+
responses: {
|
|
73
|
+
201: {
|
|
74
|
+
content: {
|
|
75
|
+
'application/json': infer R;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
} ? R : Spec extends {
|
|
80
|
+
responses: {
|
|
81
|
+
204: any;
|
|
82
|
+
};
|
|
83
|
+
} ? void : unknown : never;
|
|
84
|
+
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>;
|
|
85
|
+
interface OperationRegistry {
|
|
86
|
+
[operationId: string]: {
|
|
87
|
+
path: string;
|
|
88
|
+
method: string;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
92
|
+
operations?: OperationRegistry;
|
|
93
|
+
}): {
|
|
94
|
+
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) => _tanstack_react_query0.UseQueryResult<_tanstack_react_query0.NoInfer<OperationResponse<Paths, OpId>>, Error>;
|
|
95
|
+
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) => _tanstack_react_query0.UseMutationResult<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>, unknown>;
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
export { createOpenAPIHooks };
|
|
99
|
+
//# sourceMappingURL=openapi-hooks.d.mts.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createTypedFetcher } from "./fetcher-DLDD_7Sa.mjs";
|
|
2
|
+
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi-hooks.ts
|
|
5
|
+
function createOpenAPIHooks(options = {}) {
|
|
6
|
+
const { operations,...fetcherOptions } = options;
|
|
7
|
+
const fetcher = createTypedFetcher(fetcherOptions);
|
|
8
|
+
function buildEndpoint(operationId) {
|
|
9
|
+
if (operations && operations[operationId]) {
|
|
10
|
+
const op = operations[operationId];
|
|
11
|
+
return `${op.method.toUpperCase()} ${op.path}`;
|
|
12
|
+
}
|
|
13
|
+
return operationId;
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
useQuery: (operationId, config, options$1) => {
|
|
17
|
+
const endpoint = buildEndpoint(operationId);
|
|
18
|
+
const queryKey = [operationId, ...config ? [config] : []];
|
|
19
|
+
return useQuery({
|
|
20
|
+
queryKey,
|
|
21
|
+
queryFn: async () => {
|
|
22
|
+
const response = await fetcher(endpoint, config);
|
|
23
|
+
return response;
|
|
24
|
+
},
|
|
25
|
+
...options$1
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
useMutation: (operationId, options$1) => {
|
|
29
|
+
const endpoint = buildEndpoint(operationId);
|
|
30
|
+
return useMutation({
|
|
31
|
+
mutationFn: async (variables) => {
|
|
32
|
+
const response = await fetcher(endpoint, variables);
|
|
33
|
+
return response;
|
|
34
|
+
},
|
|
35
|
+
...options$1
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { createOpenAPIHooks };
|
|
43
|
+
//# sourceMappingURL=openapi-hooks.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi-hooks.mjs","names":["options: FetcherOptions & { operations?: OperationRegistry }","operationId: OpId","config?: RemoveNever<OperationParams<Paths, OpId>>","options?: Omit<\n UseQueryOptions<OperationResponse<Paths, OpId>, Error>,\n 'queryKey' | 'queryFn'\n >","options","options?: Omit<\n UseMutationOptions<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >,\n 'mutationFn'\n >"],"sources":["../src/openapi-hooks.ts"],"sourcesContent":["import type {\n UseMutationOptions,\n UseQueryOptions,\n} from '@tanstack/react-query';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport { createTypedFetcher } from './fetcher';\nimport type { FetcherOptions } from './types';\n\n// Simplified type helpers for OpenAPI paths\ntype HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';\n\n// Extract all operations from paths\ntype AllOperations<Paths> = {\n [Path in keyof Paths]: {\n [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {\n operationId: infer OpId;\n }\n ? OpId extends string\n ? {\n operationId: OpId;\n path: Path;\n method: Method;\n spec: Paths[Path][Method] & {\n _pathParams?: Paths[Path] extends {\n parameters: { path: infer P };\n }\n ? P\n : never;\n };\n }\n : never\n : never;\n }[keyof Paths[Path] & HttpMethods];\n}[keyof Paths];\n\n// Create operation map\ntype OperationMap<Paths> = {\n [Op in AllOperations<Paths> as Op extends {\n operationId: infer Id extends string;\n }\n ? Id\n : never]: Op;\n};\n\n// Get operation IDs\ntype OperationId<Paths> = keyof OperationMap<Paths>;\n\n// Get operations by method\ntype OperationsByMethod<Paths, Method extends HttpMethods> = {\n [K in OperationId<Paths>]: OperationMap<Paths>[K] extends { method: Method }\n ? K\n : never;\n}[OperationId<Paths>];\n\n// Extract parameter types\ntype OperationParams<\n Paths,\n OpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends {\n path: infer Path extends keyof Paths;\n spec: infer Spec;\n}\n ? {\n params?: Paths[Path] extends { parameters: { path: infer P } }\n ? P\n : Spec extends { parameters: { path: infer P } }\n ? P\n : never;\n query?: Spec extends { parameters: { query?: infer Q } } ? Q : never;\n body?: Spec extends {\n requestBody: { content: { 'application/json': infer Body } };\n }\n ? Body\n : Spec extends {\n requestBody: {\n required: true;\n content: { 'application/json': infer Body };\n };\n }\n ? Body\n : never;\n }\n : never;\n\n// Extract response type\ntype OperationResponse<\n Paths,\n OpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends { spec: infer Spec }\n ? Spec extends {\n responses: { 200: { content: { 'application/json': infer R } } };\n }\n ? R\n : Spec extends {\n responses: { 201: { content: { 'application/json': infer R } } };\n }\n ? R\n : Spec extends { responses: { 204: any } }\n ? void\n : unknown\n : never;\n\n// Remove never properties\ntype RemoveNever<T> = Pick<\n T,\n {\n [K in keyof T]: T[K] extends never ? never : K;\n }[keyof T]\n>;\n\n// Check if type is empty\ntype IsEmpty<T> = keyof T extends never ? true : false;\n\n// Runtime operation registry (would be generated)\ninterface OperationRegistry {\n [operationId: string]: {\n path: string;\n method: string;\n };\n}\n\nexport function createOpenAPIHooks<Paths>(\n options: FetcherOptions & { operations?: OperationRegistry } = {},\n) {\n const { operations, ...fetcherOptions } = options;\n const fetcher = createTypedFetcher<Paths>(fetcherOptions);\n\n function buildEndpoint<OpId extends OperationId<Paths>>(\n operationId: OpId,\n ): string {\n // Runtime lookup from registry\n if (operations && operations[operationId as string]) {\n const op = operations[operationId as string];\n return `${op.method.toUpperCase()} ${op.path}`;\n }\n // Fallback for compile-time only usage\n return operationId as string;\n }\n\n return {\n useQuery: <OpId extends OperationsByMethod<Paths, 'get'>>(\n operationId: OpId,\n config?: RemoveNever<OperationParams<Paths, OpId>>,\n options?: Omit<\n UseQueryOptions<OperationResponse<Paths, OpId>, Error>,\n 'queryKey' | 'queryFn'\n >,\n ) => {\n const endpoint = buildEndpoint(operationId);\n const queryKey = [operationId, ...(config ? [config] : [])];\n\n return useQuery<OperationResponse<Paths, OpId>, Error>({\n queryKey,\n queryFn: async () => {\n const response = await fetcher(endpoint as any, config as any);\n return response as OperationResponse<Paths, OpId>;\n },\n ...options,\n });\n },\n\n useMutation: <\n OpId extends Exclude<\n OperationId<Paths>,\n OperationsByMethod<Paths, 'get'>\n >,\n >(\n operationId: OpId,\n options?: Omit<\n UseMutationOptions<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >,\n 'mutationFn'\n >,\n ) => {\n const endpoint = buildEndpoint(operationId);\n\n return useMutation<\n OperationResponse<Paths, OpId>,\n Error,\n RemoveNever<OperationParams<Paths, OpId>>\n >({\n mutationFn: async (variables) => {\n const response = await fetcher(endpoint as any, variables as any);\n return response as OperationResponse<Paths, OpId>;\n },\n ...options,\n });\n },\n };\n}\n"],"mappings":";;;;AAyHA,SAAgB,mBACdA,UAA+D,CAAE,GACjE;CACA,MAAM,EAAE,WAAY,GAAG,gBAAgB,GAAG;CAC1C,MAAM,UAAU,mBAA0B,eAAe;CAEzD,SAAS,cACPC,aACQ;AAER,MAAI,cAAc,WAAW,cAAwB;GACnD,MAAM,KAAK,WAAW;AACtB,WAAQ,EAAE,GAAG,OAAO,aAAa,CAAC,GAAG,GAAG,KAAK;EAC9C;AAED,SAAO;CACR;AAED,QAAO;EACL,UAAU,CACRA,aACAC,QACAC,cAIG;GACH,MAAM,WAAW,cAAc,YAAY;GAC3C,MAAM,WAAW,CAAC,aAAa,GAAI,SAAS,CAAC,MAAO,IAAG,CAAE,CAAE;AAE3D,UAAO,SAAgD;IACrD;IACA,SAAS,YAAY;KACnB,MAAM,WAAW,MAAM,QAAQ,UAAiB,OAAc;AAC9D,YAAO;IACR;IACD,GAAGC;GACJ,EAAC;EACH;EAED,aAAa,CAMXH,aACAI,cAQG;GACH,MAAM,WAAW,cAAc,YAAY;AAE3C,UAAO,YAIL;IACA,YAAY,OAAO,cAAc;KAC/B,MAAM,WAAW,MAAM,QAAQ,UAAiB,UAAiB;AACjE,YAAO;IACR;IACD,GAAGD;GACJ,EAAC;EACH;CACF;AACF"}
|
|
File without changes
|