Skip to content

Commit df5579b

Browse files
committed
chore: bump to upstream version of openapi
1 parent 6e9c52d commit df5579b

File tree

5 files changed

+169
-164
lines changed

5 files changed

+169
-164
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"homepage": "https://github.com/Unleash/unleash-proxy#readme",
4141
"dependencies": {
42-
"@unleash/express-openapi": "^0.3.0",
42+
"@wesleytodd/openapi": "^1.1.0",
4343
"compression": "^1.7.4",
4444
"cors": "^2.8.5",
4545
"express": "^4.21.0",

src/index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/// <reference path="./types/openapi.d.ts" />
2-
3-
import { createApp } from './app';
4-
import Client from './client';
5-
import { createProxyConfig } from './config';
6-
import { start } from './server';
1+
import { createApp } from "./app";
2+
import Client from "./client";
3+
import { createProxyConfig } from "./config";
4+
import { start } from "./server";
75

86
export { createApp, start, Client, createProxyConfig };

src/openapi/openapi-service.ts

+49-52
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,59 @@
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";
77

88
export class OpenApiService {
9-
private readonly config: IProxyConfig;
9+
private readonly config: IProxyConfig;
1010

11-
private readonly api: IExpressOpenApi;
11+
private readonly api: IExpressOpenApi;
1212

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+
}
2421

25-
docsPath(): string {
26-
return `${this.config.proxyBasePath}/docs/openapi`;
27-
}
22+
docsPath(): string {
23+
return `${this.config.proxyBasePath}/docs/openapi`;
24+
}
2825

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+
}
3532

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+
}
4037

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,
6045
});
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+
}
6259
}

src/types/openapi.d.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Partial types for "@unleash/express-openapi".
2-
declare module '@unleash/express-openapi' {
3-
import type { RequestHandler } from 'express';
2+
declare module "@wesleytodd/openapi" {
3+
import type { RequestHandler } from "express";
44

5-
export interface IExpressOpenApi extends RequestHandler {
6-
validPath: (operation: OpenAPIV3.OperationObject) => RequestHandler;
7-
schema: (name: string, schema: OpenAPIV3.SchemaObject) => void;
8-
swaggerui: RequestHandler;
9-
}
5+
export interface IExpressOpenApi extends RequestHandler {
6+
validPath: (operation: OpenAPIV3.OperationObject) => RequestHandler;
7+
schema: (name: string, schema: OpenAPIV3.SchemaObject) => void;
8+
swaggerui: () => RequestHandler;
9+
}
1010

11-
export default function openapi(
12-
docsPath: string,
13-
document: Omit<OpenAPIV3.Document, 'paths'>,
14-
options?: { coerce: boolean },
15-
): IExpressOpenApi;
11+
export default function openapi(
12+
docsPath: string,
13+
document: Omit<OpenAPIV3.Document, "paths">,
14+
options?: { coerce: boolean },
15+
): IExpressOpenApi;
1616
}

0 commit comments

Comments
 (0)