moost
Version:
356 lines (330 loc) • 16.8 kB
TypeScript
import { useEventLogger } from '@wooksjs/event-core';
export { ContextInjector, EventLogger, THook, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
import { TConsoleBase } from '@prostojs/logger';
export { ProstoLogger, TConsoleBase } from '@prostojs/logger';
import { Hookable } from 'hookable';
import * as _prostojs_mate from '@prostojs/mate';
import { TMateParamMeta, Mate } from '@prostojs/mate';
export { Mate, TMateParamMeta, getConstructor, isConstructor } from '@prostojs/mate';
export { clearGlobalWooks, getGlobalWooks } from 'wooks';
type TAny = any;
type TAnyFn = (...a: TAny[]) => unknown;
type TObject = object;
type TFunction = Function;
type TClassConstructor<T = unknown> = new (...args: TAny[]) => T;
interface TEmpty {
}
declare function Circular<T = unknown>(resolver: () => TClassConstructor<T>): ParameterDecorator;
declare function Label(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Description(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Value(value: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Id(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Optional(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Required(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Controller(prefix?: string): ClassDecorator;
declare function ImportController(controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
declare function ImportController(prefix: string, controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
interface TClassFunction<T extends TAnyFn = TAnyFn> {
handler: T;
}
type TClassFunctionConstructor<T extends TAnyFn = TAnyFn> = new (...a: TAny[]) => TClassFunction<T>;
type TCallableClassFunction<T extends TAnyFn = TAnyFn> = T | TClassFunctionConstructor;
type TInterceptorBefore = (reply: (response: TAny) => void) => void | Promise<void>;
type TInterceptorAfter = (response: TAny, reply: (response: TAny) => void) => void | Promise<void>;
type TInterceptorOnError = (error: Error, reply: (response: TAny) => void) => void | Promise<void>;
interface TInterceptorFn {
(before: (fn: TInterceptorBefore) => void, after: (fn: TInterceptorAfter) => void, onError: (fn: TInterceptorOnError) => void): unknown | Promise<unknown>;
priority?: TInterceptorPriority;
_name?: string;
}
declare enum TInterceptorPriority {
BEFORE_ALL = 0,
BEFORE_GUARD = 1,
GUARD = 2,
AFTER_GUARD = 3,
INTERCEPTOR = 4,
CATCH_ERROR = 5,
AFTER_ALL = 6
}
declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority, name?: string): ClassDecorator & MethodDecorator;
type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
interface TInfactLoggingOptions {
newInstance?: true | false | 'FOR_EVENT' | 'SINGLETON';
warn?: true | false;
error?: true | false;
}
declare function setInfactLoggingOptions(options: TInfactLoggingOptions): void;
declare function getMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
interface TCustom {
pipes?: TPipeData[];
}
declare function defineInfactScope<T extends object>(name: string | symbol, scopeVars: T): void;
declare function getInfactScopeVars<T extends object>(name: string | symbol): T | undefined;
declare function getNewMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
interface TPipeMetas<T extends TObject = TEmpty> {
classMeta?: TMoostMetadata & T;
methodMeta?: TMoostMetadata & T;
propMeta?: TMoostMetadata & T;
paramMeta?: TMoostParamsMetadata & T;
targetMeta?: TMoostParamsMetadata & T;
instance?: TObject;
scopeId?: string | symbol;
type: TFunction;
index?: number;
key: string | symbol;
}
interface TPipeFn<T extends TObject = TEmpty> {
(value: unknown, metas: TPipeMetas<T>, level: TDecoratorLevel): unknown | Promise<unknown>;
priority?: TPipePriority;
}
declare enum TPipePriority {
BEFORE_RESOLVE = 0,
RESOLVE = 1,
AFTER_RESOLVE = 2,
BEFORE_TRANSFORM = 3,
TRANSFORM = 4,
AFTER_TRANSFORM = 5,
BEFORE_VALIDATE = 6,
VALIDATE = 7,
AFTER_VALIDATE = 8
}
interface TPipeData {
handler: TPipeFn;
priority: TPipePriority;
}
declare const resolvePipe: TPipeFn<TEmpty>;
interface TMoostMetadata<H extends TObject = TEmpty> extends TCommonMetaFields, TCommonMoostMeta {
requiredProps?: Array<string | symbol>;
controller?: {
prefix?: string;
};
importController?: Array<{
prefix?: string;
typeResolver?: TClassConstructor | (() => TClassConstructor | TObject | Promise<TClassConstructor | TObject>);
provide?: TProvideRegistry;
}>;
properties?: Array<string | symbol>;
injectable?: true | TInjectableScope;
interceptors?: TInterceptorData[];
handlers?: Array<TMoostHandler<H>>;
returnType?: TFunction;
provide?: TProvideRegistry;
replace?: TReplaceRegistry;
loggerTopic?: string;
params: Array<TMateParamMeta & TMoostParamsMetadata>;
}
interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
circular?: () => TAny;
inject?: string | symbol | TClassConstructor;
nullable?: boolean;
paramSource?: string;
paramName?: string;
fromScope?: string | symbol;
}
type TInjectableScope = 'FOR_EVENT' | 'SINGLETON';
type TMoostHandler<T> = {
type: string;
path?: string;
} & T;
interface TInterceptorData {
handler: TCallableClassFunction<TInterceptorFn>;
priority: TInterceptorPriority;
name: string;
}
declare function getMoostMate<Class extends TObject = TEmpty, Prop extends TObject = TEmpty, Param extends TObject = TEmpty>(): Mate<TMoostMetadata<TEmpty> & Class & {
params: Array<Param & TMateParamMeta>;
}, TMoostMetadata<TEmpty> & Prop & {
params: Array<Param & TMateParamMeta>;
}>;
interface TCommonMetaFields {
id?: string;
label?: string;
value?: unknown;
description?: string;
optional?: boolean;
required?: boolean;
}
interface TCommonMoostMeta {
inherit?: boolean;
pipes?: TPipeData[];
resolver?: (metas: TPipeMetas<TAny>, level: TDecoratorLevel) => unknown;
type?: TFunction;
}
declare function Injectable(scope?: true | TInjectableScope): ClassDecorator;
declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
declare function InjectMoostLogger(topic?: string): ParameterDecorator & PropertyDecorator;
declare function LoggerTopic(topic: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Pipe(handler: TPipeFn, priority?: TPipePriority): ClassDecorator & MethodDecorator & ParameterDecorator;
declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Replace(type: TClassConstructor, newType: TClassConstructor): ClassDecorator;
declare function Inject(type: string | TClassConstructor): ParameterDecorator & PropertyDecorator;
declare function InjectFromScope(name: string | symbol): ParameterDecorator & PropertyDecorator;
declare function InjectScopeVars(name?: string): ParameterDecorator & PropertyDecorator;
declare function Resolve<T extends TObject = TEmpty>(resolver: (metas: TPipeMetas<T>, level: TDecoratorLevel) => unknown, label?: string): ParameterDecorator & PropertyDecorator;
declare function Param(name: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
declare function Params(): ParameterDecorator & PropertyDecorator;
declare function Const<T>(value: T, label?: string): ParameterDecorator & PropertyDecorator;
declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
declare class InterceptorHandler {
protected handlers: Array<{
handler: TInterceptorFn;
name: string;
}>;
constructor(handlers: Array<{
handler: TInterceptorFn;
name: string;
}>);
protected before: Array<{
name: string;
fn: TInterceptorBefore;
}>;
protected after: Array<{
name: string;
fn: TInterceptorAfter;
}>;
protected onError: Array<{
name: string;
fn: TInterceptorOnError;
}>;
response?: unknown;
responseOverwritten: boolean;
get count(): number;
get countBefore(): number;
get countAfter(): number;
get countOnError(): number;
replyFn(reply: unknown): void;
init(): Promise<{} | null | undefined>;
fireBefore(response: unknown): Promise<unknown>;
fireAfter(response: unknown): Promise<unknown>;
}
interface TMoostEventHandlerHookOptions<T> {
scopeId: string;
logger: ReturnType<typeof useEventLogger>;
unscope: () => void;
instance?: T;
method?: keyof T;
getResponse: () => unknown;
reply: (r: unknown) => void;
}
interface TMoostEventHandlerOptions<T> {
contextType?: string | string[];
loggerTitle: string;
getIterceptorHandler: () => Promise<InterceptorHandler> | InterceptorHandler | undefined;
getControllerInstance: () => Promise<T> | T | undefined;
controllerMethod?: keyof T;
callControllerMethod?: (args: unknown[]) => unknown;
resolveArgs?: () => Promise<unknown[]> | unknown[];
logErrors?: boolean;
manualUnscope?: boolean;
hooks?: {
init?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
end?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
};
targetPath: string;
handlerType: string;
}
declare function registerEventScope(scopeId: string): () => void;
declare function defineMoostEventHandler<T>(options: TMoostEventHandlerOptions<T>): () => Promise<unknown>;
declare function getInstanceOwnMethods<T = TAny>(instance: T): Array<keyof T>;
declare function getInstanceOwnProps<T = TAny>(instance: T): Array<keyof T>;
interface TControllerOverview {
meta: TMoostMetadata;
computedPrefix: string;
type: TFunction;
handlers: THandlerOverview[];
}
interface THandlerOverview {
meta: TMoostMetadata;
path?: string;
type: string;
method: string;
handler: TMoostHandler<TEmpty>;
registeredAs: Array<{
path: string;
args: string[];
}>;
}
type TContextInjectorHook = 'Event:start' | 'Interceptors:init' | 'Arguments:resolve' | 'Interceptors:before' | 'Handler' | 'Interceptors:after';
interface TMoostOptions {
globalPrefix?: string;
logger?: TConsoleBase;
}
declare class Moost extends Hookable {
protected options?: TMoostOptions | undefined;
protected logger: TConsoleBase;
protected pipes: TPipeData[];
protected interceptors: TInterceptorData[];
protected adapters: Array<TMoostAdapter<TAny>>;
protected controllersOverview: TControllerOverview[];
protected provide: TProvideRegistry;
protected replace: TReplaceRegistry;
protected unregisteredControllers: Array<TObject | TFunction | [string, TObject | TFunction]>;
constructor(options?: TMoostOptions | undefined);
_fireEventStart(source: TMoostAdapter<unknown>): void;
_fireEventEnd(source: TMoostAdapter<unknown>): void;
getLogger(topic?: string): TConsoleBase;
adapter<T extends TMoostAdapter<TAny>>(a: T): T;
getControllersOverview(): TControllerOverview[];
init(): Promise<void>;
protected bindControllers(): Promise<void>;
protected bindController(controller: TFunction | TObject, provide: TProvideRegistry, replace: TReplaceRegistry, globalPrefix: string, replaceOwnPrefix?: string): Promise<void>;
applyGlobalPipes(...items: Array<TPipeFn | TPipeData>): this;
protected globalInterceptorHandler?: () => Promise<InterceptorHandler>;
getGlobalInterceptorHandler(): Promise<InterceptorHandler>;
applyGlobalInterceptors(...items: Array<TInterceptorData['handler'] | TInterceptorData>): this;
setProvideRegistry(provide: TProvideRegistry): this;
setReplaceRegistry(replace: TReplaceRegistry): this;
registerControllers(...controllers: Array<TObject | TFunction | [string, TObject | TFunction]>): this;
logMappedHandler(eventName: string, classConstructor: Function, method: string, stroke?: boolean, prefix?: string): void;
}
interface TMoostAdapterOptions<H, T> {
prefix: string;
fakeInstance: T;
getInstance: () => Promise<T>;
method: keyof T;
handlers: Array<TMoostHandler<H>>;
getIterceptorHandler: () => Promise<InterceptorHandler>;
resolveArgs: () => Promise<unknown[]>;
logHandler: (eventName: string) => void;
register: (handler: TMoostHandler<TEmpty>, path: string, args: string[]) => void;
}
interface TMoostAdapter<H> {
name: string;
bindHandler: <T extends TObject = TObject>(options: TMoostAdapterOptions<H, T>) => void | Promise<void>;
onInit?: (moost: Moost) => void | Promise<void>;
getProvideRegistry?: () => TProvideRegistry;
}
declare function setControllerContext<T>(controller: T, method: keyof T, route: string): void;
declare function useControllerContext<T extends object>(): {
instantiate: <TT>(c: TClassConstructor<TT>) => Promise<TT>;
getRoute: () => string | undefined;
getController: () => T;
getMethod: () => string | undefined;
getControllerMeta: <TT_1 extends object>() => (TMoostMetadata<TEmpty> & TT_1 & {
params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
}) | undefined;
getMethodMeta: <TT_2 extends object>(name?: string) => (TMoostMetadata<TEmpty> & TT_2 & {
params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
} & {
params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
} & _prostojs_mate.TMateClassMeta<(TMoostMetadata<TEmpty> & TT_2 & {
params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
})["params"][0]> & _prostojs_mate.TMatePropMeta<(TMoostMetadata<TEmpty> & TT_2 & {
params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
})["params"][0]>) | undefined;
getPropertiesList: () => (string | symbol)[];
getScope: () => true | TInjectableScope;
getParamsMeta: () => (_prostojs_mate.TMateParamMeta & TMoostParamsMetadata)[] & (object & _prostojs_mate.TMateParamMeta)[] & (_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object)[];
getPropMeta: (name: string) => (TMoostMetadata<TEmpty> & object & {
params: (object & _prostojs_mate.TMateParamMeta)[];
} & {
params: (object & _prostojs_mate.TMateParamMeta)[];
} & _prostojs_mate.TMateClassMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object> & _prostojs_mate.TMatePropMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object>) | undefined;
};
declare function defineInterceptorFn(fn: TInterceptorFn, priority?: TInterceptorPriority): TInterceptorFn;
type TInterceptorClass = TClassFunction<TInterceptorFn>;
declare function definePipeFn<T extends TObject = TEmpty>(fn: TPipeFn<T>, priority?: TPipePriority): TPipeFn<T>;
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, InjectEventLogger, InjectFromScope, InjectMoostLogger, InjectScopeVars, Injectable, Intercept, InterceptorHandler, Label, LoggerTopic, Moost, Optional, Param, Params, Pipe, Provide, Replace, Required, Resolve, type TCallableClassFunction, type TClassConstructor, type TClassFunction, type TContextInjectorHook, type TControllerOverview, type TInjectableScope, type TInterceptorAfter, type TInterceptorBefore, type TInterceptorClass, type TInterceptorData, type TInterceptorFn, type TInterceptorOnError, TInterceptorPriority, type TMoostAdapter, type TMoostAdapterOptions, type TMoostEventHandlerHookOptions, type TMoostEventHandlerOptions, type TMoostHandler, type TMoostMetadata, type TMoostOptions, type TMoostParamsMetadata, type TPipeData, type TPipeFn, type TPipeMetas, TPipePriority, Value, defineInfactScope, defineInterceptorFn, defineMoostEventHandler, definePipeFn, getInfactScopeVars, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, registerEventScope, resolvePipe, setControllerContext, setInfactLoggingOptions, useControllerContext };