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

ts-generate-schema

v2.1.0

Published

Generate json-schema files from your typescript definitions

Downloads

1,949

Readme

ts-generate-schema

Generate json-schema files from your typescript definitions

npm Build Test Coverage Maintainability npm

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

    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

    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.
"schema": "ts-generate-schema <pattern>"
  • You can also use it globally:
    • With npm i -g ts-generate-schema and simply running ts-generate-schema <pattern> anywhere
    • with npx, npx ts-generate-schema <pattern>

Command Line

<pattern> is a glob 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 <pattern>
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