@geekmidas/client 10.0.0-alpha.98 → 10.0.0
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 +154 -0
- package/README.md +57 -40
- package/dist/auth-fetcher.cjs +5 -7
- package/dist/auth-fetcher.cjs.map +1 -1
- package/dist/auth-fetcher.d.cts +17 -15
- package/dist/auth-fetcher.d.cts.map +1 -1
- package/dist/auth-fetcher.d.mts +17 -15
- package/dist/auth-fetcher.d.mts.map +1 -1
- package/dist/auth-fetcher.mjs +5 -6
- package/dist/auth-fetcher.mjs.map +1 -1
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/endpoint-hooks.cjs +15 -11
- package/dist/endpoint-hooks.cjs.map +1 -1
- package/dist/endpoint-hooks.d.cts +7 -4
- package/dist/endpoint-hooks.d.cts.map +1 -1
- package/dist/endpoint-hooks.d.mts +7 -4
- package/dist/endpoint-hooks.d.mts.map +1 -1
- package/dist/endpoint-hooks.mjs +9 -5
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/fetcher-CakjC9C7.mjs +88 -0
- package/dist/fetcher-CakjC9C7.mjs.map +1 -0
- package/dist/fetcher-CwnSqc4D.cjs +100 -0
- package/dist/fetcher-CwnSqc4D.cjs.map +1 -0
- package/dist/fetcher.cjs +3 -115
- package/dist/fetcher.d.cts +8 -7
- package/dist/fetcher.d.cts.map +1 -1
- package/dist/fetcher.d.mts +8 -7
- package/dist/fetcher.d.mts.map +1 -1
- package/dist/fetcher.mjs +2 -90
- package/dist/infer.d.cts +13 -10
- package/dist/infer.d.cts.map +1 -1
- package/dist/infer.d.mts +13 -10
- package/dist/infer.d.mts.map +1 -1
- package/dist/infer.mjs +0 -1
- package/dist/openapi-hooks.cjs +16 -13
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +11 -8
- package/dist/openapi-hooks.d.cts.map +1 -1
- package/dist/openapi-hooks.d.mts +11 -8
- package/dist/openapi-hooks.d.mts.map +1 -1
- package/dist/openapi-hooks.mjs +12 -9
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/openapi-types.d.cts +6 -5
- package/dist/openapi-types.d.cts.map +1 -1
- package/dist/openapi-types.d.mts +6 -5
- package/dist/openapi-types.d.mts.map +1 -1
- 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 +16 -17
- package/dist/react-query.cjs.map +1 -1
- package/dist/react-query.d.cts +17 -12
- package/dist/react-query.d.cts.map +1 -1
- package/dist/react-query.d.mts +17 -12
- package/dist/react-query.d.mts.map +1 -1
- package/dist/react-query.mjs +9 -10
- package/dist/react-query.mjs.map +1 -1
- package/dist/types-D8Jrl4o3.d.mts +110 -0
- package/dist/types-D8Jrl4o3.d.mts.map +1 -0
- package/dist/types-DMQMHePI.d.cts +110 -0
- package/dist/types-DMQMHePI.d.cts.map +1 -0
- package/dist/types.d.cts +2 -122
- package/dist/types.d.mts +2 -122
- package/dist/types.mjs +0 -1
- package/package.json +15 -31
- package/src/__tests__/auth-fetcher.spec.ts +505 -0
- package/src/__tests__/endpoint-hooks.spec.tsx +374 -0
- package/src/__tests__/fetcher.spec.ts +580 -0
- package/src/__tests__/infer.integration.spec.ts +188 -0
- package/src/__tests__/infer.spec.ts +189 -0
- package/src/__tests__/method-restrictions.spec.tsx +227 -0
- package/src/__tests__/mutation-inference.spec.tsx +343 -0
- package/src/__tests__/openapi-hooks.spec.tsx +557 -0
- package/src/__tests__/react-query-infinite.spec.tsx +1003 -0
- package/src/__tests__/react-query-invalidation.spec.tsx +239 -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/auth-fetcher.ts +288 -0
- package/src/endpoint-hooks.ts +346 -0
- package/src/fetcher.ts +171 -0
- package/src/infer.ts +162 -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 +226 -0
- package/tsconfig.json +12 -0
- package/dist/fetcher.cjs.map +0 -1
- package/dist/fetcher.mjs.map +0 -1
- package/dist/index.cjs +0 -15
- package/dist/index.d.cts +0 -7
- package/dist/index.d.mts +0 -7
- package/dist/index.mjs +0 -7
- package/dist/methods.cjs +0 -21
- package/dist/methods.cjs.map +0 -1
- package/dist/methods.d.cts +0 -23
- package/dist/methods.d.cts.map +0 -1
- package/dist/methods.d.mts +0 -23
- package/dist/methods.d.mts.map +0 -1
- package/dist/methods.mjs +0 -19
- package/dist/methods.mjs.map +0 -1
- package/dist/telemetry-g3BE86Wd.d.cts +0 -68
- package/dist/telemetry-g3BE86Wd.d.cts.map +0 -1
- package/dist/telemetry-g3BE86Wd.d.mts +0 -68
- package/dist/telemetry-g3BE86Wd.d.mts.map +0 -1
- package/dist/telemetry.cjs +0 -128
- package/dist/telemetry.cjs.map +0 -1
- package/dist/telemetry.d.cts +0 -2
- package/dist/telemetry.d.mts +0 -2
- package/dist/telemetry.mjs +0 -125
- package/dist/telemetry.mjs.map +0 -1
- package/dist/types.d.cts.map +0 -1
- package/dist/types.d.mts.map +0 -1
package/src/fetcher.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
2
|
+
import type {
|
|
3
|
+
EndpointString,
|
|
4
|
+
ErrorTransformer,
|
|
5
|
+
ExtractEndpointResponse,
|
|
6
|
+
FetcherOptions,
|
|
7
|
+
FilteredRequestConfig,
|
|
8
|
+
ParseEndpoint,
|
|
9
|
+
TypedEndpoint,
|
|
10
|
+
WrappedResult,
|
|
11
|
+
} from './types';
|
|
12
|
+
|
|
13
|
+
export type { FetcherOptions, WrappedResult } from './types';
|
|
14
|
+
|
|
15
|
+
export class TypedFetcher<Paths> {
|
|
16
|
+
private baseURL: string;
|
|
17
|
+
private defaultHeaders: Record<string, string>;
|
|
18
|
+
private options: FetcherOptions;
|
|
19
|
+
private fetchFn: FetchFn;
|
|
20
|
+
|
|
21
|
+
static getFetchFn(fn?: FetchFn): FetchFn {
|
|
22
|
+
if (fn) {
|
|
23
|
+
return fn;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
|
|
27
|
+
return window.fetch.bind(window);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
typeof globalThis !== 'undefined' &&
|
|
32
|
+
typeof globalThis.fetch === 'function'
|
|
33
|
+
) {
|
|
34
|
+
return globalThis.fetch.bind(globalThis);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw new Error('No fetch implementation found');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor(options: FetcherOptions = {}) {
|
|
41
|
+
this.baseURL = options.baseURL || '';
|
|
42
|
+
this.defaultHeaders = options.headers || {};
|
|
43
|
+
this.options = options;
|
|
44
|
+
this.fetchFn = TypedFetcher.getFetchFn(options.fetch);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async request<T extends TypedEndpoint<Paths>>(
|
|
48
|
+
endpoint: T,
|
|
49
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
50
|
+
): Promise<ExtractEndpointResponse<Paths, T>> {
|
|
51
|
+
const { method, route } = this.parseEndpoint(endpoint);
|
|
52
|
+
|
|
53
|
+
// Replace path parameters
|
|
54
|
+
let url = route;
|
|
55
|
+
if (config && 'params' in config && config.params) {
|
|
56
|
+
Object.entries(config.params as Record<string, unknown>).forEach(
|
|
57
|
+
([key, value]) => {
|
|
58
|
+
url = url.replace(`{${key}}`, encodeURIComponent(String(value)));
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Add query parameters
|
|
64
|
+
if (config && 'query' in config && config.query) {
|
|
65
|
+
const queryString = qs.stringify(config.query, {
|
|
66
|
+
encode: true,
|
|
67
|
+
arrayFormat: 'brackets',
|
|
68
|
+
skipNulls: true,
|
|
69
|
+
});
|
|
70
|
+
if (queryString) {
|
|
71
|
+
url += `?${queryString}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Build request configuration
|
|
76
|
+
let requestConfig: RequestInit = {
|
|
77
|
+
method: method.toUpperCase(),
|
|
78
|
+
headers: {
|
|
79
|
+
...this.defaultHeaders,
|
|
80
|
+
...((config && 'headers' in config && config.headers) || {}),
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Add body if present
|
|
85
|
+
if (config && 'body' in config && config.body) {
|
|
86
|
+
requestConfig.body = JSON.stringify(config.body);
|
|
87
|
+
requestConfig.headers = {
|
|
88
|
+
...requestConfig.headers,
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Apply request interceptor
|
|
94
|
+
if (this.options.onRequest) {
|
|
95
|
+
requestConfig = await this.options.onRequest(requestConfig);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
// Make the request
|
|
100
|
+
let response = await this.fetchFn(`${this.baseURL}${url}`, requestConfig);
|
|
101
|
+
|
|
102
|
+
// Apply response interceptor
|
|
103
|
+
if (this.options.onResponse) {
|
|
104
|
+
response = await this.options.onResponse(response);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Handle errors
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
throw response;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Handle empty responses (204 No Content, etc.)
|
|
113
|
+
if (
|
|
114
|
+
response.status === 204 ||
|
|
115
|
+
response.headers.get('content-length') === '0'
|
|
116
|
+
) {
|
|
117
|
+
return undefined as ExtractEndpointResponse<Paths, T>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Parse JSON response
|
|
121
|
+
const data = await response.json();
|
|
122
|
+
return data as ExtractEndpointResponse<Paths, T>;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
// Apply error handler
|
|
125
|
+
if (this.options.onError) {
|
|
126
|
+
// @ts-expect-error
|
|
127
|
+
await this.options.onError(error);
|
|
128
|
+
}
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
wrap<E = unknown>(onError?: ErrorTransformer<E>) {
|
|
134
|
+
return <T extends TypedEndpoint<Paths>>(
|
|
135
|
+
endpoint: T,
|
|
136
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
137
|
+
): Promise<WrappedResult<ExtractEndpointResponse<Paths, T>, E>> =>
|
|
138
|
+
this.request(endpoint, config).then(
|
|
139
|
+
(data) => ({ ok: true as const, data }),
|
|
140
|
+
async (error) => ({
|
|
141
|
+
ok: false as const,
|
|
142
|
+
error: onError ? await onError(error) : (error as E),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private parseEndpoint<T extends EndpointString>(
|
|
148
|
+
endpoint: T,
|
|
149
|
+
): ParseEndpoint<T> {
|
|
150
|
+
const [method, ...routeParts] = endpoint.split(' ');
|
|
151
|
+
const route = routeParts.join(' ');
|
|
152
|
+
return { method: method?.toLowerCase() ?? '', route } as ParseEndpoint<T>;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function createTypedFetcher<Paths>(options?: FetcherOptions) {
|
|
157
|
+
const fetcher = new TypedFetcher<Paths>(options);
|
|
158
|
+
const fn = <T extends TypedEndpoint<Paths>>(
|
|
159
|
+
endpoint: T,
|
|
160
|
+
config?: FilteredRequestConfig<Paths, T>,
|
|
161
|
+
) => fetcher.request(endpoint, config);
|
|
162
|
+
|
|
163
|
+
return Object.assign(fn, {
|
|
164
|
+
wrap: <E = unknown>(onError?: ErrorTransformer<E>) =>
|
|
165
|
+
Object.assign(fetcher.wrap(onError), fn, {
|
|
166
|
+
wrap: undefined,
|
|
167
|
+
}),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export type FetchFn = typeof fetch;
|
package/src/infer.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Endpoint,
|
|
3
|
+
EndpointSchemas,
|
|
4
|
+
} from '@geekmidas/constructs/endpoints';
|
|
5
|
+
import type { HttpMethod } from '@geekmidas/constructs/types';
|
|
6
|
+
import type { InferStandardSchema } from '@geekmidas/schema';
|
|
7
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Infers path parameters from a route string as an object
|
|
11
|
+
* @example '/users/{id}/posts/{postId}' -> { id: string, postId: string }
|
|
12
|
+
*/
|
|
13
|
+
type InferPathParams<TRoute extends string> =
|
|
14
|
+
TRoute extends `${string}{${infer Param}}${infer Rest}`
|
|
15
|
+
? { [K in Param]: string } & InferPathParams<Rest>
|
|
16
|
+
: {};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Converts an HTTP method to lowercase for TypedFetcher compatibility
|
|
20
|
+
*/
|
|
21
|
+
type LowercaseMethod<T extends HttpMethod> = Lowercase<T>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Infers route-level parameters (path params)
|
|
25
|
+
*/
|
|
26
|
+
type InferRouteParameters<TRoute extends string> =
|
|
27
|
+
InferPathParams<TRoute> extends Record<string, never>
|
|
28
|
+
? {}
|
|
29
|
+
: {
|
|
30
|
+
parameters: {
|
|
31
|
+
path: InferPathParams<TRoute>;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Infers operation-level parameters (query params)
|
|
37
|
+
*/
|
|
38
|
+
type InferOperationParameters<TInput extends EndpointSchemas> = TInput extends {
|
|
39
|
+
query: infer Q;
|
|
40
|
+
}
|
|
41
|
+
? {
|
|
42
|
+
parameters: {
|
|
43
|
+
query: InferStandardSchema<Q>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
: {};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Infers the operation object compatible with TypedFetcher
|
|
50
|
+
*/
|
|
51
|
+
type InferOperation<
|
|
52
|
+
TInput extends EndpointSchemas,
|
|
53
|
+
TOutput extends StandardSchemaV1 | undefined,
|
|
54
|
+
> = InferOperationParameters<TInput> & {
|
|
55
|
+
requestBody?: TInput extends { body: infer B }
|
|
56
|
+
? {
|
|
57
|
+
content: {
|
|
58
|
+
'application/json': InferStandardSchema<B>;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
: never;
|
|
62
|
+
responses: {
|
|
63
|
+
200: {
|
|
64
|
+
content: TOutput extends StandardSchemaV1
|
|
65
|
+
? {
|
|
66
|
+
'application/json': InferStandardSchema<TOutput>;
|
|
67
|
+
}
|
|
68
|
+
: never;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Infers the TypedFetcher-compatible paths structure from a single endpoint
|
|
75
|
+
*
|
|
76
|
+
* This generates a structure compatible with @geekmidas/client TypedFetcher,
|
|
77
|
+
* allowing you to create a typed client directly from endpoint definitions
|
|
78
|
+
* without needing OpenAPI JSON + codegen.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { e } from '@geekmidas/constructs';
|
|
83
|
+
* import { createTypedFetcher, type InferOpenApiFromEndpoint } from '@geekmidas/client';
|
|
84
|
+
* import { z } from 'zod';
|
|
85
|
+
*
|
|
86
|
+
* const endpoint = e
|
|
87
|
+
* .get('/users/{id}')
|
|
88
|
+
* .params(z.object({ id: z.string() }))
|
|
89
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
90
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
91
|
+
*
|
|
92
|
+
* type Paths = InferOpenApiFromEndpoint<typeof endpoint>['paths'];
|
|
93
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
94
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export type InferOpenApiFromEndpoint<T> =
|
|
98
|
+
T extends Endpoint<
|
|
99
|
+
infer TRoute,
|
|
100
|
+
infer TMethod,
|
|
101
|
+
infer TInput,
|
|
102
|
+
infer TOutput,
|
|
103
|
+
any,
|
|
104
|
+
any,
|
|
105
|
+
any
|
|
106
|
+
>
|
|
107
|
+
? {
|
|
108
|
+
paths: {
|
|
109
|
+
[K in TRoute]: InferRouteParameters<TRoute> & {
|
|
110
|
+
[M in LowercaseMethod<TMethod>]: InferOperation<TInput, TOutput>;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
: never;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Infers TypedFetcher-compatible paths structure from multiple endpoints
|
|
118
|
+
*
|
|
119
|
+
* Merges multiple endpoint definitions into a single paths object that can be
|
|
120
|
+
* used with @geekmidas/client TypedFetcher for fully type-safe API calls.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* import { e } from '@geekmidas/constructs';
|
|
125
|
+
* import { createTypedFetcher, type InferOpenApi } from '@geekmidas/client';
|
|
126
|
+
* import { z } from 'zod';
|
|
127
|
+
*
|
|
128
|
+
* // Define endpoints
|
|
129
|
+
* const getUserEndpoint = e
|
|
130
|
+
* .get('/users/{id}')
|
|
131
|
+
* .params(z.object({ id: z.string() }))
|
|
132
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
133
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
134
|
+
*
|
|
135
|
+
* const createUserEndpoint = e
|
|
136
|
+
* .post('/users')
|
|
137
|
+
* .body(z.object({ name: z.string() }))
|
|
138
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
139
|
+
* .handle(async ({ body }) => ({ id: '123', name: body.name }));
|
|
140
|
+
*
|
|
141
|
+
* // Export for client
|
|
142
|
+
* export const endpoints = [getUserEndpoint, createUserEndpoint] as const;
|
|
143
|
+
* export type Paths = InferOpenApi<typeof endpoints>['paths'];
|
|
144
|
+
*
|
|
145
|
+
* // Client usage
|
|
146
|
+
* import type { Paths } from './endpoints';
|
|
147
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
148
|
+
*
|
|
149
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
150
|
+
* const newUser = await client('POST /users', { body: { name: 'Jane' } });
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export type InferOpenApi<TEndpoints extends readonly any[]> =
|
|
154
|
+
TEndpoints extends readonly [infer First, ...infer Rest]
|
|
155
|
+
? InferOpenApiFromEndpoint<First> extends { paths: infer P1 }
|
|
156
|
+
? Rest extends []
|
|
157
|
+
? { paths: P1 }
|
|
158
|
+
: InferOpenApi<Rest> extends { paths: infer P2 }
|
|
159
|
+
? { paths: P1 & P2 }
|
|
160
|
+
: { paths: P1 }
|
|
161
|
+
: InferOpenApi<Rest>
|
|
162
|
+
: { paths: {} };
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
UseMutationOptions,
|
|
3
|
+
UseQueryOptions,
|
|
4
|
+
} from '@tanstack/react-query';
|
|
5
|
+
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
6
|
+
import { createTypedFetcher } from './fetcher';
|
|
7
|
+
import type { FetcherOptions } from './types';
|
|
8
|
+
|
|
9
|
+
// Simplified type helpers for OpenAPI paths
|
|
10
|
+
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
11
|
+
|
|
12
|
+
// Extract all operations from paths
|
|
13
|
+
type AllOperations<Paths> = {
|
|
14
|
+
[Path in keyof Paths]: {
|
|
15
|
+
[Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
16
|
+
operationId: infer OpId;
|
|
17
|
+
}
|
|
18
|
+
? OpId extends string
|
|
19
|
+
? {
|
|
20
|
+
operationId: OpId;
|
|
21
|
+
path: Path;
|
|
22
|
+
method: Method;
|
|
23
|
+
spec: Paths[Path][Method] & {
|
|
24
|
+
_pathParams?: Paths[Path] extends {
|
|
25
|
+
parameters: { path: infer P };
|
|
26
|
+
}
|
|
27
|
+
? P
|
|
28
|
+
: never;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
: never
|
|
32
|
+
: never;
|
|
33
|
+
}[keyof Paths[Path] & HttpMethods];
|
|
34
|
+
}[keyof Paths];
|
|
35
|
+
|
|
36
|
+
// Create operation map
|
|
37
|
+
type OperationMap<Paths> = {
|
|
38
|
+
[Op in AllOperations<Paths> as Op extends {
|
|
39
|
+
operationId: infer Id extends string;
|
|
40
|
+
}
|
|
41
|
+
? Id
|
|
42
|
+
: never]: Op;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Get operation IDs
|
|
46
|
+
type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
47
|
+
|
|
48
|
+
// Get operations by method
|
|
49
|
+
type OperationsByMethod<Paths, Method extends HttpMethods> = {
|
|
50
|
+
[K in OperationId<Paths>]: OperationMap<Paths>[K] extends { method: Method }
|
|
51
|
+
? K
|
|
52
|
+
: never;
|
|
53
|
+
}[OperationId<Paths>];
|
|
54
|
+
|
|
55
|
+
// Extract parameter types
|
|
56
|
+
type OperationParams<
|
|
57
|
+
Paths,
|
|
58
|
+
OpId extends OperationId<Paths>,
|
|
59
|
+
> = OperationMap<Paths>[OpId] extends {
|
|
60
|
+
path: infer Path extends keyof Paths;
|
|
61
|
+
spec: infer Spec;
|
|
62
|
+
}
|
|
63
|
+
? {
|
|
64
|
+
params?: Paths[Path] extends { parameters: { path: infer P } }
|
|
65
|
+
? P
|
|
66
|
+
: Spec extends { parameters: { path: infer P } }
|
|
67
|
+
? P
|
|
68
|
+
: never;
|
|
69
|
+
query?: Spec extends { parameters?: { query?: infer Q } } ? Q : never;
|
|
70
|
+
body?: Spec extends {
|
|
71
|
+
requestBody: { content: { 'application/json': infer Body } };
|
|
72
|
+
}
|
|
73
|
+
? Body
|
|
74
|
+
: Spec extends {
|
|
75
|
+
requestBody: {
|
|
76
|
+
required: true;
|
|
77
|
+
content: { 'application/json': infer Body };
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
? Body
|
|
81
|
+
: never;
|
|
82
|
+
}
|
|
83
|
+
: never;
|
|
84
|
+
|
|
85
|
+
// Extract response type
|
|
86
|
+
type OperationResponse<
|
|
87
|
+
Paths,
|
|
88
|
+
OpId extends OperationId<Paths>,
|
|
89
|
+
> = OperationMap<Paths>[OpId] extends { spec: infer Spec }
|
|
90
|
+
? Spec extends {
|
|
91
|
+
responses: { 200: { content: { 'application/json': infer R } } };
|
|
92
|
+
}
|
|
93
|
+
? R
|
|
94
|
+
: Spec extends {
|
|
95
|
+
responses: { 201: { content: { 'application/json': infer R } } };
|
|
96
|
+
}
|
|
97
|
+
? R
|
|
98
|
+
: Spec extends { responses: { 204: any } }
|
|
99
|
+
? undefined
|
|
100
|
+
: unknown
|
|
101
|
+
: never;
|
|
102
|
+
|
|
103
|
+
// Remove never properties
|
|
104
|
+
type RemoveNever<T> = Pick<
|
|
105
|
+
T,
|
|
106
|
+
{
|
|
107
|
+
[K in keyof T]: T[K] extends never ? never : K;
|
|
108
|
+
}[keyof T]
|
|
109
|
+
>;
|
|
110
|
+
|
|
111
|
+
// Check if type is empty
|
|
112
|
+
type IsEmpty<T> = keyof T extends never ? true : false;
|
|
113
|
+
|
|
114
|
+
// Runtime operation registry (would be generated)
|
|
115
|
+
interface OperationRegistry {
|
|
116
|
+
[operationId: string]: {
|
|
117
|
+
path: string;
|
|
118
|
+
method: string;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function createOpenAPIHooks<Paths>(
|
|
123
|
+
options: FetcherOptions & { operations?: OperationRegistry } = {},
|
|
124
|
+
) {
|
|
125
|
+
const { operations, ...fetcherOptions } = options;
|
|
126
|
+
const fetcher = createTypedFetcher<Paths>(fetcherOptions);
|
|
127
|
+
|
|
128
|
+
function buildEndpoint<OpId extends OperationId<Paths>>(
|
|
129
|
+
operationId: OpId,
|
|
130
|
+
): string {
|
|
131
|
+
// Runtime lookup from registry
|
|
132
|
+
const op = operations?.[operationId as string];
|
|
133
|
+
if (op) {
|
|
134
|
+
return `${op.method.toUpperCase()} ${op.path}`;
|
|
135
|
+
}
|
|
136
|
+
// Fallback for compile-time only usage
|
|
137
|
+
return operationId as string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
useQuery: <OpId extends OperationsByMethod<Paths, 'get'>>(
|
|
142
|
+
operationId: OpId,
|
|
143
|
+
config?: RemoveNever<OperationParams<Paths, OpId>>,
|
|
144
|
+
options?: Omit<
|
|
145
|
+
UseQueryOptions<OperationResponse<Paths, OpId>, Error>,
|
|
146
|
+
'queryKey' | 'queryFn'
|
|
147
|
+
>,
|
|
148
|
+
) => {
|
|
149
|
+
const endpoint = buildEndpoint(operationId);
|
|
150
|
+
const queryKey = [operationId, ...(config ? [config] : [])];
|
|
151
|
+
|
|
152
|
+
return useQuery<OperationResponse<Paths, OpId>, Error>({
|
|
153
|
+
queryKey,
|
|
154
|
+
queryFn: async () => {
|
|
155
|
+
const response = await fetcher(endpoint as any, config as any);
|
|
156
|
+
return response as OperationResponse<Paths, OpId>;
|
|
157
|
+
},
|
|
158
|
+
...options,
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
useMutation: <
|
|
163
|
+
OpId extends Exclude<
|
|
164
|
+
OperationId<Paths>,
|
|
165
|
+
OperationsByMethod<Paths, 'get'>
|
|
166
|
+
>,
|
|
167
|
+
>(
|
|
168
|
+
operationId: OpId,
|
|
169
|
+
options?: Omit<
|
|
170
|
+
UseMutationOptions<
|
|
171
|
+
OperationResponse<Paths, OpId>,
|
|
172
|
+
Error,
|
|
173
|
+
RemoveNever<OperationParams<Paths, OpId>>
|
|
174
|
+
>,
|
|
175
|
+
'mutationFn'
|
|
176
|
+
>,
|
|
177
|
+
) => {
|
|
178
|
+
const endpoint = buildEndpoint(operationId);
|
|
179
|
+
|
|
180
|
+
return useMutation<
|
|
181
|
+
OperationResponse<Paths, OpId>,
|
|
182
|
+
Error,
|
|
183
|
+
RemoveNever<OperationParams<Paths, OpId>>
|
|
184
|
+
>({
|
|
185
|
+
mutationFn: async (variables) => {
|
|
186
|
+
const response = await fetcher(endpoint as any, variables as any);
|
|
187
|
+
return response as OperationResponse<Paths, OpId>;
|
|
188
|
+
},
|
|
189
|
+
...options,
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|