How to use express-openapi - 3 common examples

To help you get started, we’ve selected a few express-openapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inaturalist / iNaturalistAPI / openapi_app.js View on Github external
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",
github l3tnun / EPGStation / src / server / Service / Server.ts View on Github external
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
github Chinachu / Mirakurun / src / Mirakurun / Server.ts View on Github external
}
            }

            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"
                });
            }

express-openapi

An unopinionated OpenAPI framework for express

MIT
Latest version published 12 months ago

Package Health Score

80 / 100
Full package analysis

Popular express-openapi functions