@contentstack/cli-variants 0.0.1-alpha → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/export/attributes.d.ts +2 -2
- package/lib/export/attributes.js +5 -4
- package/lib/export/audiences.d.ts +2 -2
- package/lib/export/audiences.js +5 -4
- package/lib/export/events.d.ts +2 -2
- package/lib/export/events.js +5 -4
- package/lib/export/experiences.d.ts +2 -2
- package/lib/export/experiences.js +20 -5
- package/lib/export/projects.d.ts +2 -2
- package/lib/export/projects.js +9 -6
- package/lib/export/variant-entries.js +1 -1
- package/lib/import/attribute.d.ts +1 -1
- package/lib/import/attribute.js +21 -10
- package/lib/import/audiences.d.ts +2 -2
- package/lib/import/audiences.js +21 -14
- package/lib/import/events.d.ts +1 -1
- package/lib/import/events.js +15 -8
- package/lib/import/experiences.d.ts +12 -5
- package/lib/import/experiences.js +86 -21
- package/lib/import/project.js +12 -11
- package/lib/import/variant-entries.d.ts +1 -1
- package/lib/import/variant-entries.js +28 -22
- package/lib/messages/index.d.ts +1 -1
- package/lib/messages/index.js +3 -2
- package/lib/types/export-config.d.ts +3 -3
- package/lib/types/import-config.d.ts +1 -1
- package/lib/types/personalization-api-adapter.d.ts +14 -1
- package/lib/types/variant-api-adapter.d.ts +3 -2
- package/lib/types/variant-entry.d.ts +2 -3
- package/lib/utils/attributes-helper.js +2 -2
- package/lib/utils/audiences-helper.js +14 -3
- package/lib/utils/error-helper.js +6 -6
- package/lib/utils/logger.js +5 -4
- package/lib/utils/personalization-api-adapter.d.ts +6 -2
- package/lib/utils/personalization-api-adapter.js +90 -23
- package/lib/utils/variant-api-adapter.d.ts +5 -4
- package/lib/utils/variant-api-adapter.js +29 -10
- package/package.json +2 -2
- package/src/export/attributes.ts +11 -7
- package/src/export/audiences.ts +7 -6
- package/src/export/events.ts +7 -6
- package/src/export/experiences.ts +24 -7
- package/src/export/projects.ts +11 -8
- package/src/export/variant-entries.ts +1 -2
- package/src/import/attribute.ts +31 -13
- package/src/import/audiences.ts +37 -19
- package/src/import/events.ts +25 -11
- package/src/import/experiences.ts +120 -30
- package/src/import/project.ts +13 -13
- package/src/import/variant-entries.ts +70 -37
- package/src/messages/index.ts +3 -2
- package/src/types/export-config.ts +3 -3
- package/src/types/import-config.ts +1 -1
- package/src/types/personalization-api-adapter.ts +14 -1
- package/src/types/variant-api-adapter.ts +3 -1
- package/src/types/variant-entry.ts +2 -3
- package/src/utils/attributes-helper.ts +2 -2
- package/src/utils/audiences-helper.ts +12 -2
- package/src/utils/error-helper.ts +6 -6
- package/src/utils/logger.ts +5 -4
- package/src/utils/personalization-api-adapter.ts +71 -18
- package/src/utils/variant-api-adapter.ts +21 -7
package/lib/utils/logger.js
CHANGED
|
@@ -74,9 +74,9 @@ let logger;
|
|
|
74
74
|
let errorLogger;
|
|
75
75
|
let successTransport;
|
|
76
76
|
let errorTransport;
|
|
77
|
-
function init(_logPath) {
|
|
77
|
+
function init(_logPath, module) {
|
|
78
78
|
if (!logger || !errorLogger) {
|
|
79
|
-
const logsDir = path.resolve((0, cli_utilities_1.sanitizePath)(_logPath), 'logs',
|
|
79
|
+
const logsDir = path.resolve((0, cli_utilities_1.sanitizePath)(_logPath), 'logs', (0, cli_utilities_1.sanitizePath)(module));
|
|
80
80
|
// Create dir if doesn't already exist
|
|
81
81
|
mkdirp_1.default.sync(logsDir);
|
|
82
82
|
successTransport = {
|
|
@@ -145,12 +145,13 @@ function init(_logPath) {
|
|
|
145
145
|
const log = (config, message, type) => {
|
|
146
146
|
const logsPath = config.data;
|
|
147
147
|
// ignoring the type argument, as we are not using it to create a logfile anymore
|
|
148
|
+
const module = config['backupDir'] ? 'import' : 'export';
|
|
148
149
|
if (type !== 'error') {
|
|
149
150
|
// removed type argument from init method
|
|
150
|
-
init(logsPath).log(message);
|
|
151
|
+
init(logsPath, module).log(message);
|
|
151
152
|
}
|
|
152
153
|
else {
|
|
153
|
-
init(logsPath).error(message);
|
|
154
|
+
init(logsPath, module).error(message);
|
|
154
155
|
}
|
|
155
156
|
};
|
|
156
157
|
exports.log = log;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { AdapterHelper } from './adapter-helper';
|
|
2
2
|
import { HttpClient } from '@contentstack/cli-utilities';
|
|
3
|
-
import { ProjectStruct, Personalization, GetProjectsParams, CreateProjectInput, CreateAttributeInput, APIConfig, GetVariantGroupInput, EventStruct, AudienceStruct, AttributeStruct, CreateAudienceInput, CreateEventInput, CreateExperienceInput, ExperienceStruct, UpdateExperienceInput, CMSExperienceStruct, VariantAPIRes, APIResponse, VariantGroupStruct, VariantGroup } from '../types';
|
|
3
|
+
import { ProjectStruct, Personalization, GetProjectsParams, CreateProjectInput, CreateAttributeInput, APIConfig, GetVariantGroupInput, EventStruct, AudienceStruct, AttributeStruct, CreateAudienceInput, CreateEventInput, CreateExperienceInput, ExperienceStruct, UpdateExperienceInput, CMSExperienceStruct, VariantAPIRes, APIResponse, VariantGroupStruct, VariantGroup, CreateExperienceVersionInput } from '../types';
|
|
4
4
|
export declare class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> implements Personalization<T> {
|
|
5
5
|
constructor(options: APIConfig);
|
|
6
|
+
init(): Promise<void>;
|
|
6
7
|
projects(options: GetProjectsParams): Promise<ProjectStruct[]>;
|
|
7
8
|
/**
|
|
8
9
|
* This TypeScript function creates a project by making an asynchronous API call to retrieve project
|
|
@@ -27,6 +28,9 @@ export declare class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClie
|
|
|
27
28
|
createAttribute(attribute: CreateAttributeInput): Promise<AttributeStruct>;
|
|
28
29
|
getExperiences(): Promise<ExperienceStruct[]>;
|
|
29
30
|
getExperience(experienceUid: string): Promise<ExperienceStruct | void>;
|
|
31
|
+
getExperienceVersions(experienceUid: string): Promise<ExperienceStruct | void>;
|
|
32
|
+
createExperienceVersion(experienceUid: string, input: CreateExperienceVersionInput): Promise<ExperienceStruct | void>;
|
|
33
|
+
updateExperienceVersion(experienceUid: string, versionId: string, input: CreateExperienceVersionInput): Promise<ExperienceStruct | void>;
|
|
30
34
|
getVariantGroup(input: GetVariantGroupInput): Promise<VariantGroupStruct | void>;
|
|
31
35
|
updateVariantGroup(input: VariantGroup): Promise<VariantGroup | void>;
|
|
32
36
|
getEvents(): Promise<EventStruct[] | void>;
|
|
@@ -69,5 +73,5 @@ export declare class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClie
|
|
|
69
73
|
* @returns The variant API response data.
|
|
70
74
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
71
75
|
*/
|
|
72
|
-
handleVariantAPIRes(res: APIResponse): VariantAPIRes
|
|
76
|
+
handleVariantAPIRes(res: APIResponse): Promise<VariantAPIRes>;
|
|
73
77
|
}
|
|
@@ -8,19 +8,51 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
11
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
23
|
exports.PersonalizationAdapter = void 0;
|
|
13
24
|
const adapter_helper_1 = require("./adapter-helper");
|
|
25
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
14
26
|
const error_helper_1 = require("./error-helper");
|
|
15
27
|
class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
16
28
|
constructor(options) {
|
|
17
29
|
super(options);
|
|
18
30
|
}
|
|
31
|
+
init() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
yield cli_utilities_1.authenticationHandler.getAuthDetails();
|
|
35
|
+
const token = cli_utilities_1.authenticationHandler.accessToken;
|
|
36
|
+
if (cli_utilities_1.authenticationHandler.isOauthEnabled) {
|
|
37
|
+
this.apiClient.headers({ authorization: token });
|
|
38
|
+
if (this.adapterConfig.cmaConfig) {
|
|
39
|
+
(_a = this.cmaAPIClient) === null || _a === void 0 ? void 0 : _a.headers({ authorization: token });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.apiClient.headers({ authtoken: token });
|
|
44
|
+
if (this.adapterConfig.cmaConfig) {
|
|
45
|
+
(_b = this.cmaAPIClient) === null || _b === void 0 ? void 0 : _b.headers({ authtoken: token });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
19
50
|
projects(options) {
|
|
20
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
yield this.init();
|
|
21
53
|
const getProjectEndPoint = `/projects?connectedStackApiKey=${options.connectedStackApiKey}`;
|
|
22
54
|
const data = yield this.apiClient.get(getProjectEndPoint);
|
|
23
|
-
return this.handleVariantAPIRes(data);
|
|
55
|
+
return (yield this.handleVariantAPIRes(data));
|
|
24
56
|
});
|
|
25
57
|
}
|
|
26
58
|
/**
|
|
@@ -36,7 +68,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
36
68
|
createProject(project) {
|
|
37
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
70
|
const data = yield this.apiClient.post('/projects', project);
|
|
39
|
-
return this.handleVariantAPIRes(data);
|
|
71
|
+
return (yield this.handleVariantAPIRes(data));
|
|
40
72
|
});
|
|
41
73
|
}
|
|
42
74
|
/**
|
|
@@ -51,21 +83,49 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
51
83
|
createAttribute(attribute) {
|
|
52
84
|
return __awaiter(this, void 0, void 0, function* () {
|
|
53
85
|
const data = yield this.apiClient.post('/attributes', attribute);
|
|
54
|
-
return this.handleVariantAPIRes(data);
|
|
86
|
+
return (yield this.handleVariantAPIRes(data));
|
|
55
87
|
});
|
|
56
88
|
}
|
|
57
89
|
getExperiences() {
|
|
58
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
91
|
const getExperiencesEndPoint = `/experiences`;
|
|
60
92
|
const data = yield this.apiClient.get(getExperiencesEndPoint);
|
|
61
|
-
return this.handleVariantAPIRes(data);
|
|
93
|
+
return (yield this.handleVariantAPIRes(data));
|
|
62
94
|
});
|
|
63
95
|
}
|
|
64
96
|
getExperience(experienceUid) {
|
|
65
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
98
|
const getExperiencesEndPoint = `/experiences/${experienceUid}`;
|
|
67
99
|
const data = yield this.apiClient.get(getExperiencesEndPoint);
|
|
68
|
-
return this.handleVariantAPIRes(data);
|
|
100
|
+
return (yield this.handleVariantAPIRes(data));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
getExperienceVersions(experienceUid) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const getExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
106
|
+
const data = yield this.apiClient.get(getExperiencesVersionsEndPoint);
|
|
107
|
+
return (yield this.handleVariantAPIRes(data));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
createExperienceVersion(experienceUid, input) {
|
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
const createExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
113
|
+
const data = yield this.apiClient.post(createExperiencesVersionsEndPoint, input);
|
|
114
|
+
return (yield this.handleVariantAPIRes(data));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
updateExperienceVersion(experienceUid, versionId, input) {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
// loop through input and remove shortId from variant
|
|
120
|
+
if (input === null || input === void 0 ? void 0 : input.variants) {
|
|
121
|
+
input.variants = input.variants.map((_a) => {
|
|
122
|
+
var { shortUid } = _a, rest = __rest(_a, ["shortUid"]);
|
|
123
|
+
return rest;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const updateExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions/${versionId}`;
|
|
127
|
+
const data = yield this.apiClient.put(updateExperiencesVersionsEndPoint, input);
|
|
128
|
+
return (yield this.handleVariantAPIRes(data));
|
|
69
129
|
});
|
|
70
130
|
}
|
|
71
131
|
getVariantGroup(input) {
|
|
@@ -75,7 +135,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
75
135
|
const data = yield this.cmaAPIClient
|
|
76
136
|
.queryParams({ experience_uid: input.experienceUid })
|
|
77
137
|
.get(getVariantGroupEndPoint);
|
|
78
|
-
return this.handleVariantAPIRes(data);
|
|
138
|
+
return (yield this.handleVariantAPIRes(data));
|
|
79
139
|
}
|
|
80
140
|
});
|
|
81
141
|
}
|
|
@@ -84,32 +144,32 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
84
144
|
if (this.cmaAPIClient) {
|
|
85
145
|
const updateVariantGroupEndPoint = `/variant_groups/${input.uid}`;
|
|
86
146
|
const data = yield this.cmaAPIClient.put(updateVariantGroupEndPoint, input);
|
|
87
|
-
return this.handleVariantAPIRes(data);
|
|
147
|
+
return (yield this.handleVariantAPIRes(data));
|
|
88
148
|
}
|
|
89
149
|
});
|
|
90
150
|
}
|
|
91
151
|
getEvents() {
|
|
92
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
153
|
const data = yield this.apiClient.get('/events');
|
|
94
|
-
return this.handleVariantAPIRes(data);
|
|
154
|
+
return (yield this.handleVariantAPIRes(data));
|
|
95
155
|
});
|
|
96
156
|
}
|
|
97
157
|
createEvents(event) {
|
|
98
158
|
return __awaiter(this, void 0, void 0, function* () {
|
|
99
159
|
const data = yield this.apiClient.post('/events', event);
|
|
100
|
-
return this.handleVariantAPIRes(data);
|
|
160
|
+
return (yield this.handleVariantAPIRes(data));
|
|
101
161
|
});
|
|
102
162
|
}
|
|
103
163
|
getAudiences() {
|
|
104
164
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
165
|
const data = yield this.apiClient.get('/audiences');
|
|
106
|
-
return this.handleVariantAPIRes(data);
|
|
166
|
+
return (yield this.handleVariantAPIRes(data));
|
|
107
167
|
});
|
|
108
168
|
}
|
|
109
169
|
getAttributes() {
|
|
110
170
|
return __awaiter(this, void 0, void 0, function* () {
|
|
111
171
|
const data = yield this.apiClient.get('/attributes');
|
|
112
|
-
return this.handleVariantAPIRes(data);
|
|
172
|
+
return (yield this.handleVariantAPIRes(data));
|
|
113
173
|
});
|
|
114
174
|
}
|
|
115
175
|
/**
|
|
@@ -123,7 +183,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
123
183
|
createAudience(audience) {
|
|
124
184
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
185
|
const data = yield this.apiClient.post('/audiences', audience);
|
|
126
|
-
return this.handleVariantAPIRes(data);
|
|
186
|
+
return (yield this.handleVariantAPIRes(data));
|
|
127
187
|
});
|
|
128
188
|
}
|
|
129
189
|
/**
|
|
@@ -137,7 +197,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
137
197
|
createExperience(experience) {
|
|
138
198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
139
199
|
const data = yield this.apiClient.post('/experiences', experience);
|
|
140
|
-
return this.handleVariantAPIRes(data);
|
|
200
|
+
return (yield this.handleVariantAPIRes(data));
|
|
141
201
|
});
|
|
142
202
|
}
|
|
143
203
|
/**
|
|
@@ -149,7 +209,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
149
209
|
return __awaiter(this, void 0, void 0, function* () {
|
|
150
210
|
const updateCTInExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
151
211
|
const data = yield this.apiClient.post(updateCTInExpEndPoint, experience);
|
|
152
|
-
return this.handleVariantAPIRes(data);
|
|
212
|
+
return (yield this.handleVariantAPIRes(data));
|
|
153
213
|
});
|
|
154
214
|
}
|
|
155
215
|
/**
|
|
@@ -161,7 +221,7 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
161
221
|
return __awaiter(this, void 0, void 0, function* () {
|
|
162
222
|
const getCTFromExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
163
223
|
const data = yield this.apiClient.get(getCTFromExpEndPoint);
|
|
164
|
-
return this.handleVariantAPIRes(data);
|
|
224
|
+
return (yield this.handleVariantAPIRes(data));
|
|
165
225
|
});
|
|
166
226
|
}
|
|
167
227
|
/**
|
|
@@ -171,14 +231,21 @@ class PersonalizationAdapter extends adapter_helper_1.AdapterHelper {
|
|
|
171
231
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
172
232
|
*/
|
|
173
233
|
handleVariantAPIRes(res) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
234
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
235
|
+
const { status, data } = res;
|
|
236
|
+
if (status >= 200 && status < 300) {
|
|
237
|
+
return data;
|
|
238
|
+
}
|
|
239
|
+
// Refresh the access token if it has expired
|
|
240
|
+
yield cli_utilities_1.authenticationHandler.refreshAccessToken(res);
|
|
241
|
+
const errorMsg = (data === null || data === void 0 ? void 0 : data.errors)
|
|
242
|
+
? (0, error_helper_1.formatErrors)(data.errors)
|
|
243
|
+
: (data === null || data === void 0 ? void 0 : data.error) ||
|
|
244
|
+
(data === null || data === void 0 ? void 0 : data.error_message) ||
|
|
245
|
+
(data === null || data === void 0 ? void 0 : data.message) ||
|
|
246
|
+
'Something went wrong while processing variant entries request!';
|
|
247
|
+
throw errorMsg;
|
|
248
|
+
});
|
|
182
249
|
}
|
|
183
250
|
}
|
|
184
251
|
exports.PersonalizationAdapter = PersonalizationAdapter;
|
|
@@ -5,6 +5,7 @@ import { AdapterHelper } from './adapter-helper';
|
|
|
5
5
|
export declare class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> implements VariantInterface<C, HttpClient> {
|
|
6
6
|
baseURL: string;
|
|
7
7
|
constructor(config: APIConfig, options?: HttpClientOptions);
|
|
8
|
+
init(): Promise<void>;
|
|
8
9
|
variantEntry(options: VariantOptions): Promise<{
|
|
9
10
|
entry: {};
|
|
10
11
|
}>;
|
|
@@ -48,10 +49,10 @@ export declare class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> i
|
|
|
48
49
|
* @returns The variant API response data.
|
|
49
50
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
50
51
|
*/
|
|
51
|
-
handleVariantAPIRes(res: APIResponse): VariantEntryStruct | {
|
|
52
|
+
handleVariantAPIRes(res: APIResponse): Promise<VariantEntryStruct | {
|
|
52
53
|
entries: VariantEntryStruct[];
|
|
53
54
|
count: number;
|
|
54
|
-
} | string | any
|
|
55
|
+
} | string | any>;
|
|
55
56
|
}
|
|
56
57
|
export declare class VariantManagementSDK<T> extends AdapterHelper<T, ContentstackClient> implements VariantInterface<T, ContentstackClient> {
|
|
57
58
|
apiClient: ContentstackClient;
|
|
@@ -63,10 +64,10 @@ export declare class VariantManagementSDK<T> extends AdapterHelper<T, Contentsta
|
|
|
63
64
|
entries: {}[];
|
|
64
65
|
}>;
|
|
65
66
|
createVariantEntry(input: CreateVariantEntryDto, options: CreateVariantEntryOptions, apiParams: Record<string, any>): Promise<VariantEntryStruct | string | void>;
|
|
66
|
-
handleVariantAPIRes(res: APIResponse): VariantEntryStruct | {
|
|
67
|
+
handleVariantAPIRes(res: APIResponse): Promise<VariantEntryStruct | {
|
|
67
68
|
entries: VariantEntryStruct[];
|
|
68
69
|
count: number;
|
|
69
|
-
} | string
|
|
70
|
+
} | string>;
|
|
70
71
|
constructQuery(query: Record<string, any>): string | void;
|
|
71
72
|
delay(ms: number): Promise<void>;
|
|
72
73
|
}
|
|
@@ -37,6 +37,18 @@ class VariantHttpClient extends adapter_helper_1.AdapterHelper {
|
|
|
37
37
|
this.baseURL = ((_a = config.baseURL) === null || _a === void 0 ? void 0 : _a.includes('http')) ? `${config.baseURL}/v3` : `https://${config.baseURL}/v3`;
|
|
38
38
|
this.apiClient.baseUrl(this.baseURL);
|
|
39
39
|
}
|
|
40
|
+
init() {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
yield cli_utilities_1.authenticationHandler.getAuthDetails();
|
|
43
|
+
const token = cli_utilities_1.authenticationHandler.accessToken;
|
|
44
|
+
if (cli_utilities_1.authenticationHandler.isOauthEnabled) {
|
|
45
|
+
this.apiClient.headers({ authorization: token });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.apiClient.headers({ authtoken: token });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
40
52
|
variantEntry(options) {
|
|
41
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
54
|
// TODO single entry variant
|
|
@@ -99,7 +111,7 @@ class VariantHttpClient extends adapter_helper_1.AdapterHelper {
|
|
|
99
111
|
endpoint = endpoint.concat(query);
|
|
100
112
|
}
|
|
101
113
|
const data = yield this.apiClient.get(endpoint);
|
|
102
|
-
const response = this.handleVariantAPIRes(data);
|
|
114
|
+
const response = (yield this.handleVariantAPIRes(data));
|
|
103
115
|
if (callback) {
|
|
104
116
|
callback(response.entries);
|
|
105
117
|
}
|
|
@@ -148,6 +160,7 @@ class VariantHttpClient extends adapter_helper_1.AdapterHelper {
|
|
|
148
160
|
log,
|
|
149
161
|
});
|
|
150
162
|
try {
|
|
163
|
+
this.apiClient.headers({ api_version: undefined });
|
|
151
164
|
const res = yield this.apiClient.put(endpoint, { entry: input });
|
|
152
165
|
const data = this.handleVariantAPIRes(res);
|
|
153
166
|
if (res.status >= 200 && res.status < 300) {
|
|
@@ -204,14 +217,18 @@ class VariantHttpClient extends adapter_helper_1.AdapterHelper {
|
|
|
204
217
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
205
218
|
*/
|
|
206
219
|
handleVariantAPIRes(res) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
const { status, data } = res;
|
|
222
|
+
if (status >= 200 && status < 300) {
|
|
223
|
+
return data;
|
|
224
|
+
}
|
|
225
|
+
// Refresh the access token if the response status is 401
|
|
226
|
+
yield cli_utilities_1.authenticationHandler.refreshAccessToken(res);
|
|
227
|
+
const errorMsg = (data === null || data === void 0 ? void 0 : data.errors)
|
|
228
|
+
? (0, error_helper_1.formatErrors)(data.errors)
|
|
229
|
+
: (data === null || data === void 0 ? void 0 : data.error_message) || (data === null || data === void 0 ? void 0 : data.message) || 'Something went wrong while processing entry variant request!';
|
|
230
|
+
throw errorMsg;
|
|
231
|
+
});
|
|
215
232
|
}
|
|
216
233
|
}
|
|
217
234
|
exports.VariantHttpClient = VariantHttpClient;
|
|
@@ -238,7 +255,9 @@ class VariantManagementSDK extends adapter_helper_1.AdapterHelper {
|
|
|
238
255
|
return Promise.resolve({});
|
|
239
256
|
}
|
|
240
257
|
handleVariantAPIRes(res) {
|
|
241
|
-
return
|
|
258
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
+
return res.data;
|
|
260
|
+
});
|
|
242
261
|
}
|
|
243
262
|
constructQuery(query) { }
|
|
244
263
|
delay(ms) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentstack/cli-variants",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Variants plugin",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"typescript": "^5.4.2"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@contentstack/cli-utilities": "^1.
|
|
33
|
+
"@contentstack/cli-utilities": "^1.8.0",
|
|
34
34
|
"lodash": "^4.17.21",
|
|
35
35
|
"mkdirp": "^1.0.4",
|
|
36
36
|
"winston": "^3.7.2"
|
package/src/export/attributes.ts
CHANGED
|
@@ -2,26 +2,26 @@ import omit from 'lodash/omit';
|
|
|
2
2
|
import { resolve as pResolve } from 'node:path';
|
|
3
3
|
import { sanitizePath } from '@contentstack/cli-utilities';
|
|
4
4
|
import { formatError, fsUtil, PersonalizationAdapter, log } from '../utils';
|
|
5
|
-
import {
|
|
5
|
+
import { PersonalizeConfig, ExportConfig, AttributesConfig, AttributeStruct } from '../types';
|
|
6
6
|
|
|
7
7
|
export default class ExportAttributes extends PersonalizationAdapter<ExportConfig> {
|
|
8
8
|
private attributesConfig: AttributesConfig;
|
|
9
9
|
private attributesFolderPath: string;
|
|
10
10
|
private attributes: Record<string, unknown>[];
|
|
11
|
-
public
|
|
11
|
+
public personalizeConfig: PersonalizeConfig;
|
|
12
12
|
|
|
13
13
|
constructor(readonly exportConfig: ExportConfig) {
|
|
14
14
|
super({
|
|
15
15
|
config: exportConfig,
|
|
16
|
-
baseURL: exportConfig.modules.
|
|
17
|
-
headers: {
|
|
16
|
+
baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name],
|
|
17
|
+
headers: { 'X-Project-Uid': exportConfig.project_id },
|
|
18
18
|
});
|
|
19
|
-
this.
|
|
19
|
+
this.personalizeConfig = exportConfig.modules.personalize;
|
|
20
20
|
this.attributesConfig = exportConfig.modules.attributes;
|
|
21
21
|
this.attributesFolderPath = pResolve(
|
|
22
22
|
sanitizePath(exportConfig.data),
|
|
23
23
|
sanitizePath(exportConfig.branchName || ''),
|
|
24
|
-
sanitizePath(this.
|
|
24
|
+
sanitizePath(this.personalizeConfig.dirName),
|
|
25
25
|
sanitizePath(this.attributesConfig.dirName),
|
|
26
26
|
);
|
|
27
27
|
this.attributes = [];
|
|
@@ -30,6 +30,7 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi
|
|
|
30
30
|
async start() {
|
|
31
31
|
try {
|
|
32
32
|
log(this.exportConfig, 'Starting attributes export', 'info');
|
|
33
|
+
await this.init();
|
|
33
34
|
await fsUtil.makeDirectory(this.attributesFolderPath);
|
|
34
35
|
this.attributes = (await this.getAttributes()) as AttributeStruct[];
|
|
35
36
|
|
|
@@ -37,7 +38,10 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi
|
|
|
37
38
|
log(this.exportConfig, 'No Attributes found with the given project!', 'info');
|
|
38
39
|
} else {
|
|
39
40
|
this.sanitizeAttribs();
|
|
40
|
-
fsUtil.writeFile(
|
|
41
|
+
fsUtil.writeFile(
|
|
42
|
+
pResolve(sanitizePath(this.attributesFolderPath), sanitizePath(this.attributesConfig.fileName)),
|
|
43
|
+
this.attributes,
|
|
44
|
+
);
|
|
41
45
|
log(this.exportConfig, 'All the attributes have been exported successfully!', 'success');
|
|
42
46
|
}
|
|
43
47
|
} catch (error) {
|
package/src/export/audiences.ts
CHANGED
|
@@ -2,26 +2,26 @@ import omit from 'lodash/omit';
|
|
|
2
2
|
import { resolve as pResolve } from 'node:path';
|
|
3
3
|
|
|
4
4
|
import { formatError, fsUtil, PersonalizationAdapter, log } from '../utils';
|
|
5
|
-
import {
|
|
5
|
+
import { PersonalizeConfig, ExportConfig, AudienceStruct, AudiencesConfig } from '../types';
|
|
6
6
|
|
|
7
7
|
export default class ExportAudiences extends PersonalizationAdapter<ExportConfig> {
|
|
8
8
|
private audiencesConfig: AudiencesConfig;
|
|
9
9
|
private audiencesFolderPath: string;
|
|
10
10
|
private audiences: Record<string, unknown>[];
|
|
11
|
-
public
|
|
11
|
+
public personalizeConfig: PersonalizeConfig;
|
|
12
12
|
|
|
13
13
|
constructor(readonly exportConfig: ExportConfig) {
|
|
14
14
|
super({
|
|
15
15
|
config: exportConfig,
|
|
16
|
-
baseURL: exportConfig.modules.
|
|
17
|
-
headers: {
|
|
16
|
+
baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name],
|
|
17
|
+
headers: { 'X-Project-Uid': exportConfig.project_id },
|
|
18
18
|
});
|
|
19
|
-
this.
|
|
19
|
+
this.personalizeConfig = exportConfig.modules.personalize;
|
|
20
20
|
this.audiencesConfig = exportConfig.modules.audiences;
|
|
21
21
|
this.audiencesFolderPath = pResolve(
|
|
22
22
|
exportConfig.data,
|
|
23
23
|
exportConfig.branchName || '',
|
|
24
|
-
this.
|
|
24
|
+
this.personalizeConfig.dirName,
|
|
25
25
|
this.audiencesConfig.dirName,
|
|
26
26
|
);
|
|
27
27
|
this.audiences = [];
|
|
@@ -30,6 +30,7 @@ export default class ExportAudiences extends PersonalizationAdapter<ExportConfig
|
|
|
30
30
|
async start() {
|
|
31
31
|
try {
|
|
32
32
|
log(this.exportConfig, 'Starting audiences export', 'info');
|
|
33
|
+
await this.init();
|
|
33
34
|
await fsUtil.makeDirectory(this.audiencesFolderPath);
|
|
34
35
|
this.audiences = (await this.getAudiences()) as AudienceStruct[];
|
|
35
36
|
|
package/src/export/events.ts
CHANGED
|
@@ -2,26 +2,26 @@ import omit from 'lodash/omit';
|
|
|
2
2
|
import { resolve as pResolve } from 'node:path';
|
|
3
3
|
|
|
4
4
|
import { formatError, fsUtil, PersonalizationAdapter, log } from '../utils';
|
|
5
|
-
import {
|
|
5
|
+
import { PersonalizeConfig, ExportConfig, EventStruct, EventsConfig } from '../types';
|
|
6
6
|
|
|
7
7
|
export default class ExportEvents extends PersonalizationAdapter<ExportConfig> {
|
|
8
8
|
private eventsConfig: EventsConfig;
|
|
9
9
|
private eventsFolderPath: string;
|
|
10
10
|
private events: Record<string, unknown>[];
|
|
11
|
-
public
|
|
11
|
+
public personalizeConfig: PersonalizeConfig;
|
|
12
12
|
|
|
13
13
|
constructor(readonly exportConfig: ExportConfig) {
|
|
14
14
|
super({
|
|
15
15
|
config: exportConfig,
|
|
16
|
-
baseURL: exportConfig.modules.
|
|
17
|
-
headers: {
|
|
16
|
+
baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name],
|
|
17
|
+
headers: { 'X-Project-Uid': exportConfig.project_id },
|
|
18
18
|
});
|
|
19
|
-
this.
|
|
19
|
+
this.personalizeConfig = exportConfig.modules.personalize;
|
|
20
20
|
this.eventsConfig = exportConfig.modules.events;
|
|
21
21
|
this.eventsFolderPath = pResolve(
|
|
22
22
|
exportConfig.data,
|
|
23
23
|
exportConfig.branchName || '',
|
|
24
|
-
this.
|
|
24
|
+
this.personalizeConfig.dirName,
|
|
25
25
|
this.eventsConfig.dirName,
|
|
26
26
|
);
|
|
27
27
|
this.events = [];
|
|
@@ -30,6 +30,7 @@ export default class ExportEvents extends PersonalizationAdapter<ExportConfig> {
|
|
|
30
30
|
async start() {
|
|
31
31
|
try {
|
|
32
32
|
log(this.exportConfig, 'Starting events export', 'info');
|
|
33
|
+
await this.init();
|
|
33
34
|
await fsUtil.makeDirectory(this.eventsFolderPath);
|
|
34
35
|
this.events = (await this.getEvents()) as EventStruct[];
|
|
35
36
|
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import { sanitizePath } from '@contentstack/cli-utilities';
|
|
3
|
-
import {
|
|
3
|
+
import { PersonalizeConfig, ExportConfig, ExperienceStruct } from '../types';
|
|
4
4
|
import { formatError, fsUtil, log, PersonalizationAdapter } from '../utils';
|
|
5
5
|
|
|
6
6
|
export default class ExportExperiences extends PersonalizationAdapter<ExportConfig> {
|
|
7
7
|
private experiencesFolderPath: string;
|
|
8
8
|
public exportConfig: ExportConfig;
|
|
9
|
-
public
|
|
9
|
+
public personalizeConfig: PersonalizeConfig;
|
|
10
10
|
constructor(exportConfig: ExportConfig) {
|
|
11
11
|
super({
|
|
12
12
|
config: exportConfig,
|
|
13
|
-
baseURL: exportConfig.modules.
|
|
14
|
-
headers: {
|
|
13
|
+
baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name],
|
|
14
|
+
headers: { 'X-Project-Uid': exportConfig.project_id },
|
|
15
15
|
cmaConfig: {
|
|
16
16
|
baseURL: exportConfig.region.cma + `/v3`,
|
|
17
|
-
headers: {
|
|
17
|
+
headers: { api_key: exportConfig.apiKey },
|
|
18
18
|
},
|
|
19
19
|
});
|
|
20
20
|
this.exportConfig = exportConfig;
|
|
21
|
-
this.
|
|
21
|
+
this.personalizeConfig = exportConfig.modules.personalize;
|
|
22
22
|
this.experiencesFolderPath = path.resolve(
|
|
23
23
|
sanitizePath(exportConfig.data),
|
|
24
24
|
sanitizePath(exportConfig.branchName || ''),
|
|
25
|
-
sanitizePath(this.
|
|
25
|
+
sanitizePath(this.personalizeConfig.dirName),
|
|
26
26
|
'experiences',
|
|
27
27
|
);
|
|
28
28
|
}
|
|
@@ -33,7 +33,9 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
|
|
|
33
33
|
// loop through experiences and get content types attached to it
|
|
34
34
|
// write experiences in to a file
|
|
35
35
|
log(this.exportConfig, 'Starting experiences export', 'info');
|
|
36
|
+
await this.init();
|
|
36
37
|
await fsUtil.makeDirectory(this.experiencesFolderPath);
|
|
38
|
+
await fsUtil.makeDirectory(path.resolve(sanitizePath(this.experiencesFolderPath), 'versions'));
|
|
37
39
|
const experiences: Array<ExperienceStruct> = (await this.getExperiences()) || [];
|
|
38
40
|
if (!experiences || experiences?.length < 1) {
|
|
39
41
|
log(this.exportConfig, 'No Experiences found with the give project', 'info');
|
|
@@ -51,6 +53,21 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
|
|
|
51
53
|
experienceToVariantsStrList.push(experienceToVariantsStr);
|
|
52
54
|
});
|
|
53
55
|
|
|
56
|
+
try {
|
|
57
|
+
// fetch versions of experience
|
|
58
|
+
const experienceVersions = (await this.getExperienceVersions(experience.uid)) || [];
|
|
59
|
+
if (experienceVersions.length > 0) {
|
|
60
|
+
fsUtil.writeFile(
|
|
61
|
+
path.resolve(sanitizePath(this.experiencesFolderPath), 'versions', `${experience.uid}.json`),
|
|
62
|
+
experienceVersions,
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
log(this.exportConfig, `No versions found for experience ${experience.name}`, 'info');
|
|
66
|
+
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
log(this.exportConfig, `Failed to fetch versions of experience ${experience.name}`, 'error');
|
|
69
|
+
}
|
|
70
|
+
|
|
54
71
|
try {
|
|
55
72
|
// fetch content of experience
|
|
56
73
|
const { variant_groups: [variantGroup] = [] } =
|