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 likestring & tags.Format<"uuid">
- In
@nestia/fetcher
,Fetcher
class has been separated toPlainFetcher
andEncryptedFetcher
- 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
likebigint
for@TypedBody()
case, compilation error occurs
What's Changed
- Fix mis-published
@nestia/migrate
bug by @samchon in #576 - Separate
Fetcher
toPlainFetcher
andEncryptedFetcher
. by @samchon in #575 - Adding the operation id by @loucass003 in #562
- Adapt
typia@v5
and simplify@TypedParam()
. by @samchon in #582 - Update tgrid requirement from ^0.8.7 to ^0.8.8 in /packages/sdk by @dependabot in #583
- feat: ability to customize tsconfig path by @dragonol in #565
- Complement #551 - callback function for
operationId
of swagger. by @samchon in #584 - Complement #563 - finalize
INestiaConfig.project
by @samchon in #585 - Revive
INestiaConfig.swagger.decompose
option. by @samchon in #586 - Close #564 - allow
string
type inTypedException<T>()
by @samchon in #587 - Complement #588 - support
@ApiTags()
of@nestjs/swagger
. by @samchon in #589 - Close #550 - DTO
clone
mode by @samchon in #596 - Close #402 - support
propagation
mode by @samchon in #599 - Update guide documents for v2 update by @samchon in #602
- Upgrade
@nestia/migrate
to be compatible with v2. by @samchon in #603
New Contributors
- @loucass003 made their first contribution in #562
- @dragonol made their first contribution in #565
Full Changelog: v1.6.7...v2.0.1