UNPKG

@contentstack/cli-variants

Version:

Variants plugin

349 lines (348 loc) 23.6 kB
"use strict"; 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PersonalizationAdapter = void 0; const adapter_helper_1 = require("./adapter-helper"); const cli_utilities_1 = require("@contentstack/cli-utilities"); const error_helper_1 = require("./error-helper"); class PersonalizationAdapter extends adapter_helper_1.AdapterHelper { constructor(options) { var _a; super(options); cli_utilities_1.log.debug('Personalization adapter initialized.', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); } init() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j; cli_utilities_1.log.debug('Initializing personalization adapter...', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); yield cli_utilities_1.authenticationHandler.getAuthDetails(); const token = cli_utilities_1.authenticationHandler.accessToken; cli_utilities_1.log.debug(`Authentication type: ${cli_utilities_1.authenticationHandler.isOauthEnabled ? 'OAuth' : 'Token'}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); if (cli_utilities_1.authenticationHandler.isOauthEnabled) { cli_utilities_1.log.debug('Setting OAuth authorization header...', (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); this.apiClient.headers({ authorization: token }); if (this.adapterConfig.cmaConfig) { cli_utilities_1.log.debug('Setting OAuth authorization header for CMA client...', (_d = this.exportConfig) === null || _d === void 0 ? void 0 : _d.context); (_e = this.cmaAPIClient) === null || _e === void 0 ? void 0 : _e.headers({ authorization: token }); } } else { cli_utilities_1.log.debug('Setting authtoken header...', (_f = this.exportConfig) === null || _f === void 0 ? void 0 : _f.context); this.apiClient.headers({ authtoken: token }); if (this.adapterConfig.cmaConfig) { cli_utilities_1.log.debug('Setting authtoken header for CMA client...', (_g = this.exportConfig) === null || _g === void 0 ? void 0 : _g.context); (_h = this.cmaAPIClient) === null || _h === void 0 ? void 0 : _h.headers({ authtoken: token }); } } cli_utilities_1.log.debug('Personalization adapter initialization completed.', (_j = this.exportConfig) === null || _j === void 0 ? void 0 : _j.context); }); } projects(options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; cli_utilities_1.log.debug(`Fetching projects for stack API key: ${options.connectedStackApiKey}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); yield this.init(); const getProjectEndPoint = `/projects?connectedStackApiKey=${options.connectedStackApiKey}`; cli_utilities_1.log.debug(`Making API call to: ${getProjectEndPoint}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); const data = yield this.apiClient.get(getProjectEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Fetched ${(result === null || result === void 0 ? void 0 : result.length) || 0} projects`, (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); return result; }); } /** * This TypeScript function creates a project by making an asynchronous API call to retrieve project * data. * @param {CreateProjectInput} input - The `input` parameter in the `createProject` function likely * represents the data needed to create a new project. It could include details such as the project * name, description, owner, deadline, or any other relevant information required to set up a new * project. * @returns The `createProject` function is returning a Promise that resolves to either a * `ProjectStruct` object or `void`. */ createProject(project) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating project: ${project.name}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.post('/projects', project); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Project created successfully: ${result === null || result === void 0 ? void 0 : result.uid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * The function `createAttribute` asynchronously retrieves attribute data from an API endpoint. * @param {CreateAttributeInput} input - The `input` parameter in the `createAttribute` function is * of type `CreateAttributeInput`. This parameter likely contains the necessary data or information * needed to create a new attribute. * @returns The `createAttribute` function is returning the data obtained from a GET request to the * `/attributes` endpoint using the `apiClient` with the input provided. The data returned is of type * `ProjectStruct`. */ createAttribute(attribute) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating attribute: ${attribute.name}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.post('/attributes', attribute); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Attribute created successfully: ${(result === null || result === void 0 ? void 0 : result.name) || (result === null || result === void 0 ? void 0 : result.uid)}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } getExperiences() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug('Fetching experiences from Personalize API...', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const getExperiencesEndPoint = `/experiences`; const data = yield this.apiClient.get(getExperiencesEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Fetched ${(result === null || result === void 0 ? void 0 : result.length) || 0} experiences`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } getExperience(experienceUid) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; cli_utilities_1.log.debug(`Fetching experience: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const getExperiencesEndPoint = `/experiences/${experienceUid}`; if ((_c = (_b = this.apiClient).requestConfig) === null || _c === void 0 ? void 0 : _c.call(_b).data) { (_e = (_d = this.apiClient).requestConfig) === null || _e === void 0 ? true : delete _e.call(_d).data; // explicitly prevent any accidental body } const data = yield this.apiClient.get(getExperiencesEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Experience fetched successfully: ${result === null || result === void 0 ? void 0 : result.uid}`, (_f = this.exportConfig) === null || _f === void 0 ? void 0 : _f.context); return result; }); } getExperienceVersions(experienceUid) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; cli_utilities_1.log.debug(`Fetching versions for experience: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const getExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`; if ((_c = (_b = this.apiClient).requestConfig) === null || _c === void 0 ? void 0 : _c.call(_b).data) { (_e = (_d = this.apiClient).requestConfig) === null || _e === void 0 ? true : delete _e.call(_d).data; // explicitly prevent any accidental body } const data = yield this.apiClient.get(getExperiencesVersionsEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Experience versions fetched successfully for: ${experienceUid}`, (_f = this.exportConfig) === null || _f === void 0 ? void 0 : _f.context); return result; }); } createExperienceVersion(experienceUid, input) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating experience version for: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const createExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`; const data = yield this.apiClient.post(createExperiencesVersionsEndPoint, input); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Experience version created successfully for: ${experienceUid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } updateExperienceVersion(experienceUid, versionId, input) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; cli_utilities_1.log.debug(`Updating experience version: ${versionId} for experience: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); // loop through input and remove shortId from variant if (input === null || input === void 0 ? void 0 : input.variants) { input.variants = input.variants.map((_a) => { var { shortUid } = _a, rest = __rest(_a, ["shortUid"]); return rest; }); cli_utilities_1.log.debug(`Processed ${input.variants.length} variants for update`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); } const updateExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions/${versionId}`; const data = yield this.apiClient.put(updateExperiencesVersionsEndPoint, input); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Experience version updated successfully: ${versionId}`, (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); return result; }); } getVariantGroup(input) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; cli_utilities_1.log.debug(`Fetching variant group for experience: ${input.experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); if (this.cmaAPIClient) { const getVariantGroupEndPoint = `/variant_groups`; const data = yield this.cmaAPIClient .queryParams({ experience_uid: input.experienceUid }) .get(getVariantGroupEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Variant group fetched successfully for experience: ${input === null || input === void 0 ? void 0 : input.experienceUid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; } else { cli_utilities_1.log.debug('CMA API client not available for variant group fetch.', (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); } }); } updateVariantGroup(input) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; cli_utilities_1.log.debug(`Updating variant group: ${input.uid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); if (this.cmaAPIClient) { const updateVariantGroupEndPoint = `/variant_groups/${input.uid}`; const data = yield this.cmaAPIClient.put(updateVariantGroupEndPoint, input); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Variant group updated successfully: ${input === null || input === void 0 ? void 0 : input.uid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; } else { cli_utilities_1.log.debug('CMA API client not available for variant group update.', (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); } }); } getEvents() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug('Fetching events from Personalize API...', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.get('/events'); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Fetched ${(result === null || result === void 0 ? void 0 : result.length) || 0} events`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } createEvents(event) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating event: ${event.key}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.post('/events', event); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Event created successfully: ${result === null || result === void 0 ? void 0 : result.uid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } getAudiences() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug('Fetching audiences from Personalize API...', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.get('/audiences'); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.debug(`Fetched ${(result === null || result === void 0 ? void 0 : result.length) || 0} audiences`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } getAttributes() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug('Fetching attributes from Personalize API...', (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.get('/attributes'); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Fetched ${(result === null || result === void 0 ? void 0 : result.length) || 0} attributes`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * @param {CreateAudienceInput} audience - The `audience` parameter in the `createAudience` function is * of type `CreateAudienceInput`. This parameter likely contains the necessary data or information * needed to create a new audience. * @returns The `createAudience` function is returning the data obtained from a GET request to the * `/audiences` endpoint using the `apiClient` with the input provided. The data returned is of type * `AudienceStruct`. */ createAudience(audience) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating audience: ${audience.name}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.post('/audiences', audience); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Audience created successfully: ${(result === null || result === void 0 ? void 0 : result.name) || (result === null || result === void 0 ? void 0 : result.uid)}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * @param {CreateExperienceInput} experience - The `experience` parameter in the `createExperience` function is * of type `CreateExperienceInput`. This parameter likely contains the necessary data or information * needed to create a new audience. * @returns The `createExperience` function is returning the data obtained from a GET request to the * `/experiences` endpoint using the `apiClient` with the input provided. The data returned is of type * `ExperienceStruct`. */ createExperience(experience) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Creating experience: ${experience.name}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const data = yield this.apiClient.post('/experiences', experience); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Experience created successfully: ${(result === null || result === void 0 ? void 0 : result.name) || (result === null || result === void 0 ? void 0 : result.uid)}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * @param {UpdateExperienceInput} experience - The `experience` parameter in the `updateCTsInExperience` function is * of type `UpdateExperienceInput`. This parameter likely contains the necessary data or information * needed to attach CT in new created experience. */ updateCTsInExperience(experience, experienceUid) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Updating content types in experience: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const updateCTInExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`; const data = yield this.apiClient.post(updateCTInExpEndPoint, experience); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Content types updated successfully in experience: ${experienceUid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * @param {UpdateExperienceInput} experienceUid - The `experienceUid` parameter in the `getCTsFromExperience` function is * of type `string`. This parameter likely contains the necessary data or information * needed to fetch CT details related to experience. */ getCTsFromExperience(experienceUid) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; cli_utilities_1.log.debug(`Fetching content types from experience: ${experienceUid}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); const getCTFromExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`; const data = yield this.apiClient.get(getCTFromExpEndPoint); const result = (yield this.handleVariantAPIRes(data)); cli_utilities_1.log.info(`Content types fetched successfully from experience: ${experienceUid}`, (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return result; }); } /** * Handles the API response for variant requests. * @param res - The API response object. * @returns The variant API response data. * @throws If the API response status is not within the success range, an error message is thrown. */ handleVariantAPIRes(res) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const { status, data } = res; cli_utilities_1.log.debug(`API response status: ${status}`, (_a = this.exportConfig) === null || _a === void 0 ? void 0 : _a.context); if (status >= 200 && status < 300) { cli_utilities_1.log.debug('API request successful.', (_b = this.exportConfig) === null || _b === void 0 ? void 0 : _b.context); return data; } cli_utilities_1.log.debug(`API request failed with status: ${status}`, (_c = this.exportConfig) === null || _c === void 0 ? void 0 : _c.context); // Refresh the access token if it has expired yield cli_utilities_1.authenticationHandler.refreshAccessToken(res); const errorMsg = (data === null || data === void 0 ? void 0 : data.errors) ? (0, error_helper_1.formatErrors)(data.errors) : (data === null || data === void 0 ? void 0 : data.error) || (data === null || data === void 0 ? void 0 : data.error_message) || (data === null || data === void 0 ? void 0 : data.message) || data; cli_utilities_1.log.debug(`API error: ${errorMsg}`, (_d = this.exportConfig) === null || _d === void 0 ? void 0 : _d.context); throw errorMsg; }); } } exports.PersonalizationAdapter = PersonalizationAdapter;