UNPKG

@prostojs/mate

Version:

MATE is TS Metadata Organizer

77 lines 5.37 kB
//#region dts-build/types.d.ts type TAny = any; type TObject = object; type TFunction = Function; type TClassConstructor<T = unknown> = new (...args: TAny[]) => T; //#endregion //#region dts-build/mate.d.ts type TLevels = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM'; interface TMergedDecoratorArgs { target: TFunction | TObject; propKey?: string; descriptor?: TypedPropertyDescriptor<TAny>; index?: number; level?: TLevels; } interface TMateParamMeta { type?: TFunction; } interface TMatePropMeta<TParam extends TObject = TEmpty> { params?: (TParam & TMateParamMeta)[]; } interface TMateClassMeta<TParam> { properties?: string[]; type?: TFunction; returnType?: TFunction; params?: (TParam & TMateParamMeta)[]; } type TCommonMate<TParam extends TObject> = TMateClassMeta<TParam> & TMatePropMeta<TParam>; type TCommonMateWithParam<TParam extends TObject> = TMateClassMeta<TParam> & TMatePropMeta<TParam> & TParam; interface TConsoleBase { error: (...args: any) => void; } interface TMateOptions<TClass extends TObject = TMateClassMeta<TMateParamMeta>, TProp extends { params: TMateParamMeta[]; } = Required<TMatePropMeta<TMateParamMeta>>> { logger?: TConsoleBase; readReturnType?: boolean; readType?: boolean; collectPropKeys?: boolean; inherit?: boolean | ((classMeta: TClass & TMateClassMeta<TMateParamMeta>, targetMeta: (TMatePropMeta<TProp['params'][0]> & TProp) | (TMateParamMeta & TProp['params'][0]), level: 'CLASS' | 'PROP' | 'PARAM', key?: string) => boolean); } interface TEmpty {} declare class Mate<TClass extends TObject = TMateClassMeta<TMateParamMeta>, TProp extends { params: TMateParamMeta[]; } = Required<TMatePropMeta<TMateParamMeta>>> { protected workspace: string; protected options: TMateOptions<TClass, TProp>; protected logger: TConsoleBase; protected _readCache?: WeakMap<object, Map<string | undefined, unknown>>; constructor(workspace: string, options?: TMateOptions<TClass, TProp>); /** * Cleanup function to reset metadata * * It is usefull in dev mode when server restarts */ _cleanup(): void; set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>>(args: TMergedDecoratorArgs, cb: (meta: T, level: TLevels, propKey?: string, index?: number) => T): void; set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(args: TMergedDecoratorArgs, key: keyof T, value: T[K]): void; set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(args: TMergedDecoratorArgs, key: keyof T, value: T[K], isArray: boolean | undefined): void; read<PK>(target: TFunction | TObject, propKey?: PK): PK extends string ? (TClass & TProp & TCommonMate<TProp['params'][0]>) | undefined : TClass | undefined; apply(...decorators: (MethodDecorator | ClassDecorator | ParameterDecorator | PropertyDecorator)[]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>>(cb: (meta: T, level: TLevels, propKey?: string, index?: number) => T): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(key: K, value: T[K]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: TIsArray): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K | ((meta: T, level: TLevels, propKey?: string, index?: number) => T), value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: TIsArray, level: TMergedDecoratorArgs['level']): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorateConditional(ccb: (level: TLevels) => (MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator) | void | undefined): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorateClass<T = TClass>(cb: (meta: T, level: TLevels, propKey?: string, index?: number) => T): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorateClass<T = TClass, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; decorateClass<T = TClass, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: boolean | undefined): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator; } type ArrayElementType<T> = T extends (infer E)[] ? E : never; //#endregion //#region dts-build/utils/helpers.d.ts declare function getConstructor<T = TAny>(instance: T): TFunction; declare function isConstructor<T = TAny>(v: T): v is T & TClassConstructor; //#endregion export { Mate, TMateClassMeta, TMateOptions, TMateParamMeta, TMatePropMeta, TMergedDecoratorArgs, getConstructor, isConstructor };