diff options
| author | ha-dai <contact@haradai.net> | 2018-05-04 02:49:46 +0900 |
|---|---|---|
| committer | ha-dai <contact@haradai.net> | 2018-05-04 02:49:46 +0900 |
| commit | f850283147072c681df1b39c57f8bd0b14f18016 (patch) | |
| tree | 63ff533c91097da2d8ca2070fc67a28f67ee33da /src/server/api/api-handler.ts | |
| parent | Merge branch 'master' of github.com:syuilo/misskey (diff) | |
| parent | 1.7.0 (diff) | |
| download | misskey-f850283147072c681df1b39c57f8bd0b14f18016.tar.gz misskey-f850283147072c681df1b39c57f8bd0b14f18016.tar.bz2 misskey-f850283147072c681df1b39c57f8bd0b14f18016.zip | |
Merge branch 'master' of github.com:syuilo/misskey
Diffstat (limited to 'src/server/api/api-handler.ts')
| -rw-r--r-- | src/server/api/api-handler.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/server/api/api-handler.ts b/src/server/api/api-handler.ts new file mode 100644 index 0000000000..e716dcdc01 --- /dev/null +++ b/src/server/api/api-handler.ts @@ -0,0 +1,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); +}; |