Skip to content

Commit 1fec59b

Browse files
committed
feat: initial implementation
1 parent 309dd0b commit 1fec59b

27 files changed

+3352
-3931
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20
1+
18

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1-
# Project name
1+
# openapi-ts-json-schema
22

3-
## Setup after fork
3+
Convert OpenApi definitions to TS JSON schema files.
44

5-
- Fill `package.json` file with relevant fields
6-
- Enable [Changesets bot](https://github.com/changesets/bot)
5+
Given a OpenAPI definition file, `openapi-ts-json-schema` will:
76

8-
## Contributing
7+
- Dereference the definitions with [`@apidevtools/json-schema-ref-parser`](https://github.com/APIDevTools/json-schema-ref-parser)
8+
- Convert to JSON schema with [`@openapi-contrib/openapi-schema-to-json-schema`](https://github.com/openapi-contrib/openapi-schema-to-json-schema)
9+
- Generate a TS JSON schema file for each definition (with `as const` assertion)
10+
- Store JSON schemas in a folder structure reflecting the OpenAPI definition structure
911

10-
Any contribution should be provided with a `changesets` update:
12+
## Installation
1113

1214
```
13-
npx changeset
15+
npm i openapi-ts-json-schema -D
1416
```
17+
18+
## Usage
19+
20+
```ts
21+
import { openapiToTsJsonSchema } from 'openapi-ts-json-schema';
22+
23+
const { outputFolder } = await openapiToTsJsonSchema({
24+
openApiSchema: path.resolve(fixtures, 'path/to/my/specs.yaml'),
25+
definitionPathsToGenerateFrom: ['paths', 'components.schemas'],
26+
});
27+
```
28+
29+
### Notes
30+
31+
Generated JSON schemas folders get currently saved in a `schemas-autogenerated` folder next to the provided OpenAPI definition file
32+
33+
Generated JSON schemas folders name gets escaped in order to be valid file system names.
34+
35+
## Options
36+
37+
| Property | Type | Description | Default |
38+
| --------------------------------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
39+
| **openApiSchema** _(required)_ | `string` | Path to the OpenApi file (supports yaml and json) | - |
40+
| **definitionPathsToGenerateFrom** | `string[]` | OpenApi definition object paths to generate the JSON schemas from. Only matching paths will be generated. (Supports dot notation: `["components.schemas"]`) | `[]` |
41+
| **schemaPatcher** | `(params: { schema: JSONSchema }) => void` | Dynamically patch generated JSON schemas. The provided function will be invoked against every single JSON schema node. | - |
42+
| **silent** | `boolean` | Don't console.log user messages | `false` |
43+
44+
## Todo
45+
46+
- Consider exposing an option to set the output folder path

jest.setup.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)