Skip to content

Commit 982ede1

Browse files
authored
Refactor (#968)
1 parent b2ddda8 commit 982ede1

File tree

195 files changed

+59211
-887583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+59211
-887583
lines changed

.gitignore

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
.dccache
2-
.cache
31
.DS_Store
4-
.idea/
5-
.nyc_output/
6-
**/generated/
7-
**/__snapshots__/
2+
test/fixtures/*.yaml
83
coverage/
94
dist
105
node_modules
11-
yarn.lock
12-
yarn-error.log

.npmrc

-1
This file was deleted.

README.md

+159-81
Large diffs are not rendered by default.

bin/cli.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,20 @@ Options
2020
--headersObject, -h (optional) Provide a JSON object as string of HTTP headers for remote schema request
2121
--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
2222
--httpMethod, -m (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL
23-
--immutable-types, -it (optional) Generates immutable types (readonly properties and readonly array)
24-
--content-never (optional) If supplied, an omitted reponse \`content\` property will be generated as \`never\` instead of \`unknown\`
25-
--additional-properties, -ap (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
23+
--export-type, -t (optional) Export "type" instead of "interface"
24+
--immutable-types (optional) Generates immutable types (readonly properties and readonly array)
25+
--additional-properties (optional) Allow arbitrary properties for all schema objects without "additionalProperties: false"
2626
--default-non-nullable (optional) If a schema object has a default value set, don’t mark it as nullable
27-
--prettier-config, -c (optional) specify path to Prettier config file
28-
--raw-schema (optional) Parse as partial schema (raw components)
29-
--paths-enum, -pe (optional) Generate an enum containing all API paths.
30-
--export-type (optional) Export type instead of interface
3127
--support-array-length (optional) Generate tuples using array minItems / maxItems
3228
--path-params-as-types (optional) Substitute path parameter names with their respective types
3329
--alphabetize (optional) Sort types alphabetically
34-
--version (optional) Force schema parsing version
3530
`;
3631

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

4238
const timeStart = process.hrtime();
4339

@@ -47,30 +43,28 @@ function errorAndExit(errorMessage) {
4743
}
4844

4945
const [, , input, ...args] = process.argv;
46+
if (args.includes("-ap")) errorAndExit(`The -ap alias has been deprecated. Use "--additional-properties" instead.`);
47+
if (args.includes("-it")) errorAndExit(`The -it alias has been deprecated. Use "--immutable-types" instead.`);
48+
5049
const flags = parser(args, {
5150
array: ["header"],
5251
boolean: [
5352
"defaultNonNullable",
5453
"immutableTypes",
5554
"contentNever",
56-
"rawSchema",
5755
"exportType",
5856
"supportArrayLength",
59-
"makePathsEnum",
6057
"pathParamsAsTypes",
6158
"alphabetize",
6259
],
6360
number: ["version"],
64-
string: ["auth", "header", "headersObject", "httpMethod", "prettierConfig"],
61+
string: ["auth", "header", "headersObject", "httpMethod"],
6562
alias: {
66-
additionalProperties: ["ap"],
6763
header: ["x"],
64+
exportType: ["t"],
6865
headersObject: ["h"],
6966
httpMethod: ["m"],
70-
immutableTypes: ["it"],
7167
output: ["o"],
72-
prettierConfig: ["c"],
73-
makePathsEnum: ["pe"],
7468
},
7569
default: {
7670
httpMethod: "GET",
@@ -103,9 +97,6 @@ async function generateSchema(pathToSpec) {
10397
auth: flags.auth,
10498
defaultNonNullable: flags.defaultNonNullable,
10599
immutableTypes: flags.immutableTypes,
106-
prettierConfig: flags.prettierConfig,
107-
rawSchema: flags.rawSchema,
108-
makePathsEnum: flags.makePathsEnum,
109100
contentNever: flags.contentNever,
110101
silent: output === OUTPUT_STDOUT,
111102
version: flags.version,
@@ -156,13 +147,8 @@ async function main() {
156147
console.info(`✨ ${BOLD}openapi-typescript ${packageJSON.version}${RESET}`); // only log if we’re NOT writing to stdout
157148
}
158149

159-
// error: --raw-schema
160-
if (flags.rawSchema && !flags.version) {
161-
throw new Error(`--raw-schema requires --version flag`);
162-
}
163-
164150
// handle remote schema, exit
165-
if (/^https?:\/\//.test(pathToSpec)) {
151+
if (HTTP_RE.test(pathToSpec)) {
166152
if (output !== "." && output === OUTPUT_FILE) fs.mkdirSync(outputDir, { recursive: true });
167153
await generateSchema(pathToSpec);
168154
return;

examples/github-api.ts

+34,215-42,223
Large diffs are not rendered by default.

examples/stripe-api.ts

+21,807-35,096
Large diffs are not rendered by default.

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,39 @@
3535
},
3636
"homepage": "https://github.com/drwpow/openapi-typescript#readme",
3737
"scripts": {
38-
"build": "del dist && tsc",
39-
"dev": "tsc --watch",
38+
"build": "del dist && tsc -p tsconfig.build.json",
39+
"dev": "tsc -p tsconfig.build.json --watch",
40+
"download:schemas": "node ./scripts/download-schemas.js",
4041
"format": "npm run prettier -w .",
4142
"lint": "eslint .",
42-
"prepare": "npm run build",
4343
"pregenerate": "npm run build",
44-
"update:examples": "node ./scripts/update-examples.js",
44+
"prepare": "npm run build && npm run download:schemas",
4545
"test": "vitest run",
4646
"test:coverage": "vitest run --coverage",
47+
"update:examples": "node ./scripts/update-examples.js",
4748
"version": "npm run build"
4849
},
4950
"dependencies": {
5051
"fast-glob": "^3.2.12",
5152
"js-yaml": "^4.1.0",
5253
"mime": "^3.0.0",
53-
"prettier": "^2.7.1",
5454
"undici": "^5.12.0",
5555
"yargs-parser": "^21.1.1"
5656
},
5757
"devDependencies": {
5858
"@types/js-yaml": "^4.0.5",
5959
"@types/mime": "^2.0.3",
60-
"@types/node": "^18.11.7",
61-
"@types/prettier": "^2.7.1",
62-
"@typescript-eslint/eslint-plugin": "^5.41.0",
63-
"@typescript-eslint/parser": "^5.41.0",
64-
"@vitest/coverage-c8": "^0.24.3",
60+
"@types/node": "^18.11.9",
61+
"@typescript-eslint/eslint-plugin": "^5.42.0",
62+
"@typescript-eslint/parser": "^5.42.0",
63+
"@vitest/coverage-c8": "^0.24.5",
6564
"del-cli": "^5.0.0",
6665
"eol": "^0.9.1",
6766
"eslint": "^8.26.0",
6867
"eslint-config-prettier": "^8.5.0",
6968
"eslint-plugin-prettier": "^4.2.1",
69+
"prettier": "^2.7.1",
7070
"typescript": "^4.8.4",
71-
"vitest": "^0.24.3"
71+
"vitest": "^0.24.5"
7272
}
7373
}

0 commit comments

Comments
 (0)