typed-openapi 0.7.0 → 0.9.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.
@@ -299,8 +299,8 @@ var methods = ["get", "put", "post", "delete", "options", "head", "patch", "trac
299
299
  var methodsRegex = new RegExp(`(?:${methods.join("|")})_`);
300
300
  var endpointExport = new RegExp(`export (?:type|const) (?:${methodsRegex.source})`);
301
301
  var replacerByRuntime = {
302
- yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(y\.object)(\()/).source, "g"), "$1$2("),
303
- zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(z\.object)(\()/).source, "g"), "$1$2(")
302
+ yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(y\.object)(\()/).source, "g"), "$1$2("),
303
+ zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(z\.object)(\()/).source, "g"), "$1$2(")
304
304
  };
305
305
  var generateFile = (options) => {
306
306
  const ctx = { ...options, runtime: options.runtime ?? "none" };
@@ -312,8 +312,8 @@ var generateFile = (options) => {
312
312
  const transformer = runtimeValidationGenerator[ctx.runtime];
313
313
  const generated = ctx.runtime === "typebox" ? Codegen.TypeScriptToTypeBox.Generate(file2) : transformer(model);
314
314
  let converted = "";
315
- const match2 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
316
- const content = match2?.[2];
315
+ const match3 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
316
+ const content = match3?.[2];
317
317
  if (content && ctx.runtime in replacerByRuntime) {
318
318
  const before = generated.slice(0, generated.indexOf("export type __ENDPOINTS_START"));
319
319
  converted = before + replacerByRuntime[ctx.runtime](
@@ -369,6 +369,7 @@ var generateEndpointSchemaList = (ctx) => {
369
369
  file += `export type ${endpoint.meta.alias} = {
370
370
  method: "${endpoint.method.toUpperCase()}",
371
371
  path: "${endpoint.path}",
372
+ requestFormat: "${endpoint.requestFormat}",
372
373
  ${endpoint.meta.hasParameters ? `parameters: {
373
374
  ${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
374
375
  ${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
@@ -439,6 +440,8 @@ export type EndpointParameters = {
439
440
  export type MutationMethod = "post" | "put" | "patch" | "delete";
440
441
  export type Method = "get" | "head" | MutationMethod;
441
442
 
443
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
444
+
442
445
  export type DefaultEndpoint = {
443
446
  parameters?: EndpointParameters | undefined;
444
447
  response: unknown;
@@ -448,6 +451,7 @@ export type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
448
451
  operationId: string;
449
452
  method: Method;
450
453
  path: string;
454
+ requestFormat: RequestFormat;
451
455
  parameters?: TConfig["parameters"];
452
456
  meta: {
453
457
  alias: string;
@@ -709,6 +713,7 @@ var tsFactory = createFactory({
709
713
 
710
714
  // src/map-openapi-endpoints.ts
711
715
  import { capitalize as capitalize3, pick } from "pastable/server";
716
+ import { match as match2, P } from "ts-pattern";
712
717
  var factory = tsFactory;
713
718
  var mapOpenApiEndpoints = (doc) => {
714
719
  const refs = createRefResolver(doc, factory);
@@ -723,6 +728,7 @@ var mapOpenApiEndpoints = (doc) => {
723
728
  operation,
724
729
  method,
725
730
  path,
731
+ requestFormat: "json",
726
732
  response: openApiSchemaToTs({ schema: {}, ctx }),
727
733
  meta: {
728
734
  alias: getAlias({ path, method, operation }),
@@ -771,6 +777,7 @@ var mapOpenApiEndpoints = (doc) => {
771
777
  ctx
772
778
  });
773
779
  }
780
+ endpoint.requestFormat = match2(matchingMediaType).with("application/octet-stream", () => "binary").with("multipart/form-data", () => "form-data").with("application/x-www-form-urlencoded", () => "form-url").with(P.string.includes("json"), () => "json").otherwise(() => "text");
774
781
  }
775
782
  if (params) {
776
783
  const t = createBoxFactory({}, ctx);
package/dist/cli.cjs CHANGED
@@ -31,7 +31,7 @@ var import_promises = require("fs/promises");
31
31
 
32
32
  // package.json
33
33
  var name = "typed-openapi";
34
- var version = "0.7.0";
34
+ var version = "0.9.0";
35
35
 
36
36
  // src/generator.ts
37
37
  var import_server2 = require("pastable/server");
@@ -334,8 +334,8 @@ var methods = ["get", "put", "post", "delete", "options", "head", "patch", "trac
334
334
  var methodsRegex = new RegExp(`(?:${methods.join("|")})_`);
335
335
  var endpointExport = new RegExp(`export (?:type|const) (?:${methodsRegex.source})`);
336
336
  var replacerByRuntime = {
337
- yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(y\.object)(\()/).source, "g"), "$1$2("),
338
- zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(z\.object)(\()/).source, "g"), "$1$2(")
337
+ yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(y\.object)(\()/).source, "g"), "$1$2("),
338
+ zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(z\.object)(\()/).source, "g"), "$1$2(")
339
339
  };
340
340
  var generateFile = (options) => {
341
341
  const ctx = { ...options, runtime: options.runtime ?? "none" };
@@ -347,8 +347,8 @@ var generateFile = (options) => {
347
347
  const transformer = runtimeValidationGenerator[ctx.runtime];
348
348
  const generated = ctx.runtime === "typebox" ? Codegen.TypeScriptToTypeBox.Generate(file2) : transformer(model);
349
349
  let converted = "";
350
- const match2 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
351
- const content = match2?.[2];
350
+ const match3 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
351
+ const content = match3?.[2];
352
352
  if (content && ctx.runtime in replacerByRuntime) {
353
353
  const before = generated.slice(0, generated.indexOf("export type __ENDPOINTS_START"));
354
354
  converted = before + replacerByRuntime[ctx.runtime](
@@ -404,6 +404,7 @@ var generateEndpointSchemaList = (ctx) => {
404
404
  file += `export type ${endpoint.meta.alias} = {
405
405
  method: "${endpoint.method.toUpperCase()}",
406
406
  path: "${endpoint.path}",
407
+ requestFormat: "${endpoint.requestFormat}",
407
408
  ${endpoint.meta.hasParameters ? `parameters: {
408
409
  ${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
409
410
  ${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
@@ -474,6 +475,8 @@ export type EndpointParameters = {
474
475
  export type MutationMethod = "post" | "put" | "patch" | "delete";
475
476
  export type Method = "get" | "head" | MutationMethod;
476
477
 
478
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
479
+
477
480
  export type DefaultEndpoint = {
478
481
  parameters?: EndpointParameters | undefined;
479
482
  response: unknown;
@@ -483,6 +486,7 @@ export type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
483
486
  operationId: string;
484
487
  method: Method;
485
488
  path: string;
489
+ requestFormat: RequestFormat;
486
490
  parameters?: TConfig["parameters"];
487
491
  meta: {
488
492
  alias: string;
@@ -746,6 +750,7 @@ var tsFactory = createFactory({
746
750
  });
747
751
 
748
752
  // src/map-openapi-endpoints.ts
753
+ var import_ts_pattern2 = require("ts-pattern");
749
754
  var factory = tsFactory;
750
755
  var mapOpenApiEndpoints = (doc) => {
751
756
  const refs = createRefResolver(doc, factory);
@@ -760,6 +765,7 @@ var mapOpenApiEndpoints = (doc) => {
760
765
  operation,
761
766
  method,
762
767
  path,
768
+ requestFormat: "json",
763
769
  response: openApiSchemaToTs({ schema: {}, ctx }),
764
770
  meta: {
765
771
  alias: getAlias({ path, method, operation }),
@@ -808,6 +814,7 @@ var mapOpenApiEndpoints = (doc) => {
808
814
  ctx
809
815
  });
810
816
  }
817
+ endpoint.requestFormat = (0, import_ts_pattern2.match)(matchingMediaType).with("application/octet-stream", () => "binary").with("multipart/form-data", () => "form-data").with("application/x-www-form-urlencoded", () => "form-url").with(import_ts_pattern2.P.string.includes("json"), () => "json").otherwise(() => "text");
811
818
  }
812
819
  if (params) {
813
820
  const t = createBoxFactory({}, ctx);
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  allowedRuntimes,
3
3
  generateFile,
4
4
  mapOpenApiEndpoints
5
- } from "./chunk-T4QUCLXL.js";
5
+ } from "./chunk-TKIDWJ3F.js";
6
6
 
7
7
  // src/cli.ts
8
8
  import SwaggerParser from "@apidevtools/swagger-parser";
@@ -13,7 +13,7 @@ import { writeFile } from "fs/promises";
13
13
 
14
14
  // package.json
15
15
  var name = "typed-openapi";
16
- var version = "0.7.0";
16
+ var version = "0.9.0";
17
17
 
18
18
  // src/cli.ts
19
19
  var cwd = process.cwd();
package/dist/index.cjs CHANGED
@@ -342,8 +342,8 @@ var methods = ["get", "put", "post", "delete", "options", "head", "patch", "trac
342
342
  var methodsRegex = new RegExp(`(?:${methods.join("|")})_`);
343
343
  var endpointExport = new RegExp(`export (?:type|const) (?:${methodsRegex.source})`);
344
344
  var replacerByRuntime = {
345
- yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(y\.object)(\()/).source, "g"), "$1$2("),
346
- zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(z\.object)(\()/).source, "g"), "$1$2(")
345
+ yup: (line) => line.replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(y\.object)(\()/).source, "g"), "$1$2("),
346
+ zod: (line) => line.replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1").replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(z\.object)(\()/).source, "g"), "$1$2(")
347
347
  };
348
348
  var generateFile = (options) => {
349
349
  const ctx = { ...options, runtime: options.runtime ?? "none" };
@@ -355,8 +355,8 @@ var generateFile = (options) => {
355
355
  const transformer = runtimeValidationGenerator[ctx.runtime];
356
356
  const generated = ctx.runtime === "typebox" ? Codegen.TypeScriptToTypeBox.Generate(file2) : transformer(model);
357
357
  let converted = "";
358
- const match2 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
359
- const content = match2?.[2];
358
+ const match3 = generated.match(/(const __ENDPOINTS_START__ =)([\s\S]*?)(export type __ENDPOINTS_END__)/);
359
+ const content = match3?.[2];
360
360
  if (content && ctx.runtime in replacerByRuntime) {
361
361
  const before = generated.slice(0, generated.indexOf("export type __ENDPOINTS_START"));
362
362
  converted = before + replacerByRuntime[ctx.runtime](
@@ -412,6 +412,7 @@ var generateEndpointSchemaList = (ctx) => {
412
412
  file += `export type ${endpoint.meta.alias} = {
413
413
  method: "${endpoint.method.toUpperCase()}",
414
414
  path: "${endpoint.path}",
415
+ requestFormat: "${endpoint.requestFormat}",
415
416
  ${endpoint.meta.hasParameters ? `parameters: {
416
417
  ${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
417
418
  ${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
@@ -482,6 +483,8 @@ export type EndpointParameters = {
482
483
  export type MutationMethod = "post" | "put" | "patch" | "delete";
483
484
  export type Method = "get" | "head" | MutationMethod;
484
485
 
486
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
487
+
485
488
  export type DefaultEndpoint = {
486
489
  parameters?: EndpointParameters | undefined;
487
490
  response: unknown;
@@ -491,6 +494,7 @@ export type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
491
494
  operationId: string;
492
495
  method: Method;
493
496
  path: string;
497
+ requestFormat: RequestFormat;
494
498
  parameters?: TConfig["parameters"];
495
499
  meta: {
496
500
  alias: string;
@@ -754,6 +758,7 @@ var tsFactory = createFactory({
754
758
  });
755
759
 
756
760
  // src/map-openapi-endpoints.ts
761
+ var import_ts_pattern2 = require("ts-pattern");
757
762
  var factory = tsFactory;
758
763
  var mapOpenApiEndpoints = (doc) => {
759
764
  const refs = createRefResolver(doc, factory);
@@ -768,6 +773,7 @@ var mapOpenApiEndpoints = (doc) => {
768
773
  operation,
769
774
  method,
770
775
  path,
776
+ requestFormat: "json",
771
777
  response: openApiSchemaToTs({ schema: {}, ctx }),
772
778
  meta: {
773
779
  alias: getAlias({ path, method, operation }),
@@ -816,6 +822,7 @@ var mapOpenApiEndpoints = (doc) => {
816
822
  ctx
817
823
  });
818
824
  }
825
+ endpoint.requestFormat = (0, import_ts_pattern2.match)(matchingMediaType).with("application/octet-stream", () => "binary").with("multipart/form-data", () => "form-data").with("application/x-www-form-urlencoded", () => "form-url").with(import_ts_pattern2.P.string.includes("json"), () => "json").otherwise(() => "text");
819
826
  }
820
827
  if (params) {
821
828
  const t = createBoxFactory({}, ctx);
package/dist/index.d.cts CHANGED
@@ -213,6 +213,7 @@ type EndpointParameters = {
213
213
  header?: Box<BoxRef> | Record<string, AnyBox>;
214
214
  path?: Box<BoxRef> | Record<string, AnyBox>;
215
215
  };
216
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
216
217
  type DefaultEndpoint = {
217
218
  parameters?: EndpointParameters | undefined;
218
219
  response: AnyBox;
@@ -222,6 +223,7 @@ type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
222
223
  method: Method;
223
224
  path: string;
224
225
  parameters?: TConfig["parameters"];
226
+ requestFormat: RequestFormat;
225
227
  meta: {
226
228
  alias: string;
227
229
  hasParameters: boolean;
package/dist/index.d.ts CHANGED
@@ -213,6 +213,7 @@ type EndpointParameters = {
213
213
  header?: Box<BoxRef> | Record<string, AnyBox>;
214
214
  path?: Box<BoxRef> | Record<string, AnyBox>;
215
215
  };
216
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
216
217
  type DefaultEndpoint = {
217
218
  parameters?: EndpointParameters | undefined;
218
219
  response: AnyBox;
@@ -222,6 +223,7 @@ type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
222
223
  method: Method;
223
224
  path: string;
224
225
  parameters?: TConfig["parameters"];
226
+ requestFormat: RequestFormat;
225
227
  meta: {
226
228
  alias: string;
227
229
  hasParameters: boolean;
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  openApiSchemaToTs,
8
8
  tsFactory,
9
9
  unwrap
10
- } from "./chunk-T4QUCLXL.js";
10
+ } from "./chunk-TKIDWJ3F.js";
11
11
  export {
12
12
  createBoxFactory,
13
13
  createFactory,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.9.0",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "bin": {
package/src/generator.ts CHANGED
@@ -46,11 +46,11 @@ const replacerByRuntime = {
46
46
  yup: (line: string) =>
47
47
  line
48
48
  .replace(/y\.InferType<\s*?typeof (.*?)\s*?>/g, "typeof $1")
49
- .replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(y\.object)(\()/).source, "g"), "$1$2("),
49
+ .replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(y\.object)(\()/).source, "g"), "$1$2("),
50
50
  zod: (line: string) =>
51
51
  line
52
52
  .replace(/z\.infer<\s*?typeof (.*?)\s*?>/g, "typeof $1")
53
- .replace(new RegExp(`(${endpointExport.source})` + new RegExp(/(.*? )(z\.object)(\()/).source, "g"), "$1$2("),
53
+ .replace(new RegExp(`(${endpointExport.source})` + new RegExp(/([\s\S]*? )(z\.object)(\()/).source, "g"), "$1$2("),
54
54
  };
55
55
 
56
56
  export const generateFile = (options: GeneratorOptions) => {
@@ -137,6 +137,7 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
137
137
  file += `export type ${endpoint.meta.alias} = {
138
138
  method: "${endpoint.method.toUpperCase()}",
139
139
  path: "${endpoint.path}",
140
+ requestFormat: "${endpoint.requestFormat}",
140
141
  ${
141
142
  endpoint.meta.hasParameters
142
143
  ? `parameters: {
@@ -235,6 +236,8 @@ export type EndpointParameters = {
235
236
  export type MutationMethod = "post" | "put" | "patch" | "delete";
236
237
  export type Method = "get" | "head" | MutationMethod;
237
238
 
239
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
240
+
238
241
  export type DefaultEndpoint = {
239
242
  parameters?: EndpointParameters | undefined;
240
243
  response: unknown;
@@ -244,6 +247,7 @@ export type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
244
247
  operationId: string;
245
248
  method: Method;
246
249
  path: string;
250
+ requestFormat: RequestFormat;
247
251
  parameters?: TConfig["parameters"];
248
252
  meta: {
249
253
  alias: string;
@@ -8,6 +8,7 @@ import { createRefResolver } from "./ref-resolver";
8
8
  import { tsFactory } from "./ts-factory";
9
9
  import { AnyBox, BoxRef, OpenapiSchemaConvertContext } from "./types";
10
10
  import { pathToVariableName } from "./string-utils";
11
+ import { match, P } from "ts-pattern";
11
12
 
12
13
  const factory = tsFactory;
13
14
 
@@ -25,6 +26,7 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
25
26
  operation,
26
27
  method: method as Method,
27
28
  path,
29
+ requestFormat: "json",
28
30
  response: openApiSchemaToTs({ schema: {}, ctx }),
29
31
  meta: {
30
32
  alias: getAlias({ path, method, operation } as Endpoint),
@@ -83,12 +85,21 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
83
85
  ctx,
84
86
  });
85
87
  }
88
+
89
+ endpoint.requestFormat = match(matchingMediaType)
90
+ .with("application/octet-stream", () => "binary" as const)
91
+ .with("multipart/form-data", () => "form-data" as const)
92
+ .with("application/x-www-form-urlencoded", () => "form-url" as const)
93
+ .with(P.string.includes("json"), () => "json" as const)
94
+ .otherwise(() => "text" as const);
86
95
  }
87
96
 
88
97
  // Make parameters optional if all or some of them are not required
89
98
  if (params) {
90
99
  const t = createBoxFactory({}, ctx);
91
- const filtered_params = ["query", "path", "header"] as Array<keyof Pick<typeof params, "query" | "path" | "header">>;
100
+ const filtered_params = ["query", "path", "header"] as Array<
101
+ keyof Pick<typeof params, "query" | "path" | "header">
102
+ >;
92
103
 
93
104
  for (const k of filtered_params) {
94
105
  if (params[k] && lists[k].length) {
@@ -165,6 +176,8 @@ export type EndpointParameters = {
165
176
  path?: Box<BoxRef> | Record<string, AnyBox>;
166
177
  };
167
178
 
179
+ type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text";
180
+
168
181
  type DefaultEndpoint = {
169
182
  parameters?: EndpointParameters | undefined;
170
183
  response: AnyBox;
@@ -175,6 +188,7 @@ export type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = {
175
188
  method: Method;
176
189
  path: string;
177
190
  parameters?: TConfig["parameters"];
191
+ requestFormat: RequestFormat;
178
192
  meta: {
179
193
  alias: string;
180
194
  hasParameters: boolean;