Skip to content

Commit

Permalink
chore: bump to upstream version of openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Oct 1, 2024
1 parent 6e9c52d commit df5579b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 164 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/Unleash/unleash-proxy#readme",
"dependencies": {
"@unleash/express-openapi": "^0.3.0",
"@wesleytodd/openapi": "^1.1.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.21.0",
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/// <reference path="./types/openapi.d.ts" />

import { createApp } from './app';
import Client from './client';
import { createProxyConfig } from './config';
import { start } from './server';
import { createApp } from "./app";
import Client from "./client";
import { createProxyConfig } from "./config";
import { start } from "./server";

export { createApp, start, Client, createProxyConfig };
101 changes: 49 additions & 52 deletions src/openapi/openapi-service.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
import openapi, { type IExpressOpenApi } from '@unleash/express-openapi';
import type { Application, RequestHandler } from 'express';
import type { OpenAPIV3 } from 'openapi-types';
import { createOpenApiSchema } from '.';
import type { IProxyConfig } from '../config';
import { format500ErrorMessage } from './common-responses';
import openapi, { type IExpressOpenApi } from "@wesleytodd/openapi";
import type { Application, RequestHandler } from "express";
import type { OpenAPIV3 } from "openapi-types";
import { createOpenApiSchema } from ".";
import type { IProxyConfig } from "../config";
import { format500ErrorMessage } from "./common-responses";

export class OpenApiService {
private readonly config: IProxyConfig;
private readonly config: IProxyConfig;

private readonly api: IExpressOpenApi;
private readonly api: IExpressOpenApi;

constructor(config: IProxyConfig) {
this.config = config;
this.api = openapi(
this.docsPath(),
createOpenApiSchema(
config.proxyBasePath,
config.clientKeysHeaderName,
),
{ coerce: true },
);
}
constructor(config: IProxyConfig) {
this.config = config;
this.api = openapi(
this.docsPath(),
createOpenApiSchema(config.proxyBasePath, config.clientKeysHeaderName),
{ coerce: true },
);
}

docsPath(): string {
return `${this.config.proxyBasePath}/docs/openapi`;
}
docsPath(): string {
return `${this.config.proxyBasePath}/docs/openapi`;
}

// Serve the OpenAPI JSON at `${this.docsPath()}.json`,
// and the OpenAPI SwaggerUI at `${this.docsPathPath}`.
useDocs(app: Application): void {
app.use(this.api);
app.use(this.docsPath(), this.api.swaggerui);
}
// Serve the OpenAPI JSON at `${this.docsPath()}.json`,
// and the OpenAPI SwaggerUI at `${this.docsPathPath}`.
useDocs(app: Application): void {
app.use(this.api);
app.use(this.docsPath(), this.api.swaggerui());
}

// Create request validation middleware
validPath(op: OpenAPIV3.OperationObject): RequestHandler {
return this.api.validPath(op);
}
// Create request validation middleware
validPath(op: OpenAPIV3.OperationObject): RequestHandler {
return this.api.validPath(op);
}

// Catch and format Open API validation errors.
useErrorHandler(app: Application): void {
app.use((err: any, _: any, res: any, next: any) => {
if (err?.status && err.validationErrors) {
res.status(err.statusCode).json({
error: err.message,
validation: err.validationErrors,
});
} else if (err instanceof SyntaxError) {
res.status(400).json({
error: `We were unable to parse the data you provided. Please check it for syntax errors. The message we got was: "${err.message}"`,
});
} else if (err) {
res.status(500).json({
error: format500ErrorMessage(err.message),
});
} else {
next();
}
// Catch and format Open API validation errors.
useErrorHandler(app: Application): void {
app.use((err: any, _: any, res: any, next: any) => {
if (err?.status && err.validationErrors) {
res.status(err.statusCode).json({
error: err.message,
validation: err.validationErrors,
});
}
} else if (err instanceof SyntaxError) {
res.status(400).json({
error: `We were unable to parse the data you provided. Please check it for syntax errors. The message we got was: "${err.message}"`,
});
} else if (err) {
res.status(500).json({
error: format500ErrorMessage(err.message),
});
} else {
next();
}
});
}
}
24 changes: 12 additions & 12 deletions src/types/openapi.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Partial types for "@unleash/express-openapi".
declare module '@unleash/express-openapi' {
import type { RequestHandler } from 'express';
declare module "@wesleytodd/openapi" {
import type { RequestHandler } from "express";

export interface IExpressOpenApi extends RequestHandler {
validPath: (operation: OpenAPIV3.OperationObject) => RequestHandler;
schema: (name: string, schema: OpenAPIV3.SchemaObject) => void;
swaggerui: RequestHandler;
}
export interface IExpressOpenApi extends RequestHandler {
validPath: (operation: OpenAPIV3.OperationObject) => RequestHandler;
schema: (name: string, schema: OpenAPIV3.SchemaObject) => void;
swaggerui: () => RequestHandler;
}

export default function openapi(
docsPath: string,
document: Omit<OpenAPIV3.Document, 'paths'>,
options?: { coerce: boolean },
): IExpressOpenApi;
export default function openapi(
docsPath: string,
document: Omit<OpenAPIV3.Document, "paths">,
options?: { coerce: boolean },
): IExpressOpenApi;
}
Loading

0 comments on commit df5579b

Please sign in to comment.