@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
76 lines • 3.71 kB
JavaScript
;
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.InterApiUtil = void 0;
const aws_1 = require("@bitblit/ratchet/aws");
const epsilon_constants_1 = require("../epsilon-constants");
const common_1 = require("@bitblit/ratchet/common");
const common_2 = require("@bitblit/ratchet/common");
const common_3 = require("@bitblit/ratchet/common");
const context_util_1 = require("../util/context-util");
class InterApiUtil {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static isInterApiSnsEvent(event) {
return !!InterApiUtil.extractEntryFromEvent(event);
}
static extractEntryFromEvent(evt) {
let rval = null;
if (!!evt) {
if (aws_1.LambdaEventDetector.isSingleSnsEvent(evt)) {
const cast = evt;
const msg = cast.Records[0].Sns.Message;
if (!!common_3.StringRatchet.trimToNull(msg)) {
const parsed = JSON.parse(msg);
if (!!parsed && parsed['type'] === epsilon_constants_1.EpsilonConstants.INTER_API_SNS_EVENT) {
rval = parsed['interApiEvent'];
}
}
}
}
return rval;
}
static processInterApiEvent(evt, cfg, mgr) {
return __awaiter(this, void 0, void 0, function* () {
let rval = [];
common_1.RequireRatchet.notNullOrUndefined(evt, 'InterApiEntry');
common_1.RequireRatchet.notNullOrUndefined(mgr, 'BackgroundManager');
const interApiEntry = InterApiUtil.extractEntryFromEvent(evt);
context_util_1.ContextUtil.setOverrideTraceFromInterApiEntry(interApiEntry);
common_2.Logger.info('Processing inter-api event : %j', evt);
const backgroundEntries = [];
cfg.processMappings.forEach((map) => {
if (!map.disabled && interApiEntry.source.match(map.sourceRegex) && interApiEntry.type.match(map.typeRegex)) {
map.backgroundProcessTypes.forEach((taskName) => {
const entry = mgr.createEntry(taskName, interApiEntry.data);
backgroundEntries.push(entry);
});
}
});
if (backgroundEntries.length > 0) {
common_2.Logger.info('Adding %d entries to queue', backgroundEntries.length);
rval = yield mgr.addEntriesToQueue(backgroundEntries, true);
}
else {
common_2.Logger.info('No entries mapped for this event');
}
return rval;
});
}
static addTraceToInterApiEntry(ent) {
if (ent) {
ent.traceId = ent.traceId || context_util_1.ContextUtil.currentTraceId();
ent.traceDepth = ent.traceDepth || context_util_1.ContextUtil.currentTraceDepth() + 1;
}
return ent;
}
}
exports.InterApiUtil = InterApiUtil;
//# sourceMappingURL=inter-api-util.js.map