Nestia Documentation

repository·master·Indexed 24 days ago

https://github.com/samchon/nestia

A high-performance toolkit for NestJS that automates the creation of type-safe SDKs, Swagger documentation, and E2E tests using pure TypeScript types. It includes @nestia/core for high-performance decorators and @nestia/sdk for SDK and test generation. Nestia provides significant performance gains over standard patterns, with runtime validation up to 20,000x faster than class-validator and JSON serialization up to 200x faster than class-transformer.

Tokens
63.1K
Snippets
140
Records
252
Agent score
58%

What's inside Nestia

  1. What is Nestia?

    master

    Nestia is a helper suite for NestJS that uses pure TypeScript types as the single source of truth for your API. It eliminates the need for manual DTO declarations with multiple decorators (like @ApiProperty for Swagger and @IsString for validation) by deriving everything from a single interface.

    Key capabilities include:

    • Runtime validation: Extremely fast validation of requests and responses (up to 20,000× faster than class-validator).
    • Typed client SDK: A REST-based SDK similar to tRPC that is auto-generated from your controllers.
    • OpenAPI documentation: Automatic generation of OpenAPI 3.1, 3.0, or 2.0 documents.
    • End-to-end testing: Generated test suites for every endpoint.
    • Mockup simulator: Allows frontend development by simulating API responses without a running backend.
  2. Overview of Nestia helper libraries

    master

    Nestia is a suite of helper libraries designed to enhance NestJS applications. It focuses on high performance, type safety, and automated developer workflows. Key benefits include:

    • Performance: Runtime validation is up to 20,000x faster than class-validator, and JSON serialization is up to 200x faster than class-transformer.
    • Type Safety: Uses pure TypeScript types for decorators, requiring only a single line of code to achieve advanced validation and documentation.
    • SDK Generation: Automatically generates a collection of typed fetch functions (similar to tRPC) and includes a Mockup Simulator for client-side development (similar to msw but fully automated).
    • Automated Testing: Generates E2E test functions and benchmark programs based on your API definitions.
  3. Overview of Nestia features and packages

    master

    Nestia is a suite of helper libraries designed for NestJS that optimizes performance and developer experience. Key components include:

    • @nestia/core: Provides high-performance decorators.
    • @nestia/sdk: A multi-purpose package that includes:
      • An SDK generator for clients.
      • An advanced Swagger generator.
      • An automatic E2E (End-to-End) test function generator.
    • nestia: The command-line interface (CLI) tool used to drive the generation processes.
  4. View Nestia performance benchmarks

    master

    This document provides benchmark results for nestia comparing its performance against standard NestJS implementations (Express and Fastify) across three main categories: assert, stringify, and overall performance.

    Benchmarks are measured in Megabytes/sec and evaluate different data structures including simple, hierarchical, recursive, and union types for both objects and arrays.

    Benchmark Categories

    • assert: Measures the speed of type assertion/validation.
    • stringify: Measures the speed of data serialization (stringification).
    • performance: Represents the combined performance metric.

    Environment Context

    These specific results were generated on:

    • CPU: AMD Ryzen 9 7940HS w/ Radeon 780M Graphics
    • Memory: 31,954 MB
    • OS: win32
    • NodeJS: v20.10.0
    • nestia version: v3.10.0-dev.20240803-2
  5. Benchmark results for nestia on AMD EPYC 7763

    master

    This document provides performance benchmarks for nestia compared to other frameworks (fastify, NestJS-express, NestJS-fastify) across three main categories: assert, stringify, and performance.

    Environment Details:

    • CPU: AMD EPYC 7763 64-Core Processor
    • Memory: 64,301 MB
    • OS: linux
    • NodeJS version: v19.9.0
    • Nestia version: v1.4.0

    Units: All values are measured in Megabytes/sec.

  6. Benchmark results for nestia

    master

    This document provides performance benchmarks for nestia across different operations: assert, stringify, and performance. The benchmarks compare nestia-express and nestia-fastify against standard fastify and NestJS implementations (both express and fastify based).

    Environment Details:

    • CPU: 12th Gen Intel(R) Core(TM) i5-1235U
    • Memory: 16,208 MB
    • OS: win32
    • NodeJS version: v20.6.1
    • nestia version: v2.4.3

    Benchmark Metrics: All values are measured in Megabytes/sec.

  7. What is Agentica and how does it work?

    master

    Agentica is an Agentic AI framework specialized in LLM Function Calling. It allows you to build AI chatbots directly from a Swagger (OpenAPI) document generated by @nestia/sdk.

    Unlike conventional AI agent development that requires complex agent-graph plumbing, @agentica uses the schema of your API to drive tool use automatically. It can consume both HTTP-based APIs (via OpenAPI) and local class-based logic (via typia.llm.application) to provide a unified interface for an LLM to interact with your backend services.

  8. What is Nestia Editor

    master

    Nestia Editor is a TypeScript-powered alternative to Swagger UI. It uses a StackBlitz-powered playground that comes with your generated SDK pre-installed. This allows you to:

    • Test endpoints with TypeScript: Get full autocomplete for request DTOs, response types, and helpers.
    • Use the Mockup Simulator: The SDK can answer requests using a built-in simulator, allowing API review even when the backend server is offline.
    • Run Auto e2e tests: Every endpoint includes a randomized test that can be executed with one click.

    To use it, you simply provide a swagger.json (or swagger.yaml) file.

  9. What is the TypeScript Swagger Editor

    master

    The TypeScript Swagger Editor is a web-based TypeScript editor (powered by StackBlitz) designed for Swagger API (OpenAPI) specifications. It uses the SDK library generated by nestia to provide a type-safe environment for interacting with APIs.

    Key capabilities include:

    • Testing backend API endpoints using real TypeScript code with type checking and auto-completion.
    • Mockup Simulator: An embedded backend simulator within the SDK that allows frontend development to proceed even if the backend API is not yet ready.
    • e2e Test Generation: Automatic generation of end-to-end test functions to validate APIs.

    Core Concepts

    • SDK (Software Development Kit): A collection of typed fetch functions paired with DTO (Data Transfer Object) structures, providing a developer experience similar to tRPC.
    • Mockup Simulator: A fully automated, embedded backend simulator within the SDK, similar to msw.js.
  10. Use Propagation Mode for error handling

    master

    By default, SDK calls throw an HttpError for any non-2xx response. If you enable propagate: true in your INestiaConfig, the SDK will return a discriminated union instead of throwing. This allows you to handle success and error states explicitly using type guards.

    Example Usage

    type Output = IPropagation<
      {
        200: IArticle;
        400: TypeGuardError.IProps;
      },
      200    // success branch
    >;
    
    const out = await api.functional.articles.create(connection, input);
    if (out.success)             out.data.id;       // IArticle (200)
    else if (out.status === 400) out.data.expected; // TypeGuardError.IProps (400)
    else                          out.data;          // unknown

    The status codes in the union are derived from @TypedRoute.* and @TypedException() decorators on your controller routes.

    type Output = IPropagation<
      {
        200: IArticle;
        400: TypeGuardError.IProps;
      },
      200    // success branch
    >;
    
    const out = await api.functional.articles.create(connection, input);
    if (out.success)             out.data.id;       // IArticle (200)
    else if (out.status === 400) out.data.expected; // TypeGuardError.IProps (400)
    else                          out.data;          // unknown — outside declared statuses
  11. Handle binary responses in the SDK

    master

    When a NestJS controller route declares a binary Content-Type using @Header("Content-Type", "...") or Swagger's @ApiProduces("..."), the generated SDK function automatically returns a ReadableStream<Uint8Array<ArrayBufferLike>> instead of attempting to parse the body as JSON or text.

    Supported binary formats include:

    • image/*
    • video/*
    • audio/*
    • application/octet-stream
    • application/pdf
    import { Controller, Get, Header, StreamableFile } from "@nestjs/common";
    
    @Controller("files")
    export class FileController {
      @Header("Content-Type", "image/png")
      @Get("thumbnail")
      public thumbnail(): StreamableFile {
        return new StreamableFile(/* readable source */);
      }
    }
  12. Nestia Documentation Strategy: Single-source JSDoc and Typia tags

    master

    Nestia uses a single-source documentation strategy. You write JSDoc comments and typia tags once in your source code, and Nestia automatically propagates them to three key areas:

    1. Swagger Summaries: Descriptions and metadata are extracted into your OpenAPI/Swagger documentation.
    2. DTO Schemas: Validation and structural metadata are used to generate robust Data Transfer Object schemas.
    3. LLM Function-Calling Definitions: The same metadata is used to define function-calling interfaces for Large Language Model (LLM) applications.

    This approach ensures that your API documentation, validation logic, and AI integration definitions remain perfectly synchronized without manual duplication.