UNPKG

@bitblit/ratchet-epsilon-common

Version:

Tiny adapter to simplify building API gateway Lambda APIS

79 lines 3.9 kB
import { Logger } from '@bitblit/ratchet-common/logger/logger'; import { PromiseRatchet } from '@bitblit/ratchet-common/lang/promise-ratchet'; import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet'; import { TimeoutToken } from '@bitblit/ratchet-common/lang/timeout-token'; import { RestfulApiHttpError } from '@bitblit/ratchet-common/network/restful-api-http-error'; import { RequestTimeoutError } from '../../http/error/request-timeout-error.js'; import { ResponseUtil } from '../../http/response-util.js'; import { NotFoundError } from '../../http/error/not-found-error.js'; import { NullReturnedObjectHandling } from '../../config/http/null-returned-object-handling.js'; export class RunHandlerAsFilter { static async runHandler(fCtx, rm) { const handler = RunHandlerAsFilter.findHandler(rm, fCtx.event, fCtx.context); Logger.debug('Processing event with epsilon: %j', RunHandlerAsFilter.eventToStringForLog(fCtx.event)); let tmp = await handler; if (TimeoutToken.isTimeoutToken(tmp)) { tmp.writeToLog(); throw new RequestTimeoutError('Timed out'); } Logger.debug('Initial return value : %j', tmp); tmp = RunHandlerAsFilter.applyNullReturnedObjectHandling(tmp, rm.mapping.metaProcessingConfig.nullReturnedObjectHandling); fCtx.rawResult = tmp; fCtx.result = ResponseUtil.coerceToProxyResult(tmp); return true; } static applyNullReturnedObjectHandling(result, handling) { let rval = result; if (result === null || result === undefined) { if (handling === NullReturnedObjectHandling.Error) { Logger.error('Null object returned and Error specified, throwing 500'); throw new RestfulApiHttpError('Null object').withHttpStatusCode(500); } else if (handling === NullReturnedObjectHandling.Return404NotFoundResponse) { throw new NotFoundError('Resource not found'); } else if (handling === NullReturnedObjectHandling.ConvertToEmptyString) { Logger.warn('Null object returned from handler and convert not specified, converting to empty string'); rval = ''; } else { throw new RestfulApiHttpError('Cant happen - failed enum check').withHttpStatusCode(500); } } return rval; } static async findHandler(rm, event, context, add404OnMissing = true) { let rval = null; if (rm) { event.pathParameters = Object.assign({}, event.pathParameters, rm.parsed); rval = 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 NotFoundError('No such endpoint'); } return rval; } static addRunHandlerAsFilterToList(filters, rm) { if (filters) { filters.push((fCtx) => RunHandlerAsFilter.runHandler(fCtx, rm)); } } static eventToStringForLog(event) { const eventToLog = structuredClone(event); if (eventToLog?.authorization?.raw) { eventToLog.authorization.raw = RunHandlerAsFilter.redact(eventToLog.authorization.raw); } if (eventToLog?.headers?.authorization) { eventToLog.headers.authorization = RunHandlerAsFilter.redact(eventToLog.headers.authorization); } return JSON.stringify(eventToLog); } static redact(input) { const rval = input ? StringRatchet.obscure(input, 1, 1) : input; return rval; } } //# sourceMappingURL=run-handler-as-filter.js.map