diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-04-13 14:30:10 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-13 14:30:10 +0900 |
| commit | cdf13d30f2ad47b07d9c366e9324ff6109c15be2 (patch) | |
| tree | c7facc364b259ce64c4fe78da5a0d8f723e51ada /src/server/api/api-handler.ts | |
| parent | Merge branch 'master' of https://github.com/syuilo/misskey (diff) | |
| parent | Fix bug (diff) | |
| download | misskey-cdf13d30f2ad47b07d9c366e9324ff6109c15be2.tar.gz misskey-cdf13d30f2ad47b07d9c366e9324ff6109c15be2.tar.bz2 misskey-cdf13d30f2ad47b07d9c366e9324ff6109c15be2.zip | |
Merge pull request #1460 from syuilo/koa
Koa
Diffstat (limited to 'src/server/api/api-handler.ts')
| -rw-r--r-- | src/server/api/api-handler.ts | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/src/server/api/api-handler.ts b/src/server/api/api-handler.ts index 409069b6a0..e716dcdc01 100644 --- a/src/server/api/api-handler.ts +++ b/src/server/api/api-handler.ts @@ -1,4 +1,4 @@ -import * as express from 'express'; +import * as Koa from 'koa'; import { Endpoint } from './endpoints'; import authenticate from './authenticate'; @@ -6,16 +6,19 @@ 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) => { +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) { - res.sendStatus(204); + ctx.status = 204; } else if (typeof x === 'number') { - res.status(x).send({ + ctx.status = x; + ctx.body = { error: x === 500 ? 'INTERNAL_ERROR' : y - }); + }; } else { - res.send(x); + ctx.body = x; } }; @@ -24,11 +27,21 @@ export default async (endpoint: Endpoint, req: express.Request, res: express.Res // Authentication try { - [user, app] = await authenticate(req.body['i']); + [user, app] = await authenticate(body['i']); } catch (e) { - return reply(403, 'AUTHENTICATION_FAILED'); + reply(403, 'AUTHENTICATION_FAILED'); + return; } + let res; + // API invoking - call(endpoint, user, app, req.body, req).then(reply).catch(e => reply(400, e)); + try { + res = await call(endpoint, user, app, body, (ctx.req as any).file); + } catch (e) { + reply(400, e); + return; + } + + reply(res); }; |