0.0.5 • Published 4 years ago

json-types-generator v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

JSON Types Generator

A Json Types Generator For TypeScript

Install

yarn add json-types-generator

Usage

import jsonTypesGenerator from 'json-types-generator'

const jsonStr = `
{
  "date": "2021-08-28",
  "season": "ordinary",
  "season_week": 21,
  "celebrations": [
    {
      "title": "Saint Augustine of Hippo, bishop and doctor of the Church",
      "colour": "white",
      "rank": "memorial",
      "rank_num": 3.1
    }
  ],
  "weekday": "saturday"
}
`

jsonTypesGenerator({
  data: jsonStr,
  outPutPath: '/User/xdoer/types.ts',
  rootInterfaceName: 'ChinaRegion',
  customInterfaceName(key, value, data) {
    if (key === 'aa') return 'Province'
  },
})

Options

OptionsTypeRequireMeaning
datastring | json object-xJson string or object
outPutPathstring-xtypes file output path
rootInterfaceNamestring-xroot interface name you want to get
overwriteboolean, default is true- rewrite file when file is exist
customInterfaceName(key: string, value: any, data: any) => string | void- custom intermediate interface

Test

You can add json file to test/data folder, and run npm test command, then observe whether the output of test/output meets expectations

Other

How to use this in Restful-API ? See here