jcwillox / typebox-x

Tools for working with TypeBox

Home Page:https://npmjs.com/@jcwillox/typebox-x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeBox Extensions

License Version Bundle Size Publish Size Code style

TypeBox extensions (typebox-x) is a utility library based around the sinclairzx81/typebox schema builder and validation library.

Features

  • Validate and coerce environment variables (like t3-env).
  • Additional kinds and shorthands, e.g. t.UUID.
  • Built in configuration for common formats, e.g. date, email, uri.
  • Built in support for NestJS

Install

pnpm add @jcwillox/typebox-x @sinclair/typebox

Load envs

First import the createEnv function, then pass it a typebox schema.

The createEnv function uses the clean, convert, default and decode function from typebox to parse you environment variables.

This means that it will strip extra keys, set default values, attempt to convert values to the destination type, e.g. "true" -> true, and then decode/check the value.

import { createEnv } from "@jcwillox/typebox-x";

const env = createEnv(
  t.Object({
    APP_ENV: t.Optional(t.String()),
    NODE_ENV: t.String({ default: "development" }),
    BOOL_FLAG: t.Boolean(),
  }),
);

console.log(env.BOOL_FLAG === true); // -> true

Formats

Simply import @jcwillox/typebox-x/formats before you perform any validations, usually you'll want to do this in you entrypoint. If a format is already defined with the same name, it will not be overwritten.

import "@jcwillox/typebox-x/formats";

NestJS

Use the typebox prefixed method decorators and provide the corresponding schemas.

import { Controller } from "@nestjs/common";
import { t } from "@jcwillox/typebox-x";
import { TypeboxGet } from "@jcwillox/typebox-x/nestjs";
// or if you prefer we also export non-prefixed methods
// import { Get } from "@jcwillox/typebox-x/nestjs";

@Controller()
export class AppController {
  @TypeboxGet(":my_id", {
    query: t.Object({
      limit: t.Optional(t.Integer({ default: 10 })),
    }),
    params: {
      my_id: t.UUID(),
    },
    response: t.Object({
      message: t.String(),
    }),
  })
  getHello() {
    return { message: "Hello World!" };
  }
}

You'll likely want to define your schemas outside the @TypeboxGet decorator, so you can also infer types from them, using Static<typeof schema>.

import { Controller, Param, Query } from "@nestjs/common";
import { t } from "@jcwillox/typebox-x";
import { TypeboxGet } from "@jcwillox/typebox-x/nestjs";
import { Static } from "@sinclair/typebox";

type Query = Static<typeof QuerySchema>;
const QuerySchema = t.Object({
  limit: t.Optional(t.Integer({ default: 10 })),
});

type Response = Static<typeof ResponseSchema>;
const ResponseSchema = t.Object({
  message: t.String(),
});

@Controller()
export class AppController {
  @TypeboxGet(":my_id", {
    query: QuerySchema,
    params: { my_id: t.UUID() },
    response: ResponseSchema,
  })
  getHello(@Param("my_id") myId: string, @Query() query: Query): Response {
    return { message: "Hello World!" };
  }
}

About

Tools for working with TypeBox

https://npmjs.com/@jcwillox/typebox-x

License:MIT License


Languages

Language:TypeScript 100.0%