2017-12-16 17:41:22 +01:00
|
|
|
/**
|
2018-04-12 23:06:18 +02:00
|
|
|
* Docs
|
2017-12-16 17:41:22 +01:00
|
|
|
*/
|
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
import ms = require('ms');
|
2018-04-12 23:06:18 +02:00
|
|
|
import * as Router from 'koa-router';
|
|
|
|
import * as send from 'koa-send';
|
2018-07-05 19:58:29 +02:00
|
|
|
import { Context } from 'cafy';
|
|
|
|
import ObjectContext from 'cafy/built/types/object';
|
|
|
|
import config from '../../config';
|
|
|
|
import generateVars from '../../client/docs/vars';
|
2018-07-06 05:17:38 +02:00
|
|
|
import I18n from '../../build/i18n';
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
const docs = `${__dirname}/../../client/docs/`;
|
2018-03-29 13:50:45 +02:00
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
// WIP type
|
|
|
|
const parseEPDefParam = (key: string, param: Context) => {
|
|
|
|
return Object.assign({
|
|
|
|
name: key,
|
|
|
|
type: param.getType()
|
|
|
|
}, param.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortParams = (params: Array<{name: string}>) => {
|
|
|
|
params.sort((a, b) => {
|
|
|
|
if (a.name < b.name)
|
|
|
|
return -1;
|
|
|
|
if (a.name > b.name)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
// WIP type
|
|
|
|
const extractDefs = (params: Context[]) => {
|
|
|
|
let defs: any[] = [];
|
|
|
|
|
|
|
|
params.forEach(param => {
|
|
|
|
if (param.data && param.data.ref) {
|
|
|
|
const props = (param as ObjectContext<any>).props;
|
|
|
|
defs.push({
|
|
|
|
name: param.data.ref,
|
|
|
|
params: sortParams(Object.keys(props).map(k => parseEPDefParam(k, props[k])))
|
|
|
|
});
|
|
|
|
|
|
|
|
const childDefs = extractDefs(Object.keys(props).map(k => props[k]));
|
|
|
|
|
|
|
|
defs = defs.concat(childDefs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortParams(defs);
|
|
|
|
};
|
|
|
|
|
2018-04-12 23:06:18 +02:00
|
|
|
const router = new Router();
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
router.get('/assets/*', async ctx => {
|
2018-04-13 05:08:56 +02:00
|
|
|
await send(ctx, ctx.params[0], {
|
|
|
|
root: docs + '/assets/',
|
2018-04-13 05:05:24 +02:00
|
|
|
maxage: ms('7 days'),
|
|
|
|
immutable: true
|
|
|
|
});
|
2018-04-12 23:06:18 +02:00
|
|
|
});
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
router.get('/*/api/endpoints/*', async ctx => {
|
2018-07-06 05:17:38 +02:00
|
|
|
const lang = ctx.params[0];
|
2018-07-05 19:58:29 +02:00
|
|
|
const ep = require('../../../built/server/api/endpoints/' + ctx.params[1]).meta;
|
|
|
|
|
|
|
|
const vars = {
|
|
|
|
endpoint: ep.name,
|
|
|
|
url: {
|
|
|
|
host: config.api_url,
|
|
|
|
path: ep.name
|
|
|
|
},
|
|
|
|
desc: ep.desc,
|
|
|
|
// @ts-ignore
|
|
|
|
params: sortParams(Object.keys(ep.params).map(k => parseEPDefParam(k, ep.params[k]))),
|
|
|
|
paramDefs: extractDefs(Object.keys(ep.params).map(k => ep.params[k])),
|
|
|
|
};
|
|
|
|
|
|
|
|
const commonVars = await generateVars();
|
|
|
|
|
2018-07-06 05:17:38 +02:00
|
|
|
const i18n = new I18n(lang);
|
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign({}, vars, {
|
2018-07-06 05:17:38 +02:00
|
|
|
lang,
|
2018-07-05 19:58:29 +02:00
|
|
|
title: ep.name,
|
|
|
|
kebab: (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(),
|
2018-07-06 05:17:38 +02:00
|
|
|
i18n: (key: string) => i18n.get(null, key),
|
2018-07-05 19:58:29 +02:00
|
|
|
common: commonVars
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
router.get('*', async ctx => {
|
|
|
|
await send(ctx, `${ctx.params[0]}.html`, {
|
|
|
|
root: docs
|
|
|
|
});
|
2018-04-12 23:06:18 +02:00
|
|
|
});
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
export default router;
|