summaryrefslogtreecommitdiff
path: root/src/server/api/api-handler.ts
blob: 409069b6a03315d4a58f2b6be1aa0bec09ecb34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as express from 'express';

import { Endpoint } from './endpoints';
import authenticate from './authenticate';
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) => {
	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;

	// Authentication
	try {
		[user, app] = await authenticate(req.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));
};