Skip to content

Commit caf4a1d

Browse files
committed
Update examples, update validator recommendation
1 parent 17e2ab1 commit caf4a1d

File tree

16 files changed

+2017
-496
lines changed

16 files changed

+2017
-496
lines changed

docs/src/content/docs/about.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ description: Additional info about this project
2323
## Project goals
2424

2525
1. Support converting any valid OpenAPI schema to TypeScript types, no matter how complicated.
26+
1. Generate **runtime-free types** for maximum performance.
2627
1. This library does **NOT** validate your schema, there are other libraries for that.
2728
1. The generated TypeScript types **must** match your schema as closely as possible (e.g. no renaming to `PascalCase`)
2829
1. This library should never require Java, node-gyp, or some other complex environment to work. This should require Node.js and nothing else.

docs/src/content/docs/introduction.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ The code is <a href="https://github.com/drwpow/openapi-typescript/blob/main/pack
1212
## Features
1313

1414
- ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like <a href="https://spec.openapis.org/oas/v3.1.0#discriminator-object" target="_blank" rel="noopener noreferrer">discriminators</a>)
15-
- ✅ Supports YAML and JSON
16-
- ✅ Supports loading via remote URL (simple authentication supported with the `--auth` flag)
17-
- ✅ Supports remote references: `$ref: "external.yaml#components/schemas/User"`
18-
- ✅ Fetches remote schemas quickly using <a href="https://www.npmjs.com/package/undici" target="_blank" rel="noopener noreferrer">undici</a>
15+
- ✅ Generate **runtime-free types** that outperform old-school codegen
16+
- ✅ Load schemas from YAML or JSON, locally or remotely
17+
- ✅ Native Node.js code is fast and generates types within milliseconds
1918

2019
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_
2120

@@ -45,7 +44,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
4544
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]
4645
```
4746

48-
> ⚠️ Be sure to <a href="(https://apitools.dev/swagger-cli/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
47+
> ⚠️ 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.
4948
5049
Then, import schemas from the generated file like so:
5150

docs/src/content/docs/openapi-fetch/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Next, generate TypeScript types from your OpenAPI schema using openapi-typescrip
6161
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts
6262
```
6363

64-
> ⚠️ Be sure to <a href="(https://apitools.dev/swagger-cli/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
64+
> ⚠️ 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.
6565
6666
Lastly, be sure to **run typechecking** in your project. This can be done by adding `tsc --noEmit` to your <a href="https://docs.npmjs.com/cli/v9/using-npm/scripts" target="_blank" rel="noopener noreferrer">npm scripts</a> like so:
6767

packages/openapi-fetch/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Next, generate TypeScript types from your OpenAPI schema using openapi-typescrip
5656
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts
5757
```
5858

59-
> ⚠️ Be sure to <a href="(https://apitools.dev/swagger-cli/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas.
59+
> ⚠️ 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.
6060
6161
Lastly, be sure to **run typechecking** in your project. This can be done by adding `tsc --noEmit` to your <a href="https://docs.npmjs.com/cli/v9/using-npm/scripts" target="_blank" rel="noopener noreferrer">npm scripts</a> like so:
6262

packages/openapi-typescript/README.md

+30-36
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,48 @@ The code is [MIT-licensed](./LICENSE) and free for use.
66

77
**Features**
88

9-
- ✅ Supports YAML and JSON formats
10-
- ✅ Supports advanced OpenAPI 3.1 features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object)
11-
- ✅ Supports loading via remote URL (simple authentication supported with the `--auth` flag)
12-
- ✅ Supports remote references: `$ref: "external.yaml#components/schemas/User"`
13-
- ✅ Fetches remote schemas quickly using [undici](https://www.npmjs.com/package/undici)
9+
- ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like <a href="https://spec.openapis.org/oas/v3.1.0#discriminator-object" target="_blank" rel="noopener noreferrer">discriminators</a>)
10+
- ✅ Generate **runtime-free types** that outperform old-school codegen
11+
- ✅ Load schemas from YAML or JSON, locally or remotely
12+
- ✅ Native Node.js code is fast and generates types within milliseconds
1413

1514
**Examples**
1615

1716
👀 [See examples](./examples/)
1817

1918
## Usage
2019

21-
Note:️ openapiTS requires **VALID** OpenAPI 3.x schemas to work, and this library does not handle validation. There are several quality tools that handle this like [@apidevtools/swagger-parser](https://www.npmjs.com/package/@apidevtools/swagger-parser). Make sure to validate your schemas first!
20+
First, generate a local type file by running `npx openapi-typescript`:
2221

23-
### 🖥️ CLI
22+
```bash
23+
# Local schema
24+
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
25+
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]
2426

25-
#### 🗄️ Reading schemas
27+
# Remote schema
28+
npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts
29+
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]
30+
```
2631

27-
```bash
28-
npx openapi-typescript schema.yaml --output schema.ts
32+
> ⚠️ 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.
33+
34+
Then, import schemas from the generated file like so:
35+
36+
```ts
37+
import { paths, components } from "./path/to/my/schema"; // <- generated by openapi-typescript
2938

30-
# 🚀 schema.yaml -> schema.ts [7ms]
39+
// Schema Obj
40+
type MyType = components["schemas"]["MyType"];
41+
42+
// Path params
43+
type EndpointParams = paths["/my/endpoint"]["parameters"];
44+
45+
// Response obj
46+
type SuccessResponse = paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
47+
type ErrorResponse = paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
3148
```
3249

33-
##### 🦠 Globbing local schemas
50+
#### 🦠 Globbing local schemas
3451

3552
```bash
3653
npx openapi-typescript "specs/**/*.yaml" --output schemas/
@@ -52,30 +69,6 @@ npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml --output
5269

5370
_Thanks, [@psmyrdek](https://github.com/psmyrdek)!_
5471

55-
#### 🟦 Using in TypeScript
56-
57-
Import any top-level item from the generated spec to use it. It works best if you also alias types to save on typing:
58-
59-
```ts
60-
import { components } from "./generated-schema.ts";
61-
62-
type APIResponse = components["schemas"]["APIResponse"];
63-
```
64-
65-
Because OpenAPI schemas may have invalid TypeScript characters as names, the square brackets are a safe way to access every property.
66-
67-
##### 🏗️ Operations
68-
69-
Operations can be imported directly by their [operationId](https://spec.openapis.org/oas/latest.html#operation-object):
70-
71-
```ts
72-
import { operations } from "./generated-schema.ts";
73-
74-
type getUsersById = operations["getUsersById"];
75-
```
76-
77-
_Thanks, [@gr2m](https://github.com/gr2m)!_
78-
7972
#### ⚾ Fetching data
8073

8174
Fetching data can be done simply and safely using an **automatically-typed fetch wrapper**:
@@ -268,6 +261,7 @@ There are many other uses for this besides checking `format`. Because this must
268261
## 🏅 Project Goals
269262

270263
1. Support converting any valid OpenAPI schema to TypeScript types, no matter how complicated.
264+
1. Generate **runtime-free types** for maximum performance.
271265
1. This library does **NOT** validate your schema, there are other libraries for that.
272266
1. The generated TypeScript types **must** match your schema as closely as possible (e.g. no renaming to `PascalCase`)
273267
1. This library should never require Java, node-gyp, or some other complex environment to work. This should require Node.js and nothing else.

packages/openapi-typescript/examples/digital-ocean-api.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -6162,7 +6162,33 @@ export interface external {
61626162
redis_acl_channels_default?: "allchannels" | "resetchannels";
61636163
}
61646164
"resources/databases/models/source_database.yml": {
6165-
source?: external["resources/databases/models/database_connection.yml"];
6165+
source?: {
6166+
/**
6167+
* @description The FQDN pointing to the database cluster's current primary node.
6168+
* @example backend-do-user-19081923-0.db.ondigitalocean.com
6169+
*/
6170+
host?: string;
6171+
/**
6172+
* @description The port on which the database cluster is listening.
6173+
* @example 25060
6174+
*/
6175+
port?: number;
6176+
/**
6177+
* @description The name of the default database.
6178+
* @example defaultdb
6179+
*/
6180+
dbname?: string;
6181+
/**
6182+
* @description The default user for the database.
6183+
* @example doadmin
6184+
*/
6185+
username?: string;
6186+
/**
6187+
* @description The randomly generated password for the default user.
6188+
* @example wv78n3zpz42xezdk
6189+
*/
6190+
password?: string;
6191+
};
61666192
/**
61676193
* @description Enables SSL encryption when connecting to the source database.
61686194
* @example false
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
lang: cURL
22
source: |-
3-
curl -X PUT \
3+
curl -X GET \
44
-H "Content-Type: application/json" \
55
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
6-
-d '{"source":{"host":"source-do-user-6607903-0.b.db.ondigitalocean.com","dbname":"defaultdb","port":25060,"username":"doadmin","password":"paakjnfe10rsrsmf"},"disable_ssl":false}' \
76
"https://api.digitalocean.com/v2/databases/9cc10173-e9ea-4176-9dbc-a4cee4c4ff30/online-migration"

packages/openapi-typescript/examples/digital-ocean-api/resources/databases/models/source_database.yml

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@ type: object
22

33
properties:
44
source:
5-
allOf:
6-
- $ref: './database_connection.yml'
5+
type: object
6+
properties:
7+
host:
8+
type: string
9+
description: The FQDN pointing to the database cluster's current primary node.
10+
example: backend-do-user-19081923-0.db.ondigitalocean.com
11+
port:
12+
type: integer
13+
description: The port on which the database cluster is listening.
14+
example: 25060
15+
dbname:
16+
type: string
17+
description: The name of the default database.
18+
example: defaultdb
19+
username:
20+
type: string
21+
description: The default user for the database.
22+
example: doadmin
23+
password:
24+
type: string
25+
description: The randomly generated password for the default user.
26+
example: wv78n3zpz42xezdk
727
disable_ssl:
828
type: boolean
929
description: Enables SSL encryption when connecting to the source database.

0 commit comments

Comments
 (0)