misskey/swagger.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-01-03 20:56:20 -08:00
'use strict'
2017-01-03 19:59:20 -08:00
const swaggerJSDoc = require('swagger-jsdoc');
const fs = require('fs');
2017-01-03 20:56:20 -08:00
const yaml = require('js-yaml');
2017-01-03 19:59:20 -08:00
const apiRoot = './src/api/endpoints';
const files = [
'users.js',
'auth/session/generate.js',
'auth/session/userkey.js',
2017-01-03 19:59:20 -08:00
];
const errorDefinition = {
'type': 'object',
'properties':{
'error': {
'type': 'string',
'description': 'Error message'
}
}
}
var options = {
swaggerDefinition: {
swagger: '2.0',
info: {
title: 'Misskey API',
version: 'aoi',
},
2017-01-03 20:56:20 -08:00
host: 'api.misskey.xyz',
schemes: ['https'],
2017-01-03 19:59:20 -08:00
consumes: [
'application/x-www-form-urlencoded'
],
produces: [
'application/json'
]
},
apis: []
};
options.apis = files.map(c => {return `${apiRoot}/${c}`;});
2017-01-03 20:56:20 -08:00
if(fs.existsSync('.config/config.yml')){
var config = yaml.safeLoad(fs.readFileSync('./.config/config.yml', 'utf8'));
2017-01-03 22:14:34 -08:00
options.swaggerDefinition.host = `api.${config.url}`;
2017-01-03 20:56:20 -08:00
options.swaggerDefinition.schemes = config.https.enable ? ['https'] : ['http'];
}
2017-01-03 19:59:20 -08:00
var swaggerSpec = swaggerJSDoc(options);
swaggerSpec.definitions.Error = errorDefinition;
fs.writeFileSync('api-docs.json', JSON.stringify(swaggerSpec));