openapi-typescript
Advanced tools
Comparing version 6.3.3 to 6.3.4
@@ -51,7 +51,4 @@ import { escObjKey, escStr, getEntries, getSchemaObjectComment, indent, parseRef, tsArrayOf, tsIntersectionOf, tsOmit, tsOneOf, tsOptionalProperty, tsReadonly, tsTupleOf, tsUnionOf, tsWithRequired } from "../utils.js"; | ||
if ("type" in schemaObject) { | ||
if (Array.isArray(schemaObject.type)) { | ||
return tsOneOf(...schemaObject.type.map((t) => transformSchemaObject({ ...schemaObject, type: t }, { path, ctx }))); | ||
} | ||
if (schemaObject.type === "null") | ||
return schemaObject.type; | ||
return "null"; | ||
if (schemaObject.type === "string" || schemaObject.type === "boolean") { | ||
@@ -78,13 +75,13 @@ return schemaObject.nullable ? tsUnionOf(schemaObject.type, "null") : schemaObject.type; | ||
} | ||
const minItems = typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0 ? schemaObject.minItems : 0; | ||
const maxItems = typeof schemaObject.maxItems === "number" && schemaObject.maxItems >= 0 && minItems <= schemaObject.maxItems ? schemaObject.maxItems : undefined; | ||
const estimateCodeSize = typeof maxItems !== "number" ? minItems : (maxItems * (maxItems + 1) - minItems * (minItems - 1)) / 2; | ||
if (ctx.supportArrayLength && (minItems !== 0 || maxItems !== undefined) && estimateCodeSize < 30) { | ||
const min = typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0 ? schemaObject.minItems : 0; | ||
const max = typeof schemaObject.maxItems === "number" && schemaObject.maxItems >= 0 && min <= schemaObject.maxItems ? schemaObject.maxItems : undefined; | ||
const estimateCodeSize = typeof max !== "number" ? min : (max * (max + 1) - min * (min - 1)) / 2; | ||
if (ctx.supportArrayLength && (min !== 0 || max !== undefined) && estimateCodeSize < 30) { | ||
if (typeof schemaObject.maxItems !== "number") { | ||
itemType = tsTupleOf(...Array.from({ length: minItems }).map(() => itemType), `...${tsArrayOf(itemType)}`); | ||
itemType = tsTupleOf(...Array.from({ length: min }).map(() => itemType), `...${tsArrayOf(itemType)}`); | ||
return ctx.immutableTypes || schemaObject.readOnly ? tsReadonly(itemType) : itemType; | ||
} | ||
else { | ||
return tsUnionOf(...Array.from({ length: (maxItems ?? 0) - minItems + 1 }) | ||
.map((_, i) => i + minItems) | ||
return tsUnionOf(...Array.from({ length: (max ?? 0) - min + 1 }) | ||
.map((_, i) => i + min) | ||
.map((n) => { | ||
@@ -102,2 +99,5 @@ const t = tsTupleOf(...Array.from({ length: n }).map(() => itemType)); | ||
} | ||
if (Array.isArray(schemaObject.type)) { | ||
return tsUnionOf(...schemaObject.type.map((t) => transformSchemaObject({ ...schemaObject, type: t }, { path, ctx }))); | ||
} | ||
} | ||
@@ -104,0 +104,0 @@ const coreType = []; |
@@ -210,15 +210,27 @@ /// <reference types="node" resolution-mode="require"/> | ||
oneOf: (SchemaObject | ReferenceObject)[]; | ||
} | { | ||
} | StringSubtype | NumberSubtype | IntegerSubtype | ArraySubtype | BooleanSubtype | NullSubtype | ObjectSubtype | { | ||
type: ("string" | "number" | "integer" | "array" | "boolean" | "null" | "object")[]; | ||
} | { | ||
allOf: (SchemaObject | ReferenceObject)[]; | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
required?: string[]; | ||
} | { | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
anyOf: (SchemaObject | ReferenceObject)[]; | ||
required?: string[]; | ||
} | {}); | ||
export interface StringSubtype { | ||
type: "string"; | ||
} | { | ||
} | ||
export interface NumberSubtype { | ||
type: "number"; | ||
minimum?: number; | ||
maximum?: number; | ||
} | { | ||
} | ||
export interface IntegerSubtype { | ||
type: "integer"; | ||
minimum?: number; | ||
maximum?: number; | ||
} | { | ||
} | ||
export interface ArraySubtype { | ||
type: "array"; | ||
@@ -229,8 +241,11 @@ prefixItems?: (SchemaObject | ReferenceObject)[]; | ||
maxItems?: number; | ||
} | { | ||
} | ||
export interface BooleanSubtype { | ||
type: "boolean"; | ||
} | { | ||
} | ||
export interface NullSubtype { | ||
type: "null"; | ||
} | { | ||
type: "object"; | ||
} | ||
export interface ObjectSubtype { | ||
type: "object" | ["object", "null"]; | ||
properties?: { | ||
@@ -243,11 +258,3 @@ [name: string]: SchemaObject | ReferenceObject; | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
} | { | ||
allOf: (SchemaObject | ReferenceObject)[]; | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
required?: string[]; | ||
} | { | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
anyOf: (SchemaObject | ReferenceObject)[]; | ||
required?: string[]; | ||
} | {}); | ||
} | ||
export interface DiscriminatorObject { | ||
@@ -254,0 +261,0 @@ propertyName: string; |
{ | ||
"name": "openapi-typescript", | ||
"description": "Generate TypeScript types from Swagger OpenAPI specs", | ||
"version": "6.3.3", | ||
"version": "6.3.4", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Drew Powers", |
@@ -26,3 +26,5 @@ <img src="../../docs/public/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" /> | ||
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms] | ||
``` | ||
```bash | ||
# Remote schema | ||
@@ -29,0 +31,0 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts |
@@ -54,3 +54,3 @@ import type { GlobalContext, ReferenceObject, SchemaObject } from "../types.js"; | ||
if ("type" in schemaObject) { | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) items = items.map((t) => escStr(t)); | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string" as any))) items = items.map((t) => escStr(t)); | ||
} | ||
@@ -72,11 +72,6 @@ // if no type, assume "string" | ||
if ("type" in schemaObject) { | ||
// array type | ||
if (Array.isArray(schemaObject.type)) { | ||
return tsOneOf(...schemaObject.type.map((t) => transformSchemaObject({ ...schemaObject, type: t }, { path, ctx }))); | ||
} | ||
// "type": "null" | ||
if (schemaObject.type === "null") return schemaObject.type; | ||
if (schemaObject.type === "null") return "null"; | ||
// "type": "string" / "type": "null" | ||
// "type": "string", "type": "boolean" | ||
if (schemaObject.type === "string" || schemaObject.type === "boolean") { | ||
@@ -86,3 +81,3 @@ return schemaObject.nullable ? tsUnionOf(schemaObject.type, "null") : schemaObject.type; | ||
// "type": "number" / "type": "integer" | ||
// "type": "number", "type": "integer" | ||
if (schemaObject.type === "number" || schemaObject.type === "integer") { | ||
@@ -108,14 +103,14 @@ return schemaObject.nullable ? tsUnionOf("number", "null") : "number"; | ||
} | ||
const minItems: number = typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0 ? schemaObject.minItems : 0; | ||
const maxItems: number | undefined = typeof schemaObject.maxItems === "number" && schemaObject.maxItems >= 0 && minItems <= schemaObject.maxItems ? schemaObject.maxItems : undefined; | ||
const estimateCodeSize = typeof maxItems !== "number" ? minItems : (maxItems * (maxItems + 1) - minItems * (minItems - 1)) / 2; | ||
const min: number = typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0 ? schemaObject.minItems : 0; | ||
const max: number | undefined = typeof schemaObject.maxItems === "number" && schemaObject.maxItems >= 0 && min <= schemaObject.maxItems ? schemaObject.maxItems : undefined; | ||
const estimateCodeSize = typeof max !== "number" ? min : (max * (max + 1) - min * (min - 1)) / 2; | ||
// export types | ||
if (ctx.supportArrayLength && (minItems !== 0 || maxItems !== undefined) && estimateCodeSize < 30) { | ||
if (ctx.supportArrayLength && (min !== 0 || max !== undefined) && estimateCodeSize < 30) { | ||
if (typeof schemaObject.maxItems !== "number") { | ||
itemType = tsTupleOf(...Array.from({ length: minItems }).map(() => itemType), `...${tsArrayOf(itemType)}`); | ||
itemType = tsTupleOf(...Array.from({ length: min }).map(() => itemType), `...${tsArrayOf(itemType)}`); | ||
return ctx.immutableTypes || schemaObject.readOnly ? tsReadonly(itemType) : itemType; | ||
} else { | ||
return tsUnionOf( | ||
...Array.from({ length: (maxItems ?? 0) - minItems + 1 }) | ||
.map((_, i) => i + minItems) | ||
...Array.from({ length: (max ?? 0) - min + 1 }) | ||
.map((_, i) => i + min) | ||
.map((n) => { | ||
@@ -133,4 +128,10 @@ const t = tsTupleOf(...Array.from({ length: n }).map(() => itemType)); | ||
itemType = ctx.immutableTypes || schemaObject.readOnly ? tsReadonly(itemType) : itemType; | ||
return schemaObject.nullable ? tsUnionOf(itemType, "null") : itemType; | ||
} | ||
// polymorphic, or 3.1 nullable | ||
if (Array.isArray(schemaObject.type)) { | ||
return tsUnionOf(...schemaObject.type.map((t) => transformSchemaObject({ ...schemaObject, type: t }, { path, ctx }))); | ||
} | ||
} | ||
@@ -137,0 +138,0 @@ |
@@ -435,23 +435,10 @@ import type { URL } from "node:url"; | ||
| { oneOf: (SchemaObject | ReferenceObject)[] } | ||
| StringSubtype | ||
| NumberSubtype | ||
| IntegerSubtype | ||
| ArraySubtype | ||
| BooleanSubtype | ||
| NullSubtype | ||
| ObjectSubtype | ||
| { type: ("string" | "number" | "integer" | "array" | "boolean" | "null" | "object")[] } | ||
| { type: "string" } | ||
| { type: "number"; minimum?: number; maximum?: number } | ||
| { type: "integer"; minimum?: number; maximum?: number } | ||
| { | ||
type: "array"; | ||
prefixItems?: (SchemaObject | ReferenceObject)[]; | ||
items?: SchemaObject | ReferenceObject | (SchemaObject | ReferenceObject)[]; | ||
minItems?: number; | ||
maxItems?: number; | ||
} | ||
| { type: "boolean" } | ||
| { type: "null" } | ||
| { | ||
type: "object"; | ||
properties?: { [name: string]: SchemaObject | ReferenceObject }; | ||
additionalProperties?: boolean | Record<string, never> | SchemaObject | ReferenceObject; | ||
required?: string[]; | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
| { allOf: (SchemaObject | ReferenceObject)[]; anyOf?: (SchemaObject | ReferenceObject)[]; required?: string[] } | ||
@@ -462,2 +449,43 @@ | { allOf?: (SchemaObject | ReferenceObject)[]; anyOf: (SchemaObject | ReferenceObject)[]; required?: string[] } | ||
export interface StringSubtype { | ||
type: "string"; | ||
} | ||
export interface NumberSubtype { | ||
type: "number"; | ||
minimum?: number; | ||
maximum?: number; | ||
} | ||
export interface IntegerSubtype { | ||
type: "integer"; | ||
minimum?: number; | ||
maximum?: number; | ||
} | ||
export interface ArraySubtype { | ||
type: "array"; | ||
prefixItems?: (SchemaObject | ReferenceObject)[]; | ||
items?: SchemaObject | ReferenceObject | (SchemaObject | ReferenceObject)[]; | ||
minItems?: number; | ||
maxItems?: number; | ||
} | ||
export interface BooleanSubtype { | ||
type: "boolean"; | ||
} | ||
export interface NullSubtype { | ||
type: "null"; | ||
} | ||
export interface ObjectSubtype { | ||
type: "object" | ["object", "null"]; | ||
properties?: { [name: string]: SchemaObject | ReferenceObject }; | ||
additionalProperties?: boolean | Record<string, never> | SchemaObject | ReferenceObject; | ||
required?: string[]; | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
/** | ||
@@ -464,0 +492,0 @@ * [4.8.25] Discriminator Object |
Sorry, the diff of this file is not supported yet
301460
4839
309