UNPKG

@enter-at/lambda-handlers

Version:

An opinionated Typescript package that facilitates specifying AWS Lambda handlers including input validation, error handling and response formatting.

73 lines (72 loc) 2.58 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseHandler = void 0; const inputFormat = __importStar(require("../format/InputFormat")); const outputFormat = __importStar(require("../format/OutputFormat")); const index_1 = require("../index"); class BaseHandler { inputFormat; outputFormat; constructor(args) { this.inputFormat = args?.inputFormat ?? inputFormat.json; this.outputFormat = args?.outputFormat ?? outputFormat.json; if (args?.logger) { index_1.config.logger = args.logger; } } decorator(_target, _propertyName, propertyDescriptor) { propertyDescriptor.value = this.wrapper(propertyDescriptor.value); return propertyDescriptor; } wrapper(method) { const handler = this; return async function fn(event, context) { try { const instance = this; return handler.after(await method.apply(instance, handler.before(event, context))); } catch (err) { return handler.onException(err); } }; } before(event, context) { return [this.formatInput(event), context]; } after(output) { return this.formatOutput(output); } onException(exception) { throw exception; } formatInput(input) { return this.inputFormat.apply(input); } formatOutput(output) { return this.outputFormat.apply(output); } } exports.BaseHandler = BaseHandler;