Skip to content

Commit d500d9a

Browse files
committed
Update docs
1 parent 0a5daf2 commit d500d9a

File tree

3 files changed

+79
-97
lines changed

3 files changed

+79
-97
lines changed

CONTRIBUTING.md

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,100 @@
11
# Contributing
22

3-
Thanks for being willing to contribute!
3+
Thanks for being willing to contribute! 🙏
44

5-
**Working on your first Pull Request?** You can learn how from this _free_ series [How to Contribute to an Open Source
6-
Project on GitHub][egghead]
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://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github)
76

8-
## Project setup
7+
## Open issues
8+
9+
Please check out the [the open issues](https://github.com/drwpow/openapi-typescript/issues). Issues labelled [**Help Wanted**](https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [**Good First Issue**](https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) are especially good to help with.
10+
11+
Contributing doesn’t have to be in code! Simply answering questions in open issues, or providing workarounds, is just as important a contribution as making pull requests.
12+
13+
## Setup
14+
15+
### Dependencies
916

1017
1. Install [pnpm](https://pnpm.io/)
1118
2. Fork and clone the repo
1219
3. Run `pnpm i` to install dependencies
1320
4. Create a branch for your PR with `git checkout -b pr/your-branch-name`
1421

15-
It’s also recommended you have [ESLint][eslint] installed and set up correctly. You may also run the command
16-
`npm run lint` to see lint errors.
22+
### VS Code setup
1723

18-
> Tip: Keep your `main` branch pointing at the original repository and make pull requests from branches on your fork. To
19-
> do this, run:
20-
>
21-
> ```
22-
> git remote add upstream https://github.com/drwpow/openapi-typescript.git
23-
> git fetch upstream
24-
> git branch --set-upstream-to=upstream/main main
25-
> ```
26-
>
27-
> This will add the original repository as a "remote" called "upstream," Then fetch the git information from that
28-
> remote, then set your local `main` branch to use the upstream main branch whenever you run `git pull`. Then you can
29-
> make all of your pull request branches based on this `main` branch. Whenever you want to update your version of
30-
> `main`, do a regular `git pull`.
24+
If using VS Code, the following extensions are recommended (or their equivalent extensions if using another editor)
3125

32-
## Committing and pushing changes
26+
- [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
3327

34-
Please make sure to run the tests (`npm test`) before you commit your changes.
28+
## Workflow
3529

36-
### Local schemas
30+
When working locally, run:
3731

38-
This repo supports OpenAPI 2.0 and 3.0. There are some real-world examples located in `tests/v2/specs/*.yaml` and
39-
`tests/v3/specs/*.yaml`, respectively. Testing large, real schemas was a major goal of this project. Many libraries only
40-
test the “Petstore” example from Swagger, which is contrived and is missing much complexity from companies’ production
41-
schemas.
32+
```bash
33+
npm run dev
34+
```
4235

43-
_Note: don’t update the `yaml` schemas with your own custom additions (but if the official versions have updated, then
44-
it’s fine to update them here). If you’d like to add your schema for testing, then please add them as a new schema, and
45-
add them to `./expected/*.ts`._
36+
This will compile the code as you change automatically.
4637

47-
#### Regenerating schemas
38+
### TDD
4839

49-
If you’ve added a feature or fixed a bug and need to update the generated schemas, run the following:
40+
This library is a great usecase for [test-driven development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development). If you’re new to this, the basic workflow is:
5041

51-
```
52-
# 1. re-build the package
53-
npm run build
54-
# 2. run the local CLI (not the npm one!)
55-
./bin/cli.js tests/v3/specs/github.yaml -o tests/v3/expected/github.ts
56-
# NOTE: on Windows, try running the script on WSL if getting errors
57-
```
42+
1. First, write a [test](#testing) that fully outlines what you’d _like_ the output to be.
43+
2. Make sure this test **fails** when you run `npm test` (yes, _fails!_)
44+
3. Then, make changes to `src/` until the tests pass.
45+
46+
_Code generation is hard!_ And for that reason, starting with a very clear expectation of your end-goal can make working easier.
47+
48+
### Unit tests or snapshot tests?
49+
50+
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:
51+
52+
- Fixing bugs that deal with multiple schemas with remote `$ref`s
53+
- Fixing Node.js or OS-related bugs
54+
- Adding a CLI option that changes the entire output
55+
56+
For most PRs, **snapshot tests can be avoided.** But for scenarios similar to the ones mentioned, they can ensure everything is working as expected.
5857

59-
This should update the expected TypeScript definiton.
58+
### Opening a PR
6059

61-
_Also if this appears in `examples/` feel free to update that, too!_
60+
When opening a pull request, make sure all of the following is done:
61+
62+
- [x] Tests are added
63+
- [x] Build passes (`npm run build`)
64+
- [x] Tests pass (`npm test`)
65+
- [x] Linting passes (`npm run lint`)
6266

6367
## Testing
6468

65-
Tests use [Vitest](https://vitest.dev), a modern test-runner based on Jest. To run the tests locally, run:
69+
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.
6670

67-
```
71+
### Running tests
72+
73+
💡 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)!
74+
75+
To run the entire test suite once, run:
76+
77+
78+
```bash
6879
npm test
6980
```
7081

7182
To run an individual test:
7283

73-
```
74-
npx vitest [part of filename]
84+
```bash
85+
npx vitest [partial filename]
7586
```
7687

77-
Or to start all tests in watch mode:
88+
To start the entire test suite in watch mode:
7889

79-
```
90+
```bash
8091
npx vitest
8192
```
8293

83-
[See docs](https://vitest.dev)
84-
85-
## Help needed
94+
### Running linting
8695

87-
Please check out the [the open issues][issues]. Issues labelled [**Help Wanted**][help-wanted] and [**Good First
88-
Issue**][good-first-issue] are especially good to help with.
96+
To run ESLint on the project:
8997

90-
Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks!
91-
92-
[all-contributors]: https://github.com/all-contributors/all-contributors
93-
[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
94-
[eslint]: https://eslint.org/
95-
[good-first-issue]:
96-
https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22
97-
[help-wanted]: https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22
98-
[issues]: https://github.com/drwpow/openapi-typescript/issues
98+
```bash
99+
npm run lint
100+
```

README.md

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,18 @@
88

99
# 📘️ openapi-typescript
1010

11-
🚀 Convert [OpenAPI 3.0][openapi3] and [2.0 (Swagger)][openapi2] schemas to TypeScript interfaces using Node.js.
11+
🚀 Convert [OpenAPI](https://spec.openapis.org/oas/latest.html) schemas to TypeScript interfaces painlessly using pure Node.js. No Java, node-gyp, or running OpenAPI servers necessary. Uses [Prettier](https://npmjs.com/prettier) to format the output.
1212

1313
**Features**
1414

15-
-[OpenAPI 3.0][openapi3]
16-
-[Swagger 2.0][openapi2]
17-
- ✅ Supports YAML and JSON schema formats
15+
- ✅ Supports YAML and JSON
1816
- ✅ Supports loading via remote URL (simple authentication supported with the `--auth` flag)
1917
- ✅ Supports remote references: `$ref: "external.yaml#components/schemas/User"`
20-
- ✅ Formats using [Prettier][prettier]
21-
- ✅ TypeScript 4.0 features
18+
- ✅ Prettier formatting is fully customizable to match your existing code style.
2219

2320
**Examples**
2421

25-
- [Stripe, OpenAPI 2.0](./examples/stripe-openapi2.ts)
26-
- [Stripe, OpenAPI 3.0](./examples/stripe-openapi3.ts)
22+
[See examples](./examples/)
2723

2824
## Usage
2925

@@ -54,14 +50,12 @@ _Thanks to [@sharmarajdaksh](https://github.com/sharmarajdaksh) for the glob fea
5450
#### ☁️ Reading specs from remote resource
5551

5652
```bash
57-
npx openapi-typescript https://petstore.swagger.io/v2/swagger.json --output petstore.ts
53+
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml --output petstore.d.ts
5854

59-
# 🔭 Loading spec from https://petstore.swagger.io/v2/swagger.json
60-
# 🚀 https://petstore.swagger.io/v2/swagger.json -> petstore.ts [650ms]
55+
# 🔭 Loading spec from https://petstore3.swagger.io/api/v3/openapi.yaml
56+
# 🚀 https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [650ms]
6157
```
6258

63-
_Note: globbing doesn’t work for remote schemas because there is no reliable way to determine a list of files to select from a remote file system._
64-
6559
_Thanks to [@psmyrdek](https://github.com/psmyrdek) for the remote spec feature!_
6660

6761
#### Using in TypeScript
@@ -76,16 +70,16 @@ type APIResponse = components["schemas"]["APIResponse"];
7670

7771
Because OpenAPI schemas may have invalid TypeScript characters as names, the square brackets are a safe way to access every property.
7872

79-
Also note that there’s a special `operations` interface that you can import `OperationObjects` by their [operationId][openapi-operationid]:
73+
##### Operations
74+
75+
Operations can be imported directly by their [operationId](https://spec.openapis.org/oas/latest.html#operation-object):
8076

8177
```ts
8278
import { operations } from "./generated-schema.ts";
8379

8480
type getUsersById = operations["getUsersById"];
8581
```
8682

87-
Even though `operations` isn’t present in your original schema, it’s a simple convenience and won’t disrupt any of your other types.
88-
8983
_Thanks to [@gr2m](https://github.com/gr2m) for the operations feature!_
9084

9185
#### openapi-typescript-fetch
@@ -94,7 +88,6 @@ The generated spec can also be used with [openapi-typescript-fetch](https://www.
9488

9589
```ts
9690
import { paths } from "./petstore";
97-
9891
import { Fetcher } from "openapi-typescript-fetch";
9992

10093
// declare fetcher for paths
@@ -163,7 +156,6 @@ npx openapi-typescript schema.yaml
163156
| `--make-paths-enum` | `-pe` | `false` | (optional) Generate an enum of endpoint paths |
164157
| `--path-params-as-types` | | `false` | (optional) Substitute path parameter names with their respective types |
165158
| `--raw-schema` | | `false` | Generate TS types from partial schema (e.g. having `components.schema` at the top level) |
166-
| `--version` | | | Force OpenAPI version with `--version 3` or `--version 2` (required for `--raw-schema` when version is unknown) |
167159

168160
### 🐢 Node
169161

@@ -172,7 +164,7 @@ npm i --save-dev openapi-typescript
172164
```
173165

174166
```js
175-
import fs from "fs";
167+
import fs from "node:fs";
176168
import openapiTS from "openapi-typescript";
177169

178170
// example 1: load [object] as schema (JSON only)
@@ -187,9 +179,7 @@ const output = await openapiTS(localPath);
187179
const output = await openapiTS("https://myurl.com/v1/openapi.yaml");
188180
```
189181
190-
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. Pass in either a JSON-friendly object to load a schema from memory, or a string to load a schema from a local file or remote URL (it will load the file quickly using built-in Node methods). Note that a YAML string isn’t supported in the Node.js API; either use the CLI or convert to JSON using [js-yaml][js-yaml] first.
191-
192-
⚠️ As of `v4.0`, `openapiTS()` is an async function.
182+
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. Pass in either a JSON-friendly object to load a schema from memory, or a string to load a schema from a local file or remote URL (it will load the file quickly using built-in Node methods). Note that a YAML string isn’t supported in the Node.js API; either use the CLI or convert to JSON using [js-yaml](https://www.npmjs.com/package/js-yaml) first.
193183
194184
#### Custom Formatter
195185
@@ -222,26 +212,15 @@ _Note: you don’t have to use `.format`—this is just an example! You can use
222212
223213
## 🏅 Project Goals
224214
225-
1. Support converting any OpenAPI 3.0 or 2.0 (Swagger) schema to TypeScript types, no matter how complicated
215+
1. Support converting any valid OpenAPI schema to TypeScript types, no matter how complicated.
216+
1. This library does **NOT** validate your schema, there are other libraries for that.
226217
1. The generated TypeScript types **must** match your schema as closely as possible (i.e. don’t convert names to
227218
`PascalCase` or follow any TypeScript-isms; faithfully reproduce your schema as closely as possible, capitalization
228219
and all)
229-
1. This library is a TypeScript generator, not a schema validator.
230220
231221
## 🤝 Contributing
232222
233-
PRs are welcome! Please see our [CONTRIBUTING.md](./CONTRIBUTING.md) guide. Opening an issue beforehand to discuss is
234-
encouraged but not required.
235-
236-
[glob]: https://www.npmjs.com/package/glob
237-
[js-yaml]: https://www.npmjs.com/package/js-yaml
238-
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
239-
[npm-run-all]: https://www.npmjs.com/package/npm-run-all
240-
[openapi-format]: https://swagger.io/specification/#data-types
241-
[openapi-operationid]: https://swagger.io/specification/#operation-object
242-
[openapi2]: https://swagger.io/specification/v2/
243-
[openapi3]: https://swagger.io/specification
244-
[prettier]: https://npmjs.com/prettier
223+
PRs are welcome! Please see our [CONTRIBUTING.md](./CONTRIBUTING.md) guide.
245224
246225
### Contributors ✨
247226
@@ -323,7 +302,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
323302
</tr>
324303
</tbody>
325304
<tfoot>
326-
305+
327306
</tfoot>
328307
</table>
329308

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"homepage": "https://github.com/drwpow/openapi-typescript#readme",
3737
"scripts": {
3838
"build": "del dist && tsc",
39+
"dev": "tsc --watch",
3940
"format": "npm run prettier -w .",
4041
"lint": "eslint .",
4142
"prepare": "npm run build",

0 commit comments

Comments
 (0)