@enter-at/lambda-handlers
Version:
An opinionated Typescript package that facilitates specifying AWS Lambda handlers including input validation, error handling and response formatting.
64 lines (63 loc) • 2.42 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.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"));
class BaseHandler {
constructor(args) {
var _a, _b;
this.inputFormat = (_a = args === null || args === void 0 ? void 0 : args.inputFormat) !== null && _a !== void 0 ? _a : inputFormat.json;
this.outputFormat = (_b = args === null || args === void 0 ? void 0 : args.outputFormat) !== null && _b !== void 0 ? _b : outputFormat.json;
}
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;