Skip to content

Big honkin‘ refactor, OpenAPI 3.1 features #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2022
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
.dccache
.cache
.DS_Store
.idea/
.nyc_output/
**/generated/
**/__snapshots__/
test/fixtures/*.yaml
coverage/
dist
node_modules
yarn.lock
yarn-error.log
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

240 changes: 159 additions & 81 deletions README.md

Large diffs are not rendered by default.

34 changes: 10 additions & 24 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,20 @@ Options
--headersObject, -h (optional) Provide a JSON object as string of HTTP headers for remote schema request
--header, -x (optional) Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the key: value pattern
--httpMethod, -m (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL
--immutable-types, -it (optional) Generates immutable types (readonly properties and readonly array)
--content-never (optional) If supplied, an omitted reponse \`content\` property will be generated as \`never\` instead of \`unknown\`
--additional-properties, -ap (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
--export-type, -t (optional) Export "type" instead of "interface"
--immutable-types (optional) Generates immutable types (readonly properties and readonly array)
--additional-properties (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
--default-non-nullable (optional) If a schema object has a default value set, don’t mark it as nullable
--prettier-config, -c (optional) specify path to Prettier config file
--raw-schema (optional) Parse as partial schema (raw components)
--paths-enum, -pe (optional) Generate an enum containing all API paths.
--export-type (optional) Export type instead of interface
--support-array-length (optional) Generate tuples using array minItems / maxItems
--path-params-as-types (optional) Substitute path parameter names with their respective types
--alphabetize (optional) Sort types alphabetically
--version (optional) Force schema parsing version
`;

const OUTPUT_FILE = "FILE";
const OUTPUT_STDOUT = "STDOUT";
const CWD = new URL(`file://${process.cwd()}/`);
const EXT_RE = /\.[^.]+$/i;
const HTTP_RE = /^https?:\/\//;

const timeStart = process.hrtime();

Expand All @@ -47,30 +43,28 @@ function errorAndExit(errorMessage) {
}

const [, , input, ...args] = process.argv;
if (args.includes("-ap")) errorAndExit(`The -ap alias has been deprecated. Use "--additional-properties" instead.`);
if (args.includes("-it")) errorAndExit(`The -it alias has been deprecated. Use "--immutable-types" instead.`);

const flags = parser(args, {
array: ["header"],
boolean: [
"defaultNonNullable",
"immutableTypes",
"contentNever",
"rawSchema",
"exportType",
"supportArrayLength",
"makePathsEnum",
"pathParamsAsTypes",
"alphabetize",
],
number: ["version"],
string: ["auth", "header", "headersObject", "httpMethod", "prettierConfig"],
string: ["auth", "header", "headersObject", "httpMethod"],
alias: {
additionalProperties: ["ap"],
header: ["x"],
exportType: ["t"],
headersObject: ["h"],
httpMethod: ["m"],
immutableTypes: ["it"],
output: ["o"],
prettierConfig: ["c"],
makePathsEnum: ["pe"],
},
default: {
httpMethod: "GET",
Expand Down Expand Up @@ -103,9 +97,6 @@ async function generateSchema(pathToSpec) {
auth: flags.auth,
defaultNonNullable: flags.defaultNonNullable,
immutableTypes: flags.immutableTypes,
prettierConfig: flags.prettierConfig,
rawSchema: flags.rawSchema,
makePathsEnum: flags.makePathsEnum,
contentNever: flags.contentNever,
silent: output === OUTPUT_STDOUT,
version: flags.version,
Expand Down Expand Up @@ -156,13 +147,8 @@ async function main() {
console.info(`✨ ${BOLD}openapi-typescript ${packageJSON.version}${RESET}`); // only log if we’re NOT writing to stdout
}

// error: --raw-schema
if (flags.rawSchema && !flags.version) {
throw new Error(`--raw-schema requires --version flag`);
}

// handle remote schema, exit
if (/^https?:\/\//.test(pathToSpec)) {
if (HTTP_RE.test(pathToSpec)) {
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(outputDir, { recursive: true });
await generateSchema(pathToSpec);
return;
Expand Down
76,438 changes: 34,215 additions & 42,223 deletions examples/github-api.ts

Large diffs are not rendered by default.

56,903 changes: 21,807 additions & 35,096 deletions examples/stripe-api.ts

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,39 @@
},
"homepage": "https://github.com/drwpow/openapi-typescript#readme",
"scripts": {
"build": "del dist && tsc",
"dev": "tsc --watch",
"build": "del dist && tsc -p tsconfig.build.json",
"dev": "tsc -p tsconfig.build.json --watch",
"download:schemas": "node ./scripts/download-schemas.js",
"format": "npm run prettier -w .",
"lint": "eslint .",
"prepare": "npm run build",
"pregenerate": "npm run build",
"update:examples": "node ./scripts/update-examples.js",
"prepare": "npm run build && npm run download:schemas",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"update:examples": "node ./scripts/update-examples.js",
"version": "npm run build"
},
"dependencies": {
"fast-glob": "^3.2.12",
"js-yaml": "^4.1.0",
"mime": "^3.0.0",
"prettier": "^2.7.1",
"undici": "^5.12.0",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/mime": "^2.0.3",
"@types/node": "^18.11.7",
"@types/prettier": "^2.7.1",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"@vitest/coverage-c8": "^0.24.3",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@vitest/coverage-c8": "^0.24.5",
"del-cli": "^5.0.0",
"eol": "^0.9.1",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"typescript": "^4.8.4",
"vitest": "^0.24.3"
"vitest": "^0.24.5"
}
}
Loading