UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

101 lines 5.61 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.RunHandlerAsFilter = void 0; const logger_1 = require("@bitblit/ratchet/common/logger"); const promise_ratchet_1 = require("@bitblit/ratchet/common/promise-ratchet"); const timeout_token_1 = require("@bitblit/ratchet/common/timeout-token"); const request_timeout_error_1 = require("../../http/error/request-timeout-error"); const response_util_1 = require("../../http/response-util"); const not_found_error_1 = require("../../http/error/not-found-error"); const null_returned_object_handling_1 = require("../../config/http/null-returned-object-handling"); const common_1 = require("@bitblit/ratchet/common"); const string_ratchet_1 = require("@bitblit/ratchet/common/string-ratchet"); class RunHandlerAsFilter { static runHandler(fCtx, rm) { return __awaiter(this, void 0, void 0, function* () { // Check for continue // Run the controller const handler = RunHandlerAsFilter.findHandler(rm, fCtx.event, fCtx.context); logger_1.Logger.debug('Processing event with epsilon: %s', RunHandlerAsFilter.eventToStringForLog(fCtx.event)); let tmp = yield handler; if (timeout_token_1.TimeoutToken.isTimeoutToken(tmp)) { tmp.writeToLog(); throw new request_timeout_error_1.RequestTimeoutError('Timed out'); } logger_1.Logger.debug('Initial return value : %j', tmp); tmp = RunHandlerAsFilter.applyNullReturnedObjectHandling(tmp, rm.mapping.metaProcessingConfig.nullReturnedObjectHandling); fCtx.rawResult = tmp; fCtx.result = response_util_1.ResponseUtil.coerceToProxyResult(tmp); return true; }); } static applyNullReturnedObjectHandling(result, handling) { let rval = result; if (result === null || result === undefined) { if (handling === null_returned_object_handling_1.NullReturnedObjectHandling.Error) { logger_1.Logger.error('Null object returned and Error specified, throwing 500'); throw new common_1.RestfulApiHttpError('Null object').withHttpStatusCode(500); } else if (handling === null_returned_object_handling_1.NullReturnedObjectHandling.Return404NotFoundResponse) { throw new not_found_error_1.NotFoundError('Resource not found'); } else if (handling === null_returned_object_handling_1.NullReturnedObjectHandling.ConvertToEmptyString) { logger_1.Logger.warn('Null object returned from handler and convert not specified, converting to empty string'); rval = ''; } else { throw new common_1.RestfulApiHttpError('Cant happen - failed enum check').withHttpStatusCode(500); } } return rval; } static findHandler(rm, event, context, add404OnMissing = true) { return __awaiter(this, void 0, void 0, function* () { let rval = null; // Execute if (rm) { // We extend with the parsed params here in case we are using the AWS any proxy event.pathParameters = Object.assign({}, event.pathParameters, rm.parsed); rval = promise_ratchet_1.PromiseRatchet.timeout(rm.mapping.function(event, context), 'Timed out after ' + rm.mapping.metaProcessingConfig.timeoutMS + ' ms. Request was ' + RunHandlerAsFilter.eventToStringForLog(event), rm.mapping.metaProcessingConfig.timeoutMS); } else if (add404OnMissing) { throw new not_found_error_1.NotFoundError('No such endpoint'); } return rval; }); } static addRunHandlerAsFilterToList(filters, rm) { if (filters) { filters.push((fCtx) => RunHandlerAsFilter.runHandler(fCtx, rm)); } } static eventToStringForLog(event) { var _a, _b; const eventToLog = structuredClone(event); if ((_a = eventToLog === null || eventToLog === void 0 ? void 0 : eventToLog.authorization) === null || _a === void 0 ? void 0 : _a.raw) { eventToLog.authorization.raw = RunHandlerAsFilter.redact(eventToLog.authorization.raw); } if ((_b = eventToLog === null || eventToLog === void 0 ? void 0 : eventToLog.headers) === null || _b === void 0 ? void 0 : _b.authorization) { eventToLog.headers.authorization = RunHandlerAsFilter.redact(eventToLog.headers.authorization); } return JSON.stringify(eventToLog); } static redact(input) { const rval = input ? string_ratchet_1.StringRatchet.obscure(input, 1, 1) : input; return rval; } } exports.RunHandlerAsFilter = RunHandlerAsFilter; //# sourceMappingURL=run-handler-as-filter.js.map