typed-openapi 0.2.0 → 0.4.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.
- package/dist/{chunk-A3A4AVLF.js → chunk-YRM7U5OB.js} +17 -11
- package/dist/cli.cjs +18 -12
- package/dist/cli.js +2 -2
- package/dist/index.cjs +17 -11
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/generator.ts +18 -4
- package/src/map-openapi-endpoints.ts +10 -11
|
@@ -373,7 +373,14 @@ var generateEndpointSchemaList = (ctx) => {
|
|
|
373
373
|
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
|
|
374
374
|
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
|
|
375
375
|
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
|
|
376
|
-
${parameters.body ? `body: ${parameterObjectToString(
|
|
376
|
+
${parameters.body ? `body: ${parameterObjectToString(
|
|
377
|
+
ctx.runtime === "none" ? parameters.body.recompute((box) => {
|
|
378
|
+
if (Box.isReference(box) && !box.params.generics) {
|
|
379
|
+
box.value = `Schemas.${box.value}`;
|
|
380
|
+
}
|
|
381
|
+
return box;
|
|
382
|
+
}) : parameters.body
|
|
383
|
+
)},` : ""}
|
|
377
384
|
}` : "parameters: never,"}
|
|
378
385
|
response: ${ctx.runtime === "none" ? endpoint.response.recompute((box) => {
|
|
379
386
|
if (Box.isReference(box) && !box.params.generics) {
|
|
@@ -480,7 +487,7 @@ export class ApiClient {
|
|
|
480
487
|
path: Path,
|
|
481
488
|
...params: MaybeOptionalArg<${match(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["parameters"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["parameters"]`).otherwise(() => `TEndpoint["parameters"]`)}>
|
|
482
489
|
): Promise<${match(ctx.runtime).with("zod", "yup", () => infer(`TEndpoint["response"]`)).with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`).otherwise(() => `TEndpoint["response"]`)}> {
|
|
483
|
-
return this.fetcher("${method}", this.baseUrl + path, params[0]);
|
|
490
|
+
return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
|
|
484
491
|
}
|
|
485
492
|
// </ApiClient.${method}>
|
|
486
493
|
` : "";
|
|
@@ -724,7 +731,7 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
724
731
|
}
|
|
725
732
|
};
|
|
726
733
|
const lists = { query: [], path: [], header: [] };
|
|
727
|
-
const paramObjects =
|
|
734
|
+
const paramObjects = [...pathItemObj.parameters ?? [], ...operation.parameters ?? []].reduce(
|
|
728
735
|
(acc, paramOrRef) => {
|
|
729
736
|
const param = refs.unwrap(paramOrRef);
|
|
730
737
|
const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
|
|
@@ -781,15 +788,14 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
781
788
|
endpoint.parameters = Object.keys(params).length ? params : void 0;
|
|
782
789
|
}
|
|
783
790
|
let responseObject;
|
|
784
|
-
|
|
791
|
+
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
792
|
+
const statusCode = Number(status);
|
|
793
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
794
|
+
responseObject = refs.unwrap(responseOrRef);
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
if (!responseObject && operation.responses.default) {
|
|
785
798
|
responseObject = refs.unwrap(operation.responses.default);
|
|
786
|
-
} else {
|
|
787
|
-
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
788
|
-
const statusCode = Number(status);
|
|
789
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
790
|
-
responseObject = refs.unwrap(responseOrRef);
|
|
791
|
-
}
|
|
792
|
-
});
|
|
793
799
|
}
|
|
794
800
|
const content = responseObject?.content;
|
|
795
801
|
if (content) {
|
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.
|
|
34
|
+
var version = "0.4.0";
|
|
35
35
|
|
|
36
36
|
// src/generator.ts
|
|
37
37
|
var import_server2 = require("pastable/server");
|
|
@@ -408,7 +408,14 @@ var generateEndpointSchemaList = (ctx) => {
|
|
|
408
408
|
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
|
|
409
409
|
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
|
|
410
410
|
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
|
|
411
|
-
${parameters.body ? `body: ${parameterObjectToString(
|
|
411
|
+
${parameters.body ? `body: ${parameterObjectToString(
|
|
412
|
+
ctx.runtime === "none" ? parameters.body.recompute((box) => {
|
|
413
|
+
if (Box.isReference(box) && !box.params.generics) {
|
|
414
|
+
box.value = `Schemas.${box.value}`;
|
|
415
|
+
}
|
|
416
|
+
return box;
|
|
417
|
+
}) : parameters.body
|
|
418
|
+
)},` : ""}
|
|
412
419
|
}` : "parameters: never,"}
|
|
413
420
|
response: ${ctx.runtime === "none" ? endpoint.response.recompute((box) => {
|
|
414
421
|
if (Box.isReference(box) && !box.params.generics) {
|
|
@@ -515,7 +522,7 @@ export class ApiClient {
|
|
|
515
522
|
path: Path,
|
|
516
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"]`)}>
|
|
517
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"]`)}> {
|
|
518
|
-
return this.fetcher("${method}", this.baseUrl + path, params[0]);
|
|
525
|
+
return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
|
|
519
526
|
}
|
|
520
527
|
// </ApiClient.${method}>
|
|
521
528
|
` : "";
|
|
@@ -761,7 +768,7 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
761
768
|
}
|
|
762
769
|
};
|
|
763
770
|
const lists = { query: [], path: [], header: [] };
|
|
764
|
-
const paramObjects =
|
|
771
|
+
const paramObjects = [...pathItemObj.parameters ?? [], ...operation.parameters ?? []].reduce(
|
|
765
772
|
(acc, paramOrRef) => {
|
|
766
773
|
const param = refs.unwrap(paramOrRef);
|
|
767
774
|
const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
|
|
@@ -818,15 +825,14 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
818
825
|
endpoint.parameters = Object.keys(params).length ? params : void 0;
|
|
819
826
|
}
|
|
820
827
|
let responseObject;
|
|
821
|
-
|
|
828
|
+
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
829
|
+
const statusCode = Number(status);
|
|
830
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
831
|
+
responseObject = refs.unwrap(responseOrRef);
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
if (!responseObject && operation.responses.default) {
|
|
822
835
|
responseObject = refs.unwrap(operation.responses.default);
|
|
823
|
-
} else {
|
|
824
|
-
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
825
|
-
const statusCode = Number(status);
|
|
826
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
827
|
-
responseObject = refs.unwrap(responseOrRef);
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
836
|
}
|
|
831
837
|
const content = responseObject?.content;
|
|
832
838
|
if (content) {
|
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
allowedRuntimes,
|
|
3
3
|
generateFile,
|
|
4
4
|
mapOpenApiEndpoints
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-YRM7U5OB.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.
|
|
16
|
+
var version = "0.4.0";
|
|
17
17
|
|
|
18
18
|
// src/cli.ts
|
|
19
19
|
var cwd = process.cwd();
|
package/dist/index.cjs
CHANGED
|
@@ -416,7 +416,14 @@ var generateEndpointSchemaList = (ctx) => {
|
|
|
416
416
|
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
|
|
417
417
|
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
|
|
418
418
|
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
|
|
419
|
-
${parameters.body ? `body: ${parameterObjectToString(
|
|
419
|
+
${parameters.body ? `body: ${parameterObjectToString(
|
|
420
|
+
ctx.runtime === "none" ? parameters.body.recompute((box) => {
|
|
421
|
+
if (Box.isReference(box) && !box.params.generics) {
|
|
422
|
+
box.value = `Schemas.${box.value}`;
|
|
423
|
+
}
|
|
424
|
+
return box;
|
|
425
|
+
}) : parameters.body
|
|
426
|
+
)},` : ""}
|
|
420
427
|
}` : "parameters: never,"}
|
|
421
428
|
response: ${ctx.runtime === "none" ? endpoint.response.recompute((box) => {
|
|
422
429
|
if (Box.isReference(box) && !box.params.generics) {
|
|
@@ -523,7 +530,7 @@ export class ApiClient {
|
|
|
523
530
|
path: Path,
|
|
524
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"]`)}>
|
|
525
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"]`)}> {
|
|
526
|
-
return this.fetcher("${method}", this.baseUrl + path, params[0]);
|
|
533
|
+
return this.fetcher("${method}", this.baseUrl + path, params[0])${(0, import_ts_pattern.match)(ctx.runtime).with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
|
|
527
534
|
}
|
|
528
535
|
// </ApiClient.${method}>
|
|
529
536
|
` : "";
|
|
@@ -769,7 +776,7 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
769
776
|
}
|
|
770
777
|
};
|
|
771
778
|
const lists = { query: [], path: [], header: [] };
|
|
772
|
-
const paramObjects =
|
|
779
|
+
const paramObjects = [...pathItemObj.parameters ?? [], ...operation.parameters ?? []].reduce(
|
|
773
780
|
(acc, paramOrRef) => {
|
|
774
781
|
const param = refs.unwrap(paramOrRef);
|
|
775
782
|
const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
|
|
@@ -826,15 +833,14 @@ var mapOpenApiEndpoints = (doc) => {
|
|
|
826
833
|
endpoint.parameters = Object.keys(params).length ? params : void 0;
|
|
827
834
|
}
|
|
828
835
|
let responseObject;
|
|
829
|
-
|
|
836
|
+
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
837
|
+
const statusCode = Number(status);
|
|
838
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
839
|
+
responseObject = refs.unwrap(responseOrRef);
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
if (!responseObject && operation.responses.default) {
|
|
830
843
|
responseObject = refs.unwrap(operation.responses.default);
|
|
831
|
-
} else {
|
|
832
|
-
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
833
|
-
const statusCode = Number(status);
|
|
834
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
835
|
-
responseObject = refs.unwrap(responseOrRef);
|
|
836
|
-
}
|
|
837
|
-
});
|
|
838
844
|
}
|
|
839
845
|
const content = responseObject?.content;
|
|
840
846
|
if (content) {
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
package/src/generator.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { capitalize, groupBy } from "pastable/server";
|
|
|
2
2
|
import { Box } from "./box";
|
|
3
3
|
import { prettify } from "./format";
|
|
4
4
|
import { mapOpenApiEndpoints } from "./map-openapi-endpoints";
|
|
5
|
-
import { AnyBox,
|
|
5
|
+
import { AnyBox, AnyBoxDef } from "./types";
|
|
6
6
|
import * as Codegen from "@sinclair/typebox-codegen";
|
|
7
7
|
import { match } from "ts-pattern";
|
|
8
8
|
import { type } from "arktype";
|
|
@@ -117,7 +117,7 @@ const generateSchemaList = ({ refs, runtime }: GeneratorContext) => {
|
|
|
117
117
|
);
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
-
const parameterObjectToString = (parameters: Box<
|
|
120
|
+
const parameterObjectToString = (parameters: Box<AnyBoxDef> | Record<string, AnyBox>) => {
|
|
121
121
|
if (parameters instanceof Box) return parameters.value;
|
|
122
122
|
|
|
123
123
|
let str = "{";
|
|
@@ -143,7 +143,20 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
|
|
|
143
143
|
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
|
|
144
144
|
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
|
|
145
145
|
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
|
|
146
|
-
${
|
|
146
|
+
${
|
|
147
|
+
parameters.body
|
|
148
|
+
? `body: ${parameterObjectToString(
|
|
149
|
+
ctx.runtime === "none"
|
|
150
|
+
? parameters.body.recompute((box) => {
|
|
151
|
+
if (Box.isReference(box) && !box.params.generics) {
|
|
152
|
+
box.value = `Schemas.${box.value}`;
|
|
153
|
+
}
|
|
154
|
+
return box;
|
|
155
|
+
})
|
|
156
|
+
: parameters.body,
|
|
157
|
+
)},`
|
|
158
|
+
: ""
|
|
159
|
+
}
|
|
147
160
|
}`
|
|
148
161
|
: "parameters: never,"
|
|
149
162
|
}
|
|
@@ -280,7 +293,8 @@ export class ApiClient {
|
|
|
280
293
|
.with("zod", "yup", () => infer(`TEndpoint["response"]`))
|
|
281
294
|
.with("arktype", "io-ts", "typebox", "valibot", () => infer(`TEndpoint`) + `["response"]`)
|
|
282
295
|
.otherwise(() => `TEndpoint["response"]`)}> {
|
|
283
|
-
return this.fetcher("${method}", this.baseUrl + path, params[0])
|
|
296
|
+
return this.fetcher("${method}", this.baseUrl + path, params[0])${match(ctx.runtime)
|
|
297
|
+
.with("zod", () => `as Promise<TEndpoint["response"]> `).otherwise(() => ``)};
|
|
284
298
|
}
|
|
285
299
|
// </ApiClient.${method}>
|
|
286
300
|
`
|
|
@@ -35,7 +35,7 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
|
|
|
35
35
|
|
|
36
36
|
// Build a list of parameters by type + fill an object with all of them
|
|
37
37
|
const lists = { query: [] as ParameterObject[], path: [] as ParameterObject[], header: [] as ParameterObject[] };
|
|
38
|
-
const paramObjects = (operation.parameters ?? []).reduce(
|
|
38
|
+
const paramObjects = [...(pathItemObj.parameters ?? []), ...(operation.parameters ?? [])].reduce(
|
|
39
39
|
(acc, paramOrRef) => {
|
|
40
40
|
const param = refs.unwrap(paramOrRef);
|
|
41
41
|
const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
|
|
@@ -103,17 +103,16 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
|
|
|
103
103
|
endpoint.parameters = Object.keys(params).length ? (params as any as EndpointParameters) : undefined;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
// Match the first 2xx-3xx response found, or fallback to default one otherwise
|
|
107
|
+
let responseObject: ResponseObject | undefined;
|
|
108
|
+
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
109
|
+
const statusCode = Number(status);
|
|
110
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
111
|
+
responseObject = refs.unwrap<ResponseObject>(responseOrRef);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
if (!responseObject && operation.responses.default) {
|
|
109
115
|
responseObject = refs.unwrap(operation.responses.default);
|
|
110
|
-
} else {
|
|
111
|
-
Object.entries(operation.responses).map(([status, responseOrRef]) => {
|
|
112
|
-
const statusCode = Number(status);
|
|
113
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
114
|
-
responseObject = refs.unwrap<ResponseObject>(responseOrRef);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
116
|
}
|
|
118
117
|
|
|
119
118
|
const content = responseObject?.content;
|