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

schema-to-types

v1.2.4

Published

Library to generate TypeScript types based on simpl-schema definitions.

Downloads

16

Readme

schema-to-types

Library to generate TypeScript types based on simpl-schema definitions.

Define schemas once, get both validation and type checking!

Usage

Install

npm install --save-dev schema-to-types

Define the schemas

Define all your schemas as values of a single object with type SchemaMap and export it.

The keys of the object will be used as name of the generated types.

Example:

export const schemas: SchemaMap = {};

// Will generate interface Foo
schemas["Foo"] = new SimpleSchema({
  anArrayOfBooleans: {
    type: Array
  },
  "anArrayOfBooleans.$": {
    type: Boolean
  },
  aDate: {
    type: Date,
    optional: true,
    autoValue: () => new Date()
  },
  aString: {
    type: String,
    defaultValue: "schemas"
  }
});

Will generate:

export interface Foo {
    anArrayOfBooleans: boolean[];
    aDate?: Date;
    aString: string;
}

Generate types

Very basic for the moment: run update-model.ts script, providing path to tsconfig and the file that will contain the generated types.

Example:

node node_modules/schema-to-types/dist/src/update-model.js tsconfig.json api/imports/model/schema-model.ts

Details

Schema references

To reference another schema, simply reference it via its key in the schemas object.

schemas["SubType"] = new SimpleSchema({
    aNumber: {
        type: Number,
        min: 12,
        max: 14.5
    }
});

schemas["Foo"] = new SimpleSchema({
    aSubSchemaExternal: schemas["SubType"], // Will target the other type
});

Will generate:

export interface SubType {
    aNumber: number;
}

export interface Foo {
    aSubObject: SubType;
}

Enum references

When an enum is used as a type in your schema, it will be "deconstructed" as the list of possible values.

Except... if you provide the specific typeName attribute.

For this to be accepted by Simple-schema, you will need to allow it:

SimpleSchema.extendOptions(["typeName"]);

Example

import { SchemaMap } from "../src/schema-map";
import { MyEnum } from "./models/model";

// Essential to be allowed to use typeName property and reference types explicitly
SimpleSchema.extendOptions(["typeName"]);

schemas["TypeWithEnumus"] = new SimpleSchema({
  anEnum: {
    type: MyEnum, // Will generate the list of possible values
    optional: true
  },
  anEnumWithType: {
    type: MyEnum,
    typeName: 'MyEnum' // Will reference MyEnum type directly
  }
});

Type references

To reference a type instead of the native types (such as String):

  1. explicitely name it with the typeName property (see above)
  2. trick the reference in type property to make sure the type is imported in the file (mandatory for the code generation to reference it)

Example

Let's assume an Id type has been defined in model/model.ts:

export type Id = string;

In the schema file:

import { SchemaMap, allowSchemaExtension } from "../src/schema-map";
import { Id } from "./models/model";

// Essential to be allowed to use typeName property and reference types explicitly
SimpleSchema.extendOptions(["typeName"]);

schemas["TypeWithTypedString"] = new SimpleSchema({
  aTypedString: {
      type: String as (value?: any) => Id, // To make sure Id is imported
      typeName: "Id"
    },
});

This way the library will use Id instead of string in the generated interface.

Example

To test the tool, look at /example dir and run

npm run generate-test-model

Caveats

  • not all possible schema definitions have been implemented or tested
  • very unefficient code
  • would be nice to not rely on typeName for external references
  • requires to have all schemas in a single file

TODO

  • unit tests
  • add comments based on min / max, etc.
  • improve imports (very slow)
  • somehow manage to add SimpleSchema.extendOptions(['typeName']); automatically?