Skip to content

Commit

Permalink
implement support for v4 express openapi validator
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Oct 10, 2020
1 parent 7f2a34b commit 3909360
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
1 change: 0 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ module.exports = class extends Generator {
copyOpts.globOptions.ignore.push(src + '/server/common/api.v2.yml');
} else {
files.push('server/common/api.v2.yml');
copyOpts.globOptions.ignore.push(src + '/server/common/oas.js');
copyOpts.globOptions.ignore.push(src + '/server/common/api.yml');
}
if (!this.docker) {
Expand Down
30 changes: 15 additions & 15 deletions app/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,37 @@
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pino": "^6.4.0",
"pino": "^6.7.0",
<% if (specification === 'openapi_3') { %>
"express-openapi-validator": "^3.17.1"
"express-openapi-validator": "^4.2.0"
<% } else { %>
"swagger-express-middleware": "^4.0.1"
"swagger-express-middleware": "^4.0.2"
<% } %>
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/node": "^7.10.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-proposal-optional-chaining": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/register": "^7.10.5",
"@babel/plugin-proposal-optional-chaining": "^7.11.0",
"@babel/preset-env": "^7.11.5",
"@babel/register": "^7.11.5",
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",
"eslint": "^7.5.0",
"eslint-plugin-import": "^2.22.0",
"eslint": "^7.11.0",
"eslint-plugin-import": "^2.22.1",
<% if (linter === 'airbnb') { %>
"eslint-config-airbnb-base": "^14.2.0",
<% } else { %>
"eslint-config-prettier": "^6.11.0",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"prettier": "^2.0.5",
"prettier": "^2.1.2",
<% } %>
"mocha": "^8.0.1",
"mocha": "^8.1.3",
"nodemon": "^2.0.4",
"pino-pretty": "^4.1.0",
"supertest": "^4.0.2"
"pino-pretty": "^4.3.0",
"supertest": "^5.0.0"
},
"author": "Carmine DiMascio <cdimascio@gmail.com> (https://github.com/cdimascio)"
}
23 changes: 0 additions & 23 deletions app/templates/server/common/oas.js

This file was deleted.

42 changes: 33 additions & 9 deletions app/templates/server/common/server.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
import Express from 'express';
import cookieParser from 'cookie-parser';
import * as path from 'path';
import * as bodyParser from 'body-parser';
import * as http from 'http';
import * as os from 'os';
import cookieParser from 'cookie-parser';
<% if (specification === 'openapi_3') { %>
import oas from './oas';
import l from './logger';<% if (specification === 'openapi_3') { %>
import * as OpenApiValidator from 'express-openapi-validator';
import errorHandler from '../api/middlewares/error.handler'
<% } else { %>
import oas from './swagger';
<% } %>
import l from './logger';

const app = new Express();
const exit = process.exit;

export default class ExpressServer {
constructor() {
const root = path.normalize(`${__dirname}/../..`);
<% if (specification === 'openapi_3') { %>
const apiSpec = path.join(__dirname, 'api.yml');
const validateResponses = !!(
process.env.OPENAPI_ENABLE_RESPONSE_VALIDATION &&
process.env.OPENAPI_ENABLE_RESPONSE_VALIDATION.toLowerCase() === 'true'
);
<% } %>
app.set('appPath', `${root}client`);
app.use(bodyParser.json({ limit: process.env.REQUEST_LIMIT || '100kb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: process.env.REQUEST_LIMIT || '100kb' }));
app.use(bodyParser.text({ limit: process.env.REQUEST_LIMIT || '100kb'}));
app.use(cookieParser(process.env.SESSION_SECRET));
app.use(Express.static(`${root}/public`));
<% if (specification === 'openapi_3') { %>
app.use(process.env.OPENAPI_SPEC || '/spec', Express.static(apiSpec));
app.use(
OpenApiValidator.middleware({
apiSpec,
validateResponses,
ignorePaths: /.*\/spec(\/|$)/,
})
);
<% } %>
}

router(routes) {
<% if (specification === 'openapi_3') { %>
routes(app)
app.use(errorHandler)
return this;
<% } else { %>
this.routes = routes;
return this;
<% } %>
}

listen(port = process.env.PORT) {
Expand All @@ -36,14 +57,17 @@ export default class ExpressServer {
`up and running in ${process.env.NODE_ENV ||
'development'} @: ${os.hostname()} on port: ${p}}`
);

<% if (specification === 'openapi_3') { %>
http.createServer(app).listen(port, welcome(port));
<% } else { %>
oas(app, this.routes).then(() => {
http.createServer(app).listen(port, welcome(port));
}).catch(e => {
l.error(e);
exit(1)
// eslint-disable-next-line no-process-exit
process.exit(1)
});

<% } %>
return app;
}
}

0 comments on commit 3909360

Please sign in to comment.