openapi-typescript
Advanced tools
Comparing version 7.0.0-next.10 to 7.0.0-next.11
@@ -15,3 +15,3 @@ #!/usr/bin/env node | ||
--version Display the version | ||
--redoc [path], -c Specify path to Redocly config (default: redocly.yaml) | ||
--redocly [path], -c Specify path to Redocly config (default: redocly.yaml) | ||
--output, -o Specify output file (if not specified in redocly.yaml) | ||
@@ -52,2 +52,5 @@ --enum Export true TS enums instead of unions | ||
} | ||
if (args.includes("--redoc")) { | ||
errorAndExit(`The --redoc config flag has been renamed to "--redocly" (or -c as shorthand).`); | ||
} | ||
@@ -70,5 +73,5 @@ const flags = parser(args, { | ||
], | ||
string: ["output", "redoc"], | ||
string: ["output", "redocly"], | ||
alias: { | ||
redoc: ["c"], | ||
redocly: ["c"], | ||
exportType: ["t"], | ||
@@ -81,5 +84,5 @@ output: ["o"], | ||
* @param {string | URL} schema | ||
* @param {@type import('@redocly/openapi-core').Config} redoc | ||
* @param {@type import('@redocly/openapi-core').Config} redocly | ||
*/ | ||
async function generateSchema(schema, { redoc, silent = false }) { | ||
async function generateSchema(schema, { redocly, silent = false }) { | ||
return `${COMMENT_HEADER}${astToString( | ||
@@ -99,3 +102,3 @@ await openapiTS(schema, { | ||
pathParamsAsTypes: flags.pathParamsAsTypes, | ||
redoc, | ||
redocly, | ||
silent, | ||
@@ -137,4 +140,4 @@ }), | ||
// load Redocly config | ||
const maybeRedoc = findConfig(flags.redoc ? path.dirname(flags.redoc) : undefined); | ||
const redoc = maybeRedoc | ||
const maybeRedoc = findConfig(flags.redocly ? path.dirname(flags.redocly) : undefined); | ||
const redocly = maybeRedoc | ||
? await loadConfig({ configPath: maybeRedoc }) | ||
@@ -144,3 +147,3 @@ : await createConfig({}, { extends: ["minimal"] }); | ||
// handle Redoc APIs | ||
const hasRedoclyApis = Object.keys(redoc?.apis ?? {}).length > 0; | ||
const hasRedoclyApis = Object.keys(redocly?.apis ?? {}).length > 0; | ||
if (hasRedoclyApis) { | ||
@@ -151,16 +154,11 @@ if (input) { | ||
await Promise.all( | ||
Object.entries(redoc.apis).map(async ([name, api]) => { | ||
Object.entries(redocly.apis).map(async ([name, api]) => { | ||
let configRoot = CWD; | ||
if (redoc.configFile) { | ||
if (redocly.configFile) { | ||
// note: this will be absolute if --redoc is passed; otherwise, relative | ||
configRoot = path.isAbsolute(redoc.configFile) | ||
? new URL(`file://${redoc.configFile}`) | ||
: new URL(redoc.configFile, `file://${process.cwd()}/`); | ||
configRoot = path.isAbsolute(redocly.configFile) | ||
? new URL(`file://${redocly.configFile}`) | ||
: new URL(redocly.configFile, `file://${process.cwd()}/`); | ||
} | ||
if (!api[REDOC_CONFIG_KEY]?.output) { | ||
// TODO: remove in stable v7 | ||
if (api["openapi-ts"]) { | ||
errorAndExit(`Please rename "openapi-ts" to "x-openapi-ts" in your Redoc config.`); | ||
} | ||
errorAndExit( | ||
@@ -170,5 +168,3 @@ `API ${name} is missing an \`${REDOC_CONFIG_KEY}.output\` key. See https://openapi-ts.pages.dev/cli/#multiple-schemas.`, | ||
} | ||
const result = await generateSchema(new URL(api.root, configRoot), { | ||
redoc, // TODO: merge API overrides better? | ||
}); | ||
const result = await generateSchema(new URL(api.root, configRoot), { redocly }); | ||
const outFile = new URL(api[REDOC_CONFIG_KEY].output, configRoot); | ||
@@ -185,3 +181,3 @@ fs.mkdirSync(new URL(".", outFile), { recursive: true }); | ||
const result = await generateSchema(process.stdin, { | ||
redoc, | ||
redocly, | ||
silent: outputType === OUTPUT_STDOUT, | ||
@@ -209,3 +205,3 @@ }); | ||
const result = await generateSchema(new URL(input, CWD), { | ||
redoc, | ||
redocly, | ||
silent: outputType === OUTPUT_STDOUT, | ||
@@ -212,0 +208,0 @@ }); |
import { BaseResolver, bundle, makeDocumentFromString, Source, lintDocument, } from "@redocly/openapi-core"; | ||
import { Readable } from "node:stream"; | ||
import { fileURLToPath } from "node:url"; | ||
import parseJson from "parse-json"; | ||
import { debug, error, warn } from "./utils.js"; | ||
@@ -44,3 +45,3 @@ export async function parseSchema(schema, { absoluteRef, resolver }) { | ||
source: new Source(absoluteRef, schema, "application/json"), | ||
parsed: JSON.parse(schema), | ||
parsed: parseJson(schema), | ||
}; | ||
@@ -47,0 +48,0 @@ } |
@@ -48,3 +48,7 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
} | ||
return tsUnion(schemaObject.enum.map(tsLiteral)); | ||
const enumType = schemaObject.enum.map(tsLiteral); | ||
if ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) { | ||
enumType.push(NULL); | ||
} | ||
return tsUnion(enumType); | ||
} | ||
@@ -201,5 +205,8 @@ function collectUnionCompositions(items) { | ||
} | ||
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType) | ||
const finalType = ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType) | ||
? itemType | ||
: ts.factory.createArrayTypeNode(itemType); | ||
return options.ctx.immutable | ||
? ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, finalType) | ||
: finalType; | ||
} | ||
@@ -206,0 +213,0 @@ if (Array.isArray(schemaObject.type) && !Array.isArray(schemaObject)) { |
{ | ||
"name": "openapi-typescript", | ||
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript", | ||
"version": "7.0.0-next.10", | ||
"version": "7.0.0-next.11", | ||
"author": { | ||
@@ -42,19 +42,2 @@ "name": "Drew Powers", | ||
}, | ||
"scripts": { | ||
"build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs", | ||
"build:clean": "del dist", | ||
"build:esm": "tsc -p tsconfig.build.json", | ||
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts", | ||
"dev": "tsc -p tsconfig.build.json --watch", | ||
"download:schemas": "vite-node ./scripts/download-schemas.ts", | ||
"format": "biome format . --write", | ||
"lint": "biome check .", | ||
"test": "pnpm run \"/^test:/\"", | ||
"test:examples": "tsc -p tsconfig.examples.json --noEmit", | ||
"test:js": "vitest run", | ||
"test:ts": "tsc --noEmit", | ||
"update:examples": "pnpm run build && pnpm run download:schemas && vite-node ./scripts/update-examples.ts", | ||
"prepublish": "pnpm run build", | ||
"version": "pnpm run build" | ||
}, | ||
"peerDependencies": { | ||
@@ -64,4 +47,5 @@ "typescript": "^5.x" | ||
"dependencies": { | ||
"@redocly/openapi-core": "^1.11.0", | ||
"@redocly/openapi-core": "^1.12.0", | ||
"ansi-colors": "^4.1.3", | ||
"parse-json": "^8.1.0", | ||
"supports-color": "^9.4.0", | ||
@@ -77,7 +61,25 @@ "yargs-parser": "^21.1.1" | ||
"esbuild": "^0.20.2", | ||
"execa": "^7.2.0", | ||
"execa": "^8.0.1", | ||
"strip-ansi": "^7.1.0", | ||
"typescript": "^5.4.5", | ||
"vite-node": "^1.5.0", | ||
"vitest": "^1.5.0" | ||
"vite-node": "^1.5.2", | ||
"vitest": "^1.5.2" | ||
}, | ||
"scripts": { | ||
"build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs", | ||
"build:clean": "del-cli dist", | ||
"build:esm": "tsc -p tsconfig.build.json", | ||
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts", | ||
"dev": "tsc -p tsconfig.build.json --watch", | ||
"download:schemas": "vite-node ./scripts/download-schemas.ts", | ||
"format": "biome format . --write", | ||
"lint": "biome check .", | ||
"test": "pnpm run \"/^test:/\"", | ||
"test:examples": "tsc -p tsconfig.examples.json --noEmit", | ||
"test:js": "vitest run", | ||
"test:ts": "tsc --noEmit", | ||
"update:examples": "pnpm run build && pnpm run download:schemas && vite-node ./scripts/update-examples.ts", | ||
"prepublish": "pnpm run build", | ||
"version": "pnpm run build" | ||
} | ||
} | ||
} |
@@ -51,3 +51,3 @@ <img src="../../docs/public/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" /> | ||
```ts | ||
import type { paths, components } from "./api/v1"; // generated by openapi-typescript | ||
import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript | ||
@@ -54,0 +54,0 @@ // Schema Obj |
@@ -12,2 +12,3 @@ import { | ||
import { fileURLToPath } from "node:url"; | ||
import parseJson from "parse-json"; | ||
import type { OpenAPI3 } from "../types.js"; | ||
@@ -68,3 +69,3 @@ import { debug, error, warn } from "./utils.js"; | ||
source: new Source(absoluteRef, schema, "application/json"), | ||
parsed: JSON.parse(schema), | ||
parsed: parseJson(schema), | ||
}; | ||
@@ -71,0 +72,0 @@ } |
@@ -117,3 +117,7 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
} | ||
return tsUnion(schemaObject.enum.map(tsLiteral)); | ||
const enumType = schemaObject.enum.map(tsLiteral); | ||
if ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) { | ||
enumType.push(NULL); | ||
} | ||
return tsUnion(enumType); | ||
} | ||
@@ -327,5 +331,10 @@ | ||
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType) | ||
? itemType | ||
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already | ||
const finalType = | ||
ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType) | ||
? itemType | ||
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already | ||
return options.ctx.immutable | ||
? ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, finalType) | ||
: finalType; | ||
} | ||
@@ -332,0 +341,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
High entropy strings
Supply chain riskContains high entropy strings. This could be a sign of encrypted data, leaked secrets or obfuscated code.
Found 1 instance in 1 package
518380
9827
6
11
22
+ Addedparse-json@^8.1.0
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Addedindex-to-position@0.1.2(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedparse-json@8.1.0(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedtype-fest@4.33.0(transitive)