리팩토링
This commit is contained in:
+48
-46
@@ -64,7 +64,7 @@ async function preCtx<PD extends ProcDecorator<any, any>>(preDecorator: PlainDec
|
||||
return ctx;
|
||||
}
|
||||
|
||||
async function postCtx<PD extends ProcDecoratorChain>(ctx: ResolveChain<PD>, postDecorator: (undefined|PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
||||
async function postCtx<PD extends ProcDecoratorChain>(ctx: ResolveChain<PD>, postDecorator: (undefined | PlainDecorator)[], req: Request, res: Response, index = -1): Promise<[InvalidProc, number] | undefined> {
|
||||
if (!postDecorator) {
|
||||
return;
|
||||
}
|
||||
@@ -137,53 +137,55 @@ async function apiRun(query: object, req: Request, res: Response, api: AnyAPIExe
|
||||
|
||||
export function buildAPISystem<N extends APINamespace, Q extends AnyAPIExecuter>(api: N | Q): Router {
|
||||
const router = Router();
|
||||
|
||||
if(typeof api === 'function') {
|
||||
throw 'root api cannot be function';
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(api)) {
|
||||
if (typeof value === 'function') {
|
||||
const executer = new value() as Q;
|
||||
|
||||
if (!executer.httpMethod) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
|
||||
if (executer.httpMethod === 'head') {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseParam(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
else {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseBody(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
continue;
|
||||
} else {
|
||||
if (typeof value !== 'function') {
|
||||
router.use(key, buildAPISystem(value));
|
||||
continue;
|
||||
}
|
||||
const executer = value;
|
||||
|
||||
if (!executer.httpMethod) {
|
||||
throw new Error('APIExecuter.reqType is not defined');
|
||||
}
|
||||
|
||||
if (executer.httpMethod === 'get') {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseParam(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
else {
|
||||
router[executer.httpMethod](key, async (req, res) => {
|
||||
let query: ExtractQuery<Q>;
|
||||
try {
|
||||
query = await parseBody(req, executer.argValidator);
|
||||
}
|
||||
catch (e) {
|
||||
res.json({
|
||||
result: false,
|
||||
reason: `invalid parameter: ${e}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await apiRun(query, req, res, executer);
|
||||
});
|
||||
}
|
||||
}
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user