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.
@@ -5,7 +5,7 @@ import {
5
5
  generateFile,
6
6
  generateTanstackQueryFile,
7
7
  mapOpenApiEndpoints
8
- } from "./chunk-QGMS7IBQ.js";
8
+ } from "./chunk-L5G4LNMY.js";
9
9
  import {
10
10
  prettify
11
11
  } from "./chunk-KAEXXJ7X.js";
@@ -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
- // <TypedResponseError>
672
- export class TypedResponseError extends Error {
673
- response: TypedErrorResponse<unknown, ErrorStatusCode, unknown>;
671
+ // <TypedStatusError>
672
+ export class TypedStatusError<TData = unknown> extends Error {
673
+ response: TypedErrorResponse<TData, ErrorStatusCode, unknown>;
674
674
  status: number;
675
- constructor(response: TypedErrorResponse<unknown, ErrorStatusCode, unknown>) {
675
+ constructor(response: TypedErrorResponse<TData, ErrorStatusCode, unknown>) {
676
676
  super(\`HTTP \${response.status}: \${response.statusText}\`);
677
- this.name = 'TypedResponseError';
677
+ this.name = 'TypedStatusError';
678
678
  this.response = response;
679
679
  this.status = response.status;
680
680
  }
681
681
  }
682
- // </TypedResponseError>
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 TypedResponseError(typedResponse as never);
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, TypedResponseError } from "${ctx.relativeApiClientPath}"
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 Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
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
- : Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"],
1479
+ : InferResponseData<TEndpoint, SuccessStatusCode>,
1475
1480
  TError = TEndpoint extends { responses: infer TResponses }
1476
1481
  ? TResponses extends Record<string | number, unknown>
1477
- ? InferResponseByStatus<TEndpoint, ErrorStatusCode>
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
- : Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"]
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 <TLocalWithResponse extends boolean = TWithResponse, TLocalSelection = TLocalWithResponse extends true
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<TLocalSelection> => {
1498
- const withResponse = params.withResponse ??options?.withResponse ?? false;
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 TypedResponseError(response as never);
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
- withResponse?: boolean;
1530
- throwOnStatusError?: boolean;
1530
+ withResponse?: boolean;
1531
+ throwOnStatusError?: boolean;
1531
1532
  }
1532
1533
  >, "mutationFn"> & {
1533
1534
  mutationFn: typeof mutationFn
package/dist/cli.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  generateClientFiles
3
- } from "./chunk-IB6GEWS7.js";
3
+ } from "./chunk-4VZOXNI4.js";
4
4
  import {
5
5
  allowedRuntimes
6
- } from "./chunk-QGMS7IBQ.js";
6
+ } from "./chunk-L5G4LNMY.js";
7
7
  import "./chunk-KAEXXJ7X.js";
8
8
 
9
9
  // src/cli.ts
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  openApiSchemaToTs,
9
9
  tsFactory,
10
10
  unwrap
11
- } from "./chunk-QGMS7IBQ.js";
11
+ } from "./chunk-L5G4LNMY.js";
12
12
  import "./chunk-KAEXXJ7X.js";
13
13
  export {
14
14
  createBoxFactory,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  generateClientFiles
3
- } from "./chunk-IB6GEWS7.js";
4
- import "./chunk-QGMS7IBQ.js";
3
+ } from "./chunk-4VZOXNI4.js";
4
+ import "./chunk-L5G4LNMY.js";
5
5
  import "./chunk-KAEXXJ7X.js";
6
6
  export {
7
7
  generateClientFiles
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "2.2.0",
4
+ "version": "2.2.2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "exports": {
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
- // <TypedResponseError>
428
- export class TypedResponseError extends Error {
429
- response: TypedErrorResponse<unknown, ErrorStatusCode, unknown>;
427
+ // <TypedStatusError>
428
+ export class TypedStatusError<TData = unknown> extends Error {
429
+ response: TypedErrorResponse<TData, ErrorStatusCode, unknown>;
430
430
  status: number;
431
- constructor(response: TypedErrorResponse<unknown, ErrorStatusCode, unknown>) {
431
+ constructor(response: TypedErrorResponse<TData, ErrorStatusCode, unknown>) {
432
432
  super(\`HTTP \${response.status}: \${response.statusText}\`);
433
- this.name = 'TypedResponseError';
433
+ this.name = 'TypedStatusError';
434
434
  this.response = response;
435
435
  this.status = response.status;
436
436
  }
437
437
  }
438
- // </TypedResponseError>
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 TypedResponseError(typedResponse as never);
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, TypedResponseError } from "${ctx.relativeApiClientPath}"
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 Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
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
- : Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"],
131
+ : InferResponseData<TEndpoint, SuccessStatusCode>,
127
132
  TError = TEndpoint extends { responses: infer TResponses }
128
133
  ? TResponses extends Record<string | number, unknown>
129
- ? InferResponseByStatus<TEndpoint, ErrorStatusCode>
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
- : Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"]
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 <TLocalWithResponse extends boolean = TWithResponse, TLocalSelection = TLocalWithResponse extends true
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<TLocalSelection> => {
150
- const withResponse = params.withResponse ??options?.withResponse ?? false;
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 TypedResponseError(response as never);
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
- withResponse?: boolean;
182
- throwOnStatusError?: boolean;
182
+ withResponse?: boolean;
183
+ throwOnStatusError?: boolean;
183
184
  }
184
185
  >, "mutationFn"> & {
185
186
  mutationFn: typeof mutationFn