This commit is contained in:
syuilo 2017-12-14 22:36:04 +09:00
parent 5166fc92b6
commit 5b66410031
3 changed files with 45 additions and 21 deletions

View File

@ -14,7 +14,8 @@ import config from './../../../../conf';
const parseParam = param => { const parseParam = param => {
const id = param.type.match(/^id\((.+?)\)/); const id = param.type.match(/^id\((.+?)\)/);
const object = param.type.match(/^object\((.+?)\)/); const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type);
const isArray = /\[\]$/.test(param.type); const isArray = /\[\]$/.test(param.type);
if (id) { if (id) {
param.kind = 'id'; param.kind = 'id';
@ -24,18 +25,40 @@ const parseParam = param => {
param.type += '[]'; param.type += '[]';
} }
} }
if (object) { if (entity) {
param.kind = 'object'; param.kind = 'entity';
param.type = 'object'; param.type = 'object';
param.def = object[1]; param.entity = entity[1];
if (isArray) { if (isArray) {
param.type += '[]'; param.type += '[]';
} }
} }
if (isObject) {
param.kind = 'object';
}
return param; return param;
}; };
const extractDefs = params => {
const defs = [];
params.forEach(param => {
if (param.def) {
defs.push({
name: param.defName,
params: param.def.map(p => parseParam(p))
});
const childDefs = extractDefs(param.def);
defs.concat(childDefs);
}
});
return defs;
};
gulp.task('doc:endpoints', () => { gulp.task('doc:endpoints', () => {
glob('./src/web/docs/api/endpoints/**/*.yaml', (globErr, files) => { glob('./src/web/docs/api/endpoints/**/*.yaml', (globErr, files) => {
if (globErr) { if (globErr) {
@ -50,11 +73,9 @@ gulp.task('doc:endpoints', () => {
url: `${config.api_url}/${ep.endpoint}`, url: `${config.api_url}/${ep.endpoint}`,
desc: ep.desc, desc: ep.desc,
params: ep.params.map(p => parseParam(p)), params: ep.params.map(p => parseParam(p)),
paramDefs: Object.keys(ep.paramDefs).map(key => ({ paramDefs: extractDefs(ep.params),
name: key, res: ep.res.map(p => parseParam(p)),
params: ep.paramDefs[key].map(p => parseParam(p)) resDefs: extractDefs(ep.res)
})),
res: ep.res.map(p => parseParam(p))
}; };
pug.renderFile('./src/web/docs/api/endpoints/view.pug', vars, (renderErr, html) => { pug.renderFile('./src/web/docs/api/endpoints/view.pug', vars, (renderErr, html) => {
if (renderErr) { if (renderErr) {

View File

@ -7,7 +7,7 @@ desc:
params: params:
- name: "text" - name: "text"
type: "string" type: "string"
optional: false optional: true
desc: desc:
ja: "投稿の本文" ja: "投稿の本文"
en: "Text of a post" en: "Text of a post"
@ -30,20 +30,19 @@ params:
ja: "引用する投稿" ja: "引用する投稿"
en: "A post you want to quote" en: "A post you want to quote"
- name: "poll" - name: "poll"
type: "object(poll)" type: "object"
optional: true optional: true
desc: desc:
ja: "投票" ja: "投票"
en: "A poll" en: "A poll"
defName: "poll"
paramDefs: def:
poll: - name: "choices"
- name: "choices" type: "string[]"
type: "string[]" optional: false
optional: false desc:
desc: ja: "投票の選択肢"
ja: "投票の選択肢" en: "Choices of a poll"
en: "Choices of a poll"
res: res:
- name: "created_post" - name: "created_post"

View File

@ -21,9 +21,13 @@ mixin table(params)
= ' ' = ' '
a(href=`/docs/api/entities/${param.entity}`)= param.entity a(href=`/docs/api/entities/${param.entity}`)= param.entity
| ) | )
else if param.kind == 'entity'
| #{param.type} (
a(href=`/docs/api/entities/${param.entity}`)= param.entity
| )
else if param.kind == 'object' else if param.kind == 'object'
| #{param.type} ( | #{param.type} (
a(href=`#${param.def}`)= param.def a(href=`#${param.defName}`)= param.defName
| ) | )
else else
= param.type = param.type