Skip to content

Merge v14.0.4 (+ readme updates) #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bf63b97
rm yarn.lock (fix #569)
bcherny Jan 14, 2024
569f492
[Bugfix] Fix comment emmission for closing comments in patternPropert…
bcherny Jan 14, 2024
6adcad9
v13.1.2
bcherny Jan 14, 2024
7321597
Require Node v16+
bcherny Mar 2, 2024
5369633
Target ES6
bcherny Mar 2, 2024
d92b06e
Switch to @apidevtools/json-schema-ref-parser
bcherny Mar 2, 2024
4f47952
[Chore] Update dependencies
bcherny Apr 20, 2024
7c9ecd7
[Chore] Update dev dependencies (fix #587)
bcherny Apr 20, 2024
9cbe26e
Optimize array output (fix #583)
bcherny Apr 20, 2024
e39ed18
[Docs] Add badge for node version
bcherny Apr 20, 2024
11879fa
[Docs] Update author URL in package.json
bcherny Apr 20, 2024
2f29f19
feat: support `customName` options (#585)
antfu Apr 21, 2024
5f3039a
[Chore] Downgrade ESLint -> 8.56.0
Apr 21, 2024
f97ec06
[Docs] Document customName option in readme
Apr 21, 2024
d97768e
14.0.1
Apr 21, 2024
9ec0c70
feat: [577] Add yml support (#588)
alexanderkrum Apr 21, 2024
13806bd
[Chore] Drop @types/prettier dependency. (#594)
pcorpet Apr 23, 2024
872a057
[Chore] Clean up yaml parsing
bcherny May 2, 2024
e53bb80
[Chore] Clean up legacy style imports
bcherny May 2, 2024
add8ee1
[CI] Modernize Github workflow
bcherny May 2, 2024
2b61748
[CI] Add package-lock.json
bcherny May 2, 2024
39dd574
v14.0.2
bcherny May 2, 2024
107c338
v14.0.3
bcherny May 2, 2024
c9680db
[Docs] Clean up readme
bcherny May 2, 2024
e8291d7
v14.0.4
bcherny May 2, 2024
6c69141
Improve CLI docs
bcherny May 5, 2024
49fb14d
"JSONSchema" -> "JSON Schema"
bcherny May 12, 2024
fede3b4
Merge remote-tracking branch 'upstream/master' into csnw
mdmower-csnw May 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x, 17.x, 18.x]
node-version: [16.x, 17.x, 18.x, 19.x, 20.x, 21.x]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: yarn install --ignore-engines --verbose
run: npm ci
- name: Run tests
run: yarn test
run: npm test
env:
CI: true
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ node_modules
*.map
dist/
dist_tests/
package-lock.json
yarn-error.log
6 changes: 0 additions & 6 deletions .yarnrc.yml

This file was deleted.

12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

*Note: This is a partial changelog, covering significant & breaking changes. For a full list of changes, please consult the [commit log](https://github.com/bcherny/json-schema-to-typescript/commits).

## 14.0.2

- 9ec0c70 Added .yaml support (#577)

## 14.0.1

- 2f29f19 Added `customName` option

## 14.0.0

- 967eb13 Require Node v16+

## 13.1.0

- f797848 Feat: Add support for `deprecated` keyword
Expand Down
98 changes: 61 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# json-schema-to-typescript [![Build Status][build]](https://github.com/bcherny/json-schema-to-typescript/actions?query=branch%3Amaster+workflow%3ACI) [![npm]](https://www.npmjs.com/package/json-schema-to-typescript) [![mit]](https://opensource.org/licenses/MIT)
# json-schema-to-typescript [![Build Status][build]](https://github.com/bcherny/json-schema-to-typescript/actions?query=branch%3Amaster+workflow%3ACI) [![npm]](https://www.npmjs.com/package/json-schema-to-typescript) [![mit]](https://opensource.org/licenses/MIT) ![node]

[build]: https://img.shields.io/github/actions/workflow/status/bcherny/json-schema-to-typescript/ci.yml?style=flat-square
[npm]: https://img.shields.io/npm/v/json-schema-to-typescript.svg?style=flat-square
[mit]: https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square
[node]: https://img.shields.io/badge/Node.js-16+-417e37?style=flat-square

> Compile json schema to typescript typings
> Compile JSON Schema to TypeScript typings.

## Example

Check out the [live demo](https://borischerny.com/json-schema-to-typescript-browser/).

Input:

```json
{
"title": "Example Schema",
Expand Down Expand Up @@ -36,6 +40,7 @@ Input:
```

Output:

```ts
export interface ExampleSchema {
firstName: string;
Expand All @@ -51,15 +56,59 @@ export interface ExampleSchema {
## Installation

```sh
# Using Yarn:
yarn add json-schema-to-typescript

# Or, using NPM:
npm install json-schema-to-typescript --save
npm install json-schema-to-typescript
```

## Usage

json-schema-to-typescript is easy to use via the CLI, or programmatically.

### CLI

First make the CLI available using one of the following options:

```sh
# install locally, then use `npx json2ts`
npm install json-schema-to-typescript

# or install globally, then use `json2ts`
npm install json-schema-to-typescript --global

# or install to npm cache, then use `npx --package=json-schema-to-typescript json2ts`
# (you don't need to run an install command first)
```

Then, use the CLI to convert JSON files to TypeScript typings:

```sh
cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.yaml foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/
```

You can pass any of the options described below (including style options) as CLI flags. Boolean values can be set to false using the `no-` prefix.

```sh
# generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
```

### API

To invoke json-schema-to-typescript from your TypeScript or JavaScript program, import it and call `compile` or `compileFromFile`.

```js
import { compile, compileFromFile } from 'json-schema-to-typescript'

Expand All @@ -84,7 +133,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| key | type | default | description |
|-|-|-|-|
| additionalProperties | boolean | `true` | Default value for `additionalProperties`, when it is not explicitly set |
| bannerComment | string | `"/* eslint-disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/"` | Disclaimer comment prepended to the top of each generated file |
| bannerComment | string | `"/* eslint-disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSON Schema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/"` | Disclaimer comment prepended to the top of each generated file |
| customName | `(LinkedJSONSchema, string | undefined) => string | undefined` | `undefined` | Custom function to provide a type name for a given schema
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
Expand All @@ -95,40 +145,14 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
## CLI

A CLI utility is provided with this package.
| $refOptions | object | `{}` | [$RefParser](https://github.com/APIDevTools/json-schema-ref-parser) Options, used when resolving `$ref`s |

```sh
cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.json foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/
```

You can pass any of the options described above (including style options) as CLI flags. Boolean values can be set to false using the `no-` prefix.
## Tests

```sh
# generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
$ npm test
```

## Tests

`npm test`

## Features

- [x] `title` => `interface`
Expand Down
Loading