diff --git a/server/api/defs.ts b/server/api/defs.ts index 25288fc..45f4e19 100644 --- a/server/api/defs.ts +++ b/server/api/defs.ts @@ -163,6 +163,84 @@ export abstract class APIExecuter{ + (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response): Promise; + httpMethod: HttpMethod; + argValidator?: ValidatorType; + preDecorator: ProcDecorator[]; + postDecorator: (ProcDecorator | undefined)[]; +} + +interface ngAPIGenerator{ + (procDecoratorChain: PD): ngAPIExecuter; +} + +function ngGenerateAPI(httpMethod: HttpMethod, args: { + argValidator?: ValidatorType, + procDecoratorChain: PD, + callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise, +}): ngAPIExecuter{ + const { argValidator, procDecoratorChain, callback } = args; + const preDecorator: ProcDecorator[] = []; + const postDecorator: (ProcDecorator | 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(args: { + argValidator?: ValidatorType, + procDecoratorChain: PD, + callback: (query: Q, ctx: ResolveChain, expressReq: Request, expressRes: Response) => Promise, +}): ngAPIExecuter{ + return ngGenerateAPI('get', args); +} + +interface ngGET extends ngAPIExecuter{ + httpMethod: 'get'; +} + +interface ngPOST extends ngAPIExecuter{ + httpMethod: 'post'; +} + +interface ngPUT extends ngAPIExecuter{ + httpMethod: 'put'; +} + +interface ngDELETE extends ngAPIExecuter{ + httpMethod: 'delete'; +} + +interface ngPATCH extends ngAPIExecuter{ + httpMethod: 'patch'; +} + +interface ngHEAD extends ngAPIExecuter{ + httpMethod: 'head'; +} + + export abstract class APIBodyParseExecuter extends APIExecuter{ protected async parseQuery(expressReq: Request): Promise {