JSPM

  • Created
  • Published
  • Downloads 479
  • Score
    100M100P100Q89390F
  • License MIT

Package Exports

  • typescript-scaffolder
  • typescript-scaffolder/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typescript-scaffolder) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

TypeScript Scaffolder

Generates typescript code based off of files or schemas such as JSON.

โœจ Version 1.0 Features

Interface Generation

Generate TypeScript interfaces automatically from JSON schemas or raw JSON data.

  • ๐Ÿ” Infers full TypeScript interfaces using quicktype
  • ๐Ÿง  Supports nested objects, arrays, optional fields, unions
  • ๐Ÿ›  Preserves directory structure from i.e. schemas/<folder_name> into codegen/interfaces/<folder_name>
  • ๐Ÿ“ Automatically creates output folders if they don't exist
  • ๐Ÿงช Independent logging and unit tests to aid in debugging

Installation

To install the package, run the following command npm install typescript-scaffolder

Usage in Code - Interfaces

Schema Inference

There are two functions available to infer the schema generation for output preview:

inferSchmea                 // produces a preview of the interface inferred from a schema
inferSchemaFromPath         // reads a json file and produces a preview of the interface from a schema

The above functions can be used with either async / await or Promise patterns:

const res = await inferSchema(<stringified json object)
# returns a promise containing the interpreted interface as a string, or null if fails

const res = await inferSchemaFromPath(<filepath as string>)
# returns a promise containing the interpreted interface as a string, or null if fails

Interface Generation

Interface generation automatically calls inferSchemaFromPath and generates a typescript file containing the interface

 generateInterfaces          // takes json file and generates an interface file
 
 generateInterfacesFromPath  // takes a directory of 1 or more levels and generates interfaces for each json
                             // the read directory tree will also be duplicated

The above functions can be used with either async / await or Promise patterns:

 // inferSchemaFromPath is already called in these functions, you do not need to call it separately
 await generateInterfaces(<path to file, directory where file is located, output folder name>)

 await generateInterfacesFromPath(<schema directory, output directory>)

Usage in CLI - Interfaces

You can run the package directly from the CLI if building locally or as a dependency

### Run from source (for local development)

npx ts-node src/cli.ts -i schemas -o codegen/interfaces

### Run as a linked dependency

yarn link
# in your other project:
yarn link typescript-codegen

typescript-codegen --input schemas --output codegen/interfaces

Repo

https://github.com/ejfamanas/typescript-scaffolder

Roadmap

[x] Generate typescript interfaces from schema definitions

[ ] Scaffolding for service mocking

[ ] Generate enums from definitions

[ ] Generate classes from schema definitions

[ ] Declarative function generation

License

Licensed under the MIT License.