@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
87 lines • 3.57 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.InterApiManager = void 0;
const common_1 = require("@bitblit/ratchet/common");
const epsilon_constants_1 = require("./epsilon-constants");
const inter_api_util_1 = require("./inter-api/inter-api-util");
const client_sns_1 = require("@aws-sdk/client-sns");
/**
* Handles all submission of events to the inter-api SNS topic (if any)
*/
class InterApiManager {
constructor(_aws, _sns) {
this._aws = _aws;
this._sns = _sns;
}
get config() {
return this._aws;
}
get sns() {
return this._sns;
}
createEntry(type, data) {
const rval = {
source: this._aws.source,
type: type,
data: data,
};
return rval;
}
fireInterApiEventByParts(type, data) {
return __awaiter(this, void 0, void 0, function* () {
const entry = this.createEntry(type, data);
const rval = yield this.fireInterApiEvent(entry);
return rval;
});
}
fireInterApiEvent(entry) {
return __awaiter(this, void 0, void 0, function* () {
let rval = null;
if (this.config.localMode) {
common_1.Logger.info('Fire inter-api event ignored because running locally (was %j)', entry);
rval = 'INTER-API-IGNORED';
}
else {
try {
// Guard against bad entries up front
common_1.Logger.info('Firing inter-api event (remote) : %j ', entry);
const toWrite = {
type: epsilon_constants_1.EpsilonConstants.INTER_API_SNS_EVENT,
interApiEvent: inter_api_util_1.InterApiUtil.addTraceToInterApiEntry(entry),
};
const msg = JSON.stringify(toWrite);
const snsId = yield this.writeMessageToSnsTopic(msg);
common_1.Logger.debug('Inter-api Wrote message : %s to SNS : %s', rval, msg, snsId);
}
catch (err) {
common_1.Logger.error('Failed to fireImmediateProcessRequest : %s', err, err);
}
}
return rval;
});
}
writeMessageToSnsTopic(message) {
return __awaiter(this, void 0, void 0, function* () {
let rval = null;
const params = {
Message: message,
TopicArn: this._aws.snsArn,
};
common_1.Logger.debug('Writing message to SNS topic : j', params);
const result = yield this.sns.send(new client_sns_1.PublishCommand(params));
rval = result.MessageId;
return rval;
});
}
}
exports.InterApiManager = InterApiManager;
//# sourceMappingURL=inter-api-manager.js.map