Some comments
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
// Serves a Swagger UI with API definitions either created on the fly
|
||||
// or loaded from file system.
|
||||
//
|
||||
// Needs @sap/cds-dk >= 3.3.0 installed
|
||||
|
||||
const {resolve} = require ('path')
|
||||
const {promisify} = require('util')
|
||||
const readFile = promisify(require('fs').readFile)
|
||||
const swaggerUi = require ('swagger-ui-express')
|
||||
const cds = require ('@sap/cds')
|
||||
const trace = cds.debug('openapi')
|
||||
const cors = require('cors')
|
||||
|
||||
const debug = cds.debug('openapi')
|
||||
let app, docCache={}
|
||||
|
||||
cds
|
||||
@@ -29,8 +34,9 @@ async function toOpenApiDoc(service, cache) {
|
||||
if (spec) { // pre-compiled spec file available?
|
||||
cache[service.name] = spec
|
||||
}
|
||||
else if (cds.compile.to.openapi) { // on-the-fly exporter available?
|
||||
trace && trace ('Compiling Open API spec for', service.name)
|
||||
// On-the-fly exporter available? Needs @sap/cds-dk >= 3.3.0
|
||||
else if (cds.compile.to.openapi) {
|
||||
debug && debug ('Compiling Open API spec for', service.name)
|
||||
cache[service.name] = cds.compile.to.openapi (service.model, {
|
||||
service: service.name,
|
||||
'openapi:url': service.path,
|
||||
@@ -45,7 +51,7 @@ async function openApiFromFile(service) {
|
||||
const fileName = resolve(`srv/${service.name}.openapi3.json`)
|
||||
const file = await readFile(fileName).catch(()=>{/*no such file*/})
|
||||
if (file) {
|
||||
trace && trace ('Using Open API spec from file', fileName)
|
||||
debug && debug ('Using Open API spec from file', fileName)
|
||||
return JSON.parse(file)
|
||||
}
|
||||
}
|
||||
@@ -55,6 +61,7 @@ function addLinkToIndexHtml(service, apiPath) {
|
||||
if (entity) return // avoid link on entity level, looks too messy
|
||||
return { href:apiPath, name:'Swagger UI', title:'Show in Swagger UI' }
|
||||
}
|
||||
// Needs @sap/cds >= 4.4.0
|
||||
service.$linkProviders ? service.$linkProviders.push(provider) : service.$linkProviders = [provider]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user