typed-openapi 0.4.0 → 0.5.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.
@@ -487,7 +487,7 @@ export class ApiClient {
487
487
  path: Path,
488
488
  ...params: MaybeOptionalArg<${match(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["parameters"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["parameters"]`).otherwise(() => `TEndpoint["parameters"]`)}>
489
489
  ): Promise<${match(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["response"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`).otherwise(() => `TEndpoint["response"]`)}> {
490
- return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
490
+ return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime).with("zod", () => `as Promise<${infer(`TEndpoint["response"]`)}>`).otherwise(() => ``)};
491
491
  }
492
492
  // </ApiClient.${method}>
493
493
  ` : "";
@@ -774,16 +774,19 @@ var mapOpenApiEndpoints = (doc) => {
774
774
  }
775
775
  if (params) {
776
776
  const t = createBoxFactory({}, ctx);
777
- if (params.query && lists.query.length && lists.query.every((param) => !param.required)) {
778
- if (!params.query)
779
- params.query = {};
780
- params.query = t.reference("Partial", [t.object(params.query)]);
781
- }
782
- if (params.path && lists.path.length && lists.path.every((param) => !param.required)) {
783
- params.path = t.reference("Partial", [t.object(params.path)]);
784
- }
785
- if (params.header && lists.header.length && lists.header.every((param) => !param.required)) {
786
- params.header = t.reference("Partial", [t.object(params.header)]);
777
+ const filtered_params = ["query", "path", "header"];
778
+ for (const k of filtered_params) {
779
+ if (params[k] && lists[k].length) {
780
+ if (lists[k].every((param) => !param.required)) {
781
+ params[k] = t.reference("Partial", [t.object(params[k])]);
782
+ } else {
783
+ for (const p of lists[k]) {
784
+ if (!p.required) {
785
+ params[k][p.name] = t.optional(params[k][p.name]);
786
+ }
787
+ }
788
+ }
789
+ }
787
790
  }
788
791
  endpoint.parameters = Object.keys(params).length ? params : void 0;
789
792
  }
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.4.0";
34
+ var version = "0.5.0";
35
35
 
36
36
  // src/generator.ts
37
37
  var import_server2 = require("pastable/server");
@@ -522,7 +522,7 @@ export class ApiClient {
522
522
  path: Path,
523
523
  ...params: MaybeOptionalArg<${(0, import_ts_pattern.match)(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["parameters"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["parameters"]`).otherwise(() => `TEndpoint["parameters"]`)}>
524
524
  ): Promise<${(0, import_ts_pattern.match)(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["response"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`).otherwise(() => `TEndpoint["response"]`)}> {
525
- return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
525
+ return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<${infer(`TEndpoint["response"]`)}>`).otherwise(() => ``)};
526
526
  }
527
527
  // </ApiClient.${method}>
528
528
  ` : "";
@@ -811,16 +811,19 @@ var mapOpenApiEndpoints = (doc) => {
811
811
  }
812
812
  if (params) {
813
813
  const t = createBoxFactory({}, ctx);
814
- if (params.query && lists.query.length && lists.query.every((param) => !param.required)) {
815
- if (!params.query)
816
- params.query = {};
817
- params.query = t.reference("Partial", [t.object(params.query)]);
818
- }
819
- if (params.path && lists.path.length && lists.path.every((param) => !param.required)) {
820
- params.path = t.reference("Partial", [t.object(params.path)]);
821
- }
822
- if (params.header && lists.header.length && lists.header.every((param) => !param.required)) {
823
- params.header = t.reference("Partial", [t.object(params.header)]);
814
+ const filtered_params = ["query", "path", "header"];
815
+ for (const k of filtered_params) {
816
+ if (params[k] && lists[k].length) {
817
+ if (lists[k].every((param) => !param.required)) {
818
+ params[k] = t.reference("Partial", [t.object(params[k])]);
819
+ } else {
820
+ for (const p of lists[k]) {
821
+ if (!p.required) {
822
+ params[k][p.name] = t.optional(params[k][p.name]);
823
+ }
824
+ }
825
+ }
826
+ }
824
827
  }
825
828
  endpoint.parameters = Object.keys(params).length ? params : void 0;
826
829
  }
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  allowedRuntimes,
3
3
  generateFile,
4
4
  mapOpenApiEndpoints
5
- } from "./chunk-YRM7U5OB.js";
5
+ } from "./chunk-R7C7MEWK.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.4.0";
16
+ var version = "0.5.0";
17
17
 
18
18
  // src/cli.ts
19
19
  var cwd = process.cwd();
package/dist/index.cjs CHANGED
@@ -530,7 +530,7 @@ export class ApiClient {
530
530
  path: Path,
531
531
  ...params: MaybeOptionalArg<${(0, import_ts_pattern.match)(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["parameters"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["parameters"]`).otherwise(() => `TEndpoint["parameters"]`)}>
532
532
  ): Promise<${(0, import_ts_pattern.match)(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["response"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`).otherwise(() => `TEndpoint["response"]`)}> {
533
- return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
533
+ return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<${infer(`TEndpoint["response"]`)}>`).otherwise(() => ``)};
534
534
  }
535
535
  // </ApiClient.${method}>
536
536
  ` : "";
@@ -819,16 +819,19 @@ var mapOpenApiEndpoints = (doc) => {
819
819
  }
820
820
  if (params) {
821
821
  const t = createBoxFactory({}, ctx);
822
- if (params.query && lists.query.length && lists.query.every((param) => !param.required)) {
823
- if (!params.query)
824
- params.query = {};
825
- params.query = t.reference("Partial", [t.object(params.query)]);
826
- }
827
- if (params.path && lists.path.length && lists.path.every((param) => !param.required)) {
828
- params.path = t.reference("Partial", [t.object(params.path)]);
829
- }
830
- if (params.header && lists.header.length && lists.header.every((param) => !param.required)) {
831
- params.header = t.reference("Partial", [t.object(params.header)]);
822
+ const filtered_params = ["query", "path", "header"];
823
+ for (const k of filtered_params) {
824
+ if (params[k] && lists[k].length) {
825
+ if (lists[k].every((param) => !param.required)) {
826
+ params[k] = t.reference("Partial", [t.object(params[k])]);
827
+ } else {
828
+ for (const p of lists[k]) {
829
+ if (!p.required) {
830
+ params[k][p.name] = t.optional(params[k][p.name]);
831
+ }
832
+ }
833
+ }
834
+ }
832
835
  }
833
836
  endpoint.parameters = Object.keys(params).length ? params : void 0;
834
837
  }
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  openApiSchemaToTs,
8
8
  tsFactory,
9
9
  unwrap
10
- } from "./chunk-YRM7U5OB.js";
10
+ } from "./chunk-R7C7MEWK.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.4.0",
4
+ "version": "0.5.0",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "bin": {
package/src/generator.ts CHANGED
@@ -294,7 +294,7 @@ export class ApiClient {
294
294
  .with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`)
295
295
  .otherwise(() => `TEndpoint["response"]`)}> {
296
296
  return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime)
297
- .with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
297
+ .with("zod", () => `as Promise<${infer(`TEndpoint["response"]`)}>`).otherwise(() => ``)};
298
298
  }
299
299
  // </ApiClient.${method}>
300
300
  `
@@ -85,18 +85,23 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
85
85
  }
86
86
  }
87
87
 
88
- // Make parameters optional if none of them are required
88
+ // Make parameters optional if all or some of them are not required
89
89
  if (params) {
90
90
  const t = createBoxFactory({}, ctx);
91
- if (params.query && lists.query.length && lists.query.every((param) => !param.required)) {
92
- if (!params.query) params.query = {};
93
- params.query = t.reference("Partial", [t.object(params.query)]) as any;
94
- }
95
- if (params.path && lists.path.length && lists.path.every((param) => !param.required)) {
96
- params.path = t.reference("Partial", [t.object(params.path)]) as any;
97
- }
98
- if (params.header && lists.header.length && lists.header.every((param) => !param.required)) {
99
- params.header = t.reference("Partial", [t.object(params.header)]) as any;
91
+ const filtered_params = ["query", "path", "header"] as Array<keyof Pick<typeof params, "query" | "path" | "header">>;
92
+
93
+ for (const k of filtered_params) {
94
+ if (params[k] && lists[k].length) {
95
+ if (lists[k].every((param) => !param.required)) {
96
+ params[k] = t.reference("Partial", [t.object(params[k]!)]) as any;
97
+ } else {
98
+ for (const p of lists[k]) {
99
+ if (!p.required) {
100
+ params[k]![p.name] = t.optional(params[k]![p.name] as any);
101
+ }
102
+ }
103
+ }
104
+ }
100
105
  }
101
106
 
102
107
  // No need to pass empty objects, it's confusing