|
1 |
| -import openapi, { type IExpressOpenApi } from '@unleash/express-openapi'; |
2 |
| -import type { Application, RequestHandler } from 'express'; |
3 |
| -import type { OpenAPIV3 } from 'openapi-types'; |
4 |
| -import { createOpenApiSchema } from '.'; |
5 |
| -import type { IProxyConfig } from '../config'; |
6 |
| -import { format500ErrorMessage } from './common-responses'; |
| 1 | +import openapi, { type IExpressOpenApi } from "@wesleytodd/openapi"; |
| 2 | +import type { Application, RequestHandler } from "express"; |
| 3 | +import type { OpenAPIV3 } from "openapi-types"; |
| 4 | +import { createOpenApiSchema } from "."; |
| 5 | +import type { IProxyConfig } from "../config"; |
| 6 | +import { format500ErrorMessage } from "./common-responses"; |
7 | 7 |
|
8 | 8 | export class OpenApiService {
|
9 |
| - private readonly config: IProxyConfig; |
| 9 | + private readonly config: IProxyConfig; |
10 | 10 |
|
11 |
| - private readonly api: IExpressOpenApi; |
| 11 | + private readonly api: IExpressOpenApi; |
12 | 12 |
|
13 |
| - constructor(config: IProxyConfig) { |
14 |
| - this.config = config; |
15 |
| - this.api = openapi( |
16 |
| - this.docsPath(), |
17 |
| - createOpenApiSchema( |
18 |
| - config.proxyBasePath, |
19 |
| - config.clientKeysHeaderName, |
20 |
| - ), |
21 |
| - { coerce: true }, |
22 |
| - ); |
23 |
| - } |
| 13 | + constructor(config: IProxyConfig) { |
| 14 | + this.config = config; |
| 15 | + this.api = openapi( |
| 16 | + this.docsPath(), |
| 17 | + createOpenApiSchema(config.proxyBasePath, config.clientKeysHeaderName), |
| 18 | + { coerce: true }, |
| 19 | + ); |
| 20 | + } |
24 | 21 |
|
25 |
| - docsPath(): string { |
26 |
| - return `${this.config.proxyBasePath}/docs/openapi`; |
27 |
| - } |
| 22 | + docsPath(): string { |
| 23 | + return `${this.config.proxyBasePath}/docs/openapi`; |
| 24 | + } |
28 | 25 |
|
29 |
| - // Serve the OpenAPI JSON at `${this.docsPath()}.json`, |
30 |
| - // and the OpenAPI SwaggerUI at `${this.docsPathPath}`. |
31 |
| - useDocs(app: Application): void { |
32 |
| - app.use(this.api); |
33 |
| - app.use(this.docsPath(), this.api.swaggerui); |
34 |
| - } |
| 26 | + // Serve the OpenAPI JSON at `${this.docsPath()}.json`, |
| 27 | + // and the OpenAPI SwaggerUI at `${this.docsPathPath}`. |
| 28 | + useDocs(app: Application): void { |
| 29 | + app.use(this.api); |
| 30 | + app.use(this.docsPath(), this.api.swaggerui()); |
| 31 | + } |
35 | 32 |
|
36 |
| - // Create request validation middleware |
37 |
| - validPath(op: OpenAPIV3.OperationObject): RequestHandler { |
38 |
| - return this.api.validPath(op); |
39 |
| - } |
| 33 | + // Create request validation middleware |
| 34 | + validPath(op: OpenAPIV3.OperationObject): RequestHandler { |
| 35 | + return this.api.validPath(op); |
| 36 | + } |
40 | 37 |
|
41 |
| - // Catch and format Open API validation errors. |
42 |
| - useErrorHandler(app: Application): void { |
43 |
| - app.use((err: any, _: any, res: any, next: any) => { |
44 |
| - if (err?.status && err.validationErrors) { |
45 |
| - res.status(err.statusCode).json({ |
46 |
| - error: err.message, |
47 |
| - validation: err.validationErrors, |
48 |
| - }); |
49 |
| - } else if (err instanceof SyntaxError) { |
50 |
| - res.status(400).json({ |
51 |
| - error: `We were unable to parse the data you provided. Please check it for syntax errors. The message we got was: "${err.message}"`, |
52 |
| - }); |
53 |
| - } else if (err) { |
54 |
| - res.status(500).json({ |
55 |
| - error: format500ErrorMessage(err.message), |
56 |
| - }); |
57 |
| - } else { |
58 |
| - next(); |
59 |
| - } |
| 38 | + // Catch and format Open API validation errors. |
| 39 | + useErrorHandler(app: Application): void { |
| 40 | + app.use((err: any, _: any, res: any, next: any) => { |
| 41 | + if (err?.status && err.validationErrors) { |
| 42 | + res.status(err.statusCode).json({ |
| 43 | + error: err.message, |
| 44 | + validation: err.validationErrors, |
60 | 45 | });
|
61 |
| - } |
| 46 | + } else if (err instanceof SyntaxError) { |
| 47 | + res.status(400).json({ |
| 48 | + error: `We were unable to parse the data you provided. Please check it for syntax errors. The message we got was: "${err.message}"`, |
| 49 | + }); |
| 50 | + } else if (err) { |
| 51 | + res.status(500).json({ |
| 52 | + error: format500ErrorMessage(err.message), |
| 53 | + }); |
| 54 | + } else { |
| 55 | + next(); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
62 | 59 | }
|
0 commit comments