npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@puffmeow/schema2ts

v0.2.4

Published

Transform JSON Schema to Typescript interface automatically

Downloads

1

Readme

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

| index | Task Name | ops/sec | Average Time (ns) | Margin | Samples | | ----- | --------------------- | ------- | ------------------ | ------ | ------- | | 0 | TypeScript: schema2ts | 2,796 | 357534.31021794415 | ±1.08% | 1399 | | 1 | Rust: rustySchema2ts | 5,431 | 184122.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

| key | type | required | default | description | | ----------------- | -------- | -------- | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | preffix | string | × | I | Interface preffix, if you don't like this, you can give it a empty string | | preffixOfEnum | string | × | T | Enum type preffix, if you don't like this, you can give it a empty string | | isGenComment | boolean | × | false | Whether to automatically generate comments | | isExport | boolean | × | true | Whether to export the interfaces and types | | indent | number | × | 2 | Code indent | | semi | boolean | × | true | Is enable semicolon | | optional | boolean | × | true | If this is enabled, it will generate the optional interface, default value is true | | ignoreKeys | string[] | × | [] | 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. | | explain | string | × | | Display some comments at the top of the code | | parseErrorMessage | string | × | // 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;
}