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

@peassoft/typesgen

v0.1.5

Published

A tool for building OpenAPI declarations, MongoDB schemas, and JDV validation rules from TypeScript type definitions

Downloads

10

Readme

@peassoft/typesgen

A tool for building OpenAPI declarations, MongoDB schemas, and JDV validation rules from TypeScript type definitions.

Describe your business entities one time as TypeScript types and use typesgen to automatically generate:

  • OpenAPI declarations for API contracts;
  • MongoDB validation schemas;
  • JDV validation rules.

Installation

$ npm i --save @peassoft/typesgen

Command Line Interface

Usage typesgen [options]

Options:
  -h --help            Display this usage info
  -s --source=<path>   (REQUIRED) Absolute or relative path to source file
  -d --dest=<path>     Absolute or relative path to the folder where to place
                       generated files with default names. Must be present if
                       separate destination paths are not provided.
                       If at least one separate destination path is provided,
                       this setting is ignored
  -o --openapi=<path>  Absolute or relative path to the file which OpenAPI
                       descriptions should be generated into
  -m --mongodb=<path>  Absolute or relative path to the file which MongoDB
                       validation schemas should be generated into
  -j --jdv=<path>      Absolute or relative path to the file which JDV
                       validation schemas should be generated into

API

Hybrid module, load either with import or require().

import { typesgen } from '@peassoft/typesgen';
// or
const { typesgen } = require('@peassoft/typesgen');

typesgen(config) -> void

type Config = {
  /** Absolute or relative path to the source file to be processed */
  sourceFilePath: string;
  /**
   * Absolute or relative path to the folder where to place generated files with default names
   *
   * Must be present if separate destination paths are not provided.
   * If at least one separate destination path is provided, this setting is ignored.
   */
  destFolderPath?: string;
  /** Absolute or relative path to the file which OpenAPI descriptions should be generated into */
  openapiDestPath?: string;
  /**
   * Absolute or relative path to the file which MongoDB validation schemas
   * should be generated into
   */
  mongodbDestPath?: string;
  /** Absolute or relative path to the file which JDV validation schemas should be generated into */
  jdvDestPath?: string;
};

Usage Details

If you need to process more than one file with TypeScript definitions, run typesgen against each file separately with different destination configs.

What is supported:

  • type aliases of object type;
  • string enums with each member initialized with a string literal.

If top-level declaration has export keyword, this entity will be emitted.

Each object type member can express nullability by declaring a union type with the second type of the union being null.

type MyType = {
  foo: string | null;
  bar: Record<string, boolean> | null; // OK
  // baz: Record<string, boolean | null>; // Not supported
};

For arrays, only square brackets syntax is supported.

type MyType = {
  foo: number[]; // OK
  // bar: Array<number>; // Not supported
};

Integer numbers

Some emit targets support integers and float numbers separately. Though, Typescript does not. To provide support for integers, use this approach:

  1. Declare a non-public type alias named exactly Integer.
  2. Use type Integer in place of number when integer is needed.

Example:

type Integer = number; // Actually, any primitive value will work. Only name Integer is used.

export type Foo = {
  bar: Integer;
  baz: Integer[] | null;
};

Date strings and UUIDs

Some emit targets support recognizing string values as dates in ISO format of UUIDs. You can use the same apprache as with integers:

type ISODateString = string; // Name matters, type does not.
type UUID = string; // Name matters, type does not.

export type Foo = {
  bar: ISODateString;
  baz: UUID;
  bar1: Record<ISODateString, number>;
  baz1: Record<UUID, number>;
};

Record type

This is the only Typescript's utility type supported. Although, with one restriction: the first type argument must be string, ISODateString, or UUID;

What is NOT supported:

  • Union types, except those used to express nullability (see above).
  • Intersection types.
  • Interfaces.
  • Type null, except when used as the second type in a union type to express nullability (see above).
  • Type undefined.
  • All built-in utility types, except Record.

MongoDB:

If an object property has its original type ofRecord, this property will have bsonType of object without any deeper property descriptor.