Skip to content

Commit

Permalink
feat: initial implementation (#1)
Browse files Browse the repository at this point in the history
* feat: initial implementation

* chore: run CI on Node.js v18 and v20

* refactor: remove empty schema def check

* Update package.json

* Update README.md

* chore: remove unused @types/node dependency

* chore: update changeset

* chore: setup changeset release action
  • Loading branch information
toomuchdesign authored Jul 18, 2023
1 parent 309dd0b commit 6454f47
Show file tree
Hide file tree
Showing 31 changed files with 3,374 additions and 3,936 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/silly-kiwis-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-ts-json-schema': patch
---

Initial release
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ env:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [18, 20]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
node-version: ${{ matrix.version }}

- run: npm ci
- run: npm run source:check
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: auto-merge
name: dependabot-auto-merge

on:
pull_request:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
branches:
- master

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- run: npm ci

- name: Create Release Pull Request
uses: changesets/action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
18
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
# Project name
# openapi-ts-json-schema

## Setup after fork
Generate JSON schemas TypeScript files (`.ts`) from OpenAPI definitions which can be natively used to infer types from (with [`json-schema-to-ts`](https://github.com/ThomasAribart/json-schema-to-ts) or [`fastify-type-provider-json-schema-to-ts`](https://github.com/fastify/fastify-type-provider-json-schema-to-ts)).

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

## Contributing
- Dereference the definitions with [`@apidevtools/json-schema-ref-parser`](https://github.com/APIDevTools/json-schema-ref-parser) (resolving `$ref`'s)
- Convert to JSON schema with [`@openapi-contrib/openapi-schema-to-json-schema`](https://github.com/openapi-contrib/openapi-schema-to-json-schema)
- Generate a TypeScript JSON schema file for each definition (`.ts` files with `as const` assertion)
- Store schemas in a folder structure reflecting the original OpenAPI definition structure

Any contribution should be provided with a `changesets` update:
## Installation

```
npx changeset
npm i openapi-ts-json-schema -D
```

## Usage

```ts
import { openapiToTsJsonSchema } from 'openapi-ts-json-schema';

const { outputFolder } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'path/to/my/specs.yaml'),
definitionPathsToGenerateFrom: ['paths', 'components.schemas'],
});
```

### Notes

Generated JSON schemas folders get currently saved in a `schemas-autogenerated` folder next to the provided OpenAPI definition file.

Generated JSON schemas folders name gets escaped in order to be valid file system names.

## Options

| Property | Type | Description | Default |
| --------------------------------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| **openApiSchema** _(required)_ | `string` | Path to the OpenApi file (supports yaml and json) | - |
| **definitionPathsToGenerateFrom** | `string[]` | OpenApi definition object paths to generate the JSON schemas from. Only matching paths will be generated. (Supports dot notation: `["components.schemas"]`) | `[]` |
| **schemaPatcher** | `(params: { schema: JSONSchema }) => void` | Dynamically patch generated JSON schemas. The provided function will be invoked against every single JSON schema node. | - |
| **silent** | `boolean` | Don't console.log user messages | `false` |

## Todo

- Consider exposing an option to set the output folder path
Empty file removed jest.setup.ts
Empty file.
Loading

0 comments on commit 6454f47

Please sign in to comment.