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-07-06 13:27:48 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2018-07-15 11:28:08 +02:00
|
|
|
import * as showdown from 'showdown';
|
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-07 20:21:16 +02:00
|
|
|
import { Context, ObjectContext } from 'cafy';
|
2018-07-06 13:27:48 +02:00
|
|
|
import * as glob from 'glob';
|
|
|
|
import * as yaml from 'js-yaml';
|
2018-07-05 19:58:29 +02:00
|
|
|
import config from '../../config';
|
2018-07-07 12:21:54 +02:00
|
|
|
import I18n from '../../misc/i18n';
|
|
|
|
import { fa } from '../../misc/fa';
|
2018-07-07 20:13:20 +02:00
|
|
|
import { licenseHtml } from '../../misc/license';
|
2018-07-06 13:27:48 +02:00
|
|
|
const constants = require('../../const.json');
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-07-06 13:27:48 +02:00
|
|
|
async function genVars(lang: string): Promise<{ [key: string]: any }> {
|
|
|
|
const vars = {} as { [key: string]: any };
|
|
|
|
|
|
|
|
vars['lang'] = lang;
|
|
|
|
|
|
|
|
const endpoints = glob.sync('./built/server/api/endpoints/**/*.js');
|
|
|
|
vars['endpoints'] = endpoints.map(ep => require('../../../' + ep)).filter(x => x.meta).map(x => x.meta.name);
|
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
const entities = glob.sync('./src/docs/api/entities/**/*.yaml');
|
2018-07-06 13:27:48 +02:00
|
|
|
vars['entities'] = entities.map(x => {
|
|
|
|
const _x = yaml.safeLoad(fs.readFileSync(x, 'utf-8')) as any;
|
|
|
|
return _x.name;
|
|
|
|
});
|
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
const docs = glob.sync('./src/docs/**/*.md');
|
2018-07-06 13:27:48 +02:00
|
|
|
vars['docs'] = {};
|
|
|
|
docs.forEach(x => {
|
2018-07-15 11:28:08 +02:00
|
|
|
const [, name, lang] = x.match(/docs\/(.+?)\.(.+?)\.md$/);
|
2018-07-06 13:27:48 +02:00
|
|
|
if (vars['docs'][name] == null) {
|
|
|
|
vars['docs'][name] = {
|
|
|
|
name,
|
|
|
|
title: {}
|
|
|
|
};
|
|
|
|
}
|
2018-07-15 11:28:08 +02:00
|
|
|
vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^# (.+?)\r?\n/)[1];
|
2018-07-06 13:27:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
|
|
|
|
|
|
|
|
vars['config'] = config;
|
|
|
|
|
|
|
|
vars['copyright'] = constants.copyright;
|
|
|
|
|
|
|
|
vars['facss'] = fa.dom.css();
|
|
|
|
|
|
|
|
vars['license'] = licenseHtml;
|
|
|
|
|
|
|
|
const i18n = new I18n(lang);
|
|
|
|
vars['i18n'] = (key: string) => i18n.get(null, key);
|
|
|
|
|
|
|
|
return vars;
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
// WIP type
|
2018-07-07 06:34:42 +02:00
|
|
|
const parseParamDefinition = (key: string, param: Context) => {
|
2018-07-05 19:58:29 +02:00
|
|
|
return Object.assign({
|
|
|
|
name: key,
|
|
|
|
type: param.getType()
|
|
|
|
}, param.data);
|
|
|
|
};
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
const parsePropDefinition = (key: string, prop: any) => {
|
|
|
|
const id = prop.type.match(/^id\((.+?)\)|^id/);
|
|
|
|
const entity = prop.type.match(/^entity\((.+?)\)/);
|
|
|
|
const isObject = /^object/.test(prop.type);
|
|
|
|
const isDate = /^date/.test(prop.type);
|
|
|
|
const isArray = /\[\]$/.test(prop.type);
|
2018-07-06 13:27:48 +02:00
|
|
|
if (id) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.kind = 'id';
|
|
|
|
prop.type = 'string';
|
|
|
|
prop.entity = id[1];
|
2018-07-06 13:27:48 +02:00
|
|
|
if (isArray) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.type += '[]';
|
2018-07-06 13:27:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entity) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.kind = 'entity';
|
|
|
|
prop.type = 'object';
|
|
|
|
prop.entity = entity[1];
|
2018-07-06 13:27:48 +02:00
|
|
|
if (isArray) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.type += '[]';
|
2018-07-06 13:27:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isObject) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.kind = 'object';
|
|
|
|
if (prop.props) {
|
|
|
|
prop.hasDef = true;
|
|
|
|
}
|
2018-07-06 13:27:48 +02:00
|
|
|
}
|
|
|
|
if (isDate) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.kind = 'date';
|
|
|
|
prop.type = 'string';
|
2018-07-06 13:27:48 +02:00
|
|
|
if (isArray) {
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.type += '[]';
|
2018-07-06 13:27:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
if (prop.optional) {
|
|
|
|
prop.type += '?';
|
2018-07-06 13:27:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
prop.name = key;
|
|
|
|
|
|
|
|
return prop;
|
2018-07-06 13:27:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
const sortParams = (params: Array<{name: string}>) => {
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
// WIP type
|
2018-07-07 06:34:42 +02:00
|
|
|
const extractParamDefRef = (params: Context[]) => {
|
2018-07-05 19:58:29 +02:00
|
|
|
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,
|
2018-07-07 06:34:42 +02:00
|
|
|
params: sortParams(Object.keys(props).map(k => parseParamDefinition(k, props[k])))
|
2018-07-05 19:58:29 +02:00
|
|
|
});
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
const childDefs = extractParamDefRef(Object.keys(props).map(k => props[k]));
|
2018-07-06 13:27:48 +02:00
|
|
|
|
|
|
|
defs = defs.concat(childDefs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortParams(defs);
|
|
|
|
};
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
const extractPropDefRef = (props: any[]) => {
|
2018-07-06 13:27:48 +02:00
|
|
|
let defs: any[] = [];
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
Object.entries(props).forEach(([k, v]) => {
|
|
|
|
if (v.props) {
|
2018-07-06 13:27:48 +02:00
|
|
|
defs.push({
|
2018-07-07 06:34:42 +02:00
|
|
|
name: k,
|
|
|
|
props: sortParams(Object.entries(v.props).map(([k, v]) => parsePropDefinition(k, v)))
|
2018-07-06 13:27:48 +02:00
|
|
|
});
|
|
|
|
|
2018-07-07 06:34:42 +02:00
|
|
|
const childDefs = extractPropDefRef(v.props);
|
2018-07-05 19:58:29 +02:00
|
|
|
|
|
|
|
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], {
|
2018-07-15 11:28:08 +02:00
|
|
|
root: `${__dirname}/../../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 = {
|
2018-07-06 13:27:48 +02:00
|
|
|
title: ep.name,
|
2018-07-05 19:58:29 +02:00
|
|
|
endpoint: ep.name,
|
|
|
|
url: {
|
|
|
|
host: config.api_url,
|
|
|
|
path: ep.name
|
|
|
|
},
|
|
|
|
desc: ep.desc,
|
|
|
|
// @ts-ignore
|
2018-07-07 06:34:42 +02:00
|
|
|
params: sortParams(Object.entries(ep.params).map(([k, v]) => parseParamDefinition(k, v))),
|
|
|
|
paramDefs: extractParamDefRef(Object.entries(ep.params).map(([k, v]) => v)),
|
2018-07-15 11:28:08 +02:00
|
|
|
res: ep.res && ep.res.props ? sortParams(Object.entries(ep.res.props).map(([k, v]) => parsePropDefinition(k, v))) : null,
|
2018-07-07 06:34:42 +02:00
|
|
|
resDefs: null//extractPropDefRef(Object.entries(ep.res.props).map(([k, v]) => parsePropDefinition(k, v)))
|
2018-07-05 19:58:29 +02:00
|
|
|
};
|
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
await ctx.render('../../../../src/docs/api/endpoints/view', Object.assign(await genVars(lang), vars));
|
2018-07-06 13:27:48 +02:00
|
|
|
});
|
2018-07-05 19:58:29 +02:00
|
|
|
|
2018-07-06 13:27:48 +02:00
|
|
|
router.get('/*/api/entities/*', async ctx => {
|
|
|
|
const lang = ctx.params[0];
|
|
|
|
const entity = ctx.params[1];
|
2018-07-06 05:17:38 +02:00
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
const x = yaml.safeLoad(fs.readFileSync(path.resolve('./src/docs/api/entities/' + entity + '.yaml'), 'utf-8')) as any;
|
2018-07-05 19:58:29 +02:00
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
await ctx.render('../../../../src/docs/api/entities/view', Object.assign(await genVars(lang), {
|
2018-07-06 13:27:48 +02:00
|
|
|
name: x.name,
|
|
|
|
desc: x.desc,
|
2018-07-07 06:34:42 +02:00
|
|
|
props: sortParams(Object.entries(x.props).map(([k, v]) => parsePropDefinition(k, v))),
|
|
|
|
propDefs: extractPropDefRef(x.props)
|
2018-07-06 13:27:48 +02:00
|
|
|
}));
|
2018-04-12 23:06:18 +02:00
|
|
|
});
|
2017-12-16 17:41:22 +01:00
|
|
|
|
2018-07-06 16:52:47 +02:00
|
|
|
router.get('/*/*', async ctx => {
|
|
|
|
const lang = ctx.params[0];
|
|
|
|
const doc = ctx.params[1];
|
|
|
|
|
2018-07-15 11:28:08 +02:00
|
|
|
const conv = new showdown.Converter();
|
|
|
|
const md = fs.readFileSync(`${__dirname}/../../../src/docs/${doc}.${lang}.md`, 'utf8');
|
|
|
|
|
|
|
|
await ctx.render('../../../../src/docs/article', Object.assign({
|
|
|
|
html: conv.makeHtml(md)
|
|
|
|
}, await genVars(lang)));
|
2018-07-06 16:52:47 +02:00
|
|
|
});
|
|
|
|
|
2018-04-13 05:05:24 +02:00
|
|
|
export default router;
|