@bitblit/ratchet-epsilon-common
Version:
Tiny adapter to simplify building API gateway Lambda APIS
66 lines • 2.21 kB
JavaScript
import { Logger } from '@bitblit/ratchet-common/logger/logger';
import { EpsilonConstants } from './epsilon-constants.js';
import { InterApiUtil } from './inter-api/inter-api-util.js';
import { PublishCommand } from '@aws-sdk/client-sns';
export class InterApiManager {
_aws;
_sns;
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;
}
async fireInterApiEventByParts(type, data) {
const entry = this.createEntry(type, data);
const rval = await this.fireInterApiEvent(entry);
return rval;
}
async fireInterApiEvent(entry) {
let rval = null;
if (this.config.localMode) {
Logger.info('Fire inter-api event ignored because running locally (was %j)', entry);
rval = 'INTER-API-IGNORED';
}
else {
try {
Logger.info('Firing inter-api event (remote) : %j ', entry);
const toWrite = {
type: EpsilonConstants.INTER_API_SNS_EVENT,
interApiEvent: InterApiUtil.addTraceToInterApiEntry(entry),
};
const msg = JSON.stringify(toWrite);
const snsId = await this.writeMessageToSnsTopic(msg);
Logger.debug('Inter-api Wrote message : %s to SNS : %s', rval, msg, snsId);
}
catch (err) {
Logger.error('Failed to fireImmediateProcessRequest : %s', err, err);
}
}
return rval;
}
async writeMessageToSnsTopic(message) {
let rval = null;
const params = {
Message: message,
TopicArn: this._aws.snsArn,
};
Logger.debug('Writing message to SNS topic : j', params);
const result = await this.sns.send(new PublishCommand(params));
rval = result.MessageId;
return rval;
}
}
//# sourceMappingURL=inter-api-manager.js.map