UNPKG

@senacor/azure-function-middleware

Version:

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

61 lines (60 loc) 3.8 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.requestValidation = requestValidation; const error_1 = require("../error"); function requestValidation(schema, opts) { var _a, _b; const skipIfResultIsFaulty = (_a = opts === null || opts === void 0 ? void 0 : opts.skipIfResultIsFaulty) !== null && _a !== void 0 ? _a : true; const printRequest = (_b = opts === null || opts === void 0 ? void 0 : opts.printInput) !== null && _b !== void 0 ? _b : false; return (req, context, result) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (skipIfResultIsFaulty && result.$failed) { context.info('Skipping request-validation because the result is faulty.'); return; } const clonedRequest = req.clone(); //see https://github.com/nuxt/nuxt/issues/19245 context.info('Validating the request.'); try { const toBeValidatedContent = (_b = (_a = opts === null || opts === void 0 ? void 0 : opts.extractValidationContentFromRequest) === null || _a === void 0 ? void 0 : _a.call(opts, clonedRequest, context, result)) !== null && _b !== void 0 ? _b : (yield clonedRequest.json()); const shouldThrowOnValidationError = (_c = opts === null || opts === void 0 ? void 0 : opts.shouldThrowOnValidationError) !== null && _c !== void 0 ? _c : true; const validationResult = schema.validate(toBeValidatedContent, opts === null || opts === void 0 ? void 0 : opts.joiValidationOptions); if (validationResult && validationResult.error) { context.error(`The request did not match the given schema.${printRequest ? JSON.stringify(toBeValidatedContent) : ''}: ${JSON.stringify(validationResult)}`); if (shouldThrowOnValidationError) { throw new error_1.ApplicationError('Validation Error', 400, (opts === null || opts === void 0 ? void 0 : opts.transformErrorMessage) ? opts === null || opts === void 0 ? void 0 : opts.transformErrorMessage(validationResult.error.message) : { message: validationResult.error.message, }); } } context.info('Finished validating the request.'); return; } catch (error) { if (error instanceof error_1.ApplicationError) { throw error; } if (error instanceof SyntaxError) { context.error(`The Json was probably ill-defined: ${error}`); //see https://fetch.spec.whatwg.org/#dom-body-json throw new error_1.ApplicationError('Validation Error', 400, { message: error.message, }); } context.error(`Unexpected server error occurred: `, error); throw new error_1.ApplicationError('Internal Server Error', 500, { message: 'An internal Server Error occurred while validating the request.', }); } }); }