Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'express-openapi' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const jwtValidate = req => new Promise( resolve => {
if ( !req.headers.authorization ) {
return void resolve( );
}
const token = _.last( req.headers.authorization.split( /\s+/ ) );
jwt.verify( token, config.jwtSecret || "secret", { algorithms: ["HS512"] }, ( err, payload ) => {
if ( err ) {
return void resolve( );
}
req.userSession = payload;
resolve( true );
} );
} );
initializedApi = initialize( {
app,
docPath: "api-docs",
apiDoc: {
...v1ApiDoc,
"x-express-openapi-additional-middleware": [validateAllResponses],
"x-express-openapi-validation-strict": true
},
enableObjectCoercion: true,
dependencies: {
sendWrapper
},
securityFilter: ( req, res ) => {
// remove x-express-* attributes which don't need to be in the official documentation
res.status( 200 ).json( _.pickBy( req.apiDoc, ( value, key ) => !key.match( /^x-/ ) ) );
},
paths: "./openapi/paths/v2",
const pkg = require(path.join('..', '..', '..', 'package.json'));
// read api.yml
let api = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', '..', '..', 'api.yml'), 'utf-8'));
api.info = {
version: pkg.version,
title: pkg.name,
}
// swagger ui
const swaggerUi = require('swagger-ui-express');
this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(api));
this.app.get('/api/debug', (_req, res) => res.redirect('/api-docs/?url=/api/docs'));
// init express-openapi
openapi.initialize({
app: this.app,
apiDoc: api,
paths: path.join(__dirname, 'api'),
consumesMiddleware: {
'application/json': bodyParser.json(),
'text/text': bodyParser.text()
},
errorMiddleware: (err, _req, res, _next) => {
res.status(400);
res.json(err);
},
errorTransformer: (openapi, _jsonschema) => {
return openapi.message;
},
docsPath: '/docs',
exposeApiDocs: true
}
}
res.setHeader("Server", "Mirakurun/" + pkg.version);
next();
});
if (fs.existsSync("node_modules/swagger-ui-dist") === true) {
app.use("/swagger-ui", express.static("node_modules/swagger-ui-dist"));
app.get("/api/debug", (req, res) => res.redirect("/swagger-ui/?url=/api/docs"));
}
const api = yaml.safeLoad(fs.readFileSync("api.yml", "utf8"));
api.info.version = pkg.version;
openapi.initialize({
app: app,
apiDoc: api,
docsPath: "/docs",
paths: "./lib/Mirakurun/api"
});
app.use((err, req, res: express.Response, next) => {
log.error(JSON.stringify(err, null, " "));
console.error(err.stack);
if (res.headersSent === false) {
res.writeHead(err.status || 500, {
"Content-Type": "application/json"
});
}