typed-openapi 0.1.3 → 0.1.5

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.
@@ -136,10 +136,7 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
136
136
  }
137
137
  const isRequired = Boolean(isPartial ? true : hasRequiredArray ? schema.required?.includes(prop) : false);
138
138
  const isOptional = !isPartial && !isRequired;
139
- return [
140
- `${wrapWithQuotesIfNeeded(prop)}${isOptional ? "?" : ""}`,
141
- isOptional ? t.optional(propType) : propType
142
- ];
139
+ return [`${wrapWithQuotesIfNeeded(prop)}`, isOptional ? t.optional(propType) : propType];
143
140
  })
144
141
  );
145
142
  const objectType = additionalProperties ? t.intersection([t.object(props), additionalProperties]) : t.object(props);
@@ -149,7 +146,13 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
149
146
  return t.unknown();
150
147
  throw new Error(`Unsupported schema type: ${schemaType}`);
151
148
  };
152
- return getTs();
149
+ let output = getTs();
150
+ if (!isReferenceObject(schema)) {
151
+ if (schema.nullable) {
152
+ output = t.union([output, t.reference("null")]);
153
+ }
154
+ }
155
+ return output;
153
156
  };
154
157
 
155
158
  // src/box.ts
@@ -687,7 +690,11 @@ var tsFactory = createFactory({
687
690
  any: () => "any",
688
691
  never: () => "never",
689
692
  object: (props) => {
690
- const propsString = Object.entries(props).map(([prop, type2]) => `${wrapWithQuotesIfNeeded(prop)}: ${unwrap(type2)}`).join(", ");
693
+ const propsString = Object.entries(props).map(
694
+ ([prop, type2]) => `${wrapWithQuotesIfNeeded(prop)}${typeof type2 !== "string" && Box.isOptional(type2) ? "?" : ""}: ${unwrap(
695
+ type2
696
+ )}`
697
+ ).join(", ");
691
698
  return `{ ${propsString} }`;
692
699
  }
693
700
  });
@@ -720,16 +727,21 @@ var mapOpenApiEndpoints = (doc) => {
720
727
  (acc, paramOrRef) => {
721
728
  const param = refs.unwrap(paramOrRef);
722
729
  const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
723
- lists.query.push(param);
724
730
  if (param.required)
725
731
  endpoint.meta.areParametersRequired = true;
726
732
  endpoint.meta.hasParameters = true;
727
- if (param.in === "query")
733
+ if (param.in === "query") {
734
+ lists.query.push(param);
728
735
  acc.query[param.name] = schema;
729
- if (param.in === "path")
736
+ }
737
+ if (param.in === "path") {
738
+ lists.path.push(param);
730
739
  acc.path[param.name] = schema;
731
- if (param.in === "header")
740
+ }
741
+ if (param.in === "header") {
742
+ lists.header.push(param);
732
743
  acc.header[param.name] = schema;
744
+ }
733
745
  return acc;
734
746
  },
735
747
  { query: {}, path: {}, header: {} }
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.1.3";
34
+ var version = "0.1.5";
35
35
 
36
36
  // src/generator.ts
37
37
  var import_server2 = require("pastable/server");
@@ -214,10 +214,7 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
214
214
  }
215
215
  const isRequired = Boolean(isPartial ? true : hasRequiredArray ? schema.required?.includes(prop) : false);
216
216
  const isOptional = !isPartial && !isRequired;
217
- return [
218
- `${wrapWithQuotesIfNeeded(prop)}${isOptional ? "?" : ""}`,
219
- isOptional ? t.optional(propType) : propType
220
- ];
217
+ return [`${wrapWithQuotesIfNeeded(prop)}`, isOptional ? t.optional(propType) : propType];
221
218
  })
222
219
  );
223
220
  const objectType = additionalProperties ? t.intersection([t.object(props), additionalProperties]) : t.object(props);
@@ -227,7 +224,13 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
227
224
  return t.unknown();
228
225
  throw new Error(`Unsupported schema type: ${schemaType}`);
229
226
  };
230
- return getTs();
227
+ let output = getTs();
228
+ if (!isReferenceObject(schema)) {
229
+ if (schema.nullable) {
230
+ output = t.union([output, t.reference("null")]);
231
+ }
232
+ }
233
+ return output;
231
234
  };
232
235
 
233
236
  // src/box.ts
@@ -725,7 +728,11 @@ var tsFactory = createFactory({
725
728
  any: () => "any",
726
729
  never: () => "never",
727
730
  object: (props) => {
728
- const propsString = Object.entries(props).map(([prop, type3]) => `${wrapWithQuotesIfNeeded(prop)}: ${unwrap(type3)}`).join(", ");
731
+ const propsString = Object.entries(props).map(
732
+ ([prop, type3]) => `${wrapWithQuotesIfNeeded(prop)}${typeof type3 !== "string" && Box.isOptional(type3) ? "?" : ""}: ${unwrap(
733
+ type3
734
+ )}`
735
+ ).join(", ");
729
736
  return `{ ${propsString} }`;
730
737
  }
731
738
  });
@@ -757,16 +764,21 @@ var mapOpenApiEndpoints = (doc) => {
757
764
  (acc, paramOrRef) => {
758
765
  const param = refs.unwrap(paramOrRef);
759
766
  const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
760
- lists.query.push(param);
761
767
  if (param.required)
762
768
  endpoint.meta.areParametersRequired = true;
763
769
  endpoint.meta.hasParameters = true;
764
- if (param.in === "query")
770
+ if (param.in === "query") {
771
+ lists.query.push(param);
765
772
  acc.query[param.name] = schema;
766
- if (param.in === "path")
773
+ }
774
+ if (param.in === "path") {
775
+ lists.path.push(param);
767
776
  acc.path[param.name] = schema;
768
- if (param.in === "header")
777
+ }
778
+ if (param.in === "header") {
779
+ lists.header.push(param);
769
780
  acc.header[param.name] = schema;
781
+ }
770
782
  return acc;
771
783
  },
772
784
  { query: {}, path: {}, header: {} }
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  allowedRuntimes,
3
3
  generateFile,
4
4
  mapOpenApiEndpoints
5
- } from "./chunk-E7HJBSR2.js";
5
+ } from "./chunk-RMN2CYTB.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.1.3";
16
+ var version = "0.1.5";
17
17
 
18
18
  // src/cli.ts
19
19
  var cwd = process.cwd();
package/dist/index.cjs CHANGED
@@ -179,10 +179,7 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
179
179
  }
180
180
  const isRequired = Boolean(isPartial ? true : hasRequiredArray ? schema.required?.includes(prop) : false);
181
181
  const isOptional = !isPartial && !isRequired;
182
- return [
183
- `${wrapWithQuotesIfNeeded(prop)}${isOptional ? "?" : ""}`,
184
- isOptional ? t.optional(propType) : propType
185
- ];
182
+ return [`${wrapWithQuotesIfNeeded(prop)}`, isOptional ? t.optional(propType) : propType];
186
183
  })
187
184
  );
188
185
  const objectType = additionalProperties ? t.intersection([t.object(props), additionalProperties]) : t.object(props);
@@ -192,7 +189,13 @@ var openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }) => {
192
189
  return t.unknown();
193
190
  throw new Error(`Unsupported schema type: ${schemaType}`);
194
191
  };
195
- return getTs();
192
+ let output = getTs();
193
+ if (!isReferenceObject(schema)) {
194
+ if (schema.nullable) {
195
+ output = t.union([output, t.reference("null")]);
196
+ }
197
+ }
198
+ return output;
196
199
  };
197
200
 
198
201
  // src/box.ts
@@ -733,7 +736,11 @@ var tsFactory = createFactory({
733
736
  any: () => "any",
734
737
  never: () => "never",
735
738
  object: (props) => {
736
- const propsString = Object.entries(props).map(([prop, type2]) => `${wrapWithQuotesIfNeeded(prop)}: ${unwrap(type2)}`).join(", ");
739
+ const propsString = Object.entries(props).map(
740
+ ([prop, type2]) => `${wrapWithQuotesIfNeeded(prop)}${typeof type2 !== "string" && Box.isOptional(type2) ? "?" : ""}: ${unwrap(
741
+ type2
742
+ )}`
743
+ ).join(", ");
737
744
  return `{ ${propsString} }`;
738
745
  }
739
746
  });
@@ -765,16 +772,21 @@ var mapOpenApiEndpoints = (doc) => {
765
772
  (acc, paramOrRef) => {
766
773
  const param = refs.unwrap(paramOrRef);
767
774
  const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
768
- lists.query.push(param);
769
775
  if (param.required)
770
776
  endpoint.meta.areParametersRequired = true;
771
777
  endpoint.meta.hasParameters = true;
772
- if (param.in === "query")
778
+ if (param.in === "query") {
779
+ lists.query.push(param);
773
780
  acc.query[param.name] = schema;
774
- if (param.in === "path")
781
+ }
782
+ if (param.in === "path") {
783
+ lists.path.push(param);
775
784
  acc.path[param.name] = schema;
776
- if (param.in === "header")
785
+ }
786
+ if (param.in === "header") {
787
+ lists.header.push(param);
777
788
  acc.header[param.name] = schema;
789
+ }
778
790
  return acc;
779
791
  },
780
792
  { query: {}, path: {}, header: {} }
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  openApiSchemaToTs,
8
8
  tsFactory,
9
9
  unwrap
10
- } from "./chunk-E7HJBSR2.js";
10
+ } from "./chunk-RMN2CYTB.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.1.3",
4
+ "version": "0.1.5",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "bin": {
@@ -39,14 +39,22 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
39
39
  (acc, paramOrRef) => {
40
40
  const param = refs.unwrap(paramOrRef);
41
41
  const schema = openApiSchemaToTs({ schema: refs.unwrap(param.schema ?? {}), ctx });
42
- lists.query.push(param);
43
42
 
44
43
  if (param.required) endpoint.meta.areParametersRequired = true;
45
44
  endpoint.meta.hasParameters = true;
46
45
 
47
- if (param.in === "query") acc.query[param.name] = schema;
48
- if (param.in === "path") acc.path[param.name] = schema;
49
- if (param.in === "header") acc.header[param.name] = schema;
46
+ if (param.in === "query") {
47
+ lists.query.push(param);
48
+ acc.query[param.name] = schema;
49
+ }
50
+ if (param.in === "path") {
51
+ lists.path.push(param);
52
+ acc.path[param.name] = schema;
53
+ }
54
+ if (param.in === "header") {
55
+ lists.header.push(param);
56
+ acc.header[param.name] = schema;
57
+ }
50
58
 
51
59
  return acc;
52
60
  },
@@ -4,6 +4,7 @@ import { createBoxFactory } from "./box-factory";
4
4
  import { isReferenceObject } from "./is-reference-object";
5
5
  import { AnyBoxDef, OpenapiSchemaConvertArgs } from "./types";
6
6
  import { wrapWithQuotesIfNeeded } from "./string-utils";
7
+ import type { SchemaObject } from "openapi3-ts/oas30";
7
8
 
8
9
  export const openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }: OpenapiSchemaConvertArgs): Box<AnyBoxDef> => {
9
10
  const meta = {} as OpenapiSchemaConvertArgs["meta"];
@@ -134,10 +135,7 @@ export const openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }: Openapi
134
135
 
135
136
  const isRequired = Boolean(isPartial ? true : hasRequiredArray ? schema.required?.includes(prop) : false);
136
137
  const isOptional = !isPartial && !isRequired;
137
- return [
138
- `${wrapWithQuotesIfNeeded(prop)}${isOptional ? "?" : ""}`,
139
- isOptional ? t.optional(propType) : propType,
140
- ];
138
+ return [`${wrapWithQuotesIfNeeded(prop)}`, isOptional ? t.optional(propType) : propType];
141
139
  }),
142
140
  );
143
141
 
@@ -153,5 +151,13 @@ export const openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }: Openapi
153
151
  throw new Error(`Unsupported schema type: ${schemaType}`);
154
152
  };
155
153
 
156
- return getTs();
154
+ let output = getTs();
155
+ if (!isReferenceObject(schema)) {
156
+ // OpenAPI 3.1 does not have nullable, but OpenAPI 3.0 does
157
+ if ((schema as any as SchemaObject).nullable) {
158
+ output = t.union([output, t.reference("null")]);
159
+ }
160
+ }
161
+
162
+ return output;
157
163
  };
package/src/ts-factory.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Box } from "./box";
1
2
  import { createFactory, unwrap } from "./box-factory";
2
3
  import { wrapWithQuotesIfNeeded } from "./string-utils";
3
4
 
@@ -16,7 +17,12 @@ export const tsFactory = createFactory({
16
17
  never: () => "never" as const,
17
18
  object: (props) => {
18
19
  const propsString = Object.entries(props)
19
- .map(([prop, type]) => `${wrapWithQuotesIfNeeded(prop)}: ${unwrap(type)}`)
20
+ .map(
21
+ ([prop, type]) =>
22
+ `${wrapWithQuotesIfNeeded(prop)}${typeof type !== "string" && Box.isOptional(type) ? "?" : ""}: ${unwrap(
23
+ type,
24
+ )}`,
25
+ )
20
26
  .join(", ");
21
27
 
22
28
  return `{ ${propsString} }`;