Nestia Core
Super-fast validation decorators for NestJS.
- 15,000x faster request body validation
- 10x faster JSON response, even type safe
- Do not need DTO class definition, just fine with interface
@nestia/core
is a transformer library of NestJS, supporting super-fast validation decorators, by wrapping typia. Comparing validation speed with class-validator
, typia is maximum 15,000x times faster and it is even much safer.
Furthermore, @nestia/core
can use pure interface typed DTO with only one line. With @nestia/core
, you don't need any extra dedication like defining JSON schema (@nestjs/swagger
), or using class definition with decorator function calls (class-validator
). Just enjoy the superfast decorators with pure TypeScript type.
import { Controller } from "@nestjs/common";
import { TypedBody, TypedRoute } from "@nestia/core";
import type { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
@Controller("bbs/articles")
export class BbsArticlesController {
@TypedRoute.Post()
public async store(
@TypedBody() input: IBbsArticle.IStore
): Promise<IBbsArticle>;
}
Setup
Boilerplate Project
npx nestia start <directory>
Just run above command, then boilerplate project would be constructed.
Setup Wizard
npx nestia setup
npx @nestia/core setup
When you run npx nestia setup
command, all installation and configuration processes would be automatically done. If you want to setup @nestia/core
only, run npx @nestia/core setup
command instead.
After the setup has been fully completed, you can compile your backend server code by using ttsc
command. If you want to run your TypeScript file directly through ts-node
, add -C ttypescript
argument like below:
npx ttsc
npx ts-node -C ttypescript src/index.ts
Also, you can specify package manager or target tsconfig.json
file like below:
npx @nestia/core setup --manager npm
npx @nestia/core setup --manager pnpm
npx @nestia/core setup --manager yarn
npx @nestia/core setup --project tsconfig.json
npx @nestia/core setup --project tsconfig.test.json
Manual Setup
If you want to install and configure @nestia/core
manually, read Guide Documents - Setup.
Features
import { Controller } from "@nestjs/common";
import { TypedBody, TypedRoute } from "@nestia/core";
import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
@Controller("bbs/articles")
export class BbsArticlesController {
@TypedRoute.Post()
public async store(
@TypedBody() input: IBbsArticle.IStore
): Promise<IBbsArticle>;
}
TypedBody
TypedBody()
is a decorator function of application/json
typed request body.
Also, it supports super-fast validation pipe, which is maximum 15,000x times faster then nest.Body()
function using class-validator
.
TypedRoute
TypedRoute
is a set of decorator functions for application/json
typed response body.
Also, it supports safe and fast JSON stringify function pipe, which is maximum 10x times faster than native JSON.stringify()
function. Furthermore, it is type safe through validation.
TypedRoute.Get()
TypedRoute.Post()
TypedRoute.Put()
TypedRoute.Patch()
TypedRoute.Delete()
TypedQuery
TypedQuery
is a decorator function for URLSearchParams
of path
.
Also, it supports automatic type casting for property types and super-fast validation pipe.
interface SomeSearchParams {
page: number;
limit?: number;
extension?: string;
status: "alive" | "erased" | "both";
}
@Controller("some-path")
export class SomeController {
@TypedRoute.Get()
public async index(
@TypedQuery() query: SomeSearchParams
): Promise<SomeEntity[]>;
}
Encryption
@nestia/core
supports special decorator functions EncryptedBody
and EncryptedRout
. They're almost same with TypedBody and TypedRoute, but there's only one thing different - it encrypts JSON data through AES-128/256 algorithm.
- AES-128/256
- CBC mode
- PKCS #5 Padding
- Base64 Encoding
You can enhance DTO type validation by writing comment tags.
If you want to know about it detaily, visit Guide Documents of typia.
export interface IBbsArticle {
id: string;
writer: IBbsArticle.IWriter;
contents: IBbsArticle.IContent[];
}
export namespace IBbsArticle {
export interface IWriter {
name: string;
email: string;
mobile: string;
age: number;
}
}
Appendix
Nestia SDK
npx nestia swagger
npx nestia sdk
Automatic SDK and Swagger generator for @nestia/core
.
With @nestia/core
, you can boost up validation speed maximum 15,000x times faster. However, as @nestjs/swagger
does not support @nestia/core
, you can't generate swagger documents from @nestjs/swagger
more.
Instead, I provide you @nestia/sdk
module, which can generate not only swagger documents, but also SDK (Software Development Kit) library.
BbsArticlesController.ts
import { Controller } from "@nestjs/common";
import { TypedBody, TypedRoute } from "@nestia/core";
import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
@Controller("bbs/articles")
export class BbsArticlesController {
@TypedRoute.Post()
public async store(
@TypedBody() input: IBbsArticle.IStore
): Promise<IBbsArticle>;
}
src/functional/bbs/articles/index.ts
import { Fetcher, IConnection } from "@nestia/fetcher";
import { IBbsArticle } from "../../../structures/IBbsArticle";
export function store(
connection: api.IConnection,
input: IBbsArticle.IStore
): Promise<IBbsArticle> {
return Fetcher.fetch(
connection,
store.ENCRYPTED,
store.METHOD,
store.path(),
input
);
}
export namespace store {
export const METHOD = "POST" as const;
export function path(): string {
return "/bbs/articles";
}
}
SDK utilization code
import api from "@bbs-api";
import typia from "typia";
export async function test_bbs_article_store(connection: api.IConnection) {
const article: IBbsArticle = await api.functional.bbs.articles.store(
connection,
{
name: "John Doe",
title: "some title",
content: "some content",
}
);
typia.assert(article);
console.log(article);
}
Typia
https://github.com/samchon/typia
@nestia/core
is wrapping typia
and the typia
is:
export function is<T>(input: unknown | T): input is T;
export function assert<T>(input: unknown | T): T;
export function validate<T>(input: unknown | T): IValidation<T>;
export function equals<T>(input: unknown | T): input is T;
export function assertEquals<T>(input: unknown | T): T;
export function validateEquals<T>(input: unknown | T): IValidation<T>;
export function application<T>(): IJsonApplication;
export function assertParse<T>(input: string): T;
export function assertStringify<T>(input: T): string;
typia
is a transformer library of TypeScript, supporting below features:
- Super-fast Runtime Validators
- Safe JSON parse and fast stringify functions
- JSON schema generator
All functions in typia
require only one line. You don't need any extra dedication like JSON schema definitions or decorator function calls. Just call typia
function with only one line like typia.assert<T>(input)
.
Also, as typia
performs AOT (Ahead of Time) compilation skill, its performance is much faster than other competitive libaries. For an example, when comparing validate function is()
with other competitive libraries, typia
is maximum 15,000x times faster than class-validator
.