typed-openapi 2.2.0 → 2.2.2
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/{chunk-IB6GEWS7.js → chunk-4VZOXNI4.js} +1 -1
- package/dist/{chunk-QGMS7IBQ.js → chunk-L5G4LNMY.js} +24 -23
- package/dist/cli.js +2 -2
- package/dist/index.js +1 -1
- package/dist/node.export.js +2 -2
- package/package.json +1 -1
- package/src/generator.ts +7 -7
- package/src/tanstack-query.generator.ts +17 -16
|
@@ -668,18 +668,18 @@ type NotNever<T> = [T] extends [never] ? false : true;
|
|
|
668
668
|
const infer = inferByRuntime[ctx.runtime];
|
|
669
669
|
const InferTEndpoint = match(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`)).otherwise(() => `TEndpoint`);
|
|
670
670
|
const apiClient = `
|
|
671
|
-
// <
|
|
672
|
-
export class
|
|
673
|
-
response: TypedErrorResponse<
|
|
671
|
+
// <TypedStatusError>
|
|
672
|
+
export class TypedStatusError<TData = unknown> extends Error {
|
|
673
|
+
response: TypedErrorResponse<TData, ErrorStatusCode, unknown>;
|
|
674
674
|
status: number;
|
|
675
|
-
constructor(response: TypedErrorResponse<
|
|
675
|
+
constructor(response: TypedErrorResponse<TData, ErrorStatusCode, unknown>) {
|
|
676
676
|
super(\`HTTP \${response.status}: \${response.statusText}\`);
|
|
677
|
-
this.name = '
|
|
677
|
+
this.name = 'TypedStatusError';
|
|
678
678
|
this.response = response;
|
|
679
679
|
this.status = response.status;
|
|
680
680
|
}
|
|
681
681
|
}
|
|
682
|
-
// </
|
|
682
|
+
// </TypedStatusError>
|
|
683
683
|
|
|
684
684
|
// <ApiClient>
|
|
685
685
|
export class ApiClient {
|
|
@@ -851,7 +851,7 @@ export class ApiClient {
|
|
|
851
851
|
}) as SafeApiResponse<TEndpoint>;
|
|
852
852
|
|
|
853
853
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
854
|
-
throw new
|
|
854
|
+
throw new TypedStatusError(typedResponse as never);
|
|
855
855
|
}
|
|
856
856
|
|
|
857
857
|
return withResponse ? typedResponse : data;
|
|
@@ -1364,8 +1364,8 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1364
1364
|
const endpointMethods = new Set(ctx.endpointList.map((endpoint) => endpoint.method.toLowerCase()));
|
|
1365
1365
|
const file = `
|
|
1366
1366
|
import { queryOptions } from "@tanstack/react-query"
|
|
1367
|
-
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus } from "${ctx.relativeApiClientPath}"
|
|
1368
|
-
import { errorStatusCodes,
|
|
1367
|
+
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus, TypedSuccessResponse } from "${ctx.relativeApiClientPath}"
|
|
1368
|
+
import { errorStatusCodes, TypedStatusError } from "${ctx.relativeApiClientPath}"
|
|
1369
1369
|
|
|
1370
1370
|
type EndpointQueryKey<TOptions extends EndpointParameters> = [
|
|
1371
1371
|
TOptions & {
|
|
@@ -1416,6 +1416,11 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1416
1416
|
|
|
1417
1417
|
type MaybeOptionalArg<T> = RequiredKeys<T> extends never ? [config?: T] : [config: T];
|
|
1418
1418
|
|
|
1419
|
+
type InferResponseData<TEndpoint, TStatusCode> = TypedSuccessResponse<any, any, any> extends
|
|
1420
|
+
InferResponseByStatus<TEndpoint, TStatusCode>
|
|
1421
|
+
? Extract<InferResponseByStatus<TEndpoint, TStatusCode>, { data: {}}>["data"]
|
|
1422
|
+
: Extract<InferResponseByStatus<TEndpoint, TStatusCode>["data"], {}>;
|
|
1423
|
+
|
|
1419
1424
|
// </ApiClientTypes>
|
|
1420
1425
|
|
|
1421
1426
|
// <ApiClient>
|
|
@@ -1447,7 +1452,7 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1447
1452
|
withResponse: false as const
|
|
1448
1453
|
};
|
|
1449
1454
|
const res = await this.client.${method}(path, requestParams as never);
|
|
1450
|
-
return res as
|
|
1455
|
+
return res as InferResponseData<TEndpoint, SuccessStatusCode>;
|
|
1451
1456
|
},
|
|
1452
1457
|
queryKey: queryKey
|
|
1453
1458
|
}),
|
|
@@ -1471,31 +1476,27 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1471
1476
|
TWithResponse extends boolean = false,
|
|
1472
1477
|
TSelection = TWithResponse extends true
|
|
1473
1478
|
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
1474
|
-
:
|
|
1479
|
+
: InferResponseData<TEndpoint, SuccessStatusCode>,
|
|
1475
1480
|
TError = TEndpoint extends { responses: infer TResponses }
|
|
1476
1481
|
? TResponses extends Record<string | number, unknown>
|
|
1477
|
-
?
|
|
1482
|
+
? TypedStatusError<InferResponseData<TEndpoint, ErrorStatusCode>>
|
|
1478
1483
|
: Error
|
|
1479
1484
|
: Error
|
|
1480
1485
|
>(method: TMethod, path: TPath, options?: {
|
|
1481
1486
|
withResponse?: TWithResponse;
|
|
1482
1487
|
selectFn?: (res: TWithResponse extends true
|
|
1483
1488
|
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
1484
|
-
:
|
|
1489
|
+
: InferResponseData<TEndpoint, SuccessStatusCode>
|
|
1485
1490
|
) => TSelection;
|
|
1486
1491
|
throwOnStatusError?: boolean
|
|
1487
1492
|
throwOnError?: boolean | ((error: TError) => boolean)
|
|
1488
1493
|
}) {
|
|
1489
1494
|
const mutationKey = [{ method, path }] as const;
|
|
1490
|
-
const mutationFn = async
|
|
1491
|
-
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
1492
|
-
: Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"]>
|
|
1493
|
-
(params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1494
|
-
withResponse?: TLocalWithResponse;
|
|
1495
|
+
const mutationFn = async (params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1495
1496
|
throwOnStatusError?: boolean;
|
|
1496
1497
|
overrides?: RequestInit;
|
|
1497
|
-
}): Promise<
|
|
1498
|
-
const withResponse =
|
|
1498
|
+
}): Promise<TSelection> => {
|
|
1499
|
+
const withResponse = options?.withResponse ?? false;
|
|
1499
1500
|
const throwOnStatusError = params.throwOnStatusError ?? options?.throwOnStatusError ?? (withResponse ? false : true);
|
|
1500
1501
|
const selectFn = options?.selectFn;
|
|
1501
1502
|
const response = await (this.client as any)[method](path, {
|
|
@@ -1505,7 +1506,7 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1505
1506
|
});
|
|
1506
1507
|
|
|
1507
1508
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
1508
|
-
throw new
|
|
1509
|
+
throw new TypedStatusError(response as never);
|
|
1509
1510
|
}
|
|
1510
1511
|
|
|
1511
1512
|
// Return just the data if withResponse is false, otherwise return the full response
|
|
@@ -1526,8 +1527,8 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1526
1527
|
TSelection,
|
|
1527
1528
|
TError,
|
|
1528
1529
|
(TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1529
|
-
|
|
1530
|
-
|
|
1530
|
+
withResponse?: boolean;
|
|
1531
|
+
throwOnStatusError?: boolean;
|
|
1531
1532
|
}
|
|
1532
1533
|
>, "mutationFn"> & {
|
|
1533
1534
|
mutationFn: typeof mutationFn
|
package/dist/cli.js
CHANGED
package/dist/index.js
CHANGED
package/dist/node.export.js
CHANGED
package/package.json
CHANGED
package/src/generator.ts
CHANGED
|
@@ -424,18 +424,18 @@ type NotNever<T> = [T] extends [never] ? false : true;
|
|
|
424
424
|
.otherwise(() => `TEndpoint`);
|
|
425
425
|
|
|
426
426
|
const apiClient = `
|
|
427
|
-
// <
|
|
428
|
-
export class
|
|
429
|
-
response: TypedErrorResponse<
|
|
427
|
+
// <TypedStatusError>
|
|
428
|
+
export class TypedStatusError<TData = unknown> extends Error {
|
|
429
|
+
response: TypedErrorResponse<TData, ErrorStatusCode, unknown>;
|
|
430
430
|
status: number;
|
|
431
|
-
constructor(response: TypedErrorResponse<
|
|
431
|
+
constructor(response: TypedErrorResponse<TData, ErrorStatusCode, unknown>) {
|
|
432
432
|
super(\`HTTP \${response.status}: \${response.statusText}\`);
|
|
433
|
-
this.name = '
|
|
433
|
+
this.name = 'TypedStatusError';
|
|
434
434
|
this.response = response;
|
|
435
435
|
this.status = response.status;
|
|
436
436
|
}
|
|
437
437
|
}
|
|
438
|
-
// </
|
|
438
|
+
// </TypedStatusError>
|
|
439
439
|
|
|
440
440
|
// <ApiClient>
|
|
441
441
|
export class ApiClient {
|
|
@@ -612,7 +612,7 @@ export class ApiClient {
|
|
|
612
612
|
}) as SafeApiResponse<TEndpoint>;
|
|
613
613
|
|
|
614
614
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
615
|
-
throw new
|
|
615
|
+
throw new TypedStatusError(typedResponse as never);
|
|
616
616
|
}
|
|
617
617
|
|
|
618
618
|
return withResponse ? typedResponse : data;
|
|
@@ -12,8 +12,8 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
12
12
|
|
|
13
13
|
const file = `
|
|
14
14
|
import { queryOptions } from "@tanstack/react-query"
|
|
15
|
-
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus } from "${ctx.relativeApiClientPath}"
|
|
16
|
-
import { errorStatusCodes,
|
|
15
|
+
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus, TypedSuccessResponse } from "${ctx.relativeApiClientPath}"
|
|
16
|
+
import { errorStatusCodes, TypedStatusError } from "${ctx.relativeApiClientPath}"
|
|
17
17
|
|
|
18
18
|
type EndpointQueryKey<TOptions extends EndpointParameters> = [
|
|
19
19
|
TOptions & {
|
|
@@ -66,6 +66,11 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
66
66
|
|
|
67
67
|
type MaybeOptionalArg<T> = RequiredKeys<T> extends never ? [config?: T] : [config: T];
|
|
68
68
|
|
|
69
|
+
type InferResponseData<TEndpoint, TStatusCode> = TypedSuccessResponse<any, any, any> extends
|
|
70
|
+
InferResponseByStatus<TEndpoint, TStatusCode>
|
|
71
|
+
? Extract<InferResponseByStatus<TEndpoint, TStatusCode>, { data: {}}>["data"]
|
|
72
|
+
: Extract<InferResponseByStatus<TEndpoint, TStatusCode>["data"], {}>;
|
|
73
|
+
|
|
69
74
|
// </ApiClientTypes>
|
|
70
75
|
|
|
71
76
|
// <ApiClient>
|
|
@@ -98,7 +103,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
98
103
|
withResponse: false as const
|
|
99
104
|
};
|
|
100
105
|
const res = await this.client.${method}(path, requestParams as never);
|
|
101
|
-
return res as
|
|
106
|
+
return res as InferResponseData<TEndpoint, SuccessStatusCode>;
|
|
102
107
|
},
|
|
103
108
|
queryKey: queryKey
|
|
104
109
|
}),
|
|
@@ -123,31 +128,27 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
123
128
|
TWithResponse extends boolean = false,
|
|
124
129
|
TSelection = TWithResponse extends true
|
|
125
130
|
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
126
|
-
:
|
|
131
|
+
: InferResponseData<TEndpoint, SuccessStatusCode>,
|
|
127
132
|
TError = TEndpoint extends { responses: infer TResponses }
|
|
128
133
|
? TResponses extends Record<string | number, unknown>
|
|
129
|
-
?
|
|
134
|
+
? TypedStatusError<InferResponseData<TEndpoint, ErrorStatusCode>>
|
|
130
135
|
: Error
|
|
131
136
|
: Error
|
|
132
137
|
>(method: TMethod, path: TPath, options?: {
|
|
133
138
|
withResponse?: TWithResponse;
|
|
134
139
|
selectFn?: (res: TWithResponse extends true
|
|
135
140
|
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
136
|
-
:
|
|
141
|
+
: InferResponseData<TEndpoint, SuccessStatusCode>
|
|
137
142
|
) => TSelection;
|
|
138
143
|
throwOnStatusError?: boolean
|
|
139
144
|
throwOnError?: boolean | ((error: TError) => boolean)
|
|
140
145
|
}) {
|
|
141
146
|
const mutationKey = [{ method, path }] as const;
|
|
142
|
-
const mutationFn = async
|
|
143
|
-
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
144
|
-
: Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"]>
|
|
145
|
-
(params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
146
|
-
withResponse?: TLocalWithResponse;
|
|
147
|
+
const mutationFn = async (params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
147
148
|
throwOnStatusError?: boolean;
|
|
148
149
|
overrides?: RequestInit;
|
|
149
|
-
}): Promise<
|
|
150
|
-
const withResponse =
|
|
150
|
+
}): Promise<TSelection> => {
|
|
151
|
+
const withResponse = options?.withResponse ?? false;
|
|
151
152
|
const throwOnStatusError = params.throwOnStatusError ?? options?.throwOnStatusError ?? (withResponse ? false : true);
|
|
152
153
|
const selectFn = options?.selectFn;
|
|
153
154
|
const response = await (this.client as any)[method](path, {
|
|
@@ -157,7 +158,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
157
158
|
});
|
|
158
159
|
|
|
159
160
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
160
|
-
throw new
|
|
161
|
+
throw new TypedStatusError(response as never);
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
// Return just the data if withResponse is false, otherwise return the full response
|
|
@@ -178,8 +179,8 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
178
179
|
TSelection,
|
|
179
180
|
TError,
|
|
180
181
|
(TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
181
|
-
|
|
182
|
-
|
|
182
|
+
withResponse?: boolean;
|
|
183
|
+
throwOnStatusError?: boolean;
|
|
183
184
|
}
|
|
184
185
|
>, "mutationFn"> & {
|
|
185
186
|
mutationFn: typeof mutationFn
|