Skip to content

Commit

Permalink
Merge pull request #2 from natura-cosmeticos/custom-middleware
Browse files Browse the repository at this point in the history
Custom middleware
  • Loading branch information
marcelofabricanti committed Jun 13, 2022
2 parents 3c1e97b + e626bf3 commit 557204e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
This is a forked project from https://github.com/apigee-127/swagger-tools with an attemp to add Open API 3 support. :)

## Warning
This package was generated due to slowness of PR merging in original repo [bug-hunters/oas3-tools](https://github.com/bug-hunters/oas3-tools).

We needed the custom-middlewares feature so we've forked temporary.

We do not encourage using this package, cause once the original library merges this feature, we'll exclude the duplicate package here.

## Features included in this package (since fork)
* [feat(custom-middleware): added support of custom middlewares binding #47
](https://github.com/bug-hunters/oas3-tools/pull/47)
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oas3-tools",
"version": "2.2.0",
"name": "@naturacosmeticos/oas3-tools",
"version": "1.0.0",
"description": "Swagger-UI and API Rest Routing for Open API v3.",
"main": "dist/index.js",
"scripts": {
Expand All @@ -14,11 +14,11 @@
"bugs": {
"url": "https://github.com/bug-hunters/oas3-tools/issues"
},
"homepage": "https://github.com/bug-hunters/oas3-tools",
"homepage": "https://github.com/natura-cosmeticos/oas3-tools",
"license": "MIT",
"repository": {
"type": "git",
"url": "[email protected]:bug-hunters/oas3-tools.git"
"url": "[email protected]:natura-cosmeticos/oas3-tools.git"
},
"keywords": [
"api",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';
import { ExpressAppConfig } from "./middleware/express.app.config";
import { Oas3AppOptions } from "./middleware/oas3.options";
import { OpenApiRequestHandler } from 'express-openapi-validator/dist/framework/types'

export function expressAppConfig(definitionPath: string, appOptions: Oas3AppOptions): ExpressAppConfig {
return new ExpressAppConfig(definitionPath, appOptions);
export function expressAppConfig(
definitionPath: string,
appOptions: Oas3AppOptions,
customMiddlewares?: OpenApiRequestHandler[]
): ExpressAppConfig {
return new ExpressAppConfig(definitionPath, appOptions, customMiddlewares);
}
5 changes: 4 additions & 1 deletion src/middleware/express.app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import * as fs from 'fs';
import * as jsyaml from 'js-yaml';
import * as OpenApiValidator from 'express-openapi-validator';
import { Oas3AppOptions } from './oas3.options';
import { OpenApiRequestHandler } from 'express-openapi-validator/dist/framework/types'

export class ExpressAppConfig {
private app: express.Application;
private routingOptions;
private definitionPath;
private openApiValidatorOptions;

constructor(definitionPath: string, appOptions: Oas3AppOptions) {
constructor(definitionPath: string, appOptions: Oas3AppOptions, customMiddlewares?: OpenApiRequestHandler[]) {
this.definitionPath = definitionPath;
this.routingOptions = appOptions.routing;
this.setOpenApiValidatorOptions(definitionPath, appOptions);
Expand Down Expand Up @@ -47,6 +48,8 @@ export class ExpressAppConfig {

this.app.use(OpenApiValidator.middleware(this.openApiValidatorOptions));
this.app.use(new SwaggerParameters().checkParameters());
// Bind custom middlewares which need access to the OpenApiRequest context before controllers initialization
(customMiddlewares || []).forEach(middleware => this.app.use(middleware))
this.app.use(new SwaggerRouter().initialize(this.routingOptions));

this.app.use(this.errorHandler);
Expand Down

0 comments on commit 557204e

Please sign in to comment.