From 4b96f660e21da16afa258bde8056d416f70c9907 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Mon, 25 Mar 2019 23:02:39 -0400 Subject: [PATCH] cleanup types --- src/framework/types.ts | 5 +++++ src/index.ts | 7 ++++--- test/app.common.ts | 8 ++++---- test/headers.spec.ts | 4 ++-- test/routes.spec.ts | 4 ++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/framework/types.ts b/src/framework/types.ts index e1e56152..ddeeb922 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -2,6 +2,7 @@ // import { IOpenAPIRequestCoercer } from 'openapi-request-coercer'; // import { IOpenAPIRequestValidator } from 'openapi-request-validator'; // import { IOpenAPIResponseValidator } from 'openapi-response-validator'; +import { Request } from 'express'; import { // IOpenAPISecurityHandler, SecurityHandlers, @@ -135,3 +136,7 @@ export interface OpenAPIFrameworkVisitor { visitPath?(context: OpenAPIFrameworkPathContext): void; // visitOperation?(context: OpenAPIFrameworkOperationContext): void; } + +export interface OpenApiRequest extends Request { + openapi; +} diff --git a/src/index.ts b/src/index.ts index a41941c5..61d2856c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,10 @@ import * as _ from 'lodash'; -import { ExpressApp } from 'express'; +import { Application, Response, NextFunction } from 'express'; import { OpenAPIFrameworkArgs } from './framework'; import { OpenApiContext } from './openapi.context'; import * as middlewares from './middlewares'; import ono from 'ono'; +import { OpenApiRequest } from './framework/types'; const loggingKey = 'express-openapi-validator'; @@ -24,7 +25,7 @@ export function OpenApiValidator(options: OpenApiValidatorOpts) { this.context = openApiContext; } -OpenApiValidator.prototype.install = function(app: ExpressApp) { +OpenApiValidator.prototype.install = function(app: Application) { const pathParams = []; for (const route of this.context.routes) { if (route.pathParams.length > 0) { @@ -34,7 +35,7 @@ OpenApiValidator.prototype.install = function(app: ExpressApp) { // install param on routes with paths for (const p of _.uniq(pathParams)) { - app.param(p, (req, res, next, value, name) => { + app.param(p, (req: OpenApiRequest, res, next, value, name) => { if (req.openapi.pathParams) { // override path params req.params[name] = req.openapi.pathParams[name] || req.params[name]; diff --git a/test/app.common.ts b/test/app.common.ts index ced17f0a..24b8fcc6 100644 --- a/test/app.common.ts +++ b/test/app.common.ts @@ -17,22 +17,22 @@ export function routes(app) { .Router() .post('/', function(req, res, next) { res.json({ - name: `${req.metnod}: /router_1`, + name: `${req.method}: /router_1`, }); }) .get('/', function(req, res, next) { res.json({ - name: `${req.metnod}: /router_1`, + name: `${req.method}: /router_1`, }); }) .get('/:id', function(req, res, next) { res.json({ - name: `${req.metnod}: /router_1/${req.params.id}`, + name: `${req.method}: /router_1/${req.params.id}`, }); }) .get('/:id/best/:bid', function(req, res, next) { res.json({ - name: `${req.metnod}: /router_1/${req.params.id}/best/${ + name: `${req.method}: /router_1/${req.params.id}/best/${ req.params.bid }`, }); diff --git a/test/headers.spec.ts b/test/headers.spec.ts index 415553bd..cf665e05 100644 --- a/test/headers.spec.ts +++ b/test/headers.spec.ts @@ -3,11 +3,11 @@ import * as request from 'supertest'; import app from './app'; const packageJson = require('../package.json'); -const basePath = app.basePath; +const basePath = (app).basePath; describe(packageJson.name, () => { after(() => { - app.server.close(); + (app).server.close(); }); it('should throw 400 if required header is missing', async () => diff --git a/test/routes.spec.ts b/test/routes.spec.ts index 7f63e6f5..bcf4efca 100644 --- a/test/routes.spec.ts +++ b/test/routes.spec.ts @@ -3,11 +3,11 @@ import * as request from 'supertest'; import app from './app'; const packageJson = require('../package.json'); -const basePath = app.basePath; +const basePath = (app).basePath; describe(packageJson.name, () => { after(() => { - app.server.close(); + (app).server.close(); }); it(`should test something`, () => { expect('a').to.equal('a');