### Installing Express OpenAPI Middleware Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Command to install the @wesleytodd/openapi package using npm. ```shell $ npm install --save @wesleytodd/openapi ``` -------------------------------- ### Defining OpenAPI Component - JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Shows how to use the `oapi.component` method to define a new reusable component in the OpenAPI document. This example defines an 'examples' component named 'FooExample' with a summary and value. ```javascript oapi.component('examples', 'FooExample', { summary: 'An example of foo', value: 'bar' }) ``` -------------------------------- ### Getting Full Component Definition - JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Shows how to use `oapi.component` with only the component type to retrieve the full JSON definition of the component previously defined with that type and name. ```javascript oapi.component('examples') // { summary: 'An example of foo', value: 'bar' } ``` -------------------------------- ### Getting Component Reference Object - JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Illustrates using `oapi.component` with only the component type and name (or variable holding the name) to retrieve an OpenAPI Reference Object (`{ '$ref': '#/components/examples/FooExample' }`) pointing to the defined component. ```javascript oapi.component('examples', FooExample) // { '$ref': '#/components/examples/FooExample' } ``` -------------------------------- ### Registering Validated Path with Custom Keywords - Express/JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Shows how to use `oapi.validPath` with the `pathOpts` parameter to enable custom AJV keywords like 'regexp'. It defines a request body schema with a validation rule using `not: { regexp: '/^[A-Z]/' }`. An error handler middleware is included to process validation errors, similar to the previous example. ```javascript app.get('/zoom', oapi.validPath({ ... requestBody: { required: true, content: { 'application/json': { schema: { type: 'object', properties: { name: { type: 'string', not: { regexp: '/^[A-Z]/' } } } } } } }, ... }, { keywords: ['regexp'] }), (err, req, res, next) => { res.status(err.status).json({ error: err.message, validation: err.validationErrors, schema: err.validationSchema }) }) ``` -------------------------------- ### Initializing and Using Express OpenAPI Middleware Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Demonstrates how to require the middleware, initialize it with basic OpenAPI info, apply it to an Express app, and define a route with inline OpenAPI response schema. ```javascript const openapi = require('@wesleytodd/openapi') const app = require('express')() const oapi = openapi({ openapi: '3.0.0', info: { title: 'Express Application', description: 'Generated docs from an Express api', version: '1.0.0', } }) // This will serve the generated json document(s) // (as well as the swagger-ui if configured) app.use(oapi) // To add path specific schema you can use the .path middleware app.get('/', oapi.path({ responses: { 200: { description: 'Successful response', content: { 'application/json': { schema: { type: 'object', properties: { hello: { type: 'string' } } } } } } } }), (req, res) => { res.json({ hello: 'world' }) }) app.listen(8080) ``` -------------------------------- ### Serving Swagger UI Middleware (JavaScript) Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md This snippet demonstrates how to mount the `swaggerui` middleware provided by `express-openapi` onto a specific path (`/swaggerui`) in an Express application. ```javascript app.use('/swaggerui', oapi.swaggerui()) ``` -------------------------------- ### Registering Path with OpenAPI Document - Express/JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Shows how to use `oapi.path` middleware within an Express `app.get` route. It defines the OpenAPI `OperationObject` for the path, specifying the response schema for a 200 OK response. The subsequent route handler demonstrates accessing request parameters and sending a JSON response. ```javascript app.get('/:foo', oapi.path({ description: 'Get a foo', responses: { 200: { content: { 'application/json': { schema: { type: 'object', properties: { foo: { type: 'string' } } } } } } } }), (req, res) => { res.json({ foo: req.params.foo }) }) ``` -------------------------------- ### Fetching Generated OpenAPI Spec Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Shell command using curl to retrieve the generated OpenAPI JSON document from the running Express application and jq to format the output. ```shell $ curl -s http://localhost:8080/openapi.json | jq . ``` -------------------------------- ### Registering Validated Path and Handling Errors - Express/JavaScript Source: https://github.com/wesleytodd/express-openapi/blob/main/README.md Illustrates using `oapi.validPath` middleware to register a path and automatically validate incoming requests against the defined schema. It includes an error handling middleware function that catches validation errors (`err`) and sends a formatted JSON response containing error details, validation errors, and the schema. ```javascript app.get('/:foo', oapi.validPath({ description: 'Get a foo', responses: { 200: { content: { 'application/json': { schema: { type: 'object', properties: { foo: { type: 'string' } } } } } }, 400: { content: { 'application/json': { schema: { type: 'object', properties: { error: { type: 'string' } } } } } } } }), (err, req, res, next) => { res.status(err.status).json({ error: err.message, validation: err.validationErrors, schema: err.validationSchema }) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.