包详细信息

type-crafter

sinha-sahil1.6kISC0.10.0

A tool to generate types from a yaml schema for any language

type-crafter, types, crafter, generation

自述文件

Type Crafter

TypeCrafter is a CLI tool for generating types from a YAML types specification for any language. The tool is heavily inspired by OpenAPI Generator and aims to provide similar functionality for generating types with a simple YAML specification & more flexibility.

Installation

npm i -g type-crafter

Usage

type-crafter generate <language> <types-specification-file> <output-directory>

Example

type-crafter generate typescript types.yaml ./types

Example input specification file can be found here

Input Specification

The input specification is a YAML file that contains the types specification. Refer the following sample specification for the structure:

info:
  version: 0.0.0
  title: Title of your specification
types:
  SampleType:
    type: object
    properties:
      name:
        type: string
groupedTypes:
  SampleGroupedType:
    type: object
    properties:
      name:
        type: string

The input specification yaml file must be of following syntax:

  • info - The information about the specification. Specifying the version and title of your spec. This is required.
  • types - These are types that will be generated in flat structure.
  • groupedTypes - These are types that will be generated and grouped in a folder.

Note: Passing types or groupedTypes is up to your expected results. A valid spec file can contain either types or groupedTypes or both.

The syntax for writing different types can be referred from the OpenAPI Data Types Guide.

Supported languages

  • [✔️] TypeScript
  • More languages will be added soon.

Contributing & Extending

Adding support for a new language

TypeCrafter uses Handlebars to template syntax for different languages.

To add support for a new language, you need to create a new folder in src/templates directory. The folder name will be the name of the language. The folder must implement following files:

  • index.ts - The main file that will be exporting the generator config.
  • object-syntax.hbs - This Handlebars template file that will be used to generate the object syntax.
  • type-file-syntax.hbs - This Handlebars template file that will be used to generate the syntax for file which contains the generated types & its imports.
  • exporter-module-syntax.hbs - This Handlebars template file that will be used to generate the syntax for the module that exports the generated types.

Development

To start developing type-crafter, you need to run following commands:

pnpm i
pnpm run dev

更新日志

type-crafter

0.8.2

Patch Changes

  • 004d45b: fix: issue with template file path in reading

    • fixed template directory not found issue

0.8.1

Patch Changes

  • 7e6b880: fix: conflicting type name & missing imports in generated output

    • fixed issues with conflicting types in same file & missing type imports in generated output
    • made filename case sensitive fro avoiding same types written in two different files with different naming case

0.8.0

Minor Changes

  • 0635e62: feat: added support for generating types with allOf operator

    • now use allOf operator for union of types

0.7.0

Minor Changes

  • bea1b41: feat: support for generating hashmaps

    • now generate object hashmaps using additionalProperties param

0.6.1

Patch Changes

  • 2220f71: fix: reference generation for FolderWithFiles writer mode for GroupedTypes

    • Issues with missing type imports in case of FolderWithFiles Group writer mode is now fixed

0.6.0

Minor Changes

  • 1e9e39f: feat: added support for referencing types & groups from a remote file

    • now you can reference groups & types from remote files
    • fixed issues with referenced type generation

0.5.0

Minor Changes

  • ffe6dc1: feat: added support for remote file reference

    • added support for remote file references
    • fixed issues array items decoders in typescript-with-decoders model

0.4.0

Minor Changes

  • e347421: feat: added support for oneOf operator in spec generation

    • now you use oneOf operator in spec yaml
    • supported language: typescript & typescript-with-decoders

0.3.0

Minor Changes

  • c6abf3a: feat: added support for generating enums

    • now users can generate enums from the spec.
    • this can be done using directly defined types, ref enums & inline enum declaration

0.2.1

Patch Changes

  • 6cfdb89: fix: issue with composer types of array elements in case of primitive data types

    • fixes issue with generated decoder for array types where items were of primitive type

0.2.0

Minor Changes

  • e6e788d: feat: added support for generating typescript types with decoders

    • now generate typescript types with decoders using typescript-with-decoders language param

0.1.1

Patch Changes

  • 2092d02: fix: output generation for ref array types

    • fix $ref parsing for array items
    • updated templates reader to read from package path

0.1.0

Minor Changes

  • de1d7a3: Releasing first beta version of type-crafter