|
| 1 | +import type { IRouter, Request, RequestHandler as Middleware, Response, NextFunction } from "express"; |
| 2 | +import type { OpenAPIV3 } from "openapi-types"; |
| 3 | +import type { SwaggerUIOptions } from "swagger-ui"; |
| 4 | + |
| 5 | +type Options = { |
| 6 | + basePath?: string; |
| 7 | + htmlui?: "swagger-ui"; |
| 8 | + coerce?: "true" | "false"; |
| 9 | +}; |
| 10 | + |
| 11 | +interface OpenApiMiddleware extends Middleware { |
| 12 | + document: OpenAPIV3.Document; |
| 13 | + options: Options; |
| 14 | + routePrefix: string; |
| 15 | + generateDocument: (doc: OpenAPIV3.Document, router?: IRouter, basePath?: string) => OpenAPIV3.Document; |
| 16 | + /** |
| 17 | + * Registers a path with the OpenAPI document. |
| 18 | + * The path `definition` is an {@link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject OperationObject} |
| 19 | + * with all of the information about the requests and responses on that route. |
| 20 | + * |
| 21 | + * It returns a middleware function which can be used in an express app. |
| 22 | + * |
| 23 | + * @example |
| 24 | + * |
| 25 | + * ```ts |
| 26 | + * app.get('/:foo', oapi.path({ |
| 27 | + * description: 'Get a foo', |
| 28 | + * responses: { |
| 29 | + * 200: { |
| 30 | + * content: { |
| 31 | + * 'application/json': { |
| 32 | + * schema: { |
| 33 | + * type: 'object', |
| 34 | + * properties: { |
| 35 | + * foo: { type: 'string' } |
| 36 | + * } |
| 37 | + * } |
| 38 | + * } |
| 39 | + * } |
| 40 | + * } |
| 41 | + * } |
| 42 | + * }), (req, res) => { |
| 43 | + * res.json({ |
| 44 | + * foo: req.params.foo |
| 45 | + * }) |
| 46 | + * }) |
| 47 | + * ``` |
| 48 | + */ |
| 49 | + path: (schema?: OpenAPIV3.OperationObject) => Middleware; |
| 50 | + /** |
| 51 | + * Registers a path with the OpenAPI document, also ensures incoming requests are valid against the schema. |
| 52 | + * The path `definition` is an {@link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject OperationObject} |
| 53 | + * with all of the information about the requests and responses on that route. |
| 54 | + * |
| 55 | + * It returns a middleware function which can be used in an express app and will call `next(err)` if the incoming request is invalid. |
| 56 | + * The error is created with ({@link https://www.npmjs.com/package/http-errors http-errors}), and then is augmented with information about the schema and validation errors. |
| 57 | + * Validation uses ({@link https://www.npmjs.com/package/ajv ajv}), and `err.validationErrors` is the format exposed by that package. |
| 58 | + * |
| 59 | + * Pass `{ keywords: [] }` as `pathOpts` to support custom validation based on ajv-keywords. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```js |
| 63 | + * app.get('/:foo', oapi.validPath({ |
| 64 | + * description: 'Get a foo', |
| 65 | + * responses: { |
| 66 | + * 200: { |
| 67 | + * content: { |
| 68 | + * 'application/json': { |
| 69 | + * schema: { |
| 70 | + * type: 'object', |
| 71 | + * properties: { |
| 72 | + * foo: { type: 'string' } |
| 73 | + * } |
| 74 | + * } |
| 75 | + * } |
| 76 | + * } |
| 77 | + * }, |
| 78 | + * 400: { |
| 79 | + * content: { |
| 80 | + * 'application/json': { |
| 81 | + * schema: { |
| 82 | + * type: 'object', |
| 83 | + * properties: { |
| 84 | + * error: { type: 'string' } |
| 85 | + * } |
| 86 | + * } |
| 87 | + * } |
| 88 | + * } |
| 89 | + * } |
| 90 | + * } |
| 91 | + * }), (err, req, res, next) => { |
| 92 | + * res.status(err.status).json({ |
| 93 | + * error: err.message, |
| 94 | + * validation: err.validationErrors, |
| 95 | + * schema: err.validationSchema |
| 96 | + * }) |
| 97 | + * }) |
| 98 | + * |
| 99 | + * app.get('/zoom', oapi.validPath({ |
| 100 | + * ... |
| 101 | + * requestBody: { |
| 102 | + * required: true, |
| 103 | + * content: { |
| 104 | + * 'application/json': { |
| 105 | + * schema: { |
| 106 | + * type: 'object', |
| 107 | + * properties: { |
| 108 | + * name: { type: 'string', not: { regexp: '/^[A-Z]/' } } |
| 109 | + * } |
| 110 | + * } |
| 111 | + * } |
| 112 | + * } |
| 113 | + * }, |
| 114 | + * ... |
| 115 | + * }, { keywords: ['regexp'] }), (err, req, res, next) => { |
| 116 | + * res.status(err.status).json({ |
| 117 | + * error: err.message, |
| 118 | + * validation: err.validationErrors, |
| 119 | + * schema: err.validationSchema |
| 120 | + * }) |
| 121 | + * }) |
| 122 | + * ``` |
| 123 | + */ |
| 124 | + validPath: (schema?: OpenAPIV3.OperationObject, pathOpts?: { strict?: boolan; keywords?: string | string[] }) => Middleware; |
| 125 | + /** |
| 126 | + * Defines a new {@link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#components-object Component} on the document. |
| 127 | + * |
| 128 | + * @example |
| 129 | + * ```js |
| 130 | + * oapi.component('examples', 'FooExample', { |
| 131 | + * summary: 'An example of foo', |
| 132 | + * value: 'bar' |
| 133 | + * }) |
| 134 | + * ``` |
| 135 | + * |
| 136 | + * If `name` is defined but `definition` is not, it will return a {@link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#referenceObject Reference Object} pointing to the component by that name. |
| 137 | + * |
| 138 | + * @example |
| 139 | + * ```js |
| 140 | + * oapi.component('examples', 'FooExample') |
| 141 | + * // { '$ref': '#/components/examples/FooExample' } |
| 142 | + * ``` |
| 143 | + * |
| 144 | + * If neither `definition` nor `name` are passed, the function will return the full `components` json. |
| 145 | + * |
| 146 | + * @example |
| 147 | + * ```js |
| 148 | + * oapi.component('examples') |
| 149 | + * // { summary: 'An example of foo', value: 'bar' } |
| 150 | + * ``` |
| 151 | + */ |
| 152 | + component: { |
| 153 | + <Type extends keyof Component>(type: Type): OpenAPIV3.ComponentsObject[Type]; |
| 154 | + <Type extends keyof Component>(type: Type, name: string): OpenAPIV3.ReferenceObject; |
| 155 | + <Type extends keyof Component>(type: Type, name: string, definition: Component[Type]): OpenApiMiddleware; |
| 156 | + }; |
| 157 | + schema: { |
| 158 | + (name: string): OpenAPIV3.ReferenceObject; |
| 159 | + (name: string, definition: OpenAPIV3.SchemaObject): OpenApiMiddleware; |
| 160 | + }; |
| 161 | + response: { |
| 162 | + (name: string): OpenAPIV3.ReferenceObject; |
| 163 | + (name: string, definition: OpenAPIV3.ResponseObject): OpenApiMiddleware; |
| 164 | + }; |
| 165 | + parameters: { |
| 166 | + (name: string): OpenAPIV3.ReferenceObject; |
| 167 | + (name: string, definition: OpenAPIV3.ParameterObject): OpenApiMiddleware; |
| 168 | + }; |
| 169 | + examples: { |
| 170 | + (name: string): OpenAPIV3.ReferenceObject; |
| 171 | + (name: string, definition: OpenAPIV3.ExampleObject): OpenApiMiddleware; |
| 172 | + }; |
| 173 | + requestBodies: { |
| 174 | + (name: string): OpenAPIV3.ReferenceObject; |
| 175 | + (name: string, definition: OpenAPIV3.RequestBodyObject): OpenApiMiddleware; |
| 176 | + }; |
| 177 | + headers: { |
| 178 | + (name: string): OpenAPIV3.ReferenceObject; |
| 179 | + (name: string, definition: OpenAPIV3.HeaderObject): OpenApiMiddleware; |
| 180 | + }; |
| 181 | + securitySchemes: { |
| 182 | + (name: string): OpenAPIV3.ReferenceObject; |
| 183 | + (name: string, definition: OpenAPIV3.SecuritySchemeObject): OpenApiMiddleware; |
| 184 | + }; |
| 185 | + links: { |
| 186 | + (name: string): OpenAPIV3.ReferenceObject; |
| 187 | + (name: string, definition: OpenAPIV3.LinkObject): OpenApiMiddleware; |
| 188 | + }; |
| 189 | + callbacks: { |
| 190 | + (name: string): OpenAPIV3.ReferenceObject; |
| 191 | + (name: string, definition: OpenAPIV3.CallbackObject): OpenApiMiddleware; |
| 192 | + }; |
| 193 | + /** |
| 194 | + * Serve an interactive UI for exploring the OpenAPI document. |
| 195 | + * |
| 196 | + * {@link https://www.npmjs.com/package/swagger-ui SwaggerUI} is one of the most popular tools for viewing OpenAPI documents and are bundled with the middleware. |
| 197 | + * The UI is not turned on by default but can be with the option mentioned above or by using one of these middleware. |
| 198 | + * Both interactive UIs also accept an optional object as a function argument which accepts configuration parameters for Swagger and Redoc. |
| 199 | + * The full list of Swagger and Redoc configuration options can be found {@link https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ here} |
| 200 | + * and {@link https://redocly.com/docs/redoc/config/ here} respectively. |
| 201 | + * |
| 202 | + * @example |
| 203 | + * ```js |
| 204 | + * app.use('/swaggerui', oapi.swaggerui()) |
| 205 | + * ``` |
| 206 | + */ |
| 207 | + swaggerui: (options?: SwaggerUIOptions) => Middleware[]; |
| 208 | +} |
| 209 | + |
| 210 | +/** |
| 211 | + * Utility type helper to return value types from the given Record type `T`. |
| 212 | + */ |
| 213 | +type ObjectValue<T extends Record<string, unknown>> = T extends { [key: string]: infer V } ? V : never; |
| 214 | + |
| 215 | +/** |
| 216 | + * Utility type helper to compose a map of `OpenAPIV3.ComponentsObject`. |
| 217 | + * |
| 218 | + * This map type is used to determine what type we are allowed to input or |
| 219 | + * expected to return from the {@link OpenApiMiddleware.component} function. |
| 220 | + */ |
| 221 | +type Component = { |
| 222 | + [K in keyof OpenAPIV3.ComponentsObject]-?: ObjectValue<OpenAPIV3.ComponentsObject[K]>; |
| 223 | +}; |
| 224 | + |
| 225 | +/** |
| 226 | + * Creates an instance of the documentation middleware. |
| 227 | + * The function that is returned is a middleware function decorated with helper methods for setting up the api documentation. |
| 228 | + * |
| 229 | + * @param route - A route for which the documentation will be served at |
| 230 | + * @param doc - Base document on top of which the paths will be added |
| 231 | + * @param options - Options |
| 232 | + * @param options.coerce - Enable data type {@link https://www.npmjs.com/package/ajv#coercing-data-types coercion} |
| 233 | + * @param options.htmlui - Turn on serving `swagger-ui` html ui |
| 234 | + * @param options.basePath - When set, will strip the value of `basePath` from the start of every path |
| 235 | + * |
| 236 | + * Coerce |
| 237 | + * |
| 238 | + * By default `coerceTypes` is set to `true` for `ajv`, but a copy of the `req` data is passed to prevent modifying the `req` in an unexpected way. |
| 239 | + * This is because the `coerceTypes` option in ({@link https://github.com/ajv-validator/ajv/issues/549 `ajv` modifies the input}). |
| 240 | + * If this is the behavior you want, you can pass `true` for this and a copy will not be made. |
| 241 | + * This will result in params in the path or query with type `number` will be converted to numbers {@link https://github.com/epoberezkin/ajv/blob/master/COERCION.md based on the rules from `ajv`}. |
| 242 | + */ |
| 243 | +function openapi(): OpenApiMiddleware; |
| 244 | +function openapi(doc: OpenAPIV3.Document, opts?: Options): OpenApiMiddleware; |
| 245 | +function openapi(route: string, doc?: OpenAPIV3.Document, opts?: Options): OpenApiMiddleware; |
| 246 | + |
| 247 | +namespace openapi { |
| 248 | + const minimumViableDocument: OpenAPIV3.Document; |
| 249 | + const defaultRoutePrefix: "/openapi"; |
| 250 | +} |
| 251 | + |
| 252 | +export = openapi; |
0 commit comments