1.4.2 • Published 4 months ago

untyped v1.4.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 months ago

untyped

npm version npm downloads Github Actions Codecov bundle

▶️ Check online playground

Install

# npm
npm i untyped

# yarn
yarn add untyped

# pnpm
pnpm add untyped

Usage

First we have to define a reference object that describes types, defaults, and a $resolve method (normalizer).

const defaultPlanet = {
  name: "earth",
  specs: {
    gravity: {
      $resolve: (val) => parseFloat(val),
      $default: "9.8",
    },
    moons: {
      $resolve: (val = ["moon"]) => [].concat(val),
      $schema: {
        title: "planet moons",
      },
    },
  },
};

API

resolveSchema

import { resolveSchema } from "untyped";

const schema = await resolveSchema(defaultPlanet);

Output:

{
  "properties": {
    "name": {
      "type": "string",
      "default": "earth"
    },
    "specs": {
      "properties": {
        "gravity": {
          "default": 9.8,
          "type": "number"
        },
        "moons": {
          "title": "planet moons",
          "default": ["moon"],
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}

generateTypes

import { resolveSchema, generateTypes } from "untyped";

const types = generateTypes(await resolveSchema(defaultPlanet));

Output:

interface Untyped {
  /** @default "earth" */
  name: string;

  specs: {
    /** @default 9.8 */
    gravity: number;

    /**
     * planet moons
     * @default ["moon"]
     */
    moons: string[];
  };
}

generateMarkdown

import { resolveSchema, generateMarkdown } from "untyped";

const markdown = generateMarkdown(await resolveSchema(defaultPlanet));

Output:

# `name`

- **Type**: `string`
- **Default**: `"earth"`

# `specs`

## `gravity`

- **Type**: `number`
- **Default**: `9.8`

## `moons`

- **Type**: `array`
- **Default**: `["moon"]`

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev
  • Use pnpm web to start playground website
  • Use pnpm test before push to ensure all tests and lint checks passing

License

MIT

Thanks to @dominikschreiber for donating package name.

1.4.2

4 months ago

1.4.1

4 months ago

1.4.0

9 months ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

0.4.7

2 years ago

0.4.6

2 years ago

0.5.0

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.2.13

2 years ago

0.3.0

2 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

9 years ago