Add middleware

This commit is contained in:
Nicholas Wallace 2024-06-21 22:38:01 +00:00
parent e52b695f7e
commit 4bf279588b
3 changed files with 473 additions and 3 deletions

View file

@ -1,6 +1,7 @@
const Path = require('path')
const Sequelize = require('sequelize')
const express = require('express')
const OpenApiValidator = require('express-openapi-validator')
const http = require('http')
const util = require('util')
const fs = require('./libs/fsExtra')
@ -203,6 +204,39 @@ class Server {
next()
})
// Install the OpenApiValidator middleware if using a dev environment
if (Logger.isDev) {
// If you do not want to use this, you can comment out the entire section
// or only certain parts.
//
// The default value of `validateResponses` is false, so it can be safely commented
// out if the validation is causing problems.
const apiSpec = Path.join(__dirname, '..', 'docs', 'openapi.json')
app.use(
OpenApiValidator.middleware({
apiSpec: apiSpec,
validateRequests: true,
validateResponses: {
onError: (error, body, req) => {
console.log(`Response body fails validation: `, error)
console.log(`Emitted from:`, req.originalUrl)
console.debug(body)
}
},
formats: [
// Define custom format types for OpenAPI spec
{
name: 'uuid',
type: 'string',
validate: (v) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(v.toString())
}
],
unknownFormats: ['li_[a-z0-9]{18}', '[0-9]*'],
ignoreUndocumented: true
})
)
}
// parse cookies in requests
app.use(cookieParser())
// enable express-session