typed-openapi 0.1.3 → 0.1.4

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/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.4";
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
  });
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-TUNDL3P7.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.4";
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
  });
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-TUNDL3P7.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.4",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
7
  "bin": {
@@ -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} }`;