ng2
This commit is contained in:
@@ -163,6 +163,84 @@ export abstract class APIExecuter<R extends ValidResponse, E extends InvalidResp
|
||||
}
|
||||
}
|
||||
|
||||
interface ngAPIExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain>{
|
||||
(query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response): Promise<R | E | true>;
|
||||
httpMethod: HttpMethod;
|
||||
argValidator?: ValidatorType<Q>;
|
||||
preDecorator: ProcDecorator<any, any>[];
|
||||
postDecorator: (ProcDecorator<any, any> | undefined)[];
|
||||
}
|
||||
|
||||
interface ngAPIGenerator<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType>{
|
||||
<PD extends ProcDecoratorChain>(procDecoratorChain: PD): ngAPIExecuter<R, E, Q, PD>;
|
||||
}
|
||||
|
||||
function ngGenerateAPI<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain>(httpMethod: HttpMethod, args: {
|
||||
argValidator?: ValidatorType<Q>,
|
||||
procDecoratorChain: PD,
|
||||
callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>,
|
||||
}): ngAPIExecuter<R, E, Q, PD>{
|
||||
const { argValidator, procDecoratorChain, callback } = args;
|
||||
const preDecorator: ProcDecorator<any, any>[] = [];
|
||||
const postDecorator: (ProcDecorator<any, any> | undefined)[] = [];
|
||||
if(procDecoratorChain){
|
||||
for (const procGen of procDecoratorChain) {
|
||||
const proc = procGen();
|
||||
if (Array.isArray(proc)) {
|
||||
preDecorator.push(proc[0]);
|
||||
postDecorator.push(undefined);
|
||||
}
|
||||
else {
|
||||
preDecorator.push(proc);
|
||||
postDecorator.push(proc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign(
|
||||
callback,
|
||||
{
|
||||
httpMethod,
|
||||
argValidator,
|
||||
preDecorator,
|
||||
postDecorator,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function ngGET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain>(args: {
|
||||
argValidator?: ValidatorType<Q>,
|
||||
procDecoratorChain: PD,
|
||||
callback: (query: Q, ctx: ResolveChain<PD>, expressReq: Request, expressRes: Response) => Promise<R | E | true>,
|
||||
}): ngAPIExecuter<R, E, Q, PD>{
|
||||
return ngGenerateAPI<R, E, Q, PD>('get', args);
|
||||
}
|
||||
|
||||
interface ngGET<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'get';
|
||||
}
|
||||
|
||||
interface ngPOST<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'post';
|
||||
}
|
||||
|
||||
interface ngPUT<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'put';
|
||||
}
|
||||
|
||||
interface ngDELETE<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'delete';
|
||||
}
|
||||
|
||||
interface ngPATCH<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'patch';
|
||||
}
|
||||
|
||||
interface ngHEAD<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends ngAPIExecuter<R, E, Q, PD>{
|
||||
httpMethod: 'head';
|
||||
}
|
||||
|
||||
|
||||
export abstract class APIBodyParseExecuter<R extends ValidResponse, E extends InvalidResponse, Q extends RawArgType, PD extends ProcDecoratorChain> extends APIExecuter<R, E, Q, PD>{
|
||||
protected async parseQuery(expressReq: Request): Promise<Q> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user