2022-02-27 02:07:39 +00:00
|
|
|
import Koa from 'koa';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-02-27 02:07:39 +00:00
|
|
|
import { IEndpoint } from './endpoints.js';
|
|
|
|
import authenticate, { AuthenticationError } from './authenticate.js';
|
|
|
|
import call from './call.js';
|
|
|
|
import { ApiError } from './error.js';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-10-23 11:34:37 +00:00
|
|
|
export async function handler(endpoint: IEndpoint, ctx: Koa.Context): Promise<void> {
|
2022-06-25 09:26:31 +00:00
|
|
|
const body = ctx.is('multipart/form-data')
|
2022-06-26 08:38:50 +00:00
|
|
|
? (ctx.request as any).body
|
2022-06-25 09:26:31 +00:00
|
|
|
: ctx.method === 'GET'
|
|
|
|
? ctx.query
|
|
|
|
: ctx.request.body;
|
2018-04-13 02:44:39 +00:00
|
|
|
|
2022-10-23 11:34:37 +00:00
|
|
|
const error = (e: ApiError): void => {
|
2022-10-20 21:04:24 +00:00
|
|
|
ctx.status = e.httpStatusCode;
|
2022-10-23 11:34:37 +00:00
|
|
|
if (e.httpStatusCode === 401) {
|
|
|
|
ctx.response.set('WWW-Authenticate', 'Bearer');
|
2018-04-11 08:40:01 +00:00
|
|
|
}
|
2022-10-23 11:34:37 +00:00
|
|
|
ctx.body = {
|
|
|
|
error: {
|
|
|
|
message: e!.message,
|
|
|
|
code: e!.code,
|
|
|
|
...(e!.info ? { info: e!.info } : {}),
|
|
|
|
endpoint: endpoint.name,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2018-04-11 08:40:01 +00:00
|
|
|
|
2017-02-27 07:14:41 +00:00
|
|
|
// Authentication
|
2022-07-18 15:41:08 +00:00
|
|
|
// for GET requests, do not even pass on the body parameter as it is considered unsafe
|
2022-10-23 11:34:37 +00:00
|
|
|
await authenticate(ctx.headers.authorization, ctx.method === 'GET' ? null : body['i']).then(async ([user, app]) => {
|
2019-02-22 05:46:49 +00:00
|
|
|
// API invoking
|
2022-10-23 11:34:37 +00:00
|
|
|
await call(endpoint.name, user, app, body, ctx).then((res: any) => {
|
2022-06-25 09:26:31 +00:00
|
|
|
if (ctx.method === 'GET' && endpoint.meta.cacheSec && !body['i'] && !user) {
|
|
|
|
ctx.set('Cache-Control', `public, max-age=${endpoint.meta.cacheSec}`);
|
|
|
|
}
|
2022-10-23 11:34:37 +00:00
|
|
|
if (res == null) {
|
|
|
|
ctx.status = 204;
|
|
|
|
} else {
|
|
|
|
ctx.status = 200;
|
|
|
|
// If a string is returned, it must be passed through JSON.stringify to be recognized as JSON.
|
|
|
|
ctx.body = typeof res === 'string' ? JSON.stringify(res) : res;
|
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
}).catch((e: ApiError) => {
|
2022-10-23 11:34:37 +00:00
|
|
|
error(e);
|
2019-02-22 05:46:49 +00:00
|
|
|
});
|
2021-07-17 15:53:16 +00:00
|
|
|
}).catch(e => {
|
|
|
|
if (e instanceof AuthenticationError) {
|
2022-10-20 21:04:24 +00:00
|
|
|
error(new ApiError('AUTHENTICATION_FAILED', e.message));
|
2021-07-17 15:53:16 +00:00
|
|
|
} else {
|
2022-10-23 11:34:37 +00:00
|
|
|
error(new ApiError());
|
2021-07-17 15:53:16 +00:00
|
|
|
}
|
2019-02-22 05:46:49 +00:00
|
|
|
});
|
2022-10-23 11:34:37 +00:00
|
|
|
};
|