@constructor-io/constructorio-connect-cli
Version:
CLI tool to enable users to interface with the Constructor Connect Ecosystem
104 lines (103 loc) • 3.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TYPE_OPTIONS = void 0;
exports.executeTemplate = executeTemplate;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const config_1 = require("../customer/config");
const template_source_code_1 = require("../customer/template-source-code");
const file_loaders_1 = require("../helpers/file-loaders");
const print_warnings_1 = require("../helpers/print-warnings");
const get_connections_request_1 = require("../http/get-connections-request");
const send_template_execute_request_1 = require("../http/send-template-execute-request");
exports.TYPE_OPTIONS = [
"item",
"variation",
"item_group",
"mapping",
"grouping",
];
/**
* This will execute a template with the given type, name, fixture, and external.
* It returns an object with the result of the execution, or throws if there is an error.
*
* The template will be executed against the first connection found, or you can override
* this behavior and select a specific connection by providing a connectionId.
*
* If no connection is found, an error will be thrown.
*
* @param options @type ExecuteTemplateArgs
* @returns Promise<Record<string, any>>
*/
async function executeTemplate(options) {
const { name, fixture, external } = initializeOptions(options);
const connection = await getConnection(options);
const config = await (0, config_1.getRepositoryConfigFile)();
const result = await (0, send_template_execute_request_1.sendTemplateExecuteRequest)({
template: (0, file_loaders_1.getJSONataTemplate)(name).toString(),
helpers: (await (0, template_source_code_1.getHelpersSourceCode)(config.helpers)) ?? "",
connectionId: connection.id,
templateType: options.type,
data: fixture,
showLogs: false,
external,
});
if (!result.success) {
throw new Error(`Error executing template: ${result.error.message}`);
}
if (result.warnings.length > 0) {
(0, print_warnings_1.printExecutionWarnings)({ warnings: result.warnings });
}
return result.data;
}
function initializeOptions(options) {
if (!options) {
throw new Error("No options provided");
}
if (!options.type) {
throw new Error("No type provided");
}
if (!exports.TYPE_OPTIONS.includes(options.type)) {
throw new Error(`Invalid type: ${options.type}. Expected to receive one of: ${exports.TYPE_OPTIONS.join(", ")}`);
}
if (!options.name) {
throw new Error("No name provided");
}
const platformFileDelimiter = os_1.default.platform() === "win32" ? "\\" : "/";
if (!options.name.includes(platformFileDelimiter)) {
options.name = path_1.default.join(options.type, options.name);
}
if (!options.fixture) {
options.fixture = {};
}
if (!options.external) {
options.external = {};
}
return options;
}
async function getConnection(options) {
/**
* Note: we're loading connections from `global.__connections` first to avoid
* making an unnecessary request to the API during tests.
*
* When running the entire test suite on initialized repos, we want to load
* all connections only once, and then use them across all tests.
*/
const connections = "__connections" in global
? global.__connections
: await (0, get_connections_request_1.getConnections)({ showLogs: false });
let connection;
if (options.connectionId) {
connection = connections.find((conn) => conn.id === options.connectionId);
}
else {
connection = connections[0];
}
if (!connection) {
throw new Error(`Connection with id ${options.connectionId} not found`);
}
return connection;
}