@vara/custom-logic-sdk
Version:
Server Side JavaScript SDK for Custom Business Logic
116 lines (91 loc) • 3.74 kB
JavaScript
/**
* Created by stevenchin on 2/6/17.
*/
const _ = require('lodash');
const envConfig = require('../../config/environment/env-config');
const fnRegistry = require('../services/fn-registry');
const { ROUTE_PATTERNS, CUSTOM_LOGIC } = require('../constants/constants');
const { TYPES: CL_FUNCTION_TYPES } = CUSTOM_LOGIC.FUNCTIONS;
const { VERSIONS: CL_PROTOCOL_VERSIONS } = CUSTOM_LOGIC.PROTOCOL;
const { TYPES: CL_ACTION_TYPES } = CUSTOM_LOGIC.ACTIONS;
function updateStandaloneConfigs(clFunction, configToUpdate) {
const configs = {
executionMode: clFunction.executionMode,
url: `${envConfig.TX_CL_BASE_URL}${ROUTE_PATTERNS.CL_FUNCTION_PATH_PREFIX}/${clFunction.name}`,
};
if (clFunction.supportAllMethods) {
configs.supportAllMethods = clFunction.supportAllMethods;
} else {
configs.httpMethods = clFunction.httpMethods;
}
configToUpdate[clFunction.name] = configs;
}
function updatePostHookConfigs(clFunction, configToUpdate) {
const { modelName, modelMethod } = clFunction;
const configs = {
executionMode: clFunction.executionMode,
url: `${envConfig.TX_CL_BASE_URL}${ROUTE_PATTERNS.CL_FUNCTION_PATH_PREFIX}/${clFunction.name}`,
};
configToUpdate[modelName] = configToUpdate[clFunction.modelName] || {};
configToUpdate[modelName][modelMethod] = configToUpdate[modelName][modelMethod] || [];
configToUpdate[modelName][modelMethod].push(configs);
}
function updatePreHookConfigs(clFunction, configToUpdate) {
const { urlPath, url, httpMethods } = clFunction;
const config = {
resource: {
url: urlPath || url, // support both right now
httpMethods,
},
executionMode: clFunction.executionMode,
url: `${envConfig.TX_CL_BASE_URL}${ROUTE_PATTERNS.CL_FUNCTION_PATH_PREFIX}/${clFunction.name}`,
};
configToUpdate.push(config);
}
function updateClActionConfigs(clFunction, configToUpdate) {
const configs = {
url: `${envConfig.TX_CL_BASE_URL}${ROUTE_PATTERNS.CL_ACTION_PATH_PREFIX}/${clFunction.name}`,
};
configToUpdate[clFunction.name] = configs;
}
// used to map to functions that generate configurations for each custom code type
const clConfigUpdaterMap = {
[CL_FUNCTION_TYPES.STANDALONE]: updateStandaloneConfigs,
[CL_FUNCTION_TYPES.POST_HOOK]: updatePostHookConfigs,
[CL_FUNCTION_TYPES.PRE_HOOK]: updatePreHookConfigs,
[CL_ACTION_TYPES.CL_ACTION]: updateClActionConfigs,
};
// used to map to the correct config property to update for each custom code type
const clRegistrationPropToUpdate = {
[CL_FUNCTION_TYPES.STANDALONE]: 'functions',
[CL_FUNCTION_TYPES.POST_HOOK]: 'postHooks',
[CL_FUNCTION_TYPES.PRE_HOOK]: 'preHooks',
[CL_ACTION_TYPES.CL_ACTION]: 'actions',
};
const generator = {};
/**
* Returns an object containing configurations for registering custom logic code with the Etx platform
* @param [registryFunctions] {Array} - list of registry functions to generation configurations for; defaults to all functions in registry.
*/
generator.generateRegistrationConfigForPlatform = function generateRegistrationConfigForPlatform(registryFunctions) {
let clFunctions = fnRegistry.getAll();
if (!_.isEmpty(registryFunctions)) {
clFunctions = registryFunctions;
}
const registrationConfig = {
customLogic: {
version: CL_PROTOCOL_VERSIONS.LATEST,
functions: {},
preHooks: [],
postHooks: {},
actions: {},
},
};
_.forEach(clFunctions, (clFunctionConfig) => {
const clFunction = clFunctionConfig.config;
const configToUpdate = registrationConfig.customLogic[clRegistrationPropToUpdate[clFunction.type]];
clConfigUpdaterMap[clFunction.type](clFunction, configToUpdate);
});
return registrationConfig;
};
module.exports = generator;