From 0b594f46b9af09dac4aa48c2a771174dec2fb4fe Mon Sep 17 00:00:00 2001 From: Nebulis Date: Sat, 9 Feb 2019 01:13:52 +0800 Subject: [PATCH] fix: make error optional parameter for SwaggerToolsSecurityHandler callback --- types/swagger-node-runner/index.d.ts | 13 ++++++-- .../swagger-node-runner-tests.ts | 31 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/types/swagger-node-runner/index.d.ts b/types/swagger-node-runner/index.d.ts index 88da16aceb..3b2ef423e5 100644 --- a/types/swagger-node-runner/index.d.ts +++ b/types/swagger-node-runner/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for swagger-node-runner 0.5 +// Type definitions for swagger-node-runner 0.6 // Project: https://www.npmjs.com/package/swagger-node-runner // Definitions by: Michael Mrowetz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -36,6 +36,7 @@ import { Spec } from "swagger-schema-official"; import { EventEmitter } from "events"; import * as Hapi from "hapi"; import * as Restify from "restify"; +import { OutgoingHttpHeaders } from "http"; /** * Config object for SwaggerNodeRunner @@ -117,12 +118,18 @@ export type SwaggerToolsMiddleware = (req: any, res: any, next: any) => any; /** * @param callback - Error is returned if request is unauthorized. - * The Error may include "message", "state", and "code" fields to be conveyed to the client in the response body and a + * The Error may include "message", and "code" fields to be conveyed to the client in the response body and a * "headers" field containing an object representing headers to be set on the response to the client. * In addition, if the Error has a statusCode field, the response statusCode will be set to match - * otherwise, the statusCode will be set to 403. */ -export type SwaggerToolsSecurityHandler = (request: any, securityDefinition: any, scopes: any, callback: (err: Error) => void) => void; +export interface SwaggerToolsSecurityHandlerCallbackError { + code?: string; + headers?: OutgoingHttpHeaders; + message?: string; + statusCode?: number; +} +export type SwaggerToolsSecurityHandler = (request: any, securityDefinition: any, scopes: any, callback: (err?: Error | SwaggerToolsSecurityHandlerCallbackError, result?: any) => void) => void; /** * The keys match SecurityDefinition names and the associated values are functions that accept the following parameters: diff --git a/types/swagger-node-runner/swagger-node-runner-tests.ts b/types/swagger-node-runner/swagger-node-runner-tests.ts index 1deae28f23..bccbac5d94 100644 --- a/types/swagger-node-runner/swagger-node-runner-tests.ts +++ b/types/swagger-node-runner/swagger-node-runner-tests.ts @@ -84,7 +84,7 @@ SwaggerNodeRunner.create(config, (err, runner) => { app.listen(port); }); -const swaggerSecurityHandlerCb = (err: Error) => { +const swaggerSecurityHandlerCb = (err?: Error) => { // do nothing }; @@ -98,7 +98,34 @@ const configComplex: SwaggerNodeRunner.Config = { swaggerSecurityHandlers: { // did not manage to research the typings of first 3 arguments someHandlerName: ({}, {}, {}, swaggerSecurityHandlerCb) => { - // do nothing + swaggerSecurityHandlerCb(new Error('foo')); + } + }, + validateResponse: true +}; + +const handlerWithoutError: SwaggerNodeRunner.Config = { + appRoot: __dirname, + swaggerSecurityHandlers: { + // did not manage to research the typings of first 3 arguments + someHandlerName: ({}, {}, {}, swaggerSecurityHandlerCb) => { + swaggerSecurityHandlerCb(); + } + }, + validateResponse: true +}; + +const handlerWithHeaders: SwaggerNodeRunner.Config = { + appRoot: __dirname, + swaggerSecurityHandlers: { + // did not manage to research the typings of first 3 arguments + someHandlerName: ({}, {}, {}, swaggerSecurityHandlerCb) => { + swaggerSecurityHandlerCb({ + headers: { + foo: 'bar', + baz: 2, + some: ['a', 'b'], + }}); } }, validateResponse: true