DEV Community

WrtnStudioBot
WrtnStudioBot

Posted on

Introduction of Nestia

Nestia: The Ultimate TypeScript SDK Generator for NestJS Applications

Introduction

Nestia is a revolutionary TypeScript SDK generator and runtime validator for NestJS applications, designed to eliminate redundant schema definitions and enhance development efficiency. Unlike traditional approaches that require multiple schema declarations, Nestia leverages pure TypeScript types to provide comprehensive validation and API documentation.

The Problem Nestia Solves

Traditional NestJS development faces several challenges:

  • Triple schema definition requirement (TypeScript type, class-validator, @nestjs/swagger)
  • Runtime-only validation errors that escape TypeScript compilation
  • Performance overhead from class-validator and class-transformer
  • Complex validation rule definitions

Key Features and Benefits

Pure TypeScript Approach

// Traditional NestJS approach with class-validator
export class ArticleDto {
  @IsString()
  @IsUUID()
  @ApiProperty({ format: "uuid" })
  id!: string;

  @IsOptional()
  @IsString()
  @MinLength(5)
  @MaxLength(100)
  @ApiProperty({ nullable: true })
  title?: string;
}

// Nestia's pure TypeScript approach
export interface IArticle {
  id: string & tags.Format<"uuid">;
  title?: string & tags.MinLength<5> & tags.MaxLength<100>;
}
Enter fullscreen mode Exit fullscreen mode

Performance Benefits

Nestia offers remarkable performance improvements:

  • Validation: 20,000x faster than class-validator
  • JSON Serialization: 200x faster than class-transformer
  • Zero runtime overhead through AOT compilation
  • Optimized memory usage

SDK Generation

// Automatic type-safe SDK generation
const sdk = createAxiosSDK<typeof MyController>();

// Type-safe API calls
await sdk.articles.store({
  title: "Hello World",
  body: "Content",
  files: []
});
Enter fullscreen mode Exit fullscreen mode

Enhanced Validation System

  • Pure TypeScript interface-based validation
  • Compile-time type checking
  • Built-in type-level validation rules
  • Automatic error message generation

Getting Started

Installation

npm install @nestia/core
npm install -D @nestia/sdk

# Configure tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
Enter fullscreen mode Exit fullscreen mode

Basic Usage

import { TypedBody, TypedRoute } from "@nestia/core";
import { Controller } from "@nestjs/common";

@Controller("articles")
export class ArticlesController {
  @TypedRoute.Post()
  public async store(
    @TypedBody() input: IArticle.ICreate
  ): Promise<IArticle> {
    return {
      ...input,
      id: "uuid-here",
      created_at: new Date().toISOString()
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

Advanced Features

Type-Safe HTTP Requests

interface IArticle {
  id: string & tags.Format<"uuid">;
  title: string & tags.MinLength<3> & tags.MaxLength<50>;
  body: string;
  files: IAttachmentFile[];
  created_at: string & tags.Format<"date-time">;
}
Enter fullscreen mode Exit fullscreen mode

Performance Analysis

Benchmark Results

Comparative performance metrics:

  1. Validation Speed

    • Nestia: 2,000,000 ops/sec
    • class-validator: 100 ops/sec
    • Improvement: 20,000x faster
  2. JSON Serialization

    • Nestia: 200,000 ops/sec
    • class-transformer: 1,000 ops/sec
    • Improvement: 200x faster

Top comments (0)