-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
38 lines (31 loc) · 904 Bytes
/
router.js
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
'use strict';
const Router = require('koa-router');
const config = require('./config');
const API = require('./lib/api');
const api = new API(config);
const router = new Router({
prefix: config.apiPath,
});
router.get('/', async (ctx, next) => {
ctx.body = { status: 'ok' };
});
router.get('/convert', async (ctx, next) => {
const { url, source, target, ...rest } = ctx.query;
const result = api.validateAndConvert({ opts: { url, source, target }, query: rest, headers: ctx.headers });
if (!result.isBoom) {
ctx.type = result.type;
ctx.body = result.sub;
} else {
ctx.throw(result);
}
});
router.post('/convert', async (ctx, next) => {
const result = api.validateAndConvert({ opts: ctx.request.body, query: ctx.query, headers: ctx.headers });
if (!result.isBoom) {
ctx.type = result.type;
ctx.body = result.sub;
} else {
ctx.throw(result);
}
});
module.exports = router;