@depup/openapi-typescript 7.13.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1162 -0
- package/CONTRIBUTING.md +149 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/cli.js +297 -0
- package/changes.json +14 -0
- package/dist/index.cjs +152 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +517 -0
- package/dist/index.d.mts +515 -0
- package/dist/index.d.ts +517 -0
- package/dist/index.mjs +85 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lib/redoc.cjs +129 -0
- package/dist/lib/redoc.cjs.map +1 -0
- package/dist/lib/redoc.mjs +122 -0
- package/dist/lib/redoc.mjs.map +1 -0
- package/dist/lib/ts.cjs +470 -0
- package/dist/lib/ts.cjs.map +1 -0
- package/dist/lib/ts.mjs +431 -0
- package/dist/lib/ts.mjs.map +1 -0
- package/dist/lib/utils.cjs +295 -0
- package/dist/lib/utils.cjs.map +1 -0
- package/dist/lib/utils.mjs +278 -0
- package/dist/lib/utils.mjs.map +1 -0
- package/dist/transform/components-object.cjs +150 -0
- package/dist/transform/components-object.cjs.map +1 -0
- package/dist/transform/components-object.mjs +127 -0
- package/dist/transform/components-object.mjs.map +1 -0
- package/dist/transform/header-object.cjs +48 -0
- package/dist/transform/header-object.cjs.map +1 -0
- package/dist/transform/header-object.mjs +42 -0
- package/dist/transform/header-object.mjs.map +1 -0
- package/dist/transform/index.cjs +124 -0
- package/dist/transform/index.cjs.map +1 -0
- package/dist/transform/index.mjs +118 -0
- package/dist/transform/index.mjs.map +1 -0
- package/dist/transform/media-type-object.cjs +14 -0
- package/dist/transform/media-type-object.cjs.map +1 -0
- package/dist/transform/media-type-object.mjs +12 -0
- package/dist/transform/media-type-object.mjs.map +1 -0
- package/dist/transform/operation-object.cjs +105 -0
- package/dist/transform/operation-object.cjs.map +1 -0
- package/dist/transform/operation-object.mjs +96 -0
- package/dist/transform/operation-object.mjs.map +1 -0
- package/dist/transform/parameter-object.cjs +11 -0
- package/dist/transform/parameter-object.cjs.map +1 -0
- package/dist/transform/parameter-object.mjs +9 -0
- package/dist/transform/parameter-object.mjs.map +1 -0
- package/dist/transform/parameters-array.cjs +120 -0
- package/dist/transform/parameters-array.cjs.map +1 -0
- package/dist/transform/parameters-array.mjs +114 -0
- package/dist/transform/parameters-array.mjs.map +1 -0
- package/dist/transform/path-item-object.cjs +84 -0
- package/dist/transform/path-item-object.cjs.map +1 -0
- package/dist/transform/path-item-object.mjs +78 -0
- package/dist/transform/path-item-object.mjs.map +1 -0
- package/dist/transform/paths-enum.cjs +35 -0
- package/dist/transform/paths-enum.cjs.map +1 -0
- package/dist/transform/paths-enum.mjs +33 -0
- package/dist/transform/paths-enum.mjs.map +1 -0
- package/dist/transform/paths-object.cjs +134 -0
- package/dist/transform/paths-object.cjs.map +1 -0
- package/dist/transform/paths-object.mjs +128 -0
- package/dist/transform/paths-object.mjs.map +1 -0
- package/dist/transform/request-body-object.cjs +68 -0
- package/dist/transform/request-body-object.cjs.map +1 -0
- package/dist/transform/request-body-object.mjs +62 -0
- package/dist/transform/request-body-object.mjs.map +1 -0
- package/dist/transform/response-object.cjs +123 -0
- package/dist/transform/response-object.cjs.map +1 -0
- package/dist/transform/response-object.mjs +117 -0
- package/dist/transform/response-object.mjs.map +1 -0
- package/dist/transform/responses-object.cjs +36 -0
- package/dist/transform/responses-object.cjs.map +1 -0
- package/dist/transform/responses-object.mjs +30 -0
- package/dist/transform/responses-object.mjs.map +1 -0
- package/dist/transform/schema-object.cjs +502 -0
- package/dist/transform/schema-object.cjs.map +1 -0
- package/dist/transform/schema-object.mjs +493 -0
- package/dist/transform/schema-object.mjs.map +1 -0
- package/dist/transform/webhooks-object.cjs +37 -0
- package/dist/transform/webhooks-object.cjs.map +1 -0
- package/dist/transform/webhooks-object.mjs +31 -0
- package/dist/transform/webhooks-object.mjs.map +1 -0
- package/package.json +102 -0
- package/src/index.ts +108 -0
- package/src/lib/redoc.ts +164 -0
- package/src/lib/ts.ts +616 -0
- package/src/lib/utils.ts +394 -0
- package/src/transform/components-object.ts +161 -0
- package/src/transform/header-object.ts +45 -0
- package/src/transform/index.ts +116 -0
- package/src/transform/media-type-object.ts +18 -0
- package/src/transform/operation-object.ts +104 -0
- package/src/transform/parameter-object.ts +15 -0
- package/src/transform/parameters-array.ts +129 -0
- package/src/transform/path-item-object.ts +102 -0
- package/src/transform/paths-enum.ts +43 -0
- package/src/transform/paths-object.ts +134 -0
- package/src/transform/request-body-object.ts +59 -0
- package/src/transform/response-object.ts +112 -0
- package/src/transform/responses-object.ts +36 -0
- package/src/transform/schema-object.ts +737 -0
- package/src/transform/webhooks-object.ts +27 -0
- package/src/types.ts +735 -0
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks for being willing to contribute! 🙏
|
|
4
|
+
|
|
5
|
+
**Working on your first Pull Request (PR)?** You can learn how from this free series [How to Contribute to an Open Source Project on GitHub](https://app.egghead.io/playlists/how-to-contribute-to-an-open-source-project-on-github).
|
|
6
|
+
|
|
7
|
+
## Open issues
|
|
8
|
+
|
|
9
|
+
Please check out the [the open issues](https://github.com/openapi-ts/openapi-typescript/issues). Issues labelled [**Good First Issue**](https://github.com/openapi-ts/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) are especially good to start with.
|
|
10
|
+
|
|
11
|
+
Contributing doesn’t have to be in code. Simply answering questions in open issues or providing workarounds is as important as making pull requests.
|
|
12
|
+
|
|
13
|
+
## Opening a Pull Request
|
|
14
|
+
|
|
15
|
+
Pull requests are **welcome** for this repo!
|
|
16
|
+
|
|
17
|
+
Bugfixes will always be accepted, though in some cases some small changes may be requested.
|
|
18
|
+
|
|
19
|
+
However, if adding a feature or breaking change, please **open an issue first to discuss.** This ensures no time or work is wasted writing code that won’t be accepted to the project (see [Project Goals](https://openapi-ts.dev/about/#project-goals)). Undiscussed feature work may be rejected at the discretion of the maintainers.
|
|
20
|
+
|
|
21
|
+
### Setup
|
|
22
|
+
|
|
23
|
+
1. Install [pnpm](https://pnpm.io/)
|
|
24
|
+
2. [Fork this repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo) and clone your copy locally
|
|
25
|
+
3. Run `pnpm i` to install dependencies
|
|
26
|
+
|
|
27
|
+
### Writing code
|
|
28
|
+
|
|
29
|
+
Create a new branch for your PR with `git checkout -b your-branch-name`. Add the relevant code as well as docs and tests. When you push everything up (`git push`), navigate back to your repo in GitHub and you should see a prompt to open a new PR.
|
|
30
|
+
|
|
31
|
+
While best practices for commit messages are encouraged (e.g. start with an imperative verb, keep it short, use the body if needed), this repo doesn’t follow any specific guidelines. Clarity is favored over strict rules. Changelogs are generated separately from git (see [the Changelogs section](#changelogs))
|
|
32
|
+
|
|
33
|
+
When working locally, run:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
pnpm run dev
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
This will compile the code as you change automatically.
|
|
40
|
+
|
|
41
|
+
#### Tip: use ASTExplorer.net!
|
|
42
|
+
|
|
43
|
+
Working with the TypeScript AST can be daunting. Luckily, there’s [astexplorer.net](https://astexplorer.net) which makes it much more accessible. Rather than trying to build an AST from scratch (which is near impossible), instead:
|
|
44
|
+
|
|
45
|
+
1. Switch to the **typescript** parser in the top menu
|
|
46
|
+
2. Type out code in the left-hand panel
|
|
47
|
+
3. Inspect the right-hand panel to see what the desired AST is.
|
|
48
|
+
|
|
49
|
+
From there, you can refer to existing examples in the codebase. There may even be helper utilities in `src/lib/ts.ts` to make life easier.
|
|
50
|
+
|
|
51
|
+
#### Tip: Use Test-driven Development!
|
|
52
|
+
|
|
53
|
+
Contributing to this library is hard-bordering-on-impossible without a [test-driven development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development) strategy. If you’re new to this, the basic workflow is:
|
|
54
|
+
|
|
55
|
+
1. First, write a [test](#testing) that fully outlines what you’d _like_ the output to be.
|
|
56
|
+
2. Next, make sure this test **fails** when you run `npm test` (yes, _fails!_)
|
|
57
|
+
3. Then, make changes to `src/` until the tests pass.
|
|
58
|
+
|
|
59
|
+
Reasoning about code generation can be quite difficult until you “invert your thinking” and approach it output-first. Adopting TDD can turn very unclear/abstract problems into concrete ones with clear steps to resolution.
|
|
60
|
+
|
|
61
|
+
TL;DR: When starting any task, **write a failing test first!**
|
|
62
|
+
|
|
63
|
+
#### Updating snapshot tests
|
|
64
|
+
|
|
65
|
+
To add a schema as a snapshot test, modify the [/scripts/download-schemas.ts](/scripts/download-schemas.ts) script with a path to download. There are both single-file schemas as well as multi-file schemas.
|
|
66
|
+
|
|
67
|
+
### Generating types
|
|
68
|
+
|
|
69
|
+
It may be surprising to hear, but generating TypeScript types from OpenAPI is opinionated. Even though TypeScript and OpenAPI are close relatives—both JavaScript/JSON-based—they are nonetheless 2 different languages and thus there is room for interpretation. Further, some parts of the OpenAPI specification can be ambiguous on how they’re used, and what the expected type outcomes may be (though this is generally for more advanced use cases, such as specific implementations of `anyOf` as well as [discriminator](https://spec.openapis.org/oas/latest.html#discriminatorObject) and complex polymorphism).
|
|
70
|
+
|
|
71
|
+
All that said, this library should strive to generate _the most predictable_ TypeScript output for a given schema. And to achieve that, it always helps to open an [issue](https://github.com/openapi-ts/openapi-typescript/issues) or [discussion](https://github.com/openapi-ts/openapi-typescript/discussions) to gather feedback.
|
|
72
|
+
|
|
73
|
+
### Opening a PR
|
|
74
|
+
|
|
75
|
+
When opening a pull request, make sure all of the following is done:
|
|
76
|
+
|
|
77
|
+
- [x] Tests are added
|
|
78
|
+
- [x] Build passes (`npm run build`)
|
|
79
|
+
- [x] Tests pass (`npm test`)
|
|
80
|
+
- [x] Linting passes (`npm run lint`)
|
|
81
|
+
|
|
82
|
+
Lastly, be sure to fill out the complete PR template.
|
|
83
|
+
|
|
84
|
+
### Changelogs
|
|
85
|
+
|
|
86
|
+
The changelog is generated via [changesets](https://github.com/changesets/changesets), and is separate from Git commit messages and pull request titles. To write a human-readable changelog for your changes, run:
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
npx changeset
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
This will ask if it’s a `patch`, `minor`, or `major` change ([semver](https://semver.org/)), along with a plain description of what you did. Commit this new file along with the rest of your PR, and during the next release this will go into the official changelog!
|
|
93
|
+
|
|
94
|
+
## Testing
|
|
95
|
+
|
|
96
|
+
This library uses [Vitest](https://vitest.dev/) for testing. There’s a great [VS Code extension](https://marketplace.visualstudio.com/items?itemName=ZixuanChen.vitest-explorer) you can optionally use if you’d like in-editor debugging tools.
|
|
97
|
+
|
|
98
|
+
### Running tests
|
|
99
|
+
|
|
100
|
+
💡 The tests test **the production build** in `dist/`. Be sure to run `npm run build` before running tests (or keep `npm run dev` running in the background, which compiles as-you-work)!
|
|
101
|
+
|
|
102
|
+
To run the entire test suite once, run:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
pnpm test
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
To run an individual test:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
pnpm test -- [partial filename]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
To start the entire test suite in watch mode:
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
npx vitest
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Running linting
|
|
121
|
+
|
|
122
|
+
Linting is handled via [Biome](https://biomejs.dev), a faster ESLint replacement. It was installed with `pnpm i` and can be run with:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
pnpm run lint
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Updating snapshot examples
|
|
129
|
+
|
|
130
|
+
⚠️ This may break tests if schemas have been updated
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
pnpm run update:examples
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Unit tests or snapshot tests?
|
|
137
|
+
|
|
138
|
+
This library has both unit tests (tests that test a tiny part of a schema) and snapshot tests (tests that run over an entire, complete schema). When opening a PR, the former are more valuable than the latter, and are always required. However, updating snapshot tests can help with the following:
|
|
139
|
+
|
|
140
|
+
- Fixing Node.js or OS-related bugs
|
|
141
|
+
- Adding a CLI option that changes the entire output
|
|
142
|
+
|
|
143
|
+
For most PRs, **snapshot tests can be avoided.** But for scenarios similar to the ones mentioned, they can ensure everything is working as expected.
|
|
144
|
+
|
|
145
|
+
## Troubleshooting
|
|
146
|
+
|
|
147
|
+
### When I run tests, it’s not picking up my changes
|
|
148
|
+
|
|
149
|
+
Some tests import the **built package** and not the source file. Be sure to run `pnpm run build` to build the project. You can also run `pnpm run dev` as you work so changes are always up-to-date.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Drew Powers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @depup/openapi-typescript
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [openapi-typescript](https://www.npmjs.com/package/openapi-typescript)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/openapi-typescript
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [openapi-typescript](https://www.npmjs.com/package/openapi-typescript) @ 7.13.0 |
|
|
17
|
+
| Processed | 2026-03-22 |
|
|
18
|
+
| Smoke test | failed |
|
|
19
|
+
| Deps updated | 2 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| @redocly/openapi-core | ^1.34.6 | ^2.24.1 |
|
|
26
|
+
| yargs-parser | ^21.1.1 | ^22.0.0 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/openapi-typescript
|
|
31
|
+
|
|
32
|
+
License inherited from the original package.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
6
|
+
import { createConfig, findConfig, loadConfig } from "@redocly/openapi-core";
|
|
7
|
+
import parser from "yargs-parser";
|
|
8
|
+
import openapiTS, { astToString, COMMENT_HEADER, c, error, formatTime, warn } from "../dist/index.mjs";
|
|
9
|
+
|
|
10
|
+
const HELP = `Usage
|
|
11
|
+
$ openapi-typescript [input] [options]
|
|
12
|
+
|
|
13
|
+
Options
|
|
14
|
+
--help Display this
|
|
15
|
+
--version Display the version
|
|
16
|
+
--redocly [path], -c Specify path to Redocly config (default: redocly.yaml)
|
|
17
|
+
--output, -o Specify output file (if not specified in redocly.yaml)
|
|
18
|
+
--enum Export true TS enums instead of unions
|
|
19
|
+
--enum-values Export enum values as arrays
|
|
20
|
+
--conditional-enums Only generate true TS enums when enum metadata is available (default: false)
|
|
21
|
+
--dedupe-enums Dedupe enum types when \`--enum=true\` is set
|
|
22
|
+
--check Check that the generated types are up-to-date. (default: false)
|
|
23
|
+
--export-type, -t Export top-level \`type\` instead of \`interface\`
|
|
24
|
+
--immutable Generate readonly types
|
|
25
|
+
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
|
|
26
|
+
--empty-objects-unknown Generate \`unknown\` instead of \`Record<string, never>\` for empty objects
|
|
27
|
+
--default-non-nullable Set to \`false\` to ignore default values when generating non-nullable types
|
|
28
|
+
--properties-required-by-default
|
|
29
|
+
Treat schema objects as if \`required\` is set to all properties by default
|
|
30
|
+
--array-length Generate tuples using array minItems / maxItems
|
|
31
|
+
--path-params-as-types Convert paths to template literal types
|
|
32
|
+
--alphabetize Sort object keys alphabetically
|
|
33
|
+
--exclude-deprecated Exclude deprecated types
|
|
34
|
+
--root-types (optional) Export schemas types at root level
|
|
35
|
+
--root-types-no-schema-prefix (optional)
|
|
36
|
+
Do not add "Schema" prefix to types at the root level (should only be used with --root-types)
|
|
37
|
+
--root-types-keep-casing Keep casing of root types (should only be used with --root-types)
|
|
38
|
+
--make-paths-enum Generate ApiPaths enum for all paths
|
|
39
|
+
--read-write-markers Generate $Read/$Write markers for readOnly/writeOnly properties
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
const OUTPUT_FILE = "FILE";
|
|
43
|
+
const OUTPUT_STDOUT = "STDOUT";
|
|
44
|
+
const CWD = new URL(`file://${process.cwd()}/`);
|
|
45
|
+
const REDOC_CONFIG_KEY = "x-openapi-ts";
|
|
46
|
+
|
|
47
|
+
const timeStart = performance.now();
|
|
48
|
+
|
|
49
|
+
const [, , ...args] = process.argv;
|
|
50
|
+
if (args.includes("-ap")) {
|
|
51
|
+
errorAndExit(`The -ap alias has been deprecated. Use "--additional-properties" instead.`);
|
|
52
|
+
}
|
|
53
|
+
if (args.includes("--immutable-types")) {
|
|
54
|
+
errorAndExit(`The --immutable-types flag has been renamed to "--immutable".`);
|
|
55
|
+
}
|
|
56
|
+
if (args.includes("--support-array-length")) {
|
|
57
|
+
errorAndExit(`The --support-array-length flag has been renamed to "--array-length".`);
|
|
58
|
+
}
|
|
59
|
+
if (args.includes("-it")) {
|
|
60
|
+
errorAndExit(`The -it alias has been deprecated. Use "--immutable-types" instead.`);
|
|
61
|
+
}
|
|
62
|
+
if (args.includes("--redoc")) {
|
|
63
|
+
errorAndExit(`The --redoc config flag has been renamed to "--redocly" (or -c as shorthand).`);
|
|
64
|
+
}
|
|
65
|
+
if (args.includes("--root-types-no-schema-prefix") && !args.includes("--root-types")) {
|
|
66
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
67
|
+
console.warn("--root-types-no-schema-prefix has no effect without --root-types flag");
|
|
68
|
+
}
|
|
69
|
+
if (args.includes("--root-types-keep-casing") && !args.includes("--root-types")) {
|
|
70
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
71
|
+
console.warn("--root-types-keep-casing has no effect without --root-types flag");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const flags = parser(args, {
|
|
75
|
+
boolean: [
|
|
76
|
+
"additionalProperties",
|
|
77
|
+
"alphabetize",
|
|
78
|
+
"arrayLength",
|
|
79
|
+
"contentNever",
|
|
80
|
+
"defaultNonNullable",
|
|
81
|
+
"propertiesRequiredByDefault",
|
|
82
|
+
"emptyObjectsUnknown",
|
|
83
|
+
"enum",
|
|
84
|
+
"enumValues",
|
|
85
|
+
"conditionalEnums",
|
|
86
|
+
"dedupeEnums",
|
|
87
|
+
"check",
|
|
88
|
+
"excludeDeprecated",
|
|
89
|
+
"exportType",
|
|
90
|
+
"help",
|
|
91
|
+
"immutable",
|
|
92
|
+
"pathParamsAsTypes",
|
|
93
|
+
"rootTypes",
|
|
94
|
+
"rootTypesNoSchemaPrefix",
|
|
95
|
+
"rootTypesKeepCasing",
|
|
96
|
+
"makePathsEnum",
|
|
97
|
+
"generatePathParams",
|
|
98
|
+
"readWriteMarkers",
|
|
99
|
+
],
|
|
100
|
+
string: ["output", "redocly"],
|
|
101
|
+
alias: {
|
|
102
|
+
redocly: ["c"],
|
|
103
|
+
exportType: ["t"],
|
|
104
|
+
output: ["o"],
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Normalize the output path into a file URL.
|
|
110
|
+
* @param {string} output - The output path to be transformed.
|
|
111
|
+
* @returns {URL} The transformed file URL.
|
|
112
|
+
*/
|
|
113
|
+
function normalizeOutput(output) {
|
|
114
|
+
if (path.isAbsolute(output)) {
|
|
115
|
+
return new URL(`file://${output}`);
|
|
116
|
+
}
|
|
117
|
+
return new URL(output, CWD);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check if the generated types are up-to-date.
|
|
122
|
+
* @param {string} current - The current generated types.
|
|
123
|
+
* @param {URL} outputPath - The path to the output file.
|
|
124
|
+
*/
|
|
125
|
+
function checkStaleOutput(current, outputPath) {
|
|
126
|
+
if (flags.check) {
|
|
127
|
+
const previous = fs.readFileSync(outputPath, "utf8");
|
|
128
|
+
if (current === previous) {
|
|
129
|
+
process.exit(0);
|
|
130
|
+
} else {
|
|
131
|
+
error("Generated types are not up-to-date!");
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {string | URL} schema
|
|
139
|
+
* @param {@type import('@redocly/openapi-core').Config} redocly
|
|
140
|
+
*/
|
|
141
|
+
async function generateSchema(schema, { redocly, silent = false }) {
|
|
142
|
+
return `${COMMENT_HEADER}${astToString(
|
|
143
|
+
await openapiTS(schema, {
|
|
144
|
+
additionalProperties: flags.additionalProperties,
|
|
145
|
+
alphabetize: flags.alphabetize,
|
|
146
|
+
arrayLength: flags.arrayLength,
|
|
147
|
+
contentNever: flags.contentNever,
|
|
148
|
+
propertiesRequiredByDefault: flags.propertiesRequiredByDefault,
|
|
149
|
+
defaultNonNullable: flags.defaultNonNullable,
|
|
150
|
+
emptyObjectsUnknown: flags.emptyObjectsUnknown,
|
|
151
|
+
enum: flags.enum,
|
|
152
|
+
enumValues: flags.enumValues,
|
|
153
|
+
conditionalEnums: flags.conditionalEnums,
|
|
154
|
+
dedupeEnums: flags.dedupeEnums,
|
|
155
|
+
excludeDeprecated: flags.excludeDeprecated,
|
|
156
|
+
exportType: flags.exportType,
|
|
157
|
+
immutable: flags.immutable,
|
|
158
|
+
pathParamsAsTypes: flags.pathParamsAsTypes,
|
|
159
|
+
rootTypes: flags.rootTypes,
|
|
160
|
+
rootTypesNoSchemaPrefix: flags.rootTypesNoSchemaPrefix,
|
|
161
|
+
rootTypesKeepCasing: flags.rootTypesKeepCasing,
|
|
162
|
+
makePathsEnum: flags.makePathsEnum,
|
|
163
|
+
generatePathParams: flags.generatePathParams,
|
|
164
|
+
readWriteMarkers: flags.readWriteMarkers,
|
|
165
|
+
redocly,
|
|
166
|
+
silent,
|
|
167
|
+
}),
|
|
168
|
+
)}`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** pretty-format error message but also throw */
|
|
172
|
+
function errorAndExit(message) {
|
|
173
|
+
error(message);
|
|
174
|
+
throw new Error(message);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function done(input, output, time) {
|
|
178
|
+
// final console output
|
|
179
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
180
|
+
console.log(`🚀 ${c.green(`${input} → ${c.bold(output)}`)} ${c.dim(`[${formatTime(time)}]`)}`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function findRedocConfigPath() {
|
|
184
|
+
if (!flags.redocly) {
|
|
185
|
+
return findConfig();
|
|
186
|
+
}
|
|
187
|
+
const explicitPath = path.resolve(flags.redocly);
|
|
188
|
+
if (!fs.existsSync(explicitPath)) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
const stat = fs.statSync(explicitPath);
|
|
192
|
+
return stat.isDirectory() ? findConfig(explicitPath) : explicitPath;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function main() {
|
|
196
|
+
if ("help" in flags) {
|
|
197
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
198
|
+
console.info(HELP);
|
|
199
|
+
process.exit(0);
|
|
200
|
+
}
|
|
201
|
+
const packageJSON = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
202
|
+
if ("version" in flags) {
|
|
203
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
204
|
+
console.info(`v${packageJSON.version}`);
|
|
205
|
+
process.exit(0);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const outputType = flags.output ? OUTPUT_FILE : OUTPUT_STDOUT; // FILE or STDOU
|
|
209
|
+
if (outputType !== OUTPUT_STDOUT) {
|
|
210
|
+
// biome-ignore lint/suspicious/noConsole: this is a CLI
|
|
211
|
+
console.info(`✨ ${c.bold(`openapi-typescript ${packageJSON.version}`)}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const input = flags._[0];
|
|
215
|
+
|
|
216
|
+
const redocConfigPath = findRedocConfigPath();
|
|
217
|
+
if (flags.redocly && !redocConfigPath) {
|
|
218
|
+
errorAndExit(`Redocly config not found at: ${flags.redocly}`);
|
|
219
|
+
}
|
|
220
|
+
const redocly = redocConfigPath
|
|
221
|
+
? await loadConfig({ configPath: redocConfigPath })
|
|
222
|
+
: await createConfig({}, { extends: ["minimal"] });
|
|
223
|
+
|
|
224
|
+
// handle Redoc APIs
|
|
225
|
+
const hasRedoclyApis = Object.keys(redocly?.apis ?? {}).length > 0;
|
|
226
|
+
if (hasRedoclyApis) {
|
|
227
|
+
if (input) {
|
|
228
|
+
warn("APIs are specified both in Redocly Config and CLI argument. Only using Redocly config.");
|
|
229
|
+
}
|
|
230
|
+
await Promise.all(
|
|
231
|
+
Object.entries(redocly.apis).map(async ([name, api]) => {
|
|
232
|
+
let configRoot = CWD;
|
|
233
|
+
if (redocly.configFile) {
|
|
234
|
+
// note: this will be absolute if --redoc is passed; otherwise, relative
|
|
235
|
+
configRoot = path.isAbsolute(redocly.configFile)
|
|
236
|
+
? new URL(`file://${redocly.configFile}`)
|
|
237
|
+
: new URL(redocly.configFile, `file://${process.cwd()}/`);
|
|
238
|
+
}
|
|
239
|
+
if (!api[REDOC_CONFIG_KEY]?.output) {
|
|
240
|
+
errorAndExit(
|
|
241
|
+
`API ${name} is missing an \`${REDOC_CONFIG_KEY}.output\` key. See https://openapi-ts.dev/cli/#multiple-schemas.`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
const result = await generateSchema(new URL(api.root, configRoot), { redocly });
|
|
245
|
+
const outFile = new URL(api[REDOC_CONFIG_KEY].output, configRoot);
|
|
246
|
+
checkStaleOutput(result, outFile);
|
|
247
|
+
fs.mkdirSync(new URL(".", outFile), { recursive: true });
|
|
248
|
+
fs.writeFileSync(outFile, result, "utf8");
|
|
249
|
+
done(name, api[REDOC_CONFIG_KEY].output, performance.now() - timeStart);
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// handle stdin
|
|
255
|
+
else if (!input) {
|
|
256
|
+
const result = await generateSchema(process.stdin, {
|
|
257
|
+
redocly,
|
|
258
|
+
silent: outputType === OUTPUT_STDOUT,
|
|
259
|
+
});
|
|
260
|
+
if (outputType === OUTPUT_STDOUT) {
|
|
261
|
+
// if stdout, (still) don’t log anything to console!
|
|
262
|
+
process.stdout.write(result);
|
|
263
|
+
} else {
|
|
264
|
+
const outFile = normalizeOutput(flags.output);
|
|
265
|
+
checkStaleOutput(result, outFile);
|
|
266
|
+
fs.mkdirSync(new URL(".", outFile), { recursive: true });
|
|
267
|
+
fs.writeFileSync(outFile, result, "utf8");
|
|
268
|
+
done("stdin", flags.output, performance.now() - timeStart);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// handle single file
|
|
273
|
+
else {
|
|
274
|
+
// throw error on glob
|
|
275
|
+
if (input.includes("*")) {
|
|
276
|
+
errorAndExit(
|
|
277
|
+
"Globbing has been deprecated in favor of redocly.yaml’s `apis` keys. See https://openapi-ts.dev/cli/#multiple-schemas",
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const result = await generateSchema(new URL(input, CWD), {
|
|
281
|
+
redocly,
|
|
282
|
+
silent: outputType === OUTPUT_STDOUT,
|
|
283
|
+
});
|
|
284
|
+
if (outputType === OUTPUT_STDOUT) {
|
|
285
|
+
// if stdout, (still) don’t log anything to console!
|
|
286
|
+
process.stdout.write(result);
|
|
287
|
+
} else {
|
|
288
|
+
const outFile = normalizeOutput(flags.output);
|
|
289
|
+
checkStaleOutput(result, outFile);
|
|
290
|
+
fs.mkdirSync(new URL(".", outFile), { recursive: true });
|
|
291
|
+
fs.writeFileSync(outFile, result, "utf8");
|
|
292
|
+
done(input, flags.output, performance.now() - timeStart);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
main();
|
package/changes.json
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const node_perf_hooks = require('node:perf_hooks');
|
|
6
|
+
const openapiCore = require('@redocly/openapi-core');
|
|
7
|
+
const redoc = require('./lib/redoc.cjs');
|
|
8
|
+
const utils = require('./lib/utils.cjs');
|
|
9
|
+
const index = require('./transform/index.cjs');
|
|
10
|
+
const ts = require('./lib/ts.cjs');
|
|
11
|
+
const componentsObject = require('./transform/components-object.cjs');
|
|
12
|
+
const headerObject = require('./transform/header-object.cjs');
|
|
13
|
+
const mediaTypeObject = require('./transform/media-type-object.cjs');
|
|
14
|
+
const operationObject = require('./transform/operation-object.cjs');
|
|
15
|
+
const parameterObject = require('./transform/parameter-object.cjs');
|
|
16
|
+
const pathItemObject = require('./transform/path-item-object.cjs');
|
|
17
|
+
const pathsObject = require('./transform/paths-object.cjs');
|
|
18
|
+
const requestBodyObject = require('./transform/request-body-object.cjs');
|
|
19
|
+
const responseObject = require('./transform/response-object.cjs');
|
|
20
|
+
const responsesObject = require('./transform/responses-object.cjs');
|
|
21
|
+
const schemaObject = require('./transform/schema-object.cjs');
|
|
22
|
+
const c = require('ansi-colors');
|
|
23
|
+
|
|
24
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
25
|
+
|
|
26
|
+
const c__default = /*#__PURE__*/_interopDefaultCompat(c);
|
|
27
|
+
|
|
28
|
+
const COMMENT_HEADER = `/**
|
|
29
|
+
* This file was auto-generated by openapi-typescript.
|
|
30
|
+
* Do not make direct changes to the file.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
`;
|
|
34
|
+
async function openapiTS(source, options = {}) {
|
|
35
|
+
if (!source) {
|
|
36
|
+
throw new Error("Empty schema. Please specify a URL, file path, or Redocly Config");
|
|
37
|
+
}
|
|
38
|
+
const redoc$1 = options.redocly ?? await openapiCore.createConfig(
|
|
39
|
+
{
|
|
40
|
+
rules: {
|
|
41
|
+
"operation-operationId-unique": { severity: "error" }
|
|
42
|
+
// throw error on duplicate operationIDs
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
{ extends: ["minimal"] }
|
|
46
|
+
);
|
|
47
|
+
const schema = await redoc.validateAndBundle(source, {
|
|
48
|
+
redoc: redoc$1,
|
|
49
|
+
cwd: options.cwd instanceof URL ? options.cwd : new URL(`file://${options.cwd ?? process.cwd()}/`),
|
|
50
|
+
silent: options.silent ?? false
|
|
51
|
+
});
|
|
52
|
+
const ctx = {
|
|
53
|
+
additionalProperties: options.additionalProperties ?? false,
|
|
54
|
+
alphabetize: options.alphabetize ?? false,
|
|
55
|
+
arrayLength: options.arrayLength ?? false,
|
|
56
|
+
defaultNonNullable: options.defaultNonNullable ?? true,
|
|
57
|
+
discriminators: utils.scanDiscriminators(schema, options),
|
|
58
|
+
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
|
|
59
|
+
enum: options.enum ?? false,
|
|
60
|
+
enumValues: options.enumValues ?? false,
|
|
61
|
+
conditionalEnums: options.conditionalEnums ?? false,
|
|
62
|
+
dedupeEnums: options.dedupeEnums ?? false,
|
|
63
|
+
excludeDeprecated: options.excludeDeprecated ?? false,
|
|
64
|
+
exportType: options.exportType ?? false,
|
|
65
|
+
immutable: options.immutable ?? false,
|
|
66
|
+
rootTypes: options.rootTypes ?? false,
|
|
67
|
+
rootTypesNoSchemaPrefix: options.rootTypesNoSchemaPrefix ?? false,
|
|
68
|
+
rootTypesKeepCasing: options.rootTypesKeepCasing ?? false,
|
|
69
|
+
injectFooter: [],
|
|
70
|
+
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
|
|
71
|
+
postTransform: typeof options.postTransform === "function" ? options.postTransform : void 0,
|
|
72
|
+
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,
|
|
73
|
+
redoc: redoc$1,
|
|
74
|
+
silent: options.silent ?? false,
|
|
75
|
+
inject: options.inject ?? void 0,
|
|
76
|
+
transform: typeof options.transform === "function" ? options.transform : void 0,
|
|
77
|
+
transformProperty: typeof options.transformProperty === "function" ? options.transformProperty : void 0,
|
|
78
|
+
makePathsEnum: options.makePathsEnum ?? false,
|
|
79
|
+
generatePathParams: options.generatePathParams ?? false,
|
|
80
|
+
readWriteMarkers: options.readWriteMarkers ?? false,
|
|
81
|
+
resolve($ref) {
|
|
82
|
+
return utils.resolveRef(schema, $ref, { silent: options.silent ?? false });
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const transformT = node_perf_hooks.performance.now();
|
|
86
|
+
const result = index(schema, ctx);
|
|
87
|
+
utils.debug("Completed AST transformation for entire document", "ts", node_perf_hooks.performance.now() - transformT);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
exports.createDiscriminatorProperty = utils.createDiscriminatorProperty;
|
|
92
|
+
exports.createRef = utils.createRef;
|
|
93
|
+
exports.debug = utils.debug;
|
|
94
|
+
exports.error = utils.error;
|
|
95
|
+
exports.formatTime = utils.formatTime;
|
|
96
|
+
exports.getEntries = utils.getEntries;
|
|
97
|
+
exports.resolveRef = utils.resolveRef;
|
|
98
|
+
exports.scanDiscriminators = utils.scanDiscriminators;
|
|
99
|
+
exports.walk = utils.walk;
|
|
100
|
+
exports.warn = utils.warn;
|
|
101
|
+
exports.transformSchema = index;
|
|
102
|
+
exports.BOOLEAN = ts.BOOLEAN;
|
|
103
|
+
exports.FALSE = ts.FALSE;
|
|
104
|
+
exports.JS_ENUM_INVALID_CHARS_RE = ts.JS_ENUM_INVALID_CHARS_RE;
|
|
105
|
+
exports.JS_PROPERTY_INDEX_INVALID_CHARS_RE = ts.JS_PROPERTY_INDEX_INVALID_CHARS_RE;
|
|
106
|
+
exports.JS_PROPERTY_INDEX_RE = ts.JS_PROPERTY_INDEX_RE;
|
|
107
|
+
exports.NEVER = ts.NEVER;
|
|
108
|
+
exports.NULL = ts.NULL;
|
|
109
|
+
exports.NUMBER = ts.NUMBER;
|
|
110
|
+
exports.QUESTION_TOKEN = ts.QUESTION_TOKEN;
|
|
111
|
+
exports.SPECIAL_CHARACTER_MAP = ts.SPECIAL_CHARACTER_MAP;
|
|
112
|
+
exports.STRING = ts.STRING;
|
|
113
|
+
exports.TRUE = ts.TRUE;
|
|
114
|
+
exports.UNDEFINED = ts.UNDEFINED;
|
|
115
|
+
exports.UNKNOWN = ts.UNKNOWN;
|
|
116
|
+
exports.addJSDocComment = ts.addJSDocComment;
|
|
117
|
+
exports.astToString = ts.astToString;
|
|
118
|
+
exports.enumCache = ts.enumCache;
|
|
119
|
+
exports.oapiRef = ts.oapiRef;
|
|
120
|
+
exports.stringToAST = ts.stringToAST;
|
|
121
|
+
exports.tsArrayLiteralExpression = ts.tsArrayLiteralExpression;
|
|
122
|
+
exports.tsDedupe = ts.tsDedupe;
|
|
123
|
+
exports.tsEnum = ts.tsEnum;
|
|
124
|
+
exports.tsEnumMember = ts.tsEnumMember;
|
|
125
|
+
exports.tsIntersection = ts.tsIntersection;
|
|
126
|
+
exports.tsIsPrimitive = ts.tsIsPrimitive;
|
|
127
|
+
exports.tsLiteral = ts.tsLiteral;
|
|
128
|
+
exports.tsModifiers = ts.tsModifiers;
|
|
129
|
+
exports.tsNullable = ts.tsNullable;
|
|
130
|
+
exports.tsOmit = ts.tsOmit;
|
|
131
|
+
exports.tsPropertyIndex = ts.tsPropertyIndex;
|
|
132
|
+
exports.tsReadonlyArray = ts.tsReadonlyArray;
|
|
133
|
+
exports.tsRecord = ts.tsRecord;
|
|
134
|
+
exports.tsUnion = ts.tsUnion;
|
|
135
|
+
exports.tsWithRequired = ts.tsWithRequired;
|
|
136
|
+
exports.transformComponentsObject = componentsObject.default;
|
|
137
|
+
exports.transformHeaderObject = headerObject;
|
|
138
|
+
exports.transformMediaTypeObject = mediaTypeObject;
|
|
139
|
+
exports.injectOperationObject = operationObject.injectOperationObject;
|
|
140
|
+
exports.transformOperationObject = operationObject.default;
|
|
141
|
+
exports.transformParameterObject = parameterObject;
|
|
142
|
+
exports.transformPathItemObject = pathItemObject;
|
|
143
|
+
exports.transformPathsObject = pathsObject;
|
|
144
|
+
exports.transformRequestBodyObject = requestBodyObject;
|
|
145
|
+
exports.transformResponseObject = responseObject;
|
|
146
|
+
exports.transformResponsesObject = responsesObject;
|
|
147
|
+
exports.transformSchemaObject = schemaObject.default;
|
|
148
|
+
exports.transformSchemaObjectWithComposition = schemaObject.transformSchemaObjectWithComposition;
|
|
149
|
+
exports.c = c__default;
|
|
150
|
+
exports.COMMENT_HEADER = COMMENT_HEADER;
|
|
151
|
+
exports.default = openapiTS;
|
|
152
|
+
//# sourceMappingURL=index.cjs.map
|