Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .changeset/yellow-meteors-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": minor
---

Add ability to set flags in redocly.yaml
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ apis:
root: ./openapi/external.yaml
x-openapi-ts:
output: ./openapi/external.ts
additional-properties: true # CLI flags are also supported, either in kebab-case or camelCase format
```

:::
Expand Down
5 changes: 5 additions & 0 deletions packages/openapi-fetch/test/redocly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ apis:
root: ../../openapi-typescript/examples/stripe-api.yaml
x-openapi-ts:
output: ./examples/schemas/stripe.d.ts
read-write-visibility:
root: ./read-write-visibility/schemas/read-write.yaml
x-openapi-ts:
output: ./read-write-visibility/schemas/read-write.d.ts
read-write-markers: true
101 changes: 45 additions & 56 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "node:fs";
import path from "node:path";
import { performance } from "node:perf_hooks";
import { createConfig, findConfig, loadConfig } from "@redocly/openapi-core";
import { kebabCase } from "scule";
import parser from "yargs-parser";
import openapiTS, { astToString, COMMENT_HEADER, c, error, formatTime, warn } from "../dist/index.mjs";

Expand Down Expand Up @@ -71,32 +72,34 @@ if (args.includes("--root-types-keep-casing") && !args.includes("--root-types"))
console.warn("--root-types-keep-casing has no effect without --root-types flag");
}

const BOOLEAN_FLAGS = [
"additionalProperties",
"alphabetize",
"arrayLength",
"contentNever",
"defaultNonNullable",
"propertiesRequiredByDefault",
"emptyObjectsUnknown",
"enum",
"enumValues",
"conditionalEnums",
"dedupeEnums",
"check",
"excludeDeprecated",
"exportType",
"help",
"immutable",
"pathParamsAsTypes",
"rootTypes",
"rootTypesNoSchemaPrefix",
"rootTypesKeepCasing",
"makePathsEnum",
"generatePathParams",
"readWriteMarkers",
];

const flags = parser(args, {
boolean: [
"additionalProperties",
"alphabetize",
"arrayLength",
"contentNever",
"defaultNonNullable",
"propertiesRequiredByDefault",
"emptyObjectsUnknown",
"enum",
"enumValues",
"conditionalEnums",
"dedupeEnums",
"check",
"excludeDeprecated",
"exportType",
"help",
"immutable",
"pathParamsAsTypes",
"rootTypes",
"rootTypesNoSchemaPrefix",
"rootTypesKeepCasing",
"makePathsEnum",
"generatePathParams",
"readWriteMarkers",
],
boolean: BOOLEAN_FLAGS,
string: ["output", "redocly"],
alias: {
redocly: ["c"],
Expand Down Expand Up @@ -136,36 +139,10 @@ function checkStaleOutput(current, outputPath) {

/**
* @param {string | URL} schema
* @param {@type import('@redocly/openapi-core').Config} redocly
* @param {@type import('@redocly/openapi-core').Config} config
*/
async function generateSchema(schema, { redocly, silent = false }) {
return `${COMMENT_HEADER}${astToString(
await openapiTS(schema, {
additionalProperties: flags.additionalProperties,
alphabetize: flags.alphabetize,
arrayLength: flags.arrayLength,
contentNever: flags.contentNever,
propertiesRequiredByDefault: flags.propertiesRequiredByDefault,
defaultNonNullable: flags.defaultNonNullable,
emptyObjectsUnknown: flags.emptyObjectsUnknown,
enum: flags.enum,
enumValues: flags.enumValues,
conditionalEnums: flags.conditionalEnums,
dedupeEnums: flags.dedupeEnums,
excludeDeprecated: flags.excludeDeprecated,
exportType: flags.exportType,
immutable: flags.immutable,
pathParamsAsTypes: flags.pathParamsAsTypes,
rootTypes: flags.rootTypes,
rootTypesNoSchemaPrefix: flags.rootTypesNoSchemaPrefix,
rootTypesKeepCasing: flags.rootTypesKeepCasing,
makePathsEnum: flags.makePathsEnum,
generatePathParams: flags.generatePathParams,
readWriteMarkers: flags.readWriteMarkers,
redocly,
silent,
}),
)}`;
async function generateSchema(schema, config) {
return `${COMMENT_HEADER}${astToString(await openapiTS(schema, config))}`;
}

/** pretty-format error message but also throw */
Expand Down Expand Up @@ -241,9 +218,21 @@ async function main() {
`API ${name} is missing an \`${REDOC_CONFIG_KEY}.output\` key. See https://openapi-ts.dev/cli/#multiple-schemas.`,
);
}
const result = await generateSchema(new URL(api.root, configRoot), { redocly });

const config = { redocly };
for (const name of BOOLEAN_FLAGS) {
if (typeof api[REDOC_CONFIG_KEY][name] === "boolean") {
config[name] = api[REDOC_CONFIG_KEY][name];
} else if (typeof api[REDOC_CONFIG_KEY][kebabCase(name)] === "boolean") {
config[name] = api[REDOC_CONFIG_KEY][kebabCase(name)];
} else if (typeof flags[name] === "boolean") {
config[name] = flags[name];
}
}
const result = await generateSchema(new URL(api.root, configRoot), config);

const outFile = new URL(api[REDOC_CONFIG_KEY].output, configRoot);
checkStaleOutput(result, outFile);
checkStaleOutput(result, outFile); // check boolean flags
fs.mkdirSync(new URL(".", outFile), { recursive: true });
fs.writeFileSync(outFile, result, "utf8");
done(name, api[REDOC_CONFIG_KEY].output, performance.now() - timeStart);
Expand Down
Loading
Loading