2016-12-28 16:49:51 -06:00
|
|
|
import * as express from 'express';
|
|
|
|
|
2017-03-01 07:33:43 -06:00
|
|
|
import { Endpoint } from './endpoints';
|
2016-12-28 16:49:51 -06:00
|
|
|
import authenticate from './authenticate';
|
2018-04-11 03:40:01 -05:00
|
|
|
import call from './call';
|
|
|
|
import { IUser } from '../../models/user';
|
|
|
|
import { IApp } from '../../models/app';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-03-01 07:33:43 -06:00
|
|
|
export default async (endpoint: Endpoint, req: express.Request, res: express.Response) => {
|
2018-04-11 03:40:01 -05:00
|
|
|
const reply = (x?: any, y?: any) => {
|
|
|
|
if (x === undefined) {
|
|
|
|
res.sendStatus(204);
|
|
|
|
} else if (typeof x === 'number') {
|
|
|
|
res.status(x).send({
|
|
|
|
error: x === 500 ? 'INTERNAL_ERROR' : y
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send(x);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user: IUser;
|
|
|
|
let app: IApp;
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-02-27 01:14:41 -06:00
|
|
|
// Authentication
|
2016-12-28 16:49:51 -06:00
|
|
|
try {
|
2018-04-11 03:40:01 -05:00
|
|
|
[user, app] = await authenticate(req.body['i']);
|
2016-12-28 16:49:51 -06:00
|
|
|
} catch (e) {
|
|
|
|
return reply(403, 'AUTHENTICATION_FAILED');
|
|
|
|
}
|
|
|
|
|
|
|
|
// API invoking
|
2018-04-11 03:40:01 -05:00
|
|
|
call(endpoint, user, app, req.body, req).then(reply).catch(e => reply(400, e));
|
2016-12-28 16:49:51 -06:00
|
|
|
};
|