Skip to content

Commit fbaf96d

Browse files
committed
Use Redocly APIs config
1 parent 312b7ba commit fbaf96d

Some content is hidden

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

62 files changed

+2375
-1999
lines changed

.changeset/rude-jokes-grin.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"openapi-typescript": major
33
---
44

5-
⚠️ **Breaking**: Drop auth/fetching options in favor of Redocly CLI’s
5+
⚠️ **Breaking**: Changing of several CLI flags and Node.js API options
66

7-
- The `auth`, `httpHeaders`, `httpMethod`, and `fetch` options were all removed from the CLI and Node.js API
8-
- To migrate, you’ll need to create a [redocly.yaml config](https://redocly.com/docs/cli/configuration/) that specifies your auth options [in the http setting](https://redocly.com/docs/cli/configuration/#resolve-non-public-or-non-remote-urls)
9-
- Worth noting your `redocly.yaml` config will be respected for any other related settings
7+
- The `--auth`, `--httpHeaders`, `--httpMethod`, and `fetch` (Node.js-only) options were all removed from the CLI and Node.js API
8+
- To migrate, you’ll need to create a [redocly.yaml config](https://redocly.com/docs/cli/configuration/) that specifies your auth options [in the http setting](https://redocly.com/docs/cli/configuration/#resolve-non-public-or-non-remote-urls)
9+
- You can also set your fetch client in redocly.yaml as well.
10+
- `--immutable-types` has been renamed to `--immutable`
11+
- `--support-array-length` has been renamed to `--array-length`

.changeset/shaggy-windows-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": major
3+
---
4+
5+
⚠️ **Breaking**: Remove globbing schemas in favor of `redocly.yaml` config. Specify multiple schemas with outputs in there instead. See [Multiple schemas](https://openapi-ts.pages.dev/docs/cli/#multiple-schemas) for more info.

.changeset/short-llamas-listen.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"openapi-typescript": minor
33
---
44

5-
**Feature:** allow configuration of schemas via `apis` key in redocly.config.yaml (see https://redocly.com/docs/cli/configuration/)
5+
**Feature:** allow configuration of schemas via `apis` key in redocly.config.yaml. [See docs](https://openapi-ts.pages.dev/cli/) for more info.

docs/package.json

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010
"update-contributors": "node scripts/update-contributors.js"
1111
},
1212
"dependencies": {
13-
"@algolia/client-search": "^4.19.1",
13+
"@algolia/client-search": "^4.20.0",
1414
"@astrojs/preact": "^2.2.2",
15-
"@astrojs/react": "^2.2.2",
16-
"@docsearch/css": "^3.5.1",
17-
"@docsearch/react": "^3.5.1",
18-
"@types/react": "^18.2.20",
19-
"@types/react-dom": "^18.2.7",
20-
"astro": "^2.10.9",
21-
"preact": "^10.17.0",
15+
"@astrojs/react": "^3.0.2",
16+
"@docsearch/css": "^3.5.2",
17+
"@docsearch/react": "^3.5.2",
18+
"@types/react": "^18.2.24",
19+
"@types/react-dom": "^18.2.8",
20+
"astro": "^3.2.2",
21+
"preact": "^10.18.1",
2222
"react": "^18.2.0",
2323
"react-dom": "^18.2.0",
24-
"sass": "^1.65.1"
24+
"sass": "^1.68.0"
2525
},
2626
"devDependencies": {
2727
"@astrojs/sitemap": "^2.0.2",
28-
"@cobalt-ui/cli": "^1.4.1",
29-
"@cobalt-ui/plugin-sass": "^1.2.3",
30-
"@types/node": "^20.5.0",
28+
"@cobalt-ui/cli": "^1.6.0",
29+
"@cobalt-ui/plugin-sass": "^1.3.0",
30+
"@types/node": "^20.8.2",
3131
"html-escaper": "^3.0.3",
3232
"typescript": "^5.2.2",
33-
"vite-plugin-sass-dts": "^1.3.9"
33+
"vite-plugin-sass-dts": "^1.3.11"
3434
}
3535
}

docs/src/content/docs/about.md

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ description: Additional info about this project
2424

2525
1. Support converting any valid OpenAPI schema to TypeScript types, no matter how complicated.
2626
1. Generated types should be statically-analyzable and runtime-free (with minor exceptions like <a href="https://www.typescriptlang.org/docs/handbook/enums.html" target="_blank" rel="noopener noreferrer">enums</a>).
27-
1. Don’t validate schemas; there are existing libraries for that like <a href="https://redocly.com/docs/cli/commands/lint/" target="_blank" rel="noopener noreferrer">Redocly</a>.
2827
1. Generated types should match your original schema as closely as possible, preserving original capitalization, etc.
2928
1. Typegen only needs Node.js to run (no Java, Python, etc.) and works in any environment.
3029
1. Support fetching OpenAPI schemas from files as well as local and remote servers.

docs/src/content/docs/advanced.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ $ DEBUG=openapi-ts:* npx openapi-typescript schema.yaml -o my-types.ts
185185

186186
To only see certain types of debug messages, you can set `DEBUG=openapi-ts:[scope]` instead. Valid scopes are `redoc`, `lint`, `bundle`, and `ts`.
187187

188-
Note that debug messages will be suppressed if using the CLI and outputting via `stdout`.
188+
Note that debug messages will be suppressed if the output is `stdout`.
189189

190190
## Tips
191191

docs/src/content/docs/cli.md

+73-36
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,97 @@ title: CLI
33
description: openapi-typescript CLI usage
44
---
55

6-
The CLI is the most common way to use openapi-typescript. The CLI can parse JSON and YAML (via <a href="https://www.npmjs.com/package/js-yaml" target="_blank" rel="noopener noreferrer">js-yaml</a>). It can parse local and remote schemas (and even supports basic auth).
6+
The CLI is the most common way to use openapi-typescript. The CLI can parse JSON and YAML, and even validates your schemas using the [Redocly CLI](https://redocly.com/docs/cli/commands/lint/). It can parse local and remote schemas (and even supports basic auth).
77

8-
## Reading schemas
8+
## Transforming an OpenAPI schema to TypeScript
9+
10+
### Single schema
11+
12+
The simplest way to transform schemas is by specifying an input schema (JSON or YAML), followed by `--output` (`-o`) where you’d like the output to be saved:
913

1014
```bash
1115
npx openapi-typescript schema.yaml -o schema.ts
1216

13-
# 🚀 schema.yaml -> schema.ts [7ms]
17+
# 🚀 schema.yaml -> schema.ts [50ms]
1418
```
1519

16-
### Globbing local schemas
17-
1820
```bash
19-
npx openapi-typescript "specs/**/*.yaml" -o schemas/
21+
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml -o petstore.d.ts
2022

21-
# 🚀 specs/one.yaml -> schemas/specs/one.ts [7ms]
22-
# 🚀 specs/two.yaml -> schemas/specs/two.ts [7ms]
23-
# 🚀 specs/three.yaml -> schemas/specs/three.ts [7ms]
23+
# 🚀 https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [250ms]
2424
```
2525

26-
_Thanks, [@sharmarajdaksh](https://github.com/sharmarajdaksh)!_
26+
### Multiple schemas
2727

28-
### Remote schemas
28+
To transform multiple schemas, create a `redocly.yaml` file in the root of your project with [APIs defined](https://redocly.com/docs/cli/configuration/). Under `apis`, give each schema a unique name and optionally a version (the name doesn’t matter, so long as it’s unique). Set the `root` value to your schema’s entry point—this will act as the main input. For the output, set it with `openapi-ts.output`:
29+
30+
```yaml
31+
apis:
32+
core@v2:
33+
root: ./openapi/openapi.yaml
34+
openapi-ts:
35+
output: ./openapi/openapi.ts
36+
external@v1:
37+
root: ./openapi/external.yaml
38+
openapi-ts:
39+
output: ./openapi/openapi.ts
40+
```
41+
42+
Whenver you have a `redocly.yaml` file in your project with `apis`, you can omit the input/output parameters in the CLI:
2943

3044
```bash
31-
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml -o petstore.d.ts
45+
npx openapi-typescript
46+
```
3247

33-
# 🚀 https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [250ms]
48+
> ⚠️ In previous versions globbing was supported, but that has been **deprecated** in v7 in favor of `redocly.yaml`. You’ll be able to control per-schema output locations better, as well as getting unique per-schema settings.
49+
50+
## Redoc config
51+
52+
A `redocly.yaml` file isn’t required to use openapi-typescript. By default it extends the `"minimal"` built-in config. But it is recommended if you want to have custom validation rules (or build types for [multiple schemas](#multiple-schemas)). The CLI will try to automatically find a `redocly.yaml` in the root of your project, but you can also provide its location with the `--redoc` flag:
53+
54+
```bash
55+
npx openapi-typescript --redoc ./path/to/redocly.yaml
56+
```
57+
58+
You can read more about the Redoc’s configuration options [in their docs](https://redocly.com/docs/cli/configuration/).
59+
60+
## Auth
61+
62+
Authentication for non-public schemas is handled in your [Redocly config](https://redocly.com/docs/cli/configuration/#resolve-non-public-or-non-remote-urls). You can add headers and basic authentication like so:
63+
64+
```yaml
65+
resolve:
66+
http:
67+
headers:
68+
- matches: https://api.example.com/v2/**
69+
name: X-API-KEY
70+
envVariable: SECRET_KEY
71+
- matches: https://example.com/*/test.yaml
72+
name: Authorization
73+
envVariable: SECRET_AUTH
3474
```
3575

36-
_Thanks, [@psmyrdek](https://github.com/psmyrdek)!_
76+
Refer to the [Redocly docs](https://redocly.com/docs/cli/configuration/#resolve-non-public-or-non-remote-urls) for additional options.
3777

3878
## Options
3979

40-
| Option | Alias | Default | Description |
41-
| :------------------------ | :---- | :------: | :--------------------------------------------------------------------------------------------------------------------------- |
42-
| `--help` | | | Display inline help message and exit |
43-
| `--version` | | | Display this library’s version and exit |
44-
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
45-
| `--auth [token]` | | | Provide an auth token to be passed along in the request (only if accessing a private schema) |
46-
| `--header` | `-x` | | Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the `key: value` pattern |
47-
| `--headers-object="{…}"` | `-h` | | Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over `--header` |
48-
| `--http-method` | `-m` | `GET` | Provide the HTTP Verb/Method for fetching a schema from a remote URL |
49-
| `--immutable-types` | | `false` | Generates immutable types (readonly properties and readonly array) |
50-
| `--additional-properties` | | `false` | Allow arbitrary properties for all schema objects without `additionalProperties: false` |
51-
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
52-
| `--default-non-nullable` | | `false` | Treat schema objects with default values as non-nullable |
53-
| `--export-type` | `-t` | `false` | Export `type` instead of `interface` |
54-
| `--path-params-as-types` | | `false` | Allow dynamic string lookups on the `paths` object |
55-
| `--support-array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
56-
| `--alphabetize` | | `false` | Sort types alphabetically |
57-
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
58-
59-
### `--path-params-as-types`
80+
| Option | Alias | Default | Description |
81+
| :------------------------ | :---- | :------: | :------------------------------------------------------------------------------------------------------------------ |
82+
| `--help` | | | Display inline help message and exit |
83+
| `--version` | | | Display this library’s version and exit |
84+
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
85+
| `--redoc [location]` | | | Path to a `redocly.yaml` file (see [Multiple schemas](#multiple-schemas)) |
86+
| `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) |
87+
| `--additional-properties` | | `false` | Allow arbitrary properties for all schema objects without `additionalProperties: false` |
88+
| `--empty-objects-unknown` | | `false` | Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties` |
89+
| `--default-non-nullable` | | `false` | Treat schema objects with default values as non-nullable |
90+
| `--export-type` | `-t` | `false` | Export `type` instead of `interface` |
91+
| `--path-params-as-types` | | `false` | Allow dynamic string lookups on the `paths` object |
92+
| `--array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` |
93+
| `--alphabetize` | | `false` | Sort types alphabetically |
94+
| `--exclude-deprecated` | | `false` | Exclude deprecated fields from types |
95+
96+
### pathParamsAsTypes flag
6097

6198
By default, your URLs are preserved exactly as-written in your schema:
6299

@@ -88,7 +125,7 @@ Though this is a contrived example, you could use this feature to automatically
88125

89126
_Thanks, [@Powell-v2](https://github.com/Powell-v2)!_
90127

91-
### `--support-array-length`
128+
### arrayLength flag
92129

93130
This option is useful for generating tuples if an array type specifies `minItems` or `maxItems`.
94131

@@ -105,7 +142,7 @@ components:
105142
maxItems: 2
106143
```
107144
108-
Enabling `--support-array-length` would change the typing like so:
145+
Enabling `--array-length` would change the typing like so:
109146

110147
```diff
111148
export interface components {

docs/src/content/docs/introduction.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ npm i -D openapi-typescript
3434
3535
## Basic usage
3636

37-
First, generate a local type file by running `npx openapi-typescript`:
37+
First, generate a local type file by running `npx openapi-typescript`, first specifying your input schema (JSON or YAML), and where you’d like the `--output` (`-o`) to be saved:
3838

3939
```bash
4040
# Local schema
@@ -46,9 +46,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
4646
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]
4747
```
4848

49-
> ⚠️ Be sure to <a href="https://redocly.com/docs/cli/commands/lint/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
50-
51-
Then, import schemas from the generated file like so:
49+
Then in your TypeScript project, import types where needed:
5250

5351
```ts
5452
import { paths, components } from "./path/to/my/schema"; // <- generated by openapi-typescript
@@ -60,8 +58,10 @@ type MyType = components["schemas"]["MyType"];
6058
type EndpointParams = paths["/my/endpoint"]["parameters"];
6159

6260
// Response obj
63-
type SuccessResponse = paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
64-
type ErrorResponse = paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
61+
type SuccessResponse =
62+
paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
63+
type ErrorResponse =
64+
paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
6565
```
6666

6767
From here, you can use these types for any of the following (but not limited to):

0 commit comments

Comments
 (0)