-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: bump to upstream version of openapi
- Loading branch information
Showing
5 changed files
with
169 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.