JSPM

  • Created
  • Published
  • Downloads 479
  • Score
    100M100P100Q89386F
  • 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.3.2-beta

Environment Variable Interface (Beta)

  • Reduces need for calling dotenv.config() from multiple areas
  • Creates an environment variable handler
  • Automatically generated based on keys declared in .env file
  • Automatically creates default values based on declared in .env file
  • Supports filenames such as .env.local or .env.prod as well

This file:

SCHEMAS_DIR="schemas"
OUTPUT_DIR_ROOT="codegen"
INTERFACES_ROOT="interfaces"

Will give you:

export class EnvConfig {
    static readonly SCHEMAS_DIR: string = process.env.SCHEMAS_DIR ?? '"schemas"';
    static readonly OUTPUT_DIR_ROOT: string = process.env.OUTPUT_DIR_ROOT ?? '"codegen"';
    static readonly INTERFACES_ROOT: string = process.env.INTERFACES_ROOT ?? '"interfaces"';
}

export enum EnvKeys {
    SCHEMAS_DIR = "SCHEMAS_DIR",
    OUTPUT_DIR_ROOT = "OUTPUT_DIR_ROOT",
    INTERFACES_ROOT = "INTERFACES_ROOT"
}

Interface Generation (Stable)

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

This file:

{
  "id": "u_123",
  "email": "alice@example.com",
  "age": 29,
  "isActive": true,
  "roles": ["admin", "editor"],
  "preferences": {
    "newsletter": true,
    "theme": "dark"
  },
  "lastLogin": "2024-12-01T10:15:30Z"
}

Will give you:

export interface User {
    id:          string;
    email:       string;
    age:         number;
    isActive:    boolean;
    roles:       string[];
    preferences: Preferences;
    lastLogin:   Date;
}

export interface Preferences {
    newsletter: boolean;
    theme:      string;
}

Enum Generation from Interface (Beta)

Generate TypeScript enums automatically from existing interfaces to create type-safe keys.

  • Handles multiple interfaces per file
  • Preserves directory structure from i.e. schemas/<folder_name> into codegen/enums/<folder_name>
  • Automatically creates output folders if they don't exist

This file:

export interface User {
    id:          string;
    email:       string;
    age:         number;
    isActive:    boolean;
    roles:       string[];
    preferences: Preferences;
    lastLogin:   Date;
}

export interface Preferences {
    newsletter: boolean;
    theme:      string;
}

Will give you:

export enum UserKeys {
  id = "id",
  email = "email",
  age = "age",
  isActive = "isActive",
  roles = "roles",
  preferences = "preferences",
  lastLogin = "lastLogin"
}

export enum PreferencesKeys {
  newsletter = "newsletter",
  theme = "theme"
}

Mock Server Generation (GET) (Alpha)

  • Reads all json files within a directory tree and scaffolds out mock server endpoints for GET requests

Installation

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

Usages in code

Please refer to the following code block for example usages:

import path from "path";
import {
    generateEnumsFromPath,
    generateEnvLoader,
    generateInterfacesFromPath,
    scaffoldMockServer
    } from "typescript-scaffolder";

const ROOT_DIR = process.cwd();                // Base dir where the script is run
const LOCAL_DIR = __dirname;                   // Base dir where this file lives

// Interface generation config
const SCHEMA_INPUT_DIR      = path.resolve(LOCAL_DIR, 'schemas');
const INTERFACE_OUTPUT_DIR  = path.resolve(LOCAL_DIR, 'codegen/interfaces');

// Generate enums, this will use the previously generated interface output
const ENUM_OUTPUT_DIR       = path.resolve(LOCAL_DIR, 'codegen/enums');

// Env accessor config
const ENV_FILE              = path.resolve(ROOT_DIR, '.env');
const ENV_OUTPUT_DIR        = path.resolve(LOCAL_DIR, 'codegen/config');
const ENV_OUTPUT_FILE       = 'env-config.ts';

async function main(): Promise<void> {
    // using the env accessor (BETA)
    // this is a sync function, and should be run first anyway
    generateEnvLoader(ENV_FILE, ENV_OUTPUT_DIR, ENV_OUTPUT_FILE);

    // using the interface generator (STABLE)
    await generateInterfacesFromPath(SCHEMA_INPUT_DIR, INTERFACE_OUTPUT_DIR)

    // use the enum generator from the output of the interface generator (BETA)
    await generateEnumsFromPath(INTERFACE_OUTPUT_DIR, ENUM_OUTPUT_DIR);

    // using the mock server scaffolder (ALPHA)
    await scaffoldMockServer(SCHEMA_INPUT_DIR);
}

main();

Roadmap

[X] Generate typescript interfaces from schema definitions
[X] Generate typescript enums to assert key names to avoid magic strings
[X] Generate typescript accessor to access environment variables
[ ] Command line interface access [X] Scaffolding for service mocking (GET)
[ ] Scaffolding for service mocking (POST)
[ ] Scaffolding for service mocking (PUT)
[ ] Scaffolding for service mocking (DELETE)
[ ] Generate enums from definitions
[ ] Generate classes from schema definitions
[ ] Declarative function generation

License

Licensed under the MIT License.