UNPKG

@senacor/azure-function-middleware

Version:

Middleware for azure functions to handle authentication, authorization, error handling and logging

87 lines (86 loc) 4.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.middleware = exports.isErrorResult = void 0; const error_1 = require("./error"); const stringify_1 = require("./util/stringify"); const isErrorResult = (result) => result === null || result === void 0 ? void 0 : result.$failed; exports.isErrorResult = isErrorResult; const middlewareCore = (beforeExecution, handler, postExecution) => (request, context) => __awaiter(void 0, void 0, void 0, function* () { let handlerResult = { $failed: false, $result: undefined }; if (beforeExecution) { const middlewareFunctions = beforeExecution.filter((predicate) => predicate !== false); for (const middlewareFunction of middlewareFunctions) { try { yield middlewareFunction(request, context, handlerResult); } catch (err) { if (err instanceof Error) { handlerResult = { $failed: true, $error: err }; continue; } handlerResult = { $failed: true, $error: new Error(`Caught ${(0, stringify_1.stringify)(err)} which is not of type Error`), }; } } } if (!handlerResult.$failed) { try { handlerResult = { $failed: false, $result: yield handler(request, context) }; } catch (err) { if (err instanceof Error) { handlerResult = { $failed: true, $error: err }; } } } if (postExecution) { const middlewareFunctions = postExecution.filter((predicate) => predicate !== false); for (const middlewareFunction of middlewareFunctions) { yield middlewareFunction(request, context, handlerResult); } } if ((0, exports.isErrorResult)(handlerResult)) { context.error(`An caught error occurred in the execution of the handler: ${handlerResult.$error}`); return handlerResult.$error; } return handlerResult.$result; }); function middlewareWrapper(beforeExecution, handler, postExecution, request, context, opts) { return __awaiter(this, void 0, void 0, function* () { const result = yield middlewareCore(beforeExecution, handler, postExecution)(request, context); if (result instanceof Error) { if (opts === null || opts === void 0 ? void 0 : opts.disableErrorHandling) { throw result; } return (0, error_1.errorHandler)(result, context, opts); } return result; }); } const middleware = (beforeExecution, handler, postExecution, opts) => // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - Sorry, but the type `FunctionResult<T = unknown> = T | Promise<T>` is very unhandy (request, context) => __awaiter(void 0, void 0, void 0, function* () { if (opts === null || opts === void 0 ? void 0 : opts.disableErrorHandling) { return yield middlewareWrapper(beforeExecution, handler, postExecution, request, context, opts); } try { return yield middlewareWrapper(beforeExecution, handler, postExecution, request, context, opts); } catch (error) { context.error(`An caught error occurred in the execution of the middleware: ${(0, stringify_1.stringify)(error)}`); return (0, error_1.errorHandler)(error, context, opts); } }); exports.middleware = middleware;