|
1 |
| -# Project name |
| 1 | +# openapi-ts-json-schema |
2 | 2 |
|
3 |
| -## Setup after fork |
| 3 | +Convert OpenApi definitions to TS JSON schema files. |
4 | 4 |
|
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: |
7 | 6 |
|
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 |
9 | 11 |
|
10 |
| -Any contribution should be provided with a `changesets` update: |
| 12 | +## Installation |
11 | 13 |
|
12 | 14 | ```
|
13 |
| -npx changeset |
| 15 | +npm i openapi-ts-json-schema -D |
14 | 16 | ```
|
| 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 |
0 commit comments