diff --git a/src/framework/ajv/index.ts b/src/framework/ajv/index.ts index b5d753dc..f05f0445 100644 --- a/src/framework/ajv/index.ts +++ b/src/framework/ajv/index.ts @@ -21,7 +21,7 @@ export function createResponseAjv( function createAjv( openApiSpec: OpenAPIV3.Document, options: ajv.Options = {}, - request = true, + request: boolean = true, ): Ajv.Ajv { const ajv = new Ajv({ ...options, diff --git a/src/framework/openapi.schema.validator.ts b/src/framework/openapi.schema.validator.ts index 9e9d638f..8d85d6ca 100644 --- a/src/framework/openapi.schema.validator.ts +++ b/src/framework/openapi.schema.validator.ts @@ -3,10 +3,17 @@ import * as merge from 'lodash.merge'; import * as draftSchema from 'ajv/lib/refs/json-schema-draft-04.json'; // https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json import * as openapi3Schema from './openapi.v3.schema.json'; +import { OpenAPIV3 } from './types.js'; export class OpenAPISchemaValidator { private validator: Ajv.ValidateFunction; - constructor({ version, extensions }: { version: string; extensions?: object }) { + constructor({ + version, + extensions, + }: { + version: string; + extensions?: object; + }) { const v = new Ajv({ schemaId: 'auto', allErrors: true }); v.addMetaSchema(draftSchema); @@ -19,7 +26,9 @@ export class OpenAPISchemaValidator { this.validator = v.compile(schema); } - public validate(openapiDoc) { + public validate( + openapiDoc: OpenAPIV3.Document, + ): { errors: Array | null } { const valid = this.validator(openapiDoc); if (!valid) { return { errors: this.validator.errors }; diff --git a/src/framework/types.ts b/src/framework/types.ts index 74e1cec9..db8fd0ca 100644 --- a/src/framework/types.ts +++ b/src/framework/types.ts @@ -1,4 +1,4 @@ -import ajv = require('ajv'); +import * as ajv from 'ajv'; import { Request, Response, NextFunction } from 'express'; export { OpenAPIFrameworkArgs }; diff --git a/src/middlewares/openapi.response.validator.ts b/src/middlewares/openapi.response.validator.ts index 459d4d42..937bf5f7 100644 --- a/src/middlewares/openapi.response.validator.ts +++ b/src/middlewares/openapi.response.validator.ts @@ -58,7 +58,7 @@ export class ResponseValidator { } // TODO public for test only - fix me - _validate({ validators, body, statusCode, path }) { + public _validate({ validators, body, statusCode, path }) { // find the validator for the 'status code' e.g 200, 2XX or 'default' let validator; const status = statusCode; diff --git a/src/middlewares/openapi.security.ts b/src/middlewares/openapi.security.ts index 9648ab7f..919a77ac 100644 --- a/src/middlewares/openapi.security.ts +++ b/src/middlewares/openapi.security.ts @@ -3,6 +3,7 @@ import { OpenApiRequest, SecurityHandlers, OpenApiRequestMetadata, + OpenApiRequestHandler, } from '../framework/types'; import { validationError } from './util'; import { OpenApiContext } from '../framework/openapi.context'; @@ -21,7 +22,7 @@ interface SecurityHandlerResult { export function security( context: OpenApiContext, securityHandlers: SecurityHandlers, -) { +): OpenApiRequestHandler { return async (req, res, next) => { // TODO move the folllowing 3 check conditions to a dedicated upstream middleware if (!req.openapi) { @@ -31,12 +32,13 @@ export function security( return next(); } - const expressRoute = req.openapi.expressRoute; + const openapi = req.openapi; + const expressRoute = openapi.expressRoute; if (!expressRoute) { return next(validationError(404, req.path, 'not found')); } - const pathSchema = req.openapi.schema; + const pathSchema = openapi.schema; if (!pathSchema) { // add openapi metadata to make this case more clear // its not obvious that missig schema means methodNotAllowed @@ -47,9 +49,9 @@ export function security( // use the local security object or fallbac to api doc's security or undefined const securities: OpenAPIV3.SecurityRequirementObject[] = - req.openapi.schema.security ?? context.apiDoc.security; + openapi.schema.security ?? context.apiDoc.security; - const path: string = req.openapi.openApiRoute; + const path: string = openapi.openApiRoute; if (!path || !Array.isArray(securities) || securities.length === 0) { return next(); @@ -94,8 +96,8 @@ export function security( class SecuritySchemes { private securitySchemes; private securityHandlers: SecurityHandlers; - private securities; - constructor(securitySchemes, securityHandlers: SecurityHandlers, securities) { + private securities: OpenAPIV3.SecurityRequirementObject[]; + constructor(securitySchemes, securityHandlers: SecurityHandlers, securities: OpenAPIV3.SecurityRequirementObject[]) { this.securitySchemes = securitySchemes; this.securityHandlers = securityHandlers; this.securities = securities; diff --git a/test/common/app.common.ts b/test/common/app.common.ts index 5a3f5f9b..3ade110c 100644 --- a/test/common/app.common.ts +++ b/test/common/app.common.ts @@ -1,3 +1,4 @@ +import { NextFunction, Request, Response } from 'express'; import * as http from 'http'; import * as express from 'express'; @@ -17,22 +18,22 @@ export function routes(app) { const basePath = app.basePath; const router1 = express .Router() - .post('/', function(req, res, next) { + .post('/', function(req: Request, res: Response) { res.json({ name: `${req.method}: /router_1`, }); }) - .get('/', function(req, res, next) { + .get('/', function(req: Request, res: Response) { res.json({ name: `${req.method}: /router_1`, }); }) - .get('/:id', function(req, res, next) { + .get('/:id', function(req: Request, res: Response) { res.json({ name: `${req.method}: /router_1/${req.params.id}`, }); }) - .get('/:id/best/:bid', function(req, res, next) { + .get('/:id/best/:bid', function(req: Request, res: Response) { res.json({ name: `${req.method}: /router_1/${req.params.id}/best/${req.params.bid}`, }); @@ -40,36 +41,38 @@ export function routes(app) { app.use(`${basePath}/router_1`, router1); - app.get(`${basePath}/pets`, function(req, res, next) { + app.get(`${basePath}/pets`, function(req: Request, res: Response) { res.json({ test: 'hi', ...req.body, }); }); - app.post(`${basePath}/pets`, function(req, res, next) { + app.post(`${basePath}/pets`, function(req: Request, res: Response) { res.json({ ...req.body, id: 'new-id', }); }); - app.get(`${basePath}/pets/:id`, function(req, res, next) { + app.get(`${basePath}/pets/:id`, function(req: Request, res: Response) { res.json({ id: req.params.id, }); }); - app.get(`${basePath}/pets/:id/attributes`, function(req, res, next) { + app.get(`${basePath}/pets/:id/attributes`, function( + req: Request, + res: Response, + ) { res.json({ id: req.params.id, }); }); app.get(`${basePath}/pets/:id/attributes/:attribute_id`, function( - req, - res, - next, + req: Request, + res: Response, ) { res.json({ id: req.params.id, @@ -78,22 +81,24 @@ export function routes(app) { }); app.post(`${basePath}/route_defined_in_express_not_openapi`, function( - req, - res, - next, + req: Request, + res: Response, ) { res.json({ id: req.params.id, }); }); - app.get('/not_under_an_openapi_basepath', function(req, res, next) { + app.get('/not_under_an_openapi_basepath', function( + req: Request, + res: Response, + ) { res.json({ id: '/not_under_an_openapi_basepath', }); }); - app.post('/v1/pets/:id/photos', function(req, res, next) { + app.post('/v1/pets/:id/photos', function(req: Request, res: Response) { // req.file is the `avatar` file // req.body will hold the text fields, if there were any const files = req.files; @@ -102,7 +107,7 @@ export function routes(app) { metadata: req.body.metadata, }); }); - app.post('/v1/pets_charset', function (req: Request, res: any) { + app.post('/v1/pets_charset', function(req: Request, res: Response) { // req.file is the `avatar` file // req.body will hold the text fields, if there were any res.json({ diff --git a/test/common/app.ts b/test/common/app.ts index cb1e84ee..c67bec43 100644 --- a/test/common/app.ts +++ b/test/common/app.ts @@ -10,9 +10,9 @@ import { OpenApiValidatorOpts } from '../../src/framework/types'; export async function createApp( opts?: OpenApiValidatorOpts, - port = 3000, + port: number = 3000, customRoutes = app => {}, - useRoutes = true, + useRoutes: boolean = true, ) { var app = express(); (app).basePath = '/v1'; diff --git a/test/openapi.spec.ts b/test/openapi.spec.ts index 0944f96a..2b1b7c14 100644 --- a/test/openapi.spec.ts +++ b/test/openapi.spec.ts @@ -5,7 +5,7 @@ import { createApp } from './common/app'; import * as packageJson from '../package.json'; describe(packageJson.name, () => { - let apps = []; + const apps = []; let basePath = null; before(() => { diff --git a/test/path.level.parameters.spec.ts b/test/path.level.parameters.spec.ts index 3bfa00e9..b316cfd3 100644 --- a/test/path.level.parameters.spec.ts +++ b/test/path.level.parameters.spec.ts @@ -7,7 +7,6 @@ import * as packageJson from '../package.json'; describe(packageJson.name, () => { let app = null; - let basePath = null; before(async () => { // Set up the express app diff --git a/test/query.params.spec.ts b/test/query.params.spec.ts index 1539b828..0d3e6247 100644 --- a/test/query.params.spec.ts +++ b/test/query.params.spec.ts @@ -7,7 +7,6 @@ import * as packageJson from '../package.json'; describe(packageJson.name, () => { let app = null; - let basePath = null; before(async () => { // Set up the express app diff --git a/test/security.handlers.spec.ts b/test/security.handlers.spec.ts index a51e675b..96fa2e91 100644 --- a/test/security.handlers.spec.ts +++ b/test/security.handlers.spec.ts @@ -290,7 +290,7 @@ describe('security.handlers', () => { const validateSecurity = eovConf.validateSecurity; validateSecurity.handlers.OAuth2 = function( req, - scopes, + scopes: string[], schema: OpenAPIV3.OAuth2SecurityScheme, ) { expect(schema.type).to.equal('oauth2');