UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

179 lines (178 loc) 7.63 kB
import type { IJsonSchemaCollection, IMetadataComponents, IMetadataSchema, IMetadataTypeTag, OpenApi } from "@typia/interface"; declare module "@typia/interface" { namespace IMetadataSchema { interface IReference { type?: IArrayType | ITupleType | IObjectType | IAliasType; } } } /** * Plain `IMetadataSchema` augmented with the fields the nestia transform * pre-bakes. `size`, `name`, and `empty` replace the methods the legacy * `MetadataSchema` class exposed; `jsonSchema` is the OpenAPI 3.1 conversion * typia's Go-side produces but does not expose to JS at runtime. * * Fields are declared optional so that nested `IMetadataSchema` values * (`metadata.rest`, `metadata.escaped.original`, `IObjectType.value`, …) — * which are not top-level route inputs and therefore do not carry the pre-baked * overlay — still satisfy the type when passed through legacy utilities. The * utilities read the fields via the optional accessor and fall back when * absent. */ export interface IReflectMetadata extends IMetadataSchema { size?: number; name?: string; empty?: boolean; jsonSchema?: IReflectJsonSchema; } export interface IReflectJsonSchema { version: "3.0" | "3.1"; components: OpenApi.IComponents; schema: OpenApi.IJsonSchema; } /** * Cross-reference dictionary produced by `MetadataComponents.from`. Each map is * keyed by the entry's `.name`, matching the lookup pattern the legacy * `MetadataComponents.dictionary` getter offered. */ export interface IMetadataDictionary { objects: Map<string, IMetadataSchema.IObjectType>; aliases: Map<string, IMetadataSchema.IAliasType>; arrays: Map<string, IMetadataSchema.IArrayType>; tuples: Map<string, IMetadataSchema.ITupleType>; } export type MetadataSchema = IMetadataSchema; export type MetadataComponents = IMetadataComponents & { dictionary: IMetadataDictionary; }; export type MetadataAliasType = IMetadataSchema.IAliasType; export type MetadataArrayType = IMetadataSchema.IArrayType; export type MetadataTupleType = IMetadataSchema.ITupleType; export type MetadataObjectType = IMetadataSchema.IObjectType; export type MetadataAtomic = IMetadataSchema.IAtomic; /** * Flattened constant-value shape: typia v13 splits `IConstant.IValue<T>` by the * atomic discriminator, but sdk's literal writer just needs the runtime `value` * payload, so collapse the union to a single ergonomic shape. */ export interface MetadataConstantValue { value: string | number | bigint | boolean; tags: IMetadataTypeTag[][]; } export type MetadataEscaped = IMetadataSchema.IEscaped; export type MetadataProperty = IMetadataSchema.IProperty; /** * Reference to a named array/tuple/object/alias type. typia v13's plain * `IReference` carries only the symbolic `name` + tags; the legacy class * additionally exposed `.type` as a getter that resolved against the * dictionary. `MetadataSchema.from` walks the metadata tree once and attaches * the resolved `.type` field so downstream sdk code can keep its `ref.type` * access pattern. */ export type MetadataArray = IMetadataSchema.IReference & { type: MetadataArrayType; }; export type MetadataTuple = IMetadataSchema.IReference & { type: MetadataTupleType; }; export type MetadataObject = IMetadataSchema.IReference & { type: MetadataObjectType; }; export type MetadataAlias = IMetadataSchema.IReference & { type: MetadataAliasType; }; /** `MetadataSchema.size()` → reads the pre-baked `size` field. */ export declare const sizeOf: (m: IMetadataSchema) => number; /** `MetadataSchema.getName()` → reads the pre-baked `name` field. */ export declare const nameOf: (m: IMetadataSchema) => string; /** `MetadataSchema.empty()` → reads the pre-baked `empty` field. */ export declare const emptyOf: (m: IMetadataSchema) => boolean; /** * Equivalent of the legacy `MetadataSchema.isSoleLiteral()` method: `true` when * the schema represents exactly one constant literal value and nothing else. * Used by sdk's type printer to fall back to literal emission instead of a * union. */ export declare const isSoleLiteralOf: (m: IMetadataSchema) => boolean; export declare namespace MetadataComponents { const from: (plain: IMetadataComponents) => MetadataComponents; } export declare namespace MetadataSchema { /** * Walks the metadata tree and attaches the resolved `.type` field to every * `IReference` it encounters, using the supplied dictionary as the lookup * index. This is idempotent — references whose `.type` has already been * resolved are left alone — and mutates the input, matching the in-place * resolution model `@typia/core` 12.x used. */ const from: (plain: IMetadataSchema, dictionary?: IMetadataDictionary) => IMetadataSchema; } export declare namespace MetadataFactory { interface IExplore { object: IMetadataSchema.IObjectType | null; property: string | null; parameter: string | null; output: boolean; } interface IError { name: string; explore: IExplore; messages: string[]; } type Validator = (props: { metadata: IMetadataSchema; explore: IExplore; }) => string[]; /** * Walks the metadata tree once, invoking the provided validator on each * visited node, and accumulates the produced messages into `IError` entries. * The walk skips back-edges through references so cyclic structures * terminate. This is a faithful reimplementation of the legacy `@typia/core` * walker, kept lean: the typia native transform has already validated * structural invariants, so the validator is only called for SDK-side policy * checks (JSON-serializability, query/header atomic-only rules, …). */ const validate: (props: { options?: unknown; functor: Validator; metadata: IMetadataSchema; }) => IError[]; } export declare namespace JsonMetadataFactory { /** * Rejects metadata that cannot be losslessly JSON-serialized. The typia * native runtime already screens out most structurally invalid types, so this * only adds the JSON-policy bans the legacy `@typia/core` walker enforced — * bare `bigint` payloads, function-typed properties, and `Map` / `Set` * containers that have no canonical JSON representation. */ const validate: MetadataFactory.Validator; } export declare namespace HttpQueryProgrammer { const validate: MetadataFactory.Validator; } export declare namespace HttpHeadersProgrammer { const validate: MetadataFactory.Validator; } export declare namespace HttpParameterProgrammer { const validate: MetadataFactory.Validator; } export declare namespace HttpFormDataProgrammer { const validate: MetadataFactory.Validator; } export declare namespace JsonSchemasProgrammer { /** * Consumes the per-metadata `jsonSchema` field the nestia transform * pre-bakes. Top-level route metadata (success / parameter / exception) * always carries a baked schema; for nested metadata (object property values * reached by the decomposed-query path), the bake is absent and this function * falls back to a minimal JS-side converter that handles the schema shapes * decompose actually emits — atomics, constants, templates, arrays of those, * named references — without re-implementing the typia native programmer * wholesale. */ const writeSchemas: (props: { version: "3.0" | "3.1"; metadatas: readonly IMetadataSchema[]; }) => IJsonSchemaCollection; }