Archived
2

Merge pull request #6 in ~VLE2FE/dfop-api from develop to measurement

* commit 'd4db905721cb706189ac8732ce953e321204f0a2':
  implemented feature
This commit is contained in:
Veit Lukas (PEA4-Fe) 2020-05-12 14:53:30 +02:00
commit 4185452ff3
4 changed files with 62 additions and 10 deletions

View File

@ -76,6 +76,7 @@
post:
summary: TODO add measurement
description: 'Auth: basic, levels: write, maintain, dev, admin'
x-doc: 'Adds status: 0 automatically'
tags:
- /measurement
security:

42
src/api.ts Normal file
View File

@ -0,0 +1,42 @@
import swagger from 'swagger-ui-express';
import jsonRefParser, {JSONSchema} from '@apidevtools/json-schema-ref-parser';
// modifies the normal swagger-ui-express package
// usage: app.use('/api', api.serve(), api.setup());
// the paths property can be split using allOf
// further route documentation can be included in the x-doc property
export default class api {
static serve () {
return swagger.serve;
}
static setup () {
let apiDoc: JSONSchema = {};
jsonRefParser.bundle('api/api.yaml', (err, doc) => { // parse yaml
if(err) throw err;
apiDoc = doc;
apiDoc.paths = apiDoc.paths.allOf.reduce((s, e) => Object.assign(s, e)); // bundle routes
apiDoc = this.resolveXDoc(apiDoc);
swagger.setup(apiDoc);
});
return swagger.setup(apiDoc, {customCssUrl: '/static/styles/swagger.css'})
}
private static resolveXDoc (doc) { // resolve x-doc properties recursively
Object.keys(doc).forEach(key => {
if (doc[key] !== null && doc[key].hasOwnProperty('x-doc')) {
doc[key].description += this.addHtml(doc[key]['x-doc']);
}
else if (typeof doc[key] === 'object' && doc[key] !== null) { // go deeper into recursion
doc[key] = this.resolveXDoc(doc[key]);
}
});
return doc;
}
private static addHtml (text) { // add docs HTML
return '<details class="docs"><summary>docs</summary>' + text + '</details>';
}
}

View File

@ -1,9 +1,8 @@
import express from 'express';
import bodyParser from 'body-parser';
import swagger from 'swagger-ui-express';
import jsonRefParser, {JSONSchema} from '@apidevtools/json-schema-ref-parser';
import contentFilter from 'content-filter';
import mongoSanitize from 'mongo-sanitize';
import api from './api';
import db from './db';
@ -56,14 +55,7 @@ app.use('/', require('./routes/measurement'));
app.use('/static', express.static('static'));
// Swagger UI
let apiDoc: JSONSchema = {};
jsonRefParser.bundle('api/api.yaml', (err, doc) => {
if(err) throw err;
apiDoc = doc;
apiDoc.paths = apiDoc.paths.allOf.reduce((s, e) => Object.assign(s, e));
swagger.setup(apiDoc, {defaultModelsExpandDepth: -1, customCss: '.swagger-ui .topbar { display: none }'});
});
app.use('/api', swagger.serve, swagger.setup(apiDoc, {customCssUrl: '/static/styles/swagger.css'}));
app.use('/api', api.serve(), api.setup());
app.use((req, res) => { // 404 error handling
res.status(404).json({status: 'Not found'});

View File

@ -52,6 +52,23 @@ body:after {
font-family: "Bosch Sans", sans-serif;
}
/*custom docs*/
.docs {
position: relative;
font-size: 14px;
}
.docs > summary {
position: absolute;
right: 0;
top: -25px;
cursor: pointer;
}
.docs-open:hover {
text-decoration: underline;
}
/*Remove topbar*/
.swagger-ui .topbar {
display: none