This commit is contained in:
syuilo
2018-04-13 06:06:18 +09:00
parent a3bd4ba426
commit 3368fe8552
20 changed files with 582 additions and 609 deletions

View File

@ -1,4 +1,4 @@
import * as express from 'express';
import * as Koa from 'koa';
import { Endpoint } from './endpoints';
import authenticate from './authenticate';
@ -6,16 +6,17 @@ import call from './call';
import { IUser } from '../../models/user';
import { IApp } from '../../models/app';
export default async (endpoint: Endpoint, req: express.Request, res: express.Response) => {
export default async (endpoint: Endpoint, ctx: Koa.Context) => {
const reply = (x?: any, y?: any) => {
if (x === undefined) {
res.sendStatus(204);
ctx.status = 204;
} else if (typeof x === 'number') {
res.status(x).send({
ctx.status = x;
ctx.body = {
error: x === 500 ? 'INTERNAL_ERROR' : y
});
};
} else {
res.send(x);
ctx.body = x;
}
};
@ -24,11 +25,11 @@ export default async (endpoint: Endpoint, req: express.Request, res: express.Res
// Authentication
try {
[user, app] = await authenticate(req.body['i']);
[user, app] = await authenticate(ctx.body['i']);
} catch (e) {
return reply(403, 'AUTHENTICATION_FAILED');
}
// API invoking
call(endpoint, user, app, req.body, req).then(reply).catch(e => reply(400, e));
call(endpoint, user, app, ctx.body, ctx.req).then(reply).catch(e => reply(400, e));
};