github samchon/nestia v2.0.1

latest releases: v3.19.1, v3.18.0, v3.17.0...
14 months ago

From typia v5 update

https://typia.io/docs/validators/tags/#type-tags

typia v5 version has been newly released, and it has started supporting type tags.

import { tags } from "typia";
type X = number & tags.Type<"uint32"> & tags.Minimum<3>;

From now on, nestia also supports the type tags, and TypedParam() has been concised especially by those type tags.

import { TypedParam } from "@nestia/core";
import { Controller, Get } from "@nestjs/common";
import { tags } from "typia";
 
@Controller("parameters")
export class ParametersController {
    @Get("uint32/:value")
    public async uint32(
        @TypedParam("value") value: (number & tags.Type<"uint32">) | null,
    ): Promise<(number & tags.Type<"uint32">) | null> {
        return value;
    }
 
    @Get("string/:value")
    public async string(
        @TypedParam("value") value: string,
    ): Promise<string> {
        return value;
    }
 
    @Get("uuid/:value")
    public async uuid(
        @TypedParam("value") value: string & tags.Format<"uuid">,
    ): Promise<string> {
        return value;
    }
}

Propagation mode

https://nestia.io/docs/sdk/sdk/#propagation-mode

Returns IPropagation typed instance instead of throwing exception.

When you configure propagate property of nestia.config.ts file, all of SDK functions generated by @nestia/sdk will perform propagation mode. The propagation mode means that never throwing exception (HttpError) even when response status code is not (or 201), but just returning the IPropagation typed object, which can specify its body data type through discriminated union determined by status code.

This options has been designed especially support for someone who like Try<T, E> styled types.

import { Primitive } from "./Primitive";

/**
 * Propagation type.
 *
 * `IPropagation` is a type gathering all possible status codes and their body
 * data types as a discriminated union type. You can specify the status code and
 * its body data type just by using conditional statement like below.
 *
 * ```typescript
 * type Output = IPropagation<{
 *    200: ISeller.IAuthorized;
 *    400: TypeGuardError.IProps;
 * >};
 *
 * const output: Output = await sdk.sellers.authenticate.join(input);
 * if (output.success) {
 *     // automatically casted to "ISeller.IAuthorized" type
 *     const authorized: ISeller.IAuthorized = output.data;
 * } else if (output.status === 400) {
 *     // automatically casted to "TypeGuardError.IProps" type
 *     const error: TypeGuardError.IProps = output.data;
 * } else {
 *     // unknown type when out of pre-defined status codes
 *     const result: unknown = output.data;
 * }
 * ```
 *
 * For reference, this `IPropagation` type is utilized by SDK library generated by
 * `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
 * In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
 * typed data directly, but returns this `IPropagation` typed object instead.
 *
 * @template StatusMap Map of status code and its body data type.
 * @template Success Default success status code.
 * @author Jeongho Nam - https://github.com/samchon
 */
export type IPropagation<
    StatusMap extends {
        [P in IPropagation.Status]?: any;
    },
    Success extends number = 200 | 201,
> =
    | {
          [P in keyof StatusMap]: IPropagation.IBranch<
              P extends Success ? true : false,
              P,
              StatusMap[P]
          >;
      }[keyof StatusMap]
    | IPropagation.IBranch<false, unknown, unknown>;
export namespace IPropagation {
    /**
     * Type of configurable status codes.
     *
     * The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
     * of status codes. If `5XX` is specified, it means the status code is in the
     * range of `500` to `599`.
     */
    export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";

    /**
     * Branch type of propagation.
     *
     * `IPropagation.IBranch` is a branch type composing `IPropagation` type,
     * which is gathering all possible status codes and their body data types
     * as a union type.
     */
    export interface IBranch<Success extends boolean, StatusValue, BodyData> {
        success: Success;
        status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
            ? StatusRange<StatusValue>
            : StatusValue extends number
            ? StatusValue
            : never;
        data: Primitive<BodyData>;
        headers: Record<string, string | string[]>;
    }

    /**
     * Range of status codes by the first digit.
     */
    export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> =
        T extends 0
            ? IntRange<200, 299>
            : T extends 3
            ? IntRange<300, 399>
            : T extends 4
            ? IntRange<400, 499>
            : IntRange<500, 599>;

    type IntRange<F extends number, T extends number> = Exclude<
        Enumerate<T>,
        Enumerate<F>
    >;

    type Enumerate<
        N extends number,
        Acc extends number[] = [],
    > = Acc["length"] extends N
        ? Acc[number]
        : Enumerate<N, [...Acc, Acc["length"]]>;
}

DTO clone mode

https://github.com/samchon/nestia/tree/master/test/features/clone/src/api/structures

When you configure clone property of nestia.config.ts file, SDK library generated by @nestia/sdk will utilize cloned DTO structures.

The cloned DTO structures will keep every types and even comments of original type, but if the original is a generic type, name of cloned DTO structure can be ugly.

Break Changes

  • @TypedParam() no more gets 2nd parameter like "uuid". Just define with type tag like string & tags.Format<"uuid">
  • In @nestia/fetcher, Fetcher class has been separated to PlainFetcher and EncryptedFetcher
    • Those fetcher classes are not exported in the root scope
    • SDK functions will import proper fetcher class, and it would reduce bundling size of SDK in the frontend side
    • Especially, if EncryptedFetcher not being used, crypto module of NodeJS never be bundled more
  • When unsupported type being used in @nestia/core like bigint for @TypedBody() case, compilation error occurs

What's Changed

New Contributors

Full Changelog: v1.6.7...v2.0.1

Don't miss a new nestia release

NewReleases is sending notifications on new releases.