UNPKG

@ffsm/napi

Version:

Napi - A framework using with Next.js for building APIs.

69 lines (68 loc) 4.2 kB
import { NapiExecuteContext } from "./context"; import { NapiHttpStatus, NapiMethod } from "./enums"; import { NapiCallback, NapiOptionAsync } from "./types"; export interface NapiControllerOptions { path?: string; } export interface NapiMethodContext { controller: NapiController; target: object; propertyKey: string | symbol; descriptor: PropertyDescriptor; } export interface NapiMethodOptions { path?: string; } export type NapiDecoratorHandler<Context, Data = unknown> = NapiCallback<[Data, Context], unknown>; export type NapiDecorator<Result, Data = unknown> = (options?: Data) => Result; export interface NapiParamPayload<Options = unknown, Res = unknown> { handle: NapiDecoratorHandler<NapiExecuteContext<Res>, Options>; target: object; options: Options | undefined; index: number; result?: unknown; } export type NapiHeaders<Res = unknown> = NapiOptionAsync<Record<string, string>, NapiExecuteContext<Res>>; export interface NapiResponsePayload<Res = unknown> { status?: NapiHttpStatus; statusText?: string; headers?: NapiHeaders<Res>; cookies?: Record<string, string>; } export declare class NapiController { private readonly targer; private static decorators; private prefix; private routes; private params; private response; private instance; private constructor(); setPrefix(options?: string | NapiControllerOptions): void; getPrefix(): string | undefined; setRoute(method: string, route: string): this; getRoutes<T extends string | undefined = string | undefined>(method?: T): T extends string ? string : Map<string, string>; setParam<Options = unknown, Res = unknown>(method: string, payload: NapiParamPayload<Options, Res>): this; getParams(method: string): NapiParamPayload<unknown, unknown>[] | undefined; setResponse(method: string, payload: NapiResponsePayload): this; getResponse(method: string): NapiResponsePayload<unknown> | undefined; hasPrefix(): boolean; getName(): string; hasRoutes(): boolean; getInstance(): object; static from(target: object): NapiController; static createControllerDecorator(): (options?: string | NapiControllerOptions) => ClassDecorator; static createMethodDecorator<Data = unknown>(handler: NapiDecoratorHandler<NapiMethodContext, Data>): NapiDecorator<MethodDecorator, Data>; static createParamDecorator<Data = unknown, Res = unknown>(handle: NapiDecoratorHandler<NapiExecuteContext<Res>, Data>): NapiDecorator<ParameterDecorator, Data>; static has(controller: object): boolean; static get(controller: object): NapiController; } export type NapiMethodCapitalized = Capitalize<Lowercase<NapiMethod>>; export declare const Controller: (options?: string | NapiControllerOptions) => ClassDecorator; export declare const createMethodDecorator: typeof NapiController.createMethodDecorator; export declare const createParamDecorator: typeof NapiController.createParamDecorator; export declare const Get: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Post: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Put: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Patch: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Delete: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Options: NapiDecorator<MethodDecorator, string | NapiMethodOptions>, Head: NapiDecorator<MethodDecorator, string | NapiMethodOptions>; export declare const Body: NapiDecorator<ParameterDecorator, unknown>, Query: NapiDecorator<ParameterDecorator, unknown>, Params: NapiDecorator<ParameterDecorator, unknown>, Headers: NapiDecorator<ParameterDecorator, unknown>, Cookies: NapiDecorator<ParameterDecorator, unknown>, Files: NapiDecorator<ParameterDecorator, unknown>, Session: NapiDecorator<ParameterDecorator, unknown>, Res: NapiDecorator<ParameterDecorator, unknown>, Req: NapiDecorator<ParameterDecorator, unknown>; export declare const HttpText: NapiDecorator<MethodDecorator, string>; export declare const HttpStatus: NapiDecorator<MethodDecorator, NapiHttpStatus>; export declare const SetHeaders: NapiDecorator<MethodDecorator, NapiHeaders<unknown>>;