Skip to content

Commit fede3b4

Browse files
committed
Merge remote-tracking branch 'upstream/master' into csnw
2 parents 1e27de6 + 49fb14d commit fede3b4

24 files changed

+7614
-4171
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
node-version: [12.x, 14.x, 16.x, 17.x, 18.x]
12+
node-version: [16.x, 17.x, 18.x, 19.x, 20.x, 21.x]
1313
os: [ubuntu-latest, windows-latest, macOS-latest]
1414

1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4
1717
- name: Use Node.js ${{ matrix.node-version }}
18-
uses: actions/setup-node@v1
18+
uses: actions/setup-node@v4
1919
with:
2020
node-version: ${{ matrix.node-version }}
21+
cache: 'npm'
2122
- name: Install dependencies
22-
run: yarn install --ignore-engines --verbose
23+
run: npm ci
2324
- name: Run tests
24-
run: yarn test
25+
run: npm test
2526
env:
2627
CI: true

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ node_modules
1313
*.map
1414
dist/
1515
dist_tests/
16-
package-lock.json
1716
yarn-error.log

.yarnrc.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
*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).
44

5+
## 14.0.2
6+
7+
- 9ec0c70 Added .yaml support (#577)
8+
9+
## 14.0.1
10+
11+
- 2f29f19 Added `customName` option
12+
13+
## 14.0.0
14+
15+
- 967eb13 Require Node v16+
16+
517
## 13.1.0
618

719
- f797848 Feat: Add support for `deprecated` keyword

README.md

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
# 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)
1+
# 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]
22

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

7-
> Compile json schema to typescript typings
8+
> Compile JSON Schema to TypeScript typings.
89
910
## Example
1011

12+
Check out the [live demo](https://borischerny.com/json-schema-to-typescript-browser/).
13+
1114
Input:
15+
1216
```json
1317
{
1418
"title": "Example Schema",
@@ -36,6 +40,7 @@ Input:
3640
```
3741

3842
Output:
43+
3944
```ts
4045
export interface ExampleSchema {
4146
firstName: string;
@@ -51,15 +56,59 @@ export interface ExampleSchema {
5156
## Installation
5257

5358
```sh
54-
# Using Yarn:
55-
yarn add json-schema-to-typescript
56-
57-
# Or, using NPM:
58-
npm install json-schema-to-typescript --save
59+
npm install json-schema-to-typescript
5960
```
6061

6162
## Usage
6263

64+
json-schema-to-typescript is easy to use via the CLI, or programmatically.
65+
66+
### CLI
67+
68+
First make the CLI available using one of the following options:
69+
70+
```sh
71+
# install locally, then use `npx json2ts`
72+
npm install json-schema-to-typescript
73+
74+
# or install globally, then use `json2ts`
75+
npm install json-schema-to-typescript --global
76+
77+
# or install to npm cache, then use `npx --package=json-schema-to-typescript json2ts`
78+
# (you don't need to run an install command first)
79+
```
80+
81+
Then, use the CLI to convert JSON files to TypeScript typings:
82+
83+
```sh
84+
cat foo.json | json2ts > foo.d.ts
85+
# or
86+
json2ts foo.json > foo.d.ts
87+
# or
88+
json2ts foo.yaml foo.d.ts
89+
# or
90+
json2ts --input foo.json --output foo.d.ts
91+
# or
92+
json2ts -i foo.json -o foo.d.ts
93+
# or (quote globs so that your shell doesn't expand them)
94+
json2ts -i 'schemas/**/*.json'
95+
# or
96+
json2ts -i schemas/ -o types/
97+
```
98+
99+
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.
100+
101+
```sh
102+
# generate code for definitions that aren't referenced
103+
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
104+
# use single quotes and disable trailing semicolons
105+
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi
106+
```
107+
108+
### API
109+
110+
To invoke json-schema-to-typescript from your TypeScript or JavaScript program, import it and call `compile` or `compileFromFile`.
111+
63112
```js
64113
import { compile, compileFromFile } from 'json-schema-to-typescript'
65114

@@ -84,7 +133,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
84133
| key | type | default | description |
85134
|-|-|-|-|
86135
| additionalProperties | boolean | `true` | Default value for `additionalProperties`, when it is not explicitly set |
87-
| 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 |
136+
| 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 |
137+
| customName | `(LinkedJSONSchema, string | undefined) => string | undefined` | `undefined` | Custom function to provide a type name for a given schema
88138
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
89139
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
90140
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
@@ -95,40 +145,14 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
95145
| 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 |
96146
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
97147
| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |
98-
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
99-
## CLI
100-
101-
A CLI utility is provided with this package.
148+
| $refOptions | object | `{}` | [$RefParser](https://github.com/APIDevTools/json-schema-ref-parser) Options, used when resolving `$ref`s |
102149

103-
```sh
104-
cat foo.json | json2ts > foo.d.ts
105-
# or
106-
json2ts foo.json > foo.d.ts
107-
# or
108-
json2ts foo.json foo.d.ts
109-
# or
110-
json2ts --input foo.json --output foo.d.ts
111-
# or
112-
json2ts -i foo.json -o foo.d.ts
113-
# or (quote globs so that your shell doesn't expand them)
114-
json2ts -i 'schemas/**/*.json'
115-
# or
116-
json2ts -i schemas/ -o types/
117-
```
118-
119-
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.
150+
## Tests
120151

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

128-
## Tests
129-
130-
`npm test`
131-
132156
## Features
133157

134158
- [x] `title` => `interface`

0 commit comments

Comments
 (0)