@geekmidas/client 10.0.0-alpha.12 → 10.0.0-alpha.13
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/dist/auth-fetcher.cjs +76 -3
- package/dist/auth-fetcher.cjs.map +1 -0
- package/dist/auth-fetcher.d.cts +155 -3
- package/dist/auth-fetcher.d.cts.map +1 -0
- package/dist/auth-fetcher.d.mts +155 -3
- package/dist/auth-fetcher.d.mts.map +1 -0
- package/dist/auth-fetcher.mjs +75 -3
- package/dist/auth-fetcher.mjs.map +1 -0
- package/dist/endpoint-hooks.cjs +11 -15
- package/dist/endpoint-hooks.cjs.map +1 -1
- package/dist/endpoint-hooks.d.cts +4 -7
- package/dist/endpoint-hooks.d.cts.map +1 -1
- package/dist/endpoint-hooks.d.mts +4 -7
- package/dist/endpoint-hooks.d.mts.map +1 -1
- package/dist/endpoint-hooks.mjs +5 -9
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/fetcher.cjs +110 -3
- package/dist/fetcher.cjs.map +1 -0
- package/dist/fetcher.d.cts +22 -3
- package/dist/fetcher.d.cts.map +1 -0
- package/dist/fetcher.d.mts +22 -3
- package/dist/fetcher.d.mts.map +1 -0
- package/dist/fetcher.mjs +85 -2
- package/dist/fetcher.mjs.map +1 -0
- package/dist/index.cjs +6 -6
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +5 -6
- package/dist/infer.cjs +0 -1
- package/dist/infer.d.cts +130 -2
- package/dist/infer.d.cts.map +1 -0
- package/dist/infer.d.mts +130 -2
- package/dist/infer.d.mts.map +1 -0
- package/dist/infer.mjs +1 -1
- package/dist/openapi-hooks.cjs +13 -16
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +8 -11
- package/dist/openapi-hooks.d.cts.map +1 -1
- package/dist/openapi-hooks.d.mts +8 -11
- package/dist/openapi-hooks.d.mts.map +1 -1
- package/dist/openapi-hooks.mjs +9 -12
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/openapi-types.d.cts +2 -4
- package/dist/openapi-types.d.cts.map +1 -1
- package/dist/openapi-types.d.mts +2 -4
- package/dist/openapi-types.d.mts.map +1 -1
- package/dist/openapi.cjs +25 -23
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.cts +494 -0
- package/dist/openapi.d.cts.map +1 -0
- package/dist/openapi.d.mts +494 -0
- package/dist/openapi.d.mts.map +1 -0
- package/dist/openapi.mjs +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/react-query.cjs +17 -16
- package/dist/react-query.cjs.map +1 -1
- package/dist/react-query.d.cts +12 -17
- package/dist/react-query.d.cts.map +1 -1
- package/dist/react-query.d.mts +12 -17
- package/dist/react-query.d.mts.map +1 -1
- package/dist/react-query.mjs +10 -9
- package/dist/react-query.mjs.map +1 -1
- package/dist/types.cjs +0 -1
- package/dist/types.d.cts +109 -2
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +109 -2
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +1 -1
- package/package.json +5 -5
- package/dist/auth-fetcher-CBYuL6rx.d.cts +0 -158
- package/dist/auth-fetcher-CBYuL6rx.d.cts.map +0 -1
- package/dist/auth-fetcher-DOj0iz21.mjs +0 -77
- package/dist/auth-fetcher-DOj0iz21.mjs.map +0 -1
- package/dist/auth-fetcher-DxS8zOLc.d.mts +0 -158
- package/dist/auth-fetcher-DxS8zOLc.d.mts.map +0 -1
- package/dist/auth-fetcher-HILABmws.cjs +0 -82
- package/dist/auth-fetcher-HILABmws.cjs.map +0 -1
- package/dist/chunk-CUT6urMc.cjs +0 -30
- package/dist/fetcher-C1xtCKpT.d.mts +0 -25
- package/dist/fetcher-C1xtCKpT.d.mts.map +0 -1
- package/dist/fetcher-CakjC9C7.mjs +0 -88
- package/dist/fetcher-CakjC9C7.mjs.map +0 -1
- package/dist/fetcher-CwnSqc4D.cjs +0 -100
- package/dist/fetcher-CwnSqc4D.cjs.map +0 -1
- package/dist/fetcher-cDrfwN-Y.d.cts +0 -25
- package/dist/fetcher-cDrfwN-Y.d.cts.map +0 -1
- package/dist/infer-6KKCgmg1.cjs +0 -0
- package/dist/infer-CPRqfabW.d.cts +0 -133
- package/dist/infer-CPRqfabW.d.cts.map +0 -1
- package/dist/infer-D9CgpOuc.d.mts +0 -133
- package/dist/infer-D9CgpOuc.d.mts.map +0 -1
- package/dist/infer-uCUq1whM.mjs +0 -0
- package/dist/types-BH8WgGrm.mjs +0 -0
- package/dist/types-BMGM6sZc.d.mts +0 -110
- package/dist/types-BMGM6sZc.d.mts.map +0 -1
- package/dist/types-DMQMHePI.d.cts +0 -110
- package/dist/types-DMQMHePI.d.cts.map +0 -1
- package/dist/types-a8gm_IaQ.cjs +0 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
|
2
|
-
import { ApiKeyProvider, AuthFetcherOptions, AuthStrategy, AuthenticatedEndpoints, AwsSigner, GetEndpointAuth, PublicEndpoints, SecuritySchemeObject, TokenProvider, UsedSecuritySchemes, createAuthAwareFetcher } from "./auth-fetcher
|
|
3
|
-
import { FetchFn, TypedFetcher, createTypedFetcher } from "./fetcher
|
|
4
|
-
import { InferOpenApi, InferOpenApiFromEndpoint } from "./infer
|
|
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.mjs";
|
|
2
|
+
import { ApiKeyProvider, AuthFetcherOptions, AuthStrategy, AuthenticatedEndpoints, AwsSigner, GetEndpointAuth, PublicEndpoints, SecuritySchemeObject, TokenProvider, UsedSecuritySchemes, createAuthAwareFetcher } from "./auth-fetcher.mjs";
|
|
3
|
+
import { FetchFn, TypedFetcher, createTypedFetcher } from "./fetcher.mjs";
|
|
4
|
+
import { InferOpenApi, InferOpenApiFromEndpoint } from "./infer.mjs";
|
|
5
5
|
export { ApiKeyProvider, AuthFetcherOptions, AuthStrategy, AuthenticatedEndpoints, AwsSigner, EndpointString, ErrorTransformer, ExtractEndpointConfig, ExtractEndpointResponse, ExtractMethod, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, FetchFn, FetcherOptions, FilterEndpointByMethod, FilteredRequestConfig, GetEndpointAuth, InferOpenApi, InferOpenApiFromEndpoint, IsConfigRequired, MutationEndpoint, OpenAPIRoutes, ParseEndpoint, PublicEndpoints, QueryEndpoint, RequestConfig, SecuritySchemeObject, TokenProvider, TypedApiFunction, TypedEndpoint, TypedFetcher, UsedSecuritySchemes, ValidEndpoint, WrappedApiFunction, WrappedResult, createAuthAwareFetcher, createTypedFetcher };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { TypedFetcher, createTypedFetcher } from "./fetcher
|
|
2
|
-
import { createAuthAwareFetcher } from "./auth-fetcher
|
|
3
|
-
import "./infer
|
|
4
|
-
import "./types
|
|
5
|
-
|
|
6
|
-
export { TypedFetcher, createAuthAwareFetcher, createTypedFetcher };
|
|
1
|
+
import { TypedFetcher, createTypedFetcher } from "./fetcher.mjs";
|
|
2
|
+
import { createAuthAwareFetcher } from "./auth-fetcher.mjs";
|
|
3
|
+
import "./infer.mjs";
|
|
4
|
+
import "./types.mjs";
|
|
5
|
+
export { TypedFetcher, createAuthAwareFetcher, createTypedFetcher };
|
package/dist/infer.cjs
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require('./infer-6KKCgmg1.cjs');
|
package/dist/infer.d.cts
CHANGED
|
@@ -1,2 +1,130 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Endpoint, EndpointSchemas } from "@geekmidas/constructs/endpoints";
|
|
2
|
+
import { HttpMethod } from "@geekmidas/constructs/types";
|
|
3
|
+
import { InferStandardSchema, InferStandardSchemaInput } from "@geekmidas/schema";
|
|
4
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
5
|
+
//#region src/infer.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Infers path parameters from a route string as an object
|
|
8
|
+
* @example '/users/{id}/posts/{postId}' -> { id: string, postId: string }
|
|
9
|
+
*/
|
|
10
|
+
type InferPathParams<TRoute extends string> = TRoute extends `${string}{${infer Param}}${infer Rest}` ? { [K in Param]: string; } & InferPathParams<Rest> : {};
|
|
11
|
+
/**
|
|
12
|
+
* Converts an HTTP method to lowercase for TypedFetcher compatibility
|
|
13
|
+
*/
|
|
14
|
+
type LowercaseMethod<T extends HttpMethod> = Lowercase<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Infers route-level parameters (path params)
|
|
17
|
+
*/
|
|
18
|
+
type InferRouteParameters<TRoute extends string> = InferPathParams<TRoute> extends Record<string, never> ? {} : {
|
|
19
|
+
parameters: {
|
|
20
|
+
path: InferPathParams<TRoute>;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Infers operation-level parameters (query params)
|
|
25
|
+
*/
|
|
26
|
+
type InferOperationParameters<TInput extends EndpointSchemas> = TInput extends {
|
|
27
|
+
query: infer Q;
|
|
28
|
+
} ? {
|
|
29
|
+
parameters: {
|
|
30
|
+
query: InferStandardSchemaInput<Q>;
|
|
31
|
+
};
|
|
32
|
+
} : {};
|
|
33
|
+
/**
|
|
34
|
+
* Infers the operation object compatible with TypedFetcher
|
|
35
|
+
*/
|
|
36
|
+
type InferOperation<TInput extends EndpointSchemas, TOutput extends StandardSchemaV1 | undefined> = InferOperationParameters<TInput> & {
|
|
37
|
+
requestBody?: TInput extends {
|
|
38
|
+
body: infer B;
|
|
39
|
+
} ? {
|
|
40
|
+
content: {
|
|
41
|
+
'application/json': InferStandardSchemaInput<B>;
|
|
42
|
+
};
|
|
43
|
+
} : never;
|
|
44
|
+
responses: {
|
|
45
|
+
200: {
|
|
46
|
+
content: TOutput extends StandardSchemaV1 ? {
|
|
47
|
+
'application/json': InferStandardSchema<TOutput>;
|
|
48
|
+
} : never;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Infers the TypedFetcher-compatible paths structure from a single endpoint
|
|
54
|
+
*
|
|
55
|
+
* This generates a structure compatible with @geekmidas/client TypedFetcher,
|
|
56
|
+
* allowing you to create a typed client directly from endpoint definitions
|
|
57
|
+
* without needing OpenAPI JSON + codegen.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { api } from './constructs/api.js';
|
|
62
|
+
* import { createTypedFetcher, type InferOpenApiFromEndpoint } from '@geekmidas/client';
|
|
63
|
+
* import { z } from 'zod';
|
|
64
|
+
*
|
|
65
|
+
* const endpoint = e
|
|
66
|
+
* .get('/users/{id}')
|
|
67
|
+
* .params(z.object({ id: z.string() }))
|
|
68
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
69
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
70
|
+
*
|
|
71
|
+
* type Paths = InferOpenApiFromEndpoint<typeof endpoint>['paths'];
|
|
72
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
73
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export type InferOpenApiFromEndpoint<T> = T extends Endpoint<infer TRoute, infer TMethod, infer TInput, infer TOutput, any, any, any> ? {
|
|
77
|
+
paths: { [K in TRoute]: InferRouteParameters<TRoute> & { [M in LowercaseMethod<TMethod>]: InferOperation<TInput, TOutput>; }; };
|
|
78
|
+
} : never;
|
|
79
|
+
/**
|
|
80
|
+
* Infers TypedFetcher-compatible paths structure from multiple endpoints
|
|
81
|
+
*
|
|
82
|
+
* Merges multiple endpoint definitions into a single paths object that can be
|
|
83
|
+
* used with @geekmidas/client TypedFetcher for fully type-safe API calls.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { api } from './constructs/api.js';
|
|
88
|
+
* import { createTypedFetcher, type InferOpenApi } from '@geekmidas/client';
|
|
89
|
+
* import { z } from 'zod';
|
|
90
|
+
*
|
|
91
|
+
* // Define endpoints
|
|
92
|
+
* const getUserEndpoint = e
|
|
93
|
+
* .get('/users/{id}')
|
|
94
|
+
* .params(z.object({ id: z.string() }))
|
|
95
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
96
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
97
|
+
*
|
|
98
|
+
* const createUserEndpoint = e
|
|
99
|
+
* .post('/users')
|
|
100
|
+
* .body(z.object({ name: z.string() }))
|
|
101
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
102
|
+
* .handle(async ({ body }) => ({ id: '123', name: body.name }));
|
|
103
|
+
*
|
|
104
|
+
* // Export for client
|
|
105
|
+
* export const endpoints = [getUserEndpoint, createUserEndpoint] as const;
|
|
106
|
+
* export type Paths = InferOpenApi<typeof endpoints>['paths'];
|
|
107
|
+
*
|
|
108
|
+
* // Client usage
|
|
109
|
+
* import type { Paths } from './endpoints';
|
|
110
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
111
|
+
*
|
|
112
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
113
|
+
* const newUser = await client('POST /users', { body: { name: 'Jane' } });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export type InferOpenApi<TEndpoints extends readonly any[]> = TEndpoints extends readonly [infer First, ...infer Rest] ? InferOpenApiFromEndpoint<First> extends {
|
|
117
|
+
paths: infer P1;
|
|
118
|
+
} ? Rest extends [] ? {
|
|
119
|
+
paths: P1;
|
|
120
|
+
} : InferOpenApi<Rest> extends {
|
|
121
|
+
paths: infer P2;
|
|
122
|
+
} ? {
|
|
123
|
+
paths: P1 & P2;
|
|
124
|
+
} : {
|
|
125
|
+
paths: P1;
|
|
126
|
+
} : InferOpenApi<Rest> : {
|
|
127
|
+
paths: {};
|
|
128
|
+
};
|
|
129
|
+
//#endregion
|
|
130
|
+
//# sourceMappingURL=infer.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infer.d.cts","names":[],"sources":["../src/infer.ts"],"mappings":";;;;;;;;;KAeK,gBAAgB,yBACpB,kCAAkC,eAAe,YAC3C,KAAK,oBAAmB,gBAAgB;;;;KAM1C,gBAAgB,UAAU,cAAc,UAAU;;;;KAKlD,qBAAqB,yBACzB,gBAAgB,gBAAgB;EAG7B;IACC,MAAM,gBAAgB;;;;;;KAOtB,yBAAyB,eAAe,mBAAmB;EAC/D,aAAa;;EAGX;IAMC,OAAO,yBAAyB;;;;;;KAQ/B,eACJ,eAAe,iBACf,gBAAgB,gCACb,yBAAyB;EAC5B,cAAc;IAAiB,YAAY;;IAExC;MAKC,oBAAoB,yBAAyB;;;EAIjD;IACC;MACC,SAAS,gBAAgB;QAEtB,oBAAoB,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA+BlC,yBAAyB,KACpC,UAAU,eACH,cACA,eACA,cACA;EAMJ,UACE,KAAK,SAAS,qBAAqB,aAClC,KAAK,gBAAgB,WAAW,eAAe,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA2ClD,aAAa,qCACxB,mCAAmC,gBAAgB,QAChD,yBAAyB;EAAiB,aAAa;IACtD;EACG,OAAO;IACT,aAAa;EAAgB,aAAa;;EACvC,OAAO,KAAK;;EACZ,OAAO;IACX,aAAa;EACZ"}
|
package/dist/infer.d.mts
CHANGED
|
@@ -1,2 +1,130 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Endpoint, EndpointSchemas } from "@geekmidas/constructs/endpoints";
|
|
2
|
+
import { HttpMethod } from "@geekmidas/constructs/types";
|
|
3
|
+
import { InferStandardSchema, InferStandardSchemaInput } from "@geekmidas/schema";
|
|
4
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
5
|
+
//#region src/infer.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Infers path parameters from a route string as an object
|
|
8
|
+
* @example '/users/{id}/posts/{postId}' -> { id: string, postId: string }
|
|
9
|
+
*/
|
|
10
|
+
type InferPathParams<TRoute extends string> = TRoute extends `${string}{${infer Param}}${infer Rest}` ? { [K in Param]: string; } & InferPathParams<Rest> : {};
|
|
11
|
+
/**
|
|
12
|
+
* Converts an HTTP method to lowercase for TypedFetcher compatibility
|
|
13
|
+
*/
|
|
14
|
+
type LowercaseMethod<T extends HttpMethod> = Lowercase<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Infers route-level parameters (path params)
|
|
17
|
+
*/
|
|
18
|
+
type InferRouteParameters<TRoute extends string> = InferPathParams<TRoute> extends Record<string, never> ? {} : {
|
|
19
|
+
parameters: {
|
|
20
|
+
path: InferPathParams<TRoute>;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Infers operation-level parameters (query params)
|
|
25
|
+
*/
|
|
26
|
+
type InferOperationParameters<TInput extends EndpointSchemas> = TInput extends {
|
|
27
|
+
query: infer Q;
|
|
28
|
+
} ? {
|
|
29
|
+
parameters: {
|
|
30
|
+
query: InferStandardSchemaInput<Q>;
|
|
31
|
+
};
|
|
32
|
+
} : {};
|
|
33
|
+
/**
|
|
34
|
+
* Infers the operation object compatible with TypedFetcher
|
|
35
|
+
*/
|
|
36
|
+
type InferOperation<TInput extends EndpointSchemas, TOutput extends StandardSchemaV1 | undefined> = InferOperationParameters<TInput> & {
|
|
37
|
+
requestBody?: TInput extends {
|
|
38
|
+
body: infer B;
|
|
39
|
+
} ? {
|
|
40
|
+
content: {
|
|
41
|
+
'application/json': InferStandardSchemaInput<B>;
|
|
42
|
+
};
|
|
43
|
+
} : never;
|
|
44
|
+
responses: {
|
|
45
|
+
200: {
|
|
46
|
+
content: TOutput extends StandardSchemaV1 ? {
|
|
47
|
+
'application/json': InferStandardSchema<TOutput>;
|
|
48
|
+
} : never;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Infers the TypedFetcher-compatible paths structure from a single endpoint
|
|
54
|
+
*
|
|
55
|
+
* This generates a structure compatible with @geekmidas/client TypedFetcher,
|
|
56
|
+
* allowing you to create a typed client directly from endpoint definitions
|
|
57
|
+
* without needing OpenAPI JSON + codegen.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { api } from './constructs/api.js';
|
|
62
|
+
* import { createTypedFetcher, type InferOpenApiFromEndpoint } from '@geekmidas/client';
|
|
63
|
+
* import { z } from 'zod';
|
|
64
|
+
*
|
|
65
|
+
* const endpoint = e
|
|
66
|
+
* .get('/users/{id}')
|
|
67
|
+
* .params(z.object({ id: z.string() }))
|
|
68
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
69
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
70
|
+
*
|
|
71
|
+
* type Paths = InferOpenApiFromEndpoint<typeof endpoint>['paths'];
|
|
72
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
73
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export type InferOpenApiFromEndpoint<T> = T extends Endpoint<infer TRoute, infer TMethod, infer TInput, infer TOutput, any, any, any> ? {
|
|
77
|
+
paths: { [K in TRoute]: InferRouteParameters<TRoute> & { [M in LowercaseMethod<TMethod>]: InferOperation<TInput, TOutput>; }; };
|
|
78
|
+
} : never;
|
|
79
|
+
/**
|
|
80
|
+
* Infers TypedFetcher-compatible paths structure from multiple endpoints
|
|
81
|
+
*
|
|
82
|
+
* Merges multiple endpoint definitions into a single paths object that can be
|
|
83
|
+
* used with @geekmidas/client TypedFetcher for fully type-safe API calls.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import { api } from './constructs/api.js';
|
|
88
|
+
* import { createTypedFetcher, type InferOpenApi } from '@geekmidas/client';
|
|
89
|
+
* import { z } from 'zod';
|
|
90
|
+
*
|
|
91
|
+
* // Define endpoints
|
|
92
|
+
* const getUserEndpoint = e
|
|
93
|
+
* .get('/users/{id}')
|
|
94
|
+
* .params(z.object({ id: z.string() }))
|
|
95
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
96
|
+
* .handle(async ({ params }) => ({ id: params.id, name: 'John' }));
|
|
97
|
+
*
|
|
98
|
+
* const createUserEndpoint = e
|
|
99
|
+
* .post('/users')
|
|
100
|
+
* .body(z.object({ name: z.string() }))
|
|
101
|
+
* .output(z.object({ id: z.string(), name: z.string() }))
|
|
102
|
+
* .handle(async ({ body }) => ({ id: '123', name: body.name }));
|
|
103
|
+
*
|
|
104
|
+
* // Export for client
|
|
105
|
+
* export const endpoints = [getUserEndpoint, createUserEndpoint] as const;
|
|
106
|
+
* export type Paths = InferOpenApi<typeof endpoints>['paths'];
|
|
107
|
+
*
|
|
108
|
+
* // Client usage
|
|
109
|
+
* import type { Paths } from './endpoints';
|
|
110
|
+
* const client = createTypedFetcher<Paths>({ baseURL: 'http://localhost:3000' });
|
|
111
|
+
*
|
|
112
|
+
* const user = await client('GET /users/{id}', { params: { id: '123' } });
|
|
113
|
+
* const newUser = await client('POST /users', { body: { name: 'Jane' } });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export type InferOpenApi<TEndpoints extends readonly any[]> = TEndpoints extends readonly [infer First, ...infer Rest] ? InferOpenApiFromEndpoint<First> extends {
|
|
117
|
+
paths: infer P1;
|
|
118
|
+
} ? Rest extends [] ? {
|
|
119
|
+
paths: P1;
|
|
120
|
+
} : InferOpenApi<Rest> extends {
|
|
121
|
+
paths: infer P2;
|
|
122
|
+
} ? {
|
|
123
|
+
paths: P1 & P2;
|
|
124
|
+
} : {
|
|
125
|
+
paths: P1;
|
|
126
|
+
} : InferOpenApi<Rest> : {
|
|
127
|
+
paths: {};
|
|
128
|
+
};
|
|
129
|
+
//#endregion
|
|
130
|
+
//# sourceMappingURL=infer.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infer.d.mts","names":[],"sources":["../src/infer.ts"],"mappings":";;;;;;;;;KAeK,gBAAgB,yBACpB,kCAAkC,eAAe,YAC3C,KAAK,oBAAmB,gBAAgB;;;;KAM1C,gBAAgB,UAAU,cAAc,UAAU;;;;KAKlD,qBAAqB,yBACzB,gBAAgB,gBAAgB;EAG7B;IACC,MAAM,gBAAgB;;;;;;KAOtB,yBAAyB,eAAe,mBAAmB;EAC/D,aAAa;;EAGX;IAMC,OAAO,yBAAyB;;;;;;KAQ/B,eACJ,eAAe,iBACf,gBAAgB,gCACb,yBAAyB;EAC5B,cAAc;IAAiB,YAAY;;IAExC;MAKC,oBAAoB,yBAAyB;;;EAIjD;IACC;MACC,SAAS,gBAAgB;QAEtB,oBAAoB,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA+BlC,yBAAyB,KACpC,UAAU,eACH,cACA,eACA,cACA;EAMJ,UACE,KAAK,SAAS,qBAAqB,aAClC,KAAK,gBAAgB,WAAW,eAAe,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA2ClD,aAAa,qCACxB,mCAAmC,gBAAgB,QAChD,yBAAyB;EAAiB,aAAa;IACtD;EACG,OAAO;IACT,aAAa;EAAgB,aAAa;;EACvC,OAAO,KAAK;;EACZ,OAAO;IACX,aAAa;EACZ"}
|
package/dist/infer.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
package/dist/openapi-hooks.cjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
const require_fetcher = require(
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_fetcher = require("./fetcher.cjs");
|
|
3
|
+
let _tanstack_react_query = require("@tanstack/react-query");
|
|
5
4
|
//#region src/openapi-hooks.ts
|
|
6
5
|
function createOpenAPIHooks(options = {}) {
|
|
7
|
-
const { operations
|
|
6
|
+
const { operations, ...fetcherOptions } = options;
|
|
8
7
|
const fetcher = require_fetcher.createTypedFetcher(fetcherOptions);
|
|
9
8
|
function buildEndpoint(operationId) {
|
|
10
9
|
const op = operations?.[operationId];
|
|
@@ -12,31 +11,29 @@ function createOpenAPIHooks(options = {}) {
|
|
|
12
11
|
return operationId;
|
|
13
12
|
}
|
|
14
13
|
return {
|
|
15
|
-
useQuery: (operationId, config, options
|
|
14
|
+
useQuery: (operationId, config, options) => {
|
|
16
15
|
const endpoint = buildEndpoint(operationId);
|
|
17
16
|
const queryKey = [operationId, ...config ? [config] : []];
|
|
18
|
-
return (0,
|
|
17
|
+
return (0, _tanstack_react_query.useQuery)({
|
|
19
18
|
queryKey,
|
|
20
19
|
queryFn: async () => {
|
|
21
|
-
|
|
22
|
-
return response;
|
|
20
|
+
return await fetcher(endpoint, config);
|
|
23
21
|
},
|
|
24
|
-
...options
|
|
22
|
+
...options
|
|
25
23
|
});
|
|
26
24
|
},
|
|
27
|
-
useMutation: (operationId, options
|
|
25
|
+
useMutation: (operationId, options) => {
|
|
28
26
|
const endpoint = buildEndpoint(operationId);
|
|
29
|
-
return (0,
|
|
27
|
+
return (0, _tanstack_react_query.useMutation)({
|
|
30
28
|
mutationFn: async (variables) => {
|
|
31
|
-
|
|
32
|
-
return response;
|
|
29
|
+
return await fetcher(endpoint, variables);
|
|
33
30
|
},
|
|
34
|
-
...options
|
|
31
|
+
...options
|
|
35
32
|
});
|
|
36
33
|
}
|
|
37
34
|
};
|
|
38
35
|
}
|
|
39
|
-
|
|
40
36
|
//#endregion
|
|
41
37
|
exports.createOpenAPIHooks = createOpenAPIHooks;
|
|
38
|
+
|
|
42
39
|
//# sourceMappingURL=openapi-hooks.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-hooks.cjs","names":["
|
|
1
|
+
{"version":3,"file":"openapi-hooks.cjs","names":["createTypedFetcher","useQuery","useMutation"],"sources":["../src/openapi-hooks.ts"],"sourcesContent":["import type {\n\tUseMutationOptions,\n\tUseQueryOptions,\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\t[Path in keyof Paths]: {\n\t\t[Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {\n\t\t\toperationId: infer OpId;\n\t\t}\n\t\t\t? OpId extends string\n\t\t\t\t? {\n\t\t\t\t\t\toperationId: OpId;\n\t\t\t\t\t\tpath: Path;\n\t\t\t\t\t\tmethod: Method;\n\t\t\t\t\t\tspec: Paths[Path][Method] & {\n\t\t\t\t\t\t\t_pathParams?: Paths[Path] extends {\n\t\t\t\t\t\t\t\tparameters: { path: infer P };\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t? P\n\t\t\t\t\t\t\t\t: never;\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t: never\n\t\t\t: never;\n\t}[keyof Paths[Path] & HttpMethods];\n}[keyof Paths];\n\n// Create operation map\ntype OperationMap<Paths> = {\n\t[Op in AllOperations<Paths> as Op extends {\n\t\toperationId: infer Id extends string;\n\t}\n\t\t? Id\n\t\t: 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\t[K in OperationId<Paths>]: OperationMap<Paths>[K] extends { method: Method }\n\t\t? K\n\t\t: never;\n}[OperationId<Paths>];\n\n// Extract parameter types\ntype OperationParams<\n\tPaths,\n\tOpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends {\n\tpath: infer Path extends keyof Paths;\n\tspec: infer Spec;\n}\n\t? {\n\t\t\tparams?: Paths[Path] extends { parameters: { path: infer P } }\n\t\t\t\t? P\n\t\t\t\t: Spec extends { parameters: { path: infer P } }\n\t\t\t\t\t? P\n\t\t\t\t\t: never;\n\t\t\tquery?: Spec extends { parameters?: { query?: infer Q } } ? Q : never;\n\t\t\tbody?: Spec extends {\n\t\t\t\trequestBody: { content: { 'application/json': infer Body } };\n\t\t\t}\n\t\t\t\t? Body\n\t\t\t\t: Spec extends {\n\t\t\t\t\t\t\trequestBody: {\n\t\t\t\t\t\t\t\trequired: true;\n\t\t\t\t\t\t\t\tcontent: { 'application/json': infer Body };\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t? Body\n\t\t\t\t\t: never;\n\t\t}\n\t: never;\n\n// Extract response type\ntype OperationResponse<\n\tPaths,\n\tOpId extends OperationId<Paths>,\n> = OperationMap<Paths>[OpId] extends { spec: infer Spec }\n\t? Spec extends {\n\t\t\tresponses: { 200: { content: { 'application/json': infer R } } };\n\t\t}\n\t\t? R\n\t\t: Spec extends {\n\t\t\t\t\tresponses: { 201: { content: { 'application/json': infer R } } };\n\t\t\t\t}\n\t\t\t? R\n\t\t\t: Spec extends { responses: { 204: any } }\n\t\t\t\t? undefined\n\t\t\t\t: unknown\n\t: never;\n\n// Remove never properties\ntype RemoveNever<T> = Pick<\n\tT,\n\t{\n\t\t[K in keyof T]: T[K] extends never ? never : K;\n\t}[keyof T]\n>;\n\n// Runtime operation registry (would be generated)\ninterface OperationRegistry {\n\t[operationId: string]: {\n\t\tpath: string;\n\t\tmethod: string;\n\t};\n}\n\nexport function createOpenAPIHooks<Paths>(\n\toptions: FetcherOptions & { operations?: OperationRegistry } = {},\n) {\n\tconst { operations, ...fetcherOptions } = options;\n\tconst fetcher = createTypedFetcher<Paths>(fetcherOptions);\n\n\tfunction buildEndpoint<OpId extends OperationId<Paths>>(\n\t\toperationId: OpId,\n\t): string {\n\t\t// Runtime lookup from registry\n\t\tconst op = operations?.[operationId as string];\n\t\tif (op) {\n\t\t\treturn `${op.method.toUpperCase()} ${op.path}`;\n\t\t}\n\t\t// Fallback for compile-time only usage\n\t\treturn operationId as string;\n\t}\n\n\treturn {\n\t\tuseQuery: <OpId extends OperationsByMethod<Paths, 'get'>>(\n\t\t\toperationId: OpId,\n\t\t\tconfig?: RemoveNever<OperationParams<Paths, OpId>>,\n\t\t\toptions?: Omit<\n\t\t\t\tUseQueryOptions<OperationResponse<Paths, OpId>, Error>,\n\t\t\t\t'queryKey' | 'queryFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst endpoint = buildEndpoint(operationId);\n\t\t\tconst queryKey = [operationId, ...(config ? [config] : [])];\n\n\t\t\treturn useQuery<OperationResponse<Paths, OpId>, Error>({\n\t\t\t\tqueryKey,\n\t\t\t\tqueryFn: async () => {\n\t\t\t\t\tconst response = await fetcher(endpoint as any, config as any);\n\t\t\t\t\treturn response as OperationResponse<Paths, OpId>;\n\t\t\t\t},\n\t\t\t\t...options,\n\t\t\t});\n\t\t},\n\n\t\tuseMutation: <\n\t\t\tOpId extends Exclude<\n\t\t\t\tOperationId<Paths>,\n\t\t\t\tOperationsByMethod<Paths, 'get'>\n\t\t\t>,\n\t\t>(\n\t\t\toperationId: OpId,\n\t\t\toptions?: Omit<\n\t\t\t\tUseMutationOptions<\n\t\t\t\t\tOperationResponse<Paths, OpId>,\n\t\t\t\t\tError,\n\t\t\t\t\tRemoveNever<OperationParams<Paths, OpId>>\n\t\t\t\t>,\n\t\t\t\t'mutationFn'\n\t\t\t>,\n\t\t) => {\n\t\t\tconst endpoint = buildEndpoint(operationId);\n\n\t\t\treturn useMutation<\n\t\t\t\tOperationResponse<Paths, OpId>,\n\t\t\t\tError,\n\t\t\t\tRemoveNever<OperationParams<Paths, OpId>>\n\t\t\t>({\n\t\t\t\tmutationFn: async (variables) => {\n\t\t\t\t\tconst response = await fetcher(endpoint as any, variables as any);\n\t\t\t\t\treturn response as OperationResponse<Paths, OpId>;\n\t\t\t\t},\n\t\t\t\t...options,\n\t\t\t});\n\t\t},\n\t};\n}\n"],"mappings":";;;;AAsHA,SAAgB,mBACf,UAA+D,CAAC,GAC/D;CACD,MAAM,EAAE,YAAY,GAAG,mBAAmB;CAC1C,MAAM,UAAUA,gBAAAA,mBAA0B,cAAc;CAExD,SAAS,cACR,aACS;EAET,MAAM,KAAK,aAAa;EACxB,IAAI,IACH,OAAO,GAAG,GAAG,OAAO,YAAY,EAAE,GAAG,GAAG;EAGzC,OAAO;CACR;CAEA,OAAO;EACN,WACC,aACA,QACA,YAII;GACJ,MAAM,WAAW,cAAc,WAAW;GAC1C,MAAM,WAAW,CAAC,aAAa,GAAI,SAAS,CAAC,MAAM,IAAI,CAAC,CAAE;GAE1D,QAAA,GAAOC,sBAAAA,SAAAA,CAAgD;IACtD;IACA,SAAS,YAAY;KAEpB,OAAO,MADgB,QAAQ,UAAiB,MAAa;IAE9D;IACA,GAAG;GACJ,CAAC;EACF;EAEA,cAMC,aACA,YAQI;GACJ,MAAM,WAAW,cAAc,WAAW;GAE1C,QAAA,GAAOC,sBAAAA,YAAAA,CAIL;IACD,YAAY,OAAO,cAAc;KAEhC,OAAO,MADgB,QAAQ,UAAiB,SAAgB;IAEjE;IACA,GAAG;GACJ,CAAC;EACF;CACD;AACD"}
|
package/dist/openapi-hooks.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { FetcherOptions } from "./types
|
|
2
|
-
import * as _tanstack_react_query0 from "@tanstack/react-query";
|
|
1
|
+
import { FetcherOptions } from "./types.cjs";
|
|
3
2
|
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
|
-
|
|
5
3
|
//#region src/openapi-hooks.d.ts
|
|
6
4
|
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
7
5
|
type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
@@ -17,14 +15,14 @@ type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Pa
|
|
|
17
15
|
};
|
|
18
16
|
} ? P : never;
|
|
19
17
|
};
|
|
20
|
-
} : never : never }[keyof Paths[Path] & HttpMethods] }[keyof Paths];
|
|
18
|
+
} : never : never; }[keyof Paths[Path] & HttpMethods]; }[keyof Paths];
|
|
21
19
|
type OperationMap<Paths> = { [Op in AllOperations<Paths> as Op extends {
|
|
22
20
|
operationId: infer Id extends string;
|
|
23
|
-
} ? Id : never]: Op };
|
|
21
|
+
} ? Id : never]: Op; };
|
|
24
22
|
type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
25
23
|
type OperationsByMethod<Paths, Method extends HttpMethods> = { [K in OperationId<Paths>]: OperationMap<Paths>[K] extends {
|
|
26
24
|
method: Method;
|
|
27
|
-
} ? K : never }[OperationId<Paths>];
|
|
25
|
+
} ? K : never; }[OperationId<Paths>];
|
|
28
26
|
type OperationParams<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
29
27
|
path: infer Path extends keyof Paths;
|
|
30
28
|
spec: infer Spec;
|
|
@@ -81,19 +79,18 @@ type OperationResponse<Paths, OpId extends OperationId<Paths>> = OperationMap<Pa
|
|
|
81
79
|
204: any;
|
|
82
80
|
};
|
|
83
81
|
} ? undefined : unknown : never;
|
|
84
|
-
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>;
|
|
82
|
+
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K; }[keyof T]>;
|
|
85
83
|
interface OperationRegistry {
|
|
86
84
|
[operationId: string]: {
|
|
87
85
|
path: string;
|
|
88
86
|
method: string;
|
|
89
87
|
};
|
|
90
88
|
}
|
|
91
|
-
declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
89
|
+
export declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
92
90
|
operations?: OperationRegistry;
|
|
93
91
|
}): {
|
|
94
|
-
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) =>
|
|
95
|
-
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) =>
|
|
92
|
+
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) => import("@tanstack/react-query").UseQueryResult<import("@tanstack/react-query").NoInfer<OperationResponse<Paths, OpId>>, Error>;
|
|
93
|
+
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) => import("@tanstack/react-query").UseMutationResult<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>, unknown>;
|
|
96
94
|
};
|
|
97
95
|
//#endregion
|
|
98
|
-
export { createOpenAPIHooks };
|
|
99
96
|
//# sourceMappingURL=openapi-hooks.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-hooks.d.cts","names":[],"sources":["../src/openapi-hooks.ts"],"
|
|
1
|
+
{"version":3,"file":"openapi-hooks.d.cts","names":[],"sources":["../src/openapi-hooks.ts"],"mappings":";;;KASK;KAGA,cAAc,YACjB,cAAc,WACb,gBAAgB,MAAM,QAAQ,cAAc,MAAM,MAAM;EACxD,mBAAmB;IAEjB;EAEC,aAAa;EACb,MAAM;EACN,QAAQ;EACR,MAAM,MAAM,MAAM;IACjB,cAAc,MAAM;MACnB;QAAc,YAAY;;QAExB;;2BAMD,MAAM,QAAQ,sBACf;KAGH,aAAa,YAChB,MAAM,cAAc,UAAU;EAC9B,mBAAmB;IAEjB,aACQ;KAIP,YAAY,eAAe,aAAa;KAGxC,mBAAmB,OAAO,eAAe,kBAC5C,KAAK,YAAY,SAAS,aAAa,OAAO;EAAa,QAAQ;IACjE,aAEF,YAAY;KAGT,gBACJ,OACA,aAAa,YAAY,UACtB,aAAa,OAAO;EACvB,YAAY,mBAAmB;EAC/B,YAAY;;EAGV,SAAS,MAAM;IAAgB;MAAc,YAAY;;MACtD,IACA;IAAe;MAAc,YAAY;;MACxC;EAEJ,QAAQ;IAAe;MAAe,cAAc;;MAAQ;EAC5D,OAAO;IACN;MAAe;QAAW,0BAA0B;;;MAElD,OACA;IACC;MACC;MACA;QAAW,0BAA0B;;;MAGtC;;KAMF,kBACJ,OACA,aAAa,YAAY,UACtB,aAAa,OAAO;EAAgB,YAAY;IACjD;EACA;IAAa;MAAO;QAAW,0BAA0B;;;;IAExD,IACA;EACC;IAAa;MAAO;QAAW,0BAA0B;;;;IAEzD,IACA;EAAe;IAAa;;;KAM5B,YAAY,KAAK,KACrB,MAEE,WAAW,IAAI,EAAE,2BAA2B,WACtC;UAIC;GACR;IACA;IACA;;;wBAIc,mBAAmB,OAClC,UAAS;EAAmB,aAAa;;EAkB7B,WAAA,aAAa,mBAAmB,eAAa,aAC1C,MAAI,SACR,YAAY,gBAAgB,OAAO,QAAM,UACxC,KACT,gBAAgB,kBAAkB,OAAO,OAAO,oEAEhD,+CAAA,QAAA,kBAAA,OAAA,QAAA;EAgBD,cAAA,aAAa,QACZ,YAAY,QACZ,mBAAmB,gBACnB,aAEY,MAAI,UACP,KACT,mBACC,kBAAkB,OAAO,OACzB,OACA,YAAY,gBAAgB,OAAO,2DAGpC,kBAAA,kBAAA,OAAA,OAAA,OAAA,YAAA,gBAAA,OAAA"}
|
package/dist/openapi-hooks.d.mts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { FetcherOptions } from "./types
|
|
2
|
-
import * as _tanstack_react_query0 from "@tanstack/react-query";
|
|
1
|
+
import { FetcherOptions } from "./types.mjs";
|
|
3
2
|
import { UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
4
|
-
|
|
5
3
|
//#region src/openapi-hooks.d.ts
|
|
6
4
|
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
7
5
|
type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Path] & HttpMethods]: Paths[Path][Method] extends {
|
|
@@ -17,14 +15,14 @@ type AllOperations<Paths> = { [Path in keyof Paths]: { [Method in keyof Paths[Pa
|
|
|
17
15
|
};
|
|
18
16
|
} ? P : never;
|
|
19
17
|
};
|
|
20
|
-
} : never : never }[keyof Paths[Path] & HttpMethods] }[keyof Paths];
|
|
18
|
+
} : never : never; }[keyof Paths[Path] & HttpMethods]; }[keyof Paths];
|
|
21
19
|
type OperationMap<Paths> = { [Op in AllOperations<Paths> as Op extends {
|
|
22
20
|
operationId: infer Id extends string;
|
|
23
|
-
} ? Id : never]: Op };
|
|
21
|
+
} ? Id : never]: Op; };
|
|
24
22
|
type OperationId<Paths> = keyof OperationMap<Paths>;
|
|
25
23
|
type OperationsByMethod<Paths, Method extends HttpMethods> = { [K in OperationId<Paths>]: OperationMap<Paths>[K] extends {
|
|
26
24
|
method: Method;
|
|
27
|
-
} ? K : never }[OperationId<Paths>];
|
|
25
|
+
} ? K : never; }[OperationId<Paths>];
|
|
28
26
|
type OperationParams<Paths, OpId extends OperationId<Paths>> = OperationMap<Paths>[OpId] extends {
|
|
29
27
|
path: infer Path extends keyof Paths;
|
|
30
28
|
spec: infer Spec;
|
|
@@ -81,19 +79,18 @@ type OperationResponse<Paths, OpId extends OperationId<Paths>> = OperationMap<Pa
|
|
|
81
79
|
204: any;
|
|
82
80
|
};
|
|
83
81
|
} ? undefined : unknown : never;
|
|
84
|
-
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>;
|
|
82
|
+
type RemoveNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K; }[keyof T]>;
|
|
85
83
|
interface OperationRegistry {
|
|
86
84
|
[operationId: string]: {
|
|
87
85
|
path: string;
|
|
88
86
|
method: string;
|
|
89
87
|
};
|
|
90
88
|
}
|
|
91
|
-
declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
89
|
+
export declare function createOpenAPIHooks<Paths>(options?: FetcherOptions & {
|
|
92
90
|
operations?: OperationRegistry;
|
|
93
91
|
}): {
|
|
94
|
-
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) =>
|
|
95
|
-
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) =>
|
|
92
|
+
useQuery: <OpId extends OperationsByMethod<Paths, "get">>(operationId: OpId, config?: RemoveNever<OperationParams<Paths, OpId>>, options?: Omit<UseQueryOptions<OperationResponse<Paths, OpId>, Error>, "queryKey" | "queryFn">) => import("@tanstack/react-query").UseQueryResult<import("@tanstack/react-query").NoInfer<OperationResponse<Paths, OpId>>, Error>;
|
|
93
|
+
useMutation: <OpId extends Exclude<OperationId<Paths>, OperationsByMethod<Paths, "get">>>(operationId: OpId, options?: Omit<UseMutationOptions<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>>, "mutationFn">) => import("@tanstack/react-query").UseMutationResult<OperationResponse<Paths, OpId>, Error, RemoveNever<OperationParams<Paths, OpId>>, unknown>;
|
|
96
94
|
};
|
|
97
95
|
//#endregion
|
|
98
|
-
export { createOpenAPIHooks };
|
|
99
96
|
//# sourceMappingURL=openapi-hooks.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-hooks.d.mts","names":[],"sources":["../src/openapi-hooks.ts"],"
|
|
1
|
+
{"version":3,"file":"openapi-hooks.d.mts","names":[],"sources":["../src/openapi-hooks.ts"],"mappings":";;;KASK;KAGA,cAAc,YACjB,cAAc,WACb,gBAAgB,MAAM,QAAQ,cAAc,MAAM,MAAM;EACxD,mBAAmB;IAEjB;EAEC,aAAa;EACb,MAAM;EACN,QAAQ;EACR,MAAM,MAAM,MAAM;IACjB,cAAc,MAAM;MACnB;QAAc,YAAY;;QAExB;;2BAMD,MAAM,QAAQ,sBACf;KAGH,aAAa,YAChB,MAAM,cAAc,UAAU;EAC9B,mBAAmB;IAEjB,aACQ;KAIP,YAAY,eAAe,aAAa;KAGxC,mBAAmB,OAAO,eAAe,kBAC5C,KAAK,YAAY,SAAS,aAAa,OAAO;EAAa,QAAQ;IACjE,aAEF,YAAY;KAGT,gBACJ,OACA,aAAa,YAAY,UACtB,aAAa,OAAO;EACvB,YAAY,mBAAmB;EAC/B,YAAY;;EAGV,SAAS,MAAM;IAAgB;MAAc,YAAY;;MACtD,IACA;IAAe;MAAc,YAAY;;MACxC;EAEJ,QAAQ;IAAe;MAAe,cAAc;;MAAQ;EAC5D,OAAO;IACN;MAAe;QAAW,0BAA0B;;;MAElD,OACA;IACC;MACC;MACA;QAAW,0BAA0B;;;MAGtC;;KAMF,kBACJ,OACA,aAAa,YAAY,UACtB,aAAa,OAAO;EAAgB,YAAY;IACjD;EACA;IAAa;MAAO;QAAW,0BAA0B;;;;IAExD,IACA;EACC;IAAa;MAAO;QAAW,0BAA0B;;;;IAEzD,IACA;EAAe;IAAa;;;KAM5B,YAAY,KAAK,KACrB,MAEE,WAAW,IAAI,EAAE,2BAA2B,WACtC;UAIC;GACR;IACA;IACA;;;wBAIc,mBAAmB,OAClC,UAAS;EAAmB,aAAa;;EAkB7B,WAAA,aAAa,mBAAmB,eAAa,aAC1C,MAAI,SACR,YAAY,gBAAgB,OAAO,QAAM,UACxC,KACT,gBAAgB,kBAAkB,OAAO,OAAO,oEAEhD,+CAAA,QAAA,kBAAA,OAAA,QAAA;EAgBD,cAAA,aAAa,QACZ,YAAY,QACZ,mBAAmB,gBACnB,aAEY,MAAI,UACP,KACT,mBACC,kBAAkB,OAAO,OACzB,OACA,YAAY,gBAAgB,OAAO,2DAGpC,kBAAA,kBAAA,OAAA,OAAA,OAAA,YAAA,gBAAA,OAAA"}
|
package/dist/openapi-hooks.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { createTypedFetcher } from "./fetcher
|
|
1
|
+
import { createTypedFetcher } from "./fetcher.mjs";
|
|
2
2
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
3
|
-
|
|
4
3
|
//#region src/openapi-hooks.ts
|
|
5
4
|
function createOpenAPIHooks(options = {}) {
|
|
6
|
-
const { operations
|
|
5
|
+
const { operations, ...fetcherOptions } = options;
|
|
7
6
|
const fetcher = createTypedFetcher(fetcherOptions);
|
|
8
7
|
function buildEndpoint(operationId) {
|
|
9
8
|
const op = operations?.[operationId];
|
|
@@ -11,31 +10,29 @@ function createOpenAPIHooks(options = {}) {
|
|
|
11
10
|
return operationId;
|
|
12
11
|
}
|
|
13
12
|
return {
|
|
14
|
-
useQuery: (operationId, config, options
|
|
13
|
+
useQuery: (operationId, config, options) => {
|
|
15
14
|
const endpoint = buildEndpoint(operationId);
|
|
16
15
|
const queryKey = [operationId, ...config ? [config] : []];
|
|
17
16
|
return useQuery({
|
|
18
17
|
queryKey,
|
|
19
18
|
queryFn: async () => {
|
|
20
|
-
|
|
21
|
-
return response;
|
|
19
|
+
return await fetcher(endpoint, config);
|
|
22
20
|
},
|
|
23
|
-
...options
|
|
21
|
+
...options
|
|
24
22
|
});
|
|
25
23
|
},
|
|
26
|
-
useMutation: (operationId, options
|
|
24
|
+
useMutation: (operationId, options) => {
|
|
27
25
|
const endpoint = buildEndpoint(operationId);
|
|
28
26
|
return useMutation({
|
|
29
27
|
mutationFn: async (variables) => {
|
|
30
|
-
|
|
31
|
-
return response;
|
|
28
|
+
return await fetcher(endpoint, variables);
|
|
32
29
|
},
|
|
33
|
-
...options
|
|
30
|
+
...options
|
|
34
31
|
});
|
|
35
32
|
}
|
|
36
33
|
};
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
//#endregion
|
|
40
36
|
export { createOpenAPIHooks };
|
|
37
|
+
|
|
41
38
|
//# sourceMappingURL=openapi-hooks.mjs.map
|