Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morintd/ts-generate-schema
Generate json-schema files from your typescript definitions
https://github.com/morintd/ts-generate-schema
Last synced: 2 months ago
JSON representation
Generate json-schema files from your typescript definitions
- Host: GitHub
- URL: https://github.com/morintd/ts-generate-schema
- Owner: morintd
- License: mit
- Created: 2020-11-14T12:27:13.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-11-12T14:41:50.000Z (about 2 years ago)
- Last Synced: 2024-09-26T09:36:59.484Z (3 months ago)
- Language: TypeScript
- Size: 994 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-generate-schema
Generate json-schema files from your typescript definitions
![npm](https://img.shields.io/npm/v/ts-generate-schema)
[![Build](https://circleci.com/gh/morintd/ts-generate-schema.svg?style=shield)](https://app.circleci.com/pipelines/github/morintd/ts-generate-schema)
[![Test Coverage](https://api.codeclimate.com/v1/badges/5577363312610be54f84/test_coverage)](https://codeclimate.com/github/morintd/ts-generate-schema/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/5577363312610be54f84/maintainability)](https://codeclimate.com/github/morintd/ts-generate-schema/maintainability)
![npm](https://img.shields.io/npm/dm/ts-generate-schema)## Features
- Generate json-schema files from one single command :
- Run `npx ts-generate-schema src/**/*-response.dto.ts` in your root folder.
- After writing the following TypeScript definition :
> src/types/login-user-response.dto.ts```typescript
export type LoginUserResponseDTO = {
user: {
id: string;
email: string;
username: string;
firstname: string;
};
token: {
expiresIn: number;
accessToken: string;
};
};
```- Get this file in return :
> src/types/schema/login-user-response-dto.jsc.ts
```typescript
export default {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
id: {
type: 'string',
},
email: {
type: 'string',
},
username: {
type: 'string',
},
firstname: {
type: 'string',
},
},
required: ['email', 'firstname', 'id', 'username'],
},
token: {
type: 'object',
properties: {
expiresIn: {
type: 'number',
},
accessToken: {
type: 'string',
},
},
required: ['accessToken', 'expiresIn'],
},
},
required: ['token', 'user'],
$schema: 'http://json-schema.org/draft-07/schema#',
};
```## Usage
- Install with `npm i -D ts-generate-schema` or `yarn add -D ts-generate-schema`
- Add the following scripts in your **package.json**.```json
"schema": "ts-generate-schema "
```- You can also use it globally:
- With `npm i -g ts-generate-schema` and simply running `ts-generate-schema ` anywhere
- with `npx`, `npx ts-generate-schema `### Command Line
`` is a [glob](https://github.com/isaacs/node-glob#readme) pattern to find files to handle.
I would recommand giving those files a special extension (such as _-response.dto.ts_ for request) and use `ts-generate-schema src/**/*-response.dto.ts`
```
Usage: ts-generate-schema
Options:
--help Show help [boolean]
--version Show version number [boolean]
--to Extension of generated json-schema [string] [default: "jsc.ts"]
--export How to export generated json-schema from file
[string] [default: "export default"]
--out Path to the generated schema directory [string] [default: "schema"]
```## Motivation
I'm passionate about software architecture, quality and scaling.
I've recently been working on a web and mobile project where I made tons of API calls. I came to the conlusion that my way of handling API calls was ... not good enough.
And, at some point in every project I worked on, we had a server getting an update without the front-end team being perfectly notified about it.
My aim was to be able to know when a request we received was different from our primary API. As I'm a TypeScript lover and user, I quickly found some way to use TS definitions as json-schemas with a validator such as AJV.
Nevertheless, libraries providing us with TypeScript to JSON Schema functionalities were too low level to be used as they are.
I had axios and interceptors to setup my strategies, AJV to handle validation and now this library to quickly generate my JSON-schemas from my TypeScript definitions.
## License
MIT