phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
360 lines (320 loc) • 13 kB
text/typescript
import { B as BaseCtx, b as BaseError, a as ControllerMeta, D as DefaultOptions, c as ControllerMetaData, M as Meta, E as Emitter } from './meta-EGS-4cNY.mjs';
export { d as BaseRequestMethod, C as CustomResponse, g as ERROR_SYMBOL, e as ExtractResponse, I as IS_DEV, h as IS_ONLY_GENERATE, j as IS_PURE, i as IS_STRICT, L as LOG_LEVEL, f as MetaData, P as PS_EXIT_CODE, S as ServiceMetaData } from './meta-EGS-4cNY.mjs';
import { G as Generator } from './core-mOKtuP0F.mjs';
export { F as Factory, O as Options, S as ServerPhecda, d as defaultServerInject, e as emitter, p as phecdaNamespace, u as useS } from './core-mOKtuP0F.mjs';
import { Construct, Base } from 'phecda-core';
export * from 'phecda-core';
import { H as HttpCtx } from './types-BkU6kQWV.mjs';
export { C as CookieSerializeOptions, a as HttpOptions } from './types-BkU6kQWV.mjs';
import { R as RpcCtx } from './types-CoOCYe_X.mjs';
export { a as RpcServerOptions } from './types-CoOCYe_X.mjs';
export { Mixin } from 'ts-mixer';
import 'node:http';
declare class Exception extends Error {
message: string;
metadata?: any | undefined;
status: number;
description: string;
constructor(message: string, metadata?: any | undefined, status?: number, description?: string);
get data(): {
message: string;
description: string;
status: number;
__PS_ERROR__: true;
};
}
declare class UndefinedException extends Exception {
constructor(message: string, metadata?: any);
}
declare class ValidateException extends Exception {
constructor(message: string, metadata?: any);
}
declare class ForbiddenException extends Exception {
constructor(message: string, metadata?: any);
}
declare class BadRequestException extends Exception {
constructor(message: string, metadata?: any);
}
declare class NotFoundException extends Exception {
constructor(message: string, metadata?: any);
}
declare class ConflictException extends Exception {
constructor(message: string, metadata?: any);
}
declare class BadGatewayException extends Exception {
constructor(message: string, metadata?: any);
}
declare class InvalidInputException extends Exception {
constructor(message: string, metadata?: any);
}
declare class UnsupportedMediaTypeException extends Exception {
constructor(message: string, metadata?: any);
}
declare class PayloadLargeException extends Exception {
constructor(message: string, metadata?: any);
}
declare class TimeoutException extends Exception {
constructor(message: string, metadata?: any);
}
declare class UnauthorizedException extends Exception {
constructor(message: string, metadata?: any);
}
declare class ServiceUnavailableException extends Exception {
constructor(message: string, metadata?: any);
}
declare class FrameworkException extends Exception {
constructor(message: string, metadata?: any);
}
declare class TimerException extends Exception {
constructor(message: string, metadata?: any);
}
declare class WorkerException extends Exception {
constructor(message: string, metadata?: any);
}
interface AOP {
guards: GuardType[];
pipe: PipeType[];
filter: FilterType;
}
interface PipeArg {
arg: any;
pipe?: string;
key: string;
type: string;
index: number;
reflect: any;
define: Record<string, any>;
meta: any;
}
type GuardType<Ctx extends BaseCtx = any> = (ctx: Ctx, next: () => Promise<any>) => any;
type PipeType<Ctx extends BaseCtx = any> = (arg: PipeArg, ctx: Ctx) => Promise<any>;
type FilterType<Ctx extends BaseCtx = any, E extends Exception = any> = (err: E | Error, ctx?: Ctx) => BaseError;
declare class Context<Ctx extends BaseCtx> {
data: Ctx;
method: string;
params: string[];
static filterRecord: Record<PropertyKey, FilterType>;
static pipeRecord: Record<PropertyKey, PipeType>;
static guardRecord: Record<PropertyKey, {
value: GuardType;
priority: number;
}>;
static addonRecord: Record<PropertyKey, {
value: (router: any, framework: string) => any;
priority: number;
}>;
ctx: Ctx;
constructor(data: Ctx);
static getAop(meta: ControllerMeta, opts: DefaultOptions): {
guards: GuardType<any>[];
pipe: PipeType<any>[];
filter: FilterType<any, any>;
};
run<ResponseData = any, ReturnErr = any>({ guards, filter, pipe, }: {
guards: GuardType[];
filter: FilterType;
pipe: PipeType[];
}, successCb: (data: any) => ResponseData, failCb: (err: any) => ReturnErr): Promise<ResponseData | ReturnErr>;
static getPipe(pipe: string[]): PipeType<any>[];
static getFilter(filter?: string): FilterType<any, any>;
static getGuards(guards: string[]): GuardType<any>[];
static applyAddons(addons: string[], router: any, framework: string): void;
}
declare function addPipe<C extends BaseCtx>(key: PropertyKey, pipe: PipeType<C>): void;
declare function addFilter<C extends BaseCtx>(key: PropertyKey, filter: FilterType<C>): void;
declare function addGuard<C extends BaseCtx>(key: PropertyKey, guard: GuardType<C>, priority?: number): void;
declare function addAddon(key: PropertyKey, addon: (router: any, framework: string) => void, priority?: number): void;
declare class RPCGenerator extends Generator {
name: string;
classMap: Record<string, {
[key: string]: string;
}>;
getContent(): string;
addMethod(args: ControllerMetaData): void;
generateCode(meta: Meta[]): string;
}
declare class HTTPGenerator extends Generator {
name: string;
classMap: Record<string, {
[key: string]: string;
}>;
getContent(): string;
addMethod(args: ControllerMetaData): void;
generateCode(meta: Meta[]): string;
}
declare class OpenAPIGenerator extends Generator {
ext: string;
name: string;
paths: Record<string, any>;
getContent(): string;
addMethod(args: ControllerMetaData, model: Construct): void;
generateCode(meta: Meta[]): string;
}
declare class DocGenerator extends Generator {
name: string;
classMap: Record<string, {
[key: string]: {
doc: string;
params: {
doc: string;
index: number;
}[];
};
}>;
constructor(path?: string);
getContent(): string;
addMethod(data: ControllerMetaData): void;
generateCode(meta: Meta[]): string;
}
declare class GraphGenerator extends Generator {
ext: string;
name: string;
generateCode(meta: Meta[]): string;
}
declare function BaseParam(data: Record<string, any>): ParameterDecorator;
declare function Body(key?: string): ParameterDecorator;
declare function Head(key: string): ParameterDecorator;
declare function Query(key?: string): ParameterDecorator;
declare function Param(key: string): ParameterDecorator;
declare function Arg(target: any, k: string, index: number): void;
declare function UploadFile(key?: string): ParameterDecorator;
declare function Guard(...guards: string[]): (target: any, property?: PropertyKey) => void;
declare function Addon(...addons: string[]): (target: any, property?: PropertyKey) => void;
declare function Filter(filter: string): (target: any, property?: PropertyKey) => void;
declare function Pipe(pipe?: string): (target: any, property?: any, index?: any) => void;
declare function Route(route: string, method: string): MethodDecorator;
declare function Header(headers: Record<string, string>): MethodDecorator;
declare function Get(route?: string): MethodDecorator;
declare function Post(route?: string): MethodDecorator;
declare function Put(route?: string): MethodDecorator;
declare function Search(route?: string): MethodDecorator;
declare function Patch(route?: string): MethodDecorator;
declare function Delete(route?: string): MethodDecorator;
declare function Controller(prefix?: string): (target: any) => void;
declare function Queue(queue?: string, isEvent?: boolean): (target: any, property?: PropertyKey) => void;
declare function Rpc(): ClassDecorator;
declare const Ctx: PropertyDecorator;
declare function Define(key: string, value: any): any;
interface OpenAPIBaseConfig {
summary?: string;
description?: string;
tags?: string[];
deprecated?: boolean;
}
interface OpenAPIParameterConfig {
name: string;
in: 'query' | 'header' | 'path' | 'cookie';
description?: string;
required?: boolean;
schema: {
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
items?: any;
properties?: Record<string, any>;
};
}
interface OpenAPIResponseConfig {
description: string;
content?: {
[key: string]: {
schema: {
type: string;
properties?: Record<string, any>;
items?: any;
description?: string;
};
};
};
}
interface OpenAPIConfig extends OpenAPIBaseConfig {
parameters?: OpenAPIParameterConfig[];
requestBody?: {
required?: boolean;
content: {
[key: string]: {
schema: {
type: string;
properties?: Record<string, any>;
items?: any;
[key: string]: any;
};
};
};
};
responses?: {
[statusCode: string]: OpenAPIResponseConfig;
};
}
/**
* OpenAPI 装饰器函数
* @param config OpenAPI/Swagger 配置
*/
declare function ApiDoc(config: OpenAPIConfig): (target: any, propertyKey: string, _descriptor: PropertyDescriptor) => void;
declare const defaultPipe: PipeType;
type LogLevel = 'error' | 'info' | 'warn' | 'log' | 'debug';
interface Logger {
log(msg: unknown, level: LogLevel, ctx?: string): void;
}
declare function setLogger(logger: Logger): void;
declare function getLogger(): Logger;
declare function log(msg: unknown, level?: LogLevel, ctx?: any): void;
declare function runMiddleware(ctx: HttpCtx, middleware: (req: any, res: any, next?: any) => any): Promise<unknown>;
declare class ServerBase extends Base {
emitter: Emitter;
protected log(msg: unknown, level?: LogLevel): void;
}
declare class HttpBase<Ctx extends HttpCtx = HttpCtx> extends ServerBase {
context: Ctx;
}
declare class RpcBase<Ctx extends RpcCtx = RpcCtx> extends ServerBase {
context: Ctx;
}
declare abstract class PFilter<Ctx extends BaseCtx = any, E extends Exception = Exception> extends ServerBase {
readonly key: PropertyKey;
private _init;
abstract use(error: Error | E, ctx?: Ctx): BaseError;
}
declare abstract class PGuard<Ctx extends BaseCtx = any> extends ServerBase {
readonly key: PropertyKey;
priority: number;
private _init;
abstract use(ctx: Ctx, next: () => Promise<any>): any;
}
declare abstract class PPipe<Ctx extends BaseCtx = any> extends ServerBase {
readonly key: PropertyKey;
private _init;
abstract use(param: {
arg: any;
option?: any;
key: string;
type: string;
index: number;
reflect: any;
}, ctx: Ctx): any;
}
declare abstract class PAddon extends ServerBase {
readonly key: PropertyKey;
priority: number;
private _init;
abstract use(router: any, framework: string): undefined;
}
interface PExtension<Ctx extends BaseCtx = any, E extends Exception = Exception> {
guard(ctx: Ctx, next: () => Promise<any>): any;
pipe(param: {
arg: any;
option?: any;
key: string;
type: string;
index: number;
reflect: any;
}, ctx: Ctx): any;
filter(error: Error | E, ctx?: Ctx): BaseError;
addon<Addon = any>(framework: string): Addon;
}
declare class PExtension extends ServerBase {
readonly key: PropertyKey;
guardPriority: number;
addonPriority: number;
private _init;
}
declare const defaultFilter: FilterType;
export { type AOP, Addon, ApiDoc, Arg, BadGatewayException, BadRequestException, BaseCtx, BaseError, BaseParam, Body, ConflictException, Context, Controller, ControllerMeta, ControllerMetaData, Ctx, DefaultOptions, Define, Delete, DocGenerator, Emitter, Exception, Filter, type FilterType, ForbiddenException, FrameworkException, Generator, Get, GraphGenerator, Guard, type GuardType, HTTPGenerator, Head, Header, HttpBase, HttpCtx, InvalidInputException, type LogLevel, type Logger, Meta, NotFoundException, type OpenAPIConfig, OpenAPIGenerator, PAddon, PExtension, PFilter, PGuard, PPipe, Param, Patch, PayloadLargeException, Pipe, type PipeArg, type PipeType, Post, Put, Query, Queue, RPCGenerator, Route, Rpc, RpcBase, RpcCtx, Search, ServerBase, ServiceUnavailableException, TimeoutException, TimerException, UnauthorizedException, UndefinedException, UnsupportedMediaTypeException, UploadFile, ValidateException, WorkerException, addAddon, addFilter, addGuard, addPipe, defaultFilter, defaultPipe, getLogger, log, runMiddleware, setLogger };