openapi-backend
Advanced tools
Comparing version
@@ -32,2 +32,8 @@ import type { Options as AjvOpts } from 'ajv'; | ||
export type Handler<RequestBody = any, Params = UnknownParams, Query = UnknownParams, Headers = UnknownParams, Cookies = UnknownParams, D extends Document = Document> = (context: Context<RequestBody, Params, Query, Headers, Cookies, D>, ...args: any[]) => any | Promise<any>; | ||
/** | ||
* Map of operation handlers | ||
*/ | ||
export type HandlerMap = { | ||
[operationId: string]: Handler | undefined; | ||
}; | ||
export type BoolPredicate = (context: Context, ...args: any[]) => boolean; | ||
@@ -59,11 +65,9 @@ /** | ||
customizeAjv?: AjvCustomizer; | ||
handlers?: { | ||
handlers?: HandlerMap & { | ||
notFound?: Handler; | ||
notImplemented?: Handler; | ||
validationFail?: Handler; | ||
[handler: string]: Handler | undefined; | ||
}; | ||
securityHandlers?: { | ||
[handler: string]: Handler | undefined; | ||
}; | ||
securityHandlers?: HandlerMap; | ||
ignoreTrailingSlashes?: boolean; | ||
} | ||
@@ -88,9 +92,5 @@ /** | ||
customizeAjv: AjvCustomizer | undefined; | ||
handlers: { | ||
[operationId: string]: Handler; | ||
}; | ||
handlers: HandlerMap; | ||
allowedHandlers: string[]; | ||
securityHandlers: { | ||
[name: string]: Handler; | ||
}; | ||
securityHandlers: HandlerMap; | ||
router: OpenAPIRouter<D>; | ||
@@ -160,5 +160,3 @@ validator: OpenAPIValidator<D>; | ||
*/ | ||
register(handlers: { | ||
[operationId: string]: Handler; | ||
}): void; | ||
register<Handlers extends HandlerMap = HandlerMap>(handlers: Handlers): void; | ||
/** | ||
@@ -165,0 +163,0 @@ * Registers a handler for an operation |
@@ -74,2 +74,3 @@ "use strict"; | ||
constructor(opts) { | ||
var _a, _b; | ||
this.allowedHandlers = [ | ||
@@ -93,3 +94,2 @@ '404', | ||
ignoreTrailingSlashes: true, | ||
ajvOpts: {}, | ||
handlers: {}, | ||
@@ -99,11 +99,11 @@ securityHandlers: {}, | ||
}; | ||
this.apiRoot = optsWithDefaults.apiRoot; | ||
this.apiRoot = (_a = optsWithDefaults.apiRoot) !== null && _a !== void 0 ? _a : '/'; | ||
this.inputDocument = optsWithDefaults.definition; | ||
this.strict = optsWithDefaults.strict; | ||
this.quick = optsWithDefaults.quick; | ||
this.validate = optsWithDefaults.validate; | ||
this.ignoreTrailingSlashes = optsWithDefaults.ignoreTrailingSlashes; | ||
this.strict = !!optsWithDefaults.strict; | ||
this.quick = !!optsWithDefaults.quick; | ||
this.validate = !!optsWithDefaults.validate; | ||
this.ignoreTrailingSlashes = !!optsWithDefaults.ignoreTrailingSlashes; | ||
this.handlers = { ...optsWithDefaults.handlers }; // Copy to avoid mutating passed object | ||
this.securityHandlers = { ...optsWithDefaults.securityHandlers }; // Copy to avoid mutating passed object | ||
this.ajvOpts = optsWithDefaults.ajvOpts; | ||
this.ajvOpts = (_b = optsWithDefaults.ajvOpts) !== null && _b !== void 0 ? _b : {}; | ||
this.customizeAjv = optsWithDefaults.customizeAjv; | ||
@@ -186,3 +186,5 @@ } | ||
for (const [name, handler] of Object.entries(this.securityHandlers)) { | ||
this.registerSecurityHandler(name, handler); | ||
if (handler) { | ||
this.registerSecurityHandler(name, handler); | ||
} | ||
} | ||
@@ -251,5 +253,6 @@ } | ||
if (this.securityHandlers[name]) { | ||
const securityHandler = this.securityHandlers[name]; | ||
// return a promise that will set the security handler result | ||
return await Promise.resolve() | ||
.then(() => this.securityHandlers[name](context, ...handlerArgs)) | ||
.then(() => securityHandler(context, ...handlerArgs)) | ||
.then((result) => { | ||
@@ -256,0 +259,0 @@ securityHandlerResults[name] = result; |
{ | ||
"name": "openapi-backend", | ||
"description": "Build, Validate, Route, Authenticate and Mock using OpenAPI definitions. Framework-agnostic", | ||
"version": "5.10.6", | ||
"version": "5.11.0", | ||
"author": "Viljami Kuosmanen <viljami@viljami.io>", | ||
@@ -58,3 +58,3 @@ "funding": "https://github.com/sponsors/anttiviljami", | ||
"@types/cookie": "^0.5.2", | ||
"@types/jest": "^27.0.3", | ||
"@types/jest": "^29.5.12", | ||
"@types/json-schema": "^7.0.7", | ||
@@ -61,0 +61,0 @@ "@types/lodash": "^4.14.122", |
@@ -20,2 +20,3 @@ import type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; | ||
export type AnyRequestBody = any; | ||
export type UnknownParams = any; | ||
export interface Request { | ||
@@ -32,5 +33,2 @@ method: string; | ||
} | ||
export type UnknownParams = { | ||
[key: string]: string | string[]; | ||
}; | ||
export interface ParsedRequest<RequestBody = AnyRequestBody, Params = UnknownParams, Query = UnknownParams, Headers = UnknownParams, Cookies = UnknownParams> { | ||
@@ -37,0 +35,0 @@ method: string; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
154742
0.19%2461
-0.04%