@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
152 lines • 6.55 kB
JavaScript
"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.ComputedFieldPlugin = void 0;
const BasePlugin_1 = require("../common/BasePlugin");
const ion = __importStar(require("ion-js"));
class ComputedFieldPlugin extends BasePlugin_1.BasePlugin {
constructor() {
super();
this.initUpdateRoute();
this.initUpdateBatchRoute();
this.initBuildResultRoute();
}
fetchComputedField(computedFieldId) {
return super
.requestGatewayHelper('GET', `${this.outboundPlatformUrl}/v1/computed_fields/${computedFieldId}`)
.then((res) => {
this.logger.debug(`Fetched computed field: ${computedFieldId}`, { res });
return res.data;
});
}
// This is a default provided implementation
instanceContextBuilder(computedFieldId) {
return this.fetchComputedField(computedFieldId).then((computedField) => {
return {
computedField: computedField,
};
});
}
getInstanceContext(computedFieldId) {
if (!this.pluginCache.get(computedFieldId)) {
void this.pluginCache.put(computedFieldId, this.instanceContextBuilder(computedFieldId).catch((error) => {
this.logger.error('Error while caching instance context', error);
this.pluginCache.del(computedFieldId);
throw error;
}), this.getInstanceContextCacheExpiration());
}
return this.pluginCache.get(computedFieldId);
}
getUpdateMethod(state, update, instanceContext) {
switch (update.data_type) {
case 'USER_ACTIVITY':
return this.onUpdateActivity(state, update.data, instanceContext);
case 'USER_PROFILE':
return this.onUpdateUserProfile(state, update.data, update.operation, instanceContext);
case 'COMPUTED_FIELD':
return this.onUpdateComputedField(state, update.data, instanceContext);
default:
return state;
}
}
onUpdateBatch(state, updates, instanceContext) {
return updates.reduce((acc, curr) => {
return this.getUpdateMethod(acc, curr, instanceContext);
}, state);
}
formatResponse(req, data) {
return req.headers['accept'] === 'application/ion'
? Buffer.from(ion.dumpBinary(ion.load(JSON.stringify(data))))
: JSON.stringify(data);
}
formatRequestData(req) {
return req.headers['content-type'] === 'application/ion'
? JSON.parse(JSON.stringify(ion.load(req.body)))
: req.body;
}
initUpdateRoute() {
this.app.post('/v1/computed_field/update/single', async (req, res) => {
try {
const body = this.formatRequestData(req);
const instanceContext = await this.getInstanceContext(body.computed_field_id);
const updatedState = this.getUpdateMethod(body.state, body.update, instanceContext);
const pluginResponse = {
status: 'ok',
data: {
state: updatedState,
},
};
const response = this.formatResponse(req, pluginResponse);
res.status(200).send(response);
}
catch (error) {
this.logger.error('Something bad happened on single update route', error);
return res.status(500).send({ status: 'error', message: `${error.message}` });
}
});
}
initUpdateBatchRoute() {
this.app.post('/v1/computed_field/update/batch', async (req, res) => {
try {
const body = this.formatRequestData(req);
const instanceContext = await this.getInstanceContext(body.computed_field_id);
const updatedState = this.onUpdateBatch(body.state, body.updates, instanceContext);
const pluginResponse = {
status: 'ok',
data: {
state: updatedState,
},
};
res.status(200).send(this.formatResponse(req, pluginResponse));
}
catch (error) {
this.logger.error('Something bad happened on single update route', error);
return res.status(500).send({ status: 'error', message: `${error.message}` });
}
});
}
initBuildResultRoute() {
this.app.post('/v1/computed_field/build_result', async (req, res) => {
try {
const body = this.formatRequestData(req);
const instanceContext = await this.getInstanceContext(body.computed_field_id);
const buildResult = this.buildResult(body.state, instanceContext);
const pluginResponse = {
status: 'ok',
data: {
result: buildResult,
},
};
res.status(200).send(this.formatResponse(req, pluginResponse));
}
catch (error) {
this.logger.error('Something bad happened on single update route', error);
return res.status(500).send({ status: 'error', message: `${error.message}` });
}
});
}
}
exports.ComputedFieldPlugin = ComputedFieldPlugin;
//# sourceMappingURL=ComputedFieldBasePlugin.js.map