UNPKG

@tsed/schema

Version:
171 lines (170 loc) 5.22 kB
import { Type } from "@tsed/core"; import { type JsonClassStore } from "./JsonClassStore.js"; import { JsonEntityStore, JsonEntityStoreOptions } from "./JsonEntityStore.js"; import { JsonOperation } from "./JsonOperation.js"; import { type JsonParameterStore } from "./JsonParameterStore.js"; import { JsonSchema } from "./JsonSchema.js"; /** * Configuration options for view rendering. * * @public */ export interface JsonViewOptions { path: string; options: any; } /** * Configuration options for HTTP redirects. * * @public */ export interface JsonRedirectOptions { status: number | undefined; url: string; } /** * Store for method metadata, operations, and parameter information. * * JsonMethodStore manages metadata for controller methods decorated with operation * decorators like `@Get()`, `@Post()`, `@Returns()`, etc. It maintains the JsonOperation * for OpenAPI generation and coordinates method parameters. * * ### Responsibilities * * - **Operation Management**: Maintains JsonOperation for OpenAPI spec generation * - **Parameter Coordination**: Manages child JsonParameterStore instances * - **Middleware Configuration**: Handles before/after/use middleware registration * - **Response Configuration**: Manages return types and status codes * - **Route Information**: Stores HTTP methods, paths, and route metadata * * ### Usage * * ```typescript * // Get method store * const methodStore = JsonEntityStore.from(MyController, "myMethod"); * * // Access operation for OpenAPI * const operation = methodStore.operation; * * // Get parameters * const params = methodStore.parameters; * * // Check response type * const returnType = methodStore.type; * ``` * * ### Operation Structure * * Each method store contains a JsonOperation that includes: * - HTTP method and path information * - Request parameters (path, query, body, headers) * - Response definitions by status code * - Security requirements * - Tags and metadata * * ### Parameter Management * * The store maintains parameter metadata: * - Parameters stored in `children` map by index * - Accessible via `parameters` or `params` getters * - Each parameter has its own JsonParameterStore * * ### Middleware Integration * * Supports three middleware phases: * - **before**: Execute before the method * - **use**: Execute as main middleware * - **after**: Execute after the method * * @public */ export declare class JsonMethodStore extends JsonEntityStore { readonly parent: JsonClassStore; middlewares: any[]; beforeMiddlewares: any[]; afterMiddlewares: any[]; /** * Ref to JsonOperation when the decorated object is a method. */ readonly operation: JsonOperation; /** * List of children JsonEntityStore (properties or methods or params) */ readonly children: Map<string | number, JsonParameterStore>; constructor(options: JsonEntityStoreOptions); get params(): JsonParameterStore[]; get view(): JsonViewOptions; set view(view: JsonViewOptions); get acceptMimes(): string[]; set acceptMimes(mimes: string[]); get parameters(): JsonParameterStore[]; get operationPaths(): Map<string, import("./JsonOperation.js").JsonMethodPath>; get collectionType(): Type<any>; set collectionType(type: Type<any>); get isCollection(): boolean; get schema(): JsonSchema; /** * Get an endpoint. * @param target * @param propertyKey * @param descriptor */ static get(target: Type<any>, propertyKey: string | symbol, descriptor?: PropertyDescriptor): JsonMethodStore; /** * TODO must be located on JsonOperation level directly * @param status * @param contentType * @param includes */ getResponseOptions(status: number, { contentType, includes }?: { contentType?: string; includes?: string[]; }): undefined | any; /** * Append middlewares to the beforeMiddlewares list. * @param args * @returns {EndpointMetadata} */ before(args: Function[]): this; /** * Append middlewares to the afterMiddlewares list. * @param args * @returns {EndpointMetadata} */ after(args: Function[]): this; /** * Store all arguments collected via Annotation. * @param args */ use(args: Function[]): this; /** * Find the value at the controller level. Let this value be extended or overridden by the endpoint itself. * * @param key * @returns {any} */ get<T = any>(key: any): T; getParamTypes(): Record<string, boolean>; protected build(): void; } /** * EndpointMetadata contains metadata about a controller and his method. * Each annotation (@Get, @Body...) attached to a method are stored into endpoint. * EndpointMetadata converts this metadata to an array which contain arguments to call an Express method. * * Example : * *```ts * @Controller("/my-path") * provide MyClass { * * @Get("/") * @Authenticated() * public myMethod(){} * } *``` * * @alias JsonMethodStore */ export type EndpointMetadata = JsonMethodStore; export declare const EndpointMetadata: typeof JsonMethodStore;