summaryrefslogtreecommitdiff
path: root/src/server/api/api-handler.ts
blob: e716dcdc01d327b108d757e1f1f41294bf345b1c (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
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as Koa from 'koa';

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, ctx: Koa.Context) => {
	const body = ctx.is('multipart/form-data') ? (ctx.req as any).body : ctx.request.body;

	const reply = (x?: any, y?: any) => {
		if (x === undefined) {
			ctx.status = 204;
		} else if (typeof x === 'number') {
			ctx.status = x;
			ctx.body = {
				error: x === 500 ? 'INTERNAL_ERROR' : y
			};
		} else {
			ctx.body = x;
		}
	};

	let user: IUser;
	let app: IApp;

	// Authentication
	try {
		[user, app] = await authenticate(body['i']);
	} catch (e) {
		reply(403, 'AUTHENTICATION_FAILED');
		return;
	}

	let res;

	// API invoking
	try {
		res = await call(endpoint, user, app, body, (ctx.req as any).file);
	} catch (e) {
		reply(400, e);
		return;
	}

	reply(res);
};