typed-openapi 2.2.1 → 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-TC2ECJEG.js → chunk-4VZOXNI4.js} +1 -1
- package/dist/{chunk-ISAFCW3H.js → chunk-L5G4LNMY.js} +15 -19
- 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 +8 -12
|
@@ -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;
|
|
@@ -1365,7 +1365,7 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1365
1365
|
const file = `
|
|
1366
1366
|
import { queryOptions } from "@tanstack/react-query"
|
|
1367
1367
|
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus, TypedSuccessResponse } from "${ctx.relativeApiClientPath}"
|
|
1368
|
-
import { errorStatusCodes,
|
|
1368
|
+
import { errorStatusCodes, TypedStatusError } from "${ctx.relativeApiClientPath}"
|
|
1369
1369
|
|
|
1370
1370
|
type EndpointQueryKey<TOptions extends EndpointParameters> = [
|
|
1371
1371
|
TOptions & {
|
|
@@ -1479,7 +1479,7 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1479
1479
|
: InferResponseData<TEndpoint, SuccessStatusCode>,
|
|
1480
1480
|
TError = TEndpoint extends { responses: infer TResponses }
|
|
1481
1481
|
? TResponses extends Record<string | number, unknown>
|
|
1482
|
-
?
|
|
1482
|
+
? TypedStatusError<InferResponseData<TEndpoint, ErrorStatusCode>>
|
|
1483
1483
|
: Error
|
|
1484
1484
|
: Error
|
|
1485
1485
|
>(method: TMethod, path: TPath, options?: {
|
|
@@ -1492,15 +1492,11 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1492
1492
|
throwOnError?: boolean | ((error: TError) => boolean)
|
|
1493
1493
|
}) {
|
|
1494
1494
|
const mutationKey = [{ method, path }] as const;
|
|
1495
|
-
const mutationFn = async
|
|
1496
|
-
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
1497
|
-
: InferResponseData<TEndpoint, SuccessStatusCode>>
|
|
1498
|
-
(params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1499
|
-
withResponse?: TLocalWithResponse;
|
|
1495
|
+
const mutationFn = async (params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1500
1496
|
throwOnStatusError?: boolean;
|
|
1501
1497
|
overrides?: RequestInit;
|
|
1502
|
-
}): Promise<
|
|
1503
|
-
const withResponse =
|
|
1498
|
+
}): Promise<TSelection> => {
|
|
1499
|
+
const withResponse = options?.withResponse ?? false;
|
|
1504
1500
|
const throwOnStatusError = params.throwOnStatusError ?? options?.throwOnStatusError ?? (withResponse ? false : true);
|
|
1505
1501
|
const selectFn = options?.selectFn;
|
|
1506
1502
|
const response = await (this.client as any)[method](path, {
|
|
@@ -1510,7 +1506,7 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1510
1506
|
});
|
|
1511
1507
|
|
|
1512
1508
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
1513
|
-
throw new
|
|
1509
|
+
throw new TypedStatusError(response as never);
|
|
1514
1510
|
}
|
|
1515
1511
|
|
|
1516
1512
|
// Return just the data if withResponse is false, otherwise return the full response
|
|
@@ -1531,8 +1527,8 @@ var generateTanstackQueryFile = async (ctx) => {
|
|
|
1531
1527
|
TSelection,
|
|
1532
1528
|
TError,
|
|
1533
1529
|
(TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
1534
|
-
|
|
1535
|
-
|
|
1530
|
+
withResponse?: boolean;
|
|
1531
|
+
throwOnStatusError?: boolean;
|
|
1536
1532
|
}
|
|
1537
1533
|
>, "mutationFn"> & {
|
|
1538
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;
|
|
@@ -13,7 +13,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
13
13
|
const file = `
|
|
14
14
|
import { queryOptions } from "@tanstack/react-query"
|
|
15
15
|
import type { EndpointByMethod, ApiClient, SuccessStatusCode, ErrorStatusCode, InferResponseByStatus, TypedSuccessResponse } from "${ctx.relativeApiClientPath}"
|
|
16
|
-
import { errorStatusCodes,
|
|
16
|
+
import { errorStatusCodes, TypedStatusError } from "${ctx.relativeApiClientPath}"
|
|
17
17
|
|
|
18
18
|
type EndpointQueryKey<TOptions extends EndpointParameters> = [
|
|
19
19
|
TOptions & {
|
|
@@ -131,7 +131,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
131
131
|
: InferResponseData<TEndpoint, SuccessStatusCode>,
|
|
132
132
|
TError = TEndpoint extends { responses: infer TResponses }
|
|
133
133
|
? TResponses extends Record<string | number, unknown>
|
|
134
|
-
?
|
|
134
|
+
? TypedStatusError<InferResponseData<TEndpoint, ErrorStatusCode>>
|
|
135
135
|
: Error
|
|
136
136
|
: Error
|
|
137
137
|
>(method: TMethod, path: TPath, options?: {
|
|
@@ -144,15 +144,11 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
144
144
|
throwOnError?: boolean | ((error: TError) => boolean)
|
|
145
145
|
}) {
|
|
146
146
|
const mutationKey = [{ method, path }] as const;
|
|
147
|
-
const mutationFn = async
|
|
148
|
-
? InferResponseByStatus<TEndpoint, SuccessStatusCode>
|
|
149
|
-
: InferResponseData<TEndpoint, SuccessStatusCode>>
|
|
150
|
-
(params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
151
|
-
withResponse?: TLocalWithResponse;
|
|
147
|
+
const mutationFn = async (params: (TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
152
148
|
throwOnStatusError?: boolean;
|
|
153
149
|
overrides?: RequestInit;
|
|
154
|
-
}): Promise<
|
|
155
|
-
const withResponse =
|
|
150
|
+
}): Promise<TSelection> => {
|
|
151
|
+
const withResponse = options?.withResponse ?? false;
|
|
156
152
|
const throwOnStatusError = params.throwOnStatusError ?? options?.throwOnStatusError ?? (withResponse ? false : true);
|
|
157
153
|
const selectFn = options?.selectFn;
|
|
158
154
|
const response = await (this.client as any)[method](path, {
|
|
@@ -162,7 +158,7 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
162
158
|
});
|
|
163
159
|
|
|
164
160
|
if (throwOnStatusError && errorStatusCodes.includes(response.status as never)) {
|
|
165
|
-
throw new
|
|
161
|
+
throw new TypedStatusError(response as never);
|
|
166
162
|
}
|
|
167
163
|
|
|
168
164
|
// Return just the data if withResponse is false, otherwise return the full response
|
|
@@ -183,8 +179,8 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
|
|
|
183
179
|
TSelection,
|
|
184
180
|
TError,
|
|
185
181
|
(TEndpoint extends { parameters: infer Parameters } ? Parameters : {}) & {
|
|
186
|
-
|
|
187
|
-
|
|
182
|
+
withResponse?: boolean;
|
|
183
|
+
throwOnStatusError?: boolean;
|
|
188
184
|
}
|
|
189
185
|
>, "mutationFn"> & {
|
|
190
186
|
mutationFn: typeof mutationFn
|