0.2.4 • Published 9 months ago

@puffmeow/schema2ts v0.2.4

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

Introduction

This tool can help you transform your JSON Schema to TypeScript interface quickly🧲.

Git repository. If you like it, please give me a little star⭐, thanks~

If you want a faster implementation, you can see rusty-schema2ts, it uses napi-rs to implement and it's faster than this TypeScript version.

The api of them are all the same.

TypeScript vs Rust

You can find benchmark here

indexTask Nameops/secAverage Time (ns)MarginSamples
0TypeScript: schema2ts2,796357534.31021794415±1.08%1399
1Rust: rustySchema2ts5,431184122.05448994122±0.29%2716

Install

npm

npm i @puffmeow/schema2ts

pnpm

pnpm i @puffmeow/schema2ts

yarn

yarn add @puffmeow/schema2ts

Quick start

It's really easy to use.

import { schema2ts } from '@puffmeow/schema2ts';

// The "options" we will introduce later
// schema2ts(schema: string, options?: IOptions): string
schema2ts(`your schema`, options);

Options

keytyperequireddefaultdescription
preffixstring×IInterface preffix, if you don't like this, you can give it a empty string
preffixOfEnumstring×TEnum type preffix, if you don't like this, you can give it a empty string
isGenCommentboolean×falseWhether to automatically generate comments
isExportboolean×trueWhether to export the interfaces and types
indentnumber×2Code indent
semiboolean×trueIs enable semicolon
optionalboolean×trueIf this is enabled, it will generate the optional interface, default value is true
ignoreKeysstring[]×[]If you don't want to generate the type of an attribute in a root object, you can pass in the key name of the corresponding attribute.Like this, ignoreKeys: "firstName", "lastName"Schema2ts will ignore the two attributes and doesn't generate the type of them.
explainstring×Display some comments at the top of the code
parseErrorMessagestring×// Parse schema error, please check your schema.When parsing schema error, this message will be return

Example:

Input schema

If you have a schema like this:

{
  "title": "Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "type": "number"
    },
    "hairColor": {
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "properties": {
        "key1": {
          "type": "string"
        },
        "key2": {
          "type": "number"
        },
        "key3": {
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "arr1": {
            "type": "string"
          },
          "arr2": {
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "enen1": {
                  "type": "string"
                },
                "enen2": {
                  "type": "number"
                },
                "enen3": {
                  "type": "boolean"
                },
                "enen4": {
                  "type": "unknow type will transform to any"
                }
              }
            }
          }
        }
      }
    }
  }
}

Output TypeScript interface

Finally it will output like this:

export type THairColor = 'color1' | 'color2' | 'color3';

export interface ISchema {
  firstName?: string;
  lastName?: string;
  age?: number;
  hairColor?: THairColor;
  obj?: IObj;
  arr?: IArr[];
}

export interface IObj {
  key1?: string;
  key2?: number;
  key3?: boolean;
}

export interface IArr {
  arr1?: string;
  arr2?: number;
  arr3?: IArr3[];
}

export interface IArr3 {
  enen1?: string;
  enen2?: number;
  enen3?: boolean;
  enen4?: any;
}

More examples

1.generate comment

schema2ts(`below json`, { isGenComment: true });

input json

{
  "title": "Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "title": "This is the first name",
      "type": "string"
    },
    "lastName": {
      "title": "This is the last name",
      "type": "string"
    },
    "age": {
      "title": "This is the age",
      "type": "number"
    },
    "hairColor": {
      "title": "This is the hair color",
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "title": "Object test",
      "properties": {
        "key1": {
          "title": "This is the key1",
          "type": "string"
        },
        "key2": {
          "title": "This is the key2",
          "type": "number"
        },
        "key3": {
          "title": "This is the key3",
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "title": "Arr test",
      "items": {
        "type": "object",
        "title": "Nested array items",
        "properties": {
          "arr1": {
            "title": "This is the arr1",
            "type": "string"
          },
          "arr2": {
            "title": "This is the arr2",
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "title": "Test arr3",
            "items": {
              "type": "object",
              "title": "Test nested arr3 items",
              "properties": {
                "enen1": {
                  "title": "This is the enen1",
                  "type": "string"
                },
                "enen2": {
                  "title": "This is the enen2",
                  "type": "number"
                },
                "enen3": {
                  "title": "This is the enen3",
                  "type": "boolean"
                }
              }
            }
          }
        }
      }
    }
  }
}

output

export type THairColor = 'color1' | 'color2' | 'color3';

/** Schema */
export interface ISchema {
  /** This is the first name */
  firstName?: string;
  /** This is the last name */
  lastName?: string;
  /** This is the age */
  age?: number;
  /** This is the hair color */
  hairColor?: THairColor;
  /** Object test */
  obj?: IObj;
  /** Arr test Nested array items */
  arr?: IArr[];
}

/** Object test */
export interface IObj {
  /** This is the key1 */
  key1?: string;
  /** This is the key2 */
  key2?: number;
  /** This is the key3 */
  key3?: boolean;
}

/** Nested array items */
export interface IArr {
  /** This is the arr1 */
  arr1?: string;
  /** This is the arr2 */
  arr2?: number;
  /** Test arr3 Test nested arr3 items */
  arr3?: IArr3[];
}

/** Test nested arr3 items */
export interface IArr3 {
  /** This is the enen1 */
  enen1?: string;
  /** This is the enen2 */
  enen2?: number;
  /** This is the enen3 */
  enen3?: boolean;
}

2.ignoreKeys

schema2ts(`below json`, {
  ignoreKeys: ['firstName', 'obj', 'hairColor', 'arr'],
  isGenComment: true,
  optional: false,
});

input json

{
  "title": "Test",
  "type": "object",
  "properties": {
    "firstName": {
      "title": "This is the first name",
      "type": "string"
    },
    "lastName": {
      "title": "This is the last name",
      "type": "string"
    },
    "age": {
      "title": "This is the age",
      "type": "number"
    },
    "hairColor": {
      "title": "This is the hair color",
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "title": "Object test",
      "properties": {
        "key1": {
          "title": "This is the key1",
          "type": "string"
        },
        "key2": {
          "title": "This is the key2",
          "type": "number"
        },
        "key3": {
          "title": "This is the key3",
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "title": "Arr test",
      "items": {
        "type": "object",
        "title": "Nested array items",
        "properties": {
          "arr1": {
            "title": "This is the arr1",
            "type": "string"
          },
          "arr2": {
            "title": "This is the arr2",
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "title": "Test arr3",
            "items": {
              "type": "object",
              "title": "Test nested arr3 items",
              "properties": {
                "enen1": {
                  "title": "This is the enen1",
                  "type": "string"
                },
                "enen2": {
                  "title": "This is the enen2",
                  "type": "number"
                },
                "enen3": {
                  "title": "This is the enen3",
                  "type": "boolean"
                }
              }
            }
          }
        }
      }
    }
  }
}

output

/** Test */
export interface ITest {
  /** This is the last name */
  lastName: string;
  /** This is the age */
  age: number;
}
0.2.1

9 months ago

0.1.2

9 months ago

0.2.0

9 months ago

0.2.3

9 months ago

0.2.2

9 months ago

0.1.3

9 months ago

0.2.4

9 months ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago