Sharkey/src/server/web/docs.ts

209 lines
4.9 KiB
TypeScript
Raw Normal View History

2017-12-16 16:41:22 +00:00
/**
2018-04-12 21:06:18 +00:00
* Docs
2017-12-16 16:41:22 +00:00
*/
2018-07-06 11:27:48 +00:00
import * as fs from 'fs';
import * as path from 'path';
2018-04-13 03:05:24 +00:00
import ms = require('ms');
2018-04-12 21:06:18 +00:00
import * as Router from 'koa-router';
import * as send from 'koa-send';
2018-07-05 17:58:29 +00:00
import { Context } from 'cafy';
2018-07-06 11:27:48 +00:00
import * as glob from 'glob';
import * as yaml from 'js-yaml';
2018-07-05 17:58:29 +00:00
import ObjectContext from 'cafy/built/types/object';
import config from '../../config';
2018-07-06 03:17:38 +00:00
import I18n from '../../build/i18n';
2018-07-06 11:27:48 +00:00
import { fa } from '../../build/fa';
import { licenseHtml } from '../../build/license';
const constants = require('../../const.json');
2017-12-16 16:41:22 +00:00
2018-04-13 03:05:24 +00:00
const docs = `${__dirname}/../../client/docs/`;
2018-03-29 11:50:45 +00:00
2018-07-06 11:27:48 +00: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);
const entities = glob.sync('./src/client/docs/api/entities/**/*.yaml');
vars['entities'] = entities.map(x => {
const _x = yaml.safeLoad(fs.readFileSync(x, 'utf-8')) as any;
return _x.name;
});
const docs = glob.sync('./src/client/docs/**/*.*.pug');
vars['docs'] = {};
docs.forEach(x => {
const [, name, lang] = x.match(/docs\/(.+?)\.(.+?)\.pug$/);
if (vars['docs'][name] == null) {
vars['docs'][name] = {
name,
title: {}
};
}
vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1];
});
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 17:58:29 +00:00
// WIP type
const parseEPDefParam = (key: string, param: Context) => {
return Object.assign({
name: key,
type: param.getType()
}, param.data);
};
2018-07-06 11:27:48 +00:00
const parseParam = (param: any) => {
const id = param.type.match(/^id\((.+?)\)|^id/);
const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type);
const isDate = /^date/.test(param.type);
const isArray = /\[\]$/.test(param.type);
if (id) {
param.kind = 'id';
param.type = 'string';
param.entity = id[1];
if (isArray) {
param.type += '[]';
}
}
if (entity) {
param.kind = 'entity';
param.type = 'object';
param.entity = entity[1];
if (isArray) {
param.type += '[]';
}
}
if (isObject) {
param.kind = 'object';
}
if (isDate) {
param.kind = 'date';
param.type = 'string';
if (isArray) {
param.type += '[]';
}
}
if (param.optional) {
param.type += '?';
}
return param;
};
2018-07-05 17:58:29 +00:00
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
2018-07-06 11:27:48 +00:00
const extractEPDefs = (params: Context[]) => {
2018-07-05 17:58:29 +00: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,
params: sortParams(Object.keys(props).map(k => parseEPDefParam(k, props[k])))
});
2018-07-06 11:27:48 +00:00
const childDefs = extractEPDefs(Object.keys(props).map(k => props[k]));
defs = defs.concat(childDefs);
}
});
return sortParams(defs);
};
const extractDefs = (params: any[]) => {
let defs: any[] = [];
params.forEach(param => {
if (param.def) {
defs.push({
name: param.defName,
params: sortParams(param.def.map((p: any) => parseParam(p)))
});
const childDefs = extractDefs(param.def);
2018-07-05 17:58:29 +00:00
defs = defs.concat(childDefs);
}
});
return sortParams(defs);
};
2018-04-12 21:06:18 +00:00
const router = new Router();
2017-12-16 16:41:22 +00:00
2018-04-13 03:05:24 +00:00
router.get('/assets/*', async ctx => {
2018-04-13 03:08:56 +00:00
await send(ctx, ctx.params[0], {
root: docs + '/assets/',
2018-04-13 03:05:24 +00:00
maxage: ms('7 days'),
immutable: true
});
2018-04-12 21:06:18 +00:00
});
2017-12-16 16:41:22 +00:00
2018-07-05 17:58:29 +00:00
router.get('/*/api/endpoints/*', async ctx => {
2018-07-06 03:17:38 +00:00
const lang = ctx.params[0];
2018-07-05 17:58:29 +00:00
const ep = require('../../../built/server/api/endpoints/' + ctx.params[1]).meta;
const vars = {
2018-07-06 11:27:48 +00:00
title: ep.name,
2018-07-05 17:58:29 +00:00
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]))),
2018-07-06 11:27:48 +00:00
paramDefs: extractEPDefs(Object.keys(ep.params).map(k => ep.params[k])),
2018-07-05 17:58:29 +00:00
};
2018-07-06 11:27:48 +00:00
await ctx.render('../../../../src/client/docs/api/endpoints/view', Object.assign(await genVars(lang), vars));
});
2018-07-05 17:58:29 +00:00
2018-07-06 11:27:48 +00:00
router.get('/*/api/entities/*', async ctx => {
const lang = ctx.params[0];
const entity = ctx.params[1];
2018-07-06 03:17:38 +00:00
2018-07-06 11:27:48 +00:00
const x = yaml.safeLoad(fs.readFileSync(path.resolve('./src/client/docs/api/entities/' + entity + '.yaml'), 'utf-8')) as any;
2018-07-05 17:58:29 +00:00
2018-07-06 11:27:48 +00:00
await ctx.render('../../../../src/client/docs/api/entities/view', Object.assign(await genVars(lang), {
name: x.name,
desc: x.desc,
props: sortParams(x.props.map((p: any) => parseParam(p))),
propDefs: extractDefs(x.props)
}));
2018-04-12 21:06:18 +00:00
});
2017-12-16 16:41:22 +00:00
2018-04-13 03:05:24 +00:00
export default router;