@contentstack/cli-variants 1.0.0 → 1.1.1
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.js +2 -1
- package/lib/export/audiences.js +2 -1
- package/lib/export/events.js +2 -1
- package/lib/export/experiences.js +3 -2
- package/lib/export/projects.js +2 -1
- package/lib/export/variant-entries.js +1 -1
- package/lib/import/attribute.js +2 -1
- package/lib/import/audiences.js +2 -1
- package/lib/import/events.js +2 -1
- package/lib/import/experiences.js +3 -2
- package/lib/import/project.js +3 -2
- package/lib/import/variant-entries.js +18 -12
- package/lib/messages/index.d.ts +1 -0
- package/lib/messages/index.js +2 -1
- package/lib/types/personalization-api-adapter.d.ts +1 -1
- package/lib/types/variant-api-adapter.d.ts +3 -2
- package/lib/types/variant-entry.d.ts +0 -1
- package/lib/utils/attributes-helper.js +2 -2
- package/lib/utils/error-helper.js +6 -6
- package/lib/utils/logger.js +5 -4
- package/lib/utils/personalization-api-adapter.d.ts +2 -1
- package/lib/utils/personalization-api-adapter.js +54 -26
- package/lib/utils/variant-api-adapter.d.ts +5 -4
- package/lib/utils/variant-api-adapter.js +28 -10
- package/package.json +2 -2
- package/src/export/attributes.ts +6 -2
- package/src/export/audiences.ts +2 -1
- package/src/export/events.ts +2 -1
- package/src/export/experiences.ts +3 -2
- package/src/export/projects.ts +2 -1
- package/src/export/variant-entries.ts +1 -2
- package/src/import/attribute.ts +2 -2
- package/src/import/audiences.ts +2 -2
- package/src/import/events.ts +2 -2
- package/src/import/experiences.ts +3 -3
- package/src/import/project.ts +4 -4
- package/src/import/variant-entries.ts +23 -15
- package/src/messages/index.ts +2 -1
- package/src/types/personalization-api-adapter.ts +1 -1
- package/src/types/variant-api-adapter.ts +3 -1
- package/src/types/variant-entry.ts +0 -1
- package/src/utils/attributes-helper.ts +2 -2
- package/src/utils/error-helper.ts +6 -6
- package/src/utils/logger.ts +5 -4
- package/src/utils/personalization-api-adapter.ts +45 -22
- package/src/utils/variant-api-adapter.ts +19 -5
|
@@ -49,7 +49,6 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
49
49
|
headers: {
|
|
50
50
|
api_key: config.apiKey,
|
|
51
51
|
branch: config.branchName,
|
|
52
|
-
authtoken: config.auth_token,
|
|
53
52
|
organization_uid: config.org_uid,
|
|
54
53
|
'X-Project-Uid': config.modules.personalize.project_id,
|
|
55
54
|
},
|
|
@@ -57,14 +56,12 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
57
56
|
super(Object.assign(omit(config, ['helpers']), conf));
|
|
58
57
|
this.entriesMapperPath = resolve(
|
|
59
58
|
sanitizePath(config.backupDir),
|
|
60
|
-
sanitizePath(config.branchName || ''),
|
|
61
59
|
'mapper',
|
|
62
60
|
'entries',
|
|
63
61
|
);
|
|
64
62
|
this.personalizeConfig = this.config.modules.personalize;
|
|
65
63
|
this.entriesDirPath = resolve(
|
|
66
64
|
sanitizePath(config.backupDir),
|
|
67
|
-
sanitizePath(config.branchName || ''),
|
|
68
65
|
sanitizePath(config.modules.entries.dirName),
|
|
69
66
|
);
|
|
70
67
|
this.failedVariantPath = resolve(sanitizePath(this.entriesMapperPath), 'failed-entry-variants.json');
|
|
@@ -90,7 +87,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
90
87
|
);
|
|
91
88
|
|
|
92
89
|
if (!existsSync(filePath)) {
|
|
93
|
-
log(this.config, this.messages.
|
|
90
|
+
log(this.config, this.messages.VARIANT_ENTRY_FILE_NOT_FOUND, 'info');
|
|
94
91
|
return;
|
|
95
92
|
}
|
|
96
93
|
|
|
@@ -138,7 +135,8 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
138
135
|
this.assetUidMapper = (fsUtil.readFile(assetUidMapperPath, true) || {}) as Record<string, any>;
|
|
139
136
|
this.assetUrlMapper = (fsUtil.readFile(assetUrlMapperPath, true) || {}) as Record<string, any>;
|
|
140
137
|
this.environments = (fsUtil.readFile(envPath, true) || {}) as Record<string, any>;
|
|
141
|
-
|
|
138
|
+
// set the token
|
|
139
|
+
await this.variantInstance.init();
|
|
142
140
|
for (const entriesForVariant of entriesForVariants) {
|
|
143
141
|
await this.importVariantEntries(entriesForVariant);
|
|
144
142
|
}
|
|
@@ -368,7 +366,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
368
366
|
* @param variantEntry - The entry variant to update.
|
|
369
367
|
*/
|
|
370
368
|
updateFileFields(variantEntry: VariantEntryStruct) {
|
|
371
|
-
const setValue = (currentObj: VariantEntryStruct, keys:
|
|
369
|
+
const setValue = (currentObj: VariantEntryStruct, keys: string[]) => {
|
|
372
370
|
if (!currentObj || keys.length === 0) return;
|
|
373
371
|
|
|
374
372
|
const [firstKey, ...restKeys] = keys;
|
|
@@ -380,7 +378,14 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
380
378
|
} else if (currentObj && typeof currentObj === 'object') {
|
|
381
379
|
if (firstKey in currentObj) {
|
|
382
380
|
if (keys.length === 1) {
|
|
383
|
-
|
|
381
|
+
// Check if the current property is already an object with uid and filename
|
|
382
|
+
const existingValue = currentObj[firstKey];
|
|
383
|
+
|
|
384
|
+
if (existingValue && typeof existingValue === 'object' && existingValue.uid) {
|
|
385
|
+
currentObj[firstKey] = { uid: existingValue.uid, filename: 'dummy.jpeg' };
|
|
386
|
+
} else {
|
|
387
|
+
currentObj[firstKey] = { uid: currentObj[firstKey], filename: 'dummy.jpeg' };
|
|
388
|
+
}
|
|
384
389
|
} else {
|
|
385
390
|
setValue(currentObj[firstKey], restKeys);
|
|
386
391
|
}
|
|
@@ -388,13 +393,12 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
388
393
|
}
|
|
389
394
|
};
|
|
390
395
|
|
|
391
|
-
const pathsToUpdate =
|
|
392
|
-
|
|
393
|
-
|
|
396
|
+
const pathsToUpdate =
|
|
397
|
+
variantEntry?._metadata?.references
|
|
398
|
+
?.filter((ref: any) => ref._content_type_uid === 'sys_assets')
|
|
399
|
+
.map((ref: any) => ref.path) || [];
|
|
394
400
|
|
|
395
|
-
|
|
396
|
-
pathsToUpdate.forEach((path: string) => setValue(variantEntry, path.split('.')));
|
|
397
|
-
}
|
|
401
|
+
pathsToUpdate.forEach((path: string) => setValue(variantEntry, path.split('.')));
|
|
398
402
|
}
|
|
399
403
|
|
|
400
404
|
/**
|
|
@@ -406,6 +410,11 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
406
410
|
*/
|
|
407
411
|
async publishVariantEntries(batch: VariantEntryStruct[], entryUid: string, content_type: string) {
|
|
408
412
|
const allPromise = [];
|
|
413
|
+
log(
|
|
414
|
+
this.config,
|
|
415
|
+
`Publishing variant entries for entry uid '${entryUid}' of Content Type '${content_type}'`,
|
|
416
|
+
'info',
|
|
417
|
+
);
|
|
409
418
|
for (let [, variantEntry] of entries(batch)) {
|
|
410
419
|
const variantEntryUID = variantEntry.uid;
|
|
411
420
|
const oldVariantUid = variantEntry._variant._uid || '';
|
|
@@ -446,11 +455,9 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
446
455
|
entry: {
|
|
447
456
|
environments,
|
|
448
457
|
locales,
|
|
449
|
-
publish_with_base_entry: false,
|
|
450
458
|
variants: [{ uid: newVariantUid, version: 1 }],
|
|
451
459
|
},
|
|
452
460
|
locale: variantEntry.locale,
|
|
453
|
-
version: 1,
|
|
454
461
|
};
|
|
455
462
|
|
|
456
463
|
const promise = this.variantInstance.publishVariantEntry(
|
|
@@ -470,6 +477,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
|
|
|
470
477
|
allPromise.push(promise);
|
|
471
478
|
}
|
|
472
479
|
await Promise.allSettled(allPromise);
|
|
480
|
+
log(this.config, `Published variant entries for entry uid '${entryUid}' of Content Type '${content_type}'`, 'info');
|
|
473
481
|
}
|
|
474
482
|
|
|
475
483
|
/**
|
package/src/messages/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ const migrationMsg = {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
const variantEntry = {
|
|
16
|
+
VARIANT_ENTRY_FILE_NOT_FOUND: 'Variant entry file not found!',
|
|
16
17
|
IMPORT_ENTRY_NOT_FOUND: 'Entries data not found to import variant entries',
|
|
17
18
|
EMPTY_VARIANT_UID_DATA: 'Empty variants entry mapper found!',
|
|
18
19
|
VARIANT_ID_NOT_FOUND: 'Variant ID not found',
|
|
@@ -23,7 +24,7 @@ const expImportMsg = {
|
|
|
23
24
|
UPDATING_CT_IN_EXP: 'Updating content types in experiences...',
|
|
24
25
|
UPDATED_CT_IN_EXP: 'Successfully updated content types in experiences!',
|
|
25
26
|
VALIDATE_VARIANT_AND_VARIANT_GRP: 'Validating variant group and variants creation...',
|
|
26
|
-
PERSONALIZE_JOB_FAILURE: 'Something went wrong
|
|
27
|
+
PERSONALIZE_JOB_FAILURE: 'Something went wrong! Failed to fetch some variant and variant groups.',
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
const messages: typeof errors & typeof commonMsg & typeof migrationMsg & typeof variantEntry & typeof expImportMsg = {
|
|
@@ -206,5 +206,5 @@ export interface Personalization<T> extends AdapterHelperInterface<T, HttpClient
|
|
|
206
206
|
|
|
207
207
|
updateCTsInExperience(experience: UpdateExperienceInput, experienceUid: string): Promise<CMSExperienceStruct | void>;
|
|
208
208
|
|
|
209
|
-
handleVariantAPIRes(res: any): VariantAPIRes
|
|
209
|
+
handleVariantAPIRes(res: any): Promise<VariantAPIRes>;
|
|
210
210
|
}
|
|
@@ -42,6 +42,8 @@ export type VariantOptions = VariantsOption & {
|
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
export interface VariantInterface<T, ApiClient> extends AdapterHelperInterface<T, ApiClient> {
|
|
45
|
+
init(): Promise<void>;
|
|
46
|
+
|
|
45
47
|
variantEntry(options: VariantOptions): Promise<{ entry: Record<string, any> }>;
|
|
46
48
|
|
|
47
49
|
variantEntries(options: VariantsOption): Promise<{ entries?: Record<string, any>[] | unknown[] } | void>;
|
|
@@ -52,5 +54,5 @@ export interface VariantInterface<T, ApiClient> extends AdapterHelperInterface<T
|
|
|
52
54
|
apiParams: Record<string, any>,
|
|
53
55
|
): Promise<VariantEntryStruct | string | void>;
|
|
54
56
|
|
|
55
|
-
handleVariantAPIRes(res: APIResponse): VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string
|
|
57
|
+
handleVariantAPIRes(res: APIResponse): Promise<VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string>;
|
|
56
58
|
}
|
|
@@ -12,8 +12,8 @@ export const lookUpAttributes = (attributeRules: Record<string, any>[], attribut
|
|
|
12
12
|
// Check if attribute reference exists in attributesUid
|
|
13
13
|
const attributeRef = rule.attribute?.ref;
|
|
14
14
|
const attributeType = rule.attribute['__type'];
|
|
15
|
-
// check if type is
|
|
16
|
-
if (attributeType === '
|
|
15
|
+
// check if type is CustomAttributeReference
|
|
16
|
+
if (attributeType === 'CustomAttributeReference') {
|
|
17
17
|
if (attributeRef && attributesUid.hasOwnProperty(attributeRef) && attributesUid[attributeRef]) {
|
|
18
18
|
rule.attribute.ref = attributesUid[attributeRef];
|
|
19
19
|
} else {
|
|
@@ -9,18 +9,18 @@ export function formatErrors(errors: any): string {
|
|
|
9
9
|
for (const errorKey in errors) {
|
|
10
10
|
const errorValue = errors[errorKey];
|
|
11
11
|
if (Array.isArray(errorValue)) {
|
|
12
|
-
errorMessages.push(...errorValue.map((error: any) => formatError(error)));
|
|
12
|
+
errorMessages.push(...errorValue.map((error: any) => formatError(errorKey, error)));
|
|
13
13
|
} else {
|
|
14
|
-
errorMessages.push(formatError(errorValue));
|
|
14
|
+
errorMessages.push(formatError(errorKey, errorValue));
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
return errorMessages.join(' ');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function formatError(error: any): string {
|
|
22
|
-
if (typeof error === 'object') {
|
|
23
|
-
return Object.values(error).join(' ')
|
|
21
|
+
function formatError(errorKey: string, error: any): string {
|
|
22
|
+
if (typeof error === 'object' && error !== null) {
|
|
23
|
+
return `${errorKey}: ${Object.values(error).join(' ')}`;
|
|
24
24
|
}
|
|
25
|
-
return error
|
|
25
|
+
return `${errorKey}: ${error}`;
|
|
26
26
|
}
|
package/src/utils/logger.ts
CHANGED
|
@@ -51,9 +51,9 @@ let errorLogger: winston.Logger;
|
|
|
51
51
|
let successTransport;
|
|
52
52
|
let errorTransport;
|
|
53
53
|
|
|
54
|
-
function init(_logPath: string) {
|
|
54
|
+
function init(_logPath: string, module: string) {
|
|
55
55
|
if (!logger || !errorLogger) {
|
|
56
|
-
const logsDir = path.resolve(sanitizePath(_logPath), 'logs',
|
|
56
|
+
const logsDir = path.resolve(sanitizePath(_logPath), 'logs', sanitizePath(module));
|
|
57
57
|
// Create dir if doesn't already exist
|
|
58
58
|
mkdirp.sync(logsDir);
|
|
59
59
|
|
|
@@ -131,11 +131,12 @@ function init(_logPath: string) {
|
|
|
131
131
|
export const log = (config: ExportConfig | ImportConfig, message: any, type: 'info' | 'error' | 'success') => {
|
|
132
132
|
const logsPath = config.data;
|
|
133
133
|
// ignoring the type argument, as we are not using it to create a logfile anymore
|
|
134
|
+
const module = (config as ImportConfig)['backupDir'] ? 'import' : 'export';
|
|
134
135
|
if (type !== 'error') {
|
|
135
136
|
// removed type argument from init method
|
|
136
|
-
init(logsPath).log(message);
|
|
137
|
+
init(logsPath, module).log(message);
|
|
137
138
|
} else {
|
|
138
|
-
init(logsPath).error(message);
|
|
139
|
+
init(logsPath, module).error(message);
|
|
139
140
|
}
|
|
140
141
|
};
|
|
141
142
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdapterHelper } from './adapter-helper';
|
|
2
|
-
import { HttpClient } from '@contentstack/cli-utilities';
|
|
2
|
+
import { HttpClient, authenticationHandler } from '@contentstack/cli-utilities';
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
ProjectStruct,
|
|
@@ -31,10 +31,27 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
31
31
|
super(options);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
async init(): Promise<void> {
|
|
35
|
+
await authenticationHandler.getAuthDetails();
|
|
36
|
+
const token = authenticationHandler.accessToken;
|
|
37
|
+
if (authenticationHandler.isOauthEnabled) {
|
|
38
|
+
this.apiClient.headers({ authorization: token });
|
|
39
|
+
if(this.adapterConfig.cmaConfig) {
|
|
40
|
+
this.cmaAPIClient?.headers({ authorization: token });
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
this.apiClient.headers({ authtoken: token });
|
|
44
|
+
if(this.adapterConfig.cmaConfig) {
|
|
45
|
+
this.cmaAPIClient?.headers({ authtoken: token });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
34
50
|
async projects(options: GetProjectsParams): Promise<ProjectStruct[]> {
|
|
51
|
+
await this.init();
|
|
35
52
|
const getProjectEndPoint = `/projects?connectedStackApiKey=${options.connectedStackApiKey}`;
|
|
36
53
|
const data = await this.apiClient.get(getProjectEndPoint);
|
|
37
|
-
return this.handleVariantAPIRes(data) as ProjectStruct[];
|
|
54
|
+
return (await this.handleVariantAPIRes(data)) as ProjectStruct[];
|
|
38
55
|
}
|
|
39
56
|
|
|
40
57
|
/**
|
|
@@ -49,7 +66,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
49
66
|
*/
|
|
50
67
|
async createProject(project: CreateProjectInput): Promise<ProjectStruct> {
|
|
51
68
|
const data = await this.apiClient.post<ProjectStruct>('/projects', project);
|
|
52
|
-
return this.handleVariantAPIRes(data) as ProjectStruct;
|
|
69
|
+
return (await this.handleVariantAPIRes(data)) as ProjectStruct;
|
|
53
70
|
}
|
|
54
71
|
|
|
55
72
|
/**
|
|
@@ -63,25 +80,25 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
63
80
|
*/
|
|
64
81
|
async createAttribute(attribute: CreateAttributeInput): Promise<AttributeStruct> {
|
|
65
82
|
const data = await this.apiClient.post<AttributeStruct>('/attributes', attribute);
|
|
66
|
-
return this.handleVariantAPIRes(data) as AttributeStruct;
|
|
83
|
+
return (await this.handleVariantAPIRes(data)) as AttributeStruct;
|
|
67
84
|
}
|
|
68
85
|
|
|
69
86
|
async getExperiences(): Promise<ExperienceStruct[]> {
|
|
70
87
|
const getExperiencesEndPoint = `/experiences`;
|
|
71
88
|
const data = await this.apiClient.get(getExperiencesEndPoint);
|
|
72
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct[];
|
|
89
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct[];
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
async getExperience(experienceUid: string): Promise<ExperienceStruct | void> {
|
|
76
93
|
const getExperiencesEndPoint = `/experiences/${experienceUid}`;
|
|
77
94
|
const data = await this.apiClient.get(getExperiencesEndPoint);
|
|
78
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct;
|
|
95
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
79
96
|
}
|
|
80
97
|
|
|
81
98
|
async getExperienceVersions(experienceUid: string): Promise<ExperienceStruct | void> {
|
|
82
99
|
const getExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
83
100
|
const data = await this.apiClient.get(getExperiencesVersionsEndPoint);
|
|
84
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct;
|
|
101
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
85
102
|
}
|
|
86
103
|
|
|
87
104
|
async createExperienceVersion(
|
|
@@ -90,7 +107,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
90
107
|
): Promise<ExperienceStruct | void> {
|
|
91
108
|
const createExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
92
109
|
const data = await this.apiClient.post(createExperiencesVersionsEndPoint, input);
|
|
93
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct;
|
|
110
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
94
111
|
}
|
|
95
112
|
|
|
96
113
|
async updateExperienceVersion(
|
|
@@ -104,7 +121,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
104
121
|
}
|
|
105
122
|
const updateExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions/${versionId}`;
|
|
106
123
|
const data = await this.apiClient.put(updateExperiencesVersionsEndPoint, input);
|
|
107
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct;
|
|
124
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
108
125
|
}
|
|
109
126
|
|
|
110
127
|
async getVariantGroup(input: GetVariantGroupInput): Promise<VariantGroupStruct | void> {
|
|
@@ -113,7 +130,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
113
130
|
const data = await this.cmaAPIClient
|
|
114
131
|
.queryParams({ experience_uid: input.experienceUid })
|
|
115
132
|
.get(getVariantGroupEndPoint);
|
|
116
|
-
return this.handleVariantAPIRes(data) as VariantGroupStruct;
|
|
133
|
+
return (await this.handleVariantAPIRes(data)) as VariantGroupStruct;
|
|
117
134
|
}
|
|
118
135
|
}
|
|
119
136
|
|
|
@@ -121,28 +138,28 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
121
138
|
if (this.cmaAPIClient) {
|
|
122
139
|
const updateVariantGroupEndPoint = `/variant_groups/${input.uid}`;
|
|
123
140
|
const data = await this.cmaAPIClient.put(updateVariantGroupEndPoint, input);
|
|
124
|
-
return this.handleVariantAPIRes(data) as VariantGroup;
|
|
141
|
+
return (await this.handleVariantAPIRes(data)) as VariantGroup;
|
|
125
142
|
}
|
|
126
143
|
}
|
|
127
144
|
|
|
128
145
|
async getEvents(): Promise<EventStruct[] | void> {
|
|
129
146
|
const data = await this.apiClient.get<EventStruct>('/events');
|
|
130
|
-
return this.handleVariantAPIRes(data) as EventStruct[];
|
|
147
|
+
return (await this.handleVariantAPIRes(data)) as EventStruct[];
|
|
131
148
|
}
|
|
132
149
|
|
|
133
150
|
async createEvents(event: CreateEventInput): Promise<void | EventStruct> {
|
|
134
151
|
const data = await this.apiClient.post<EventStruct>('/events', event);
|
|
135
|
-
return this.handleVariantAPIRes(data) as EventStruct;
|
|
152
|
+
return (await this.handleVariantAPIRes(data)) as EventStruct;
|
|
136
153
|
}
|
|
137
154
|
|
|
138
155
|
async getAudiences(): Promise<AudienceStruct[] | void> {
|
|
139
156
|
const data = await this.apiClient.get<AudienceStruct>('/audiences');
|
|
140
|
-
return this.handleVariantAPIRes(data) as AudienceStruct[];
|
|
157
|
+
return (await this.handleVariantAPIRes(data)) as AudienceStruct[];
|
|
141
158
|
}
|
|
142
159
|
|
|
143
160
|
async getAttributes(): Promise<AttributeStruct[] | void> {
|
|
144
161
|
const data = await this.apiClient.get<AttributeStruct>('/attributes');
|
|
145
|
-
return this.handleVariantAPIRes(data) as AttributeStruct[];
|
|
162
|
+
return (await this.handleVariantAPIRes(data)) as AttributeStruct[];
|
|
146
163
|
}
|
|
147
164
|
|
|
148
165
|
/**
|
|
@@ -155,7 +172,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
155
172
|
*/
|
|
156
173
|
async createAudience(audience: CreateAudienceInput): Promise<void | AudienceStruct> {
|
|
157
174
|
const data = await this.apiClient.post<AudienceStruct>('/audiences', audience);
|
|
158
|
-
return this.handleVariantAPIRes(data) as AudienceStruct;
|
|
175
|
+
return (await this.handleVariantAPIRes(data)) as AudienceStruct;
|
|
159
176
|
}
|
|
160
177
|
|
|
161
178
|
/**
|
|
@@ -168,7 +185,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
168
185
|
*/
|
|
169
186
|
async createExperience(experience: CreateExperienceInput): Promise<void | ExperienceStruct> {
|
|
170
187
|
const data = await this.apiClient.post<ExperienceStruct>('/experiences', experience);
|
|
171
|
-
return this.handleVariantAPIRes(data) as ExperienceStruct;
|
|
188
|
+
return (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
172
189
|
}
|
|
173
190
|
|
|
174
191
|
/**
|
|
@@ -182,7 +199,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
182
199
|
): Promise<void | CMSExperienceStruct> {
|
|
183
200
|
const updateCTInExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
184
201
|
const data = await this.apiClient.post<CMSExperienceStruct>(updateCTInExpEndPoint, experience);
|
|
185
|
-
return this.handleVariantAPIRes(data) as CMSExperienceStruct;
|
|
202
|
+
return (await this.handleVariantAPIRes(data)) as CMSExperienceStruct;
|
|
186
203
|
}
|
|
187
204
|
|
|
188
205
|
/**
|
|
@@ -193,7 +210,7 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
193
210
|
async getCTsFromExperience(experienceUid: string): Promise<void | CMSExperienceStruct> {
|
|
194
211
|
const getCTFromExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
195
212
|
const data = await this.apiClient.get<CMSExperienceStruct>(getCTFromExpEndPoint);
|
|
196
|
-
return this.handleVariantAPIRes(data) as CMSExperienceStruct;
|
|
213
|
+
return (await this.handleVariantAPIRes(data)) as CMSExperienceStruct;
|
|
197
214
|
}
|
|
198
215
|
|
|
199
216
|
/**
|
|
@@ -202,16 +219,22 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
202
219
|
* @returns The variant API response data.
|
|
203
220
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
204
221
|
*/
|
|
205
|
-
handleVariantAPIRes(res: APIResponse): VariantAPIRes {
|
|
222
|
+
async handleVariantAPIRes(res: APIResponse): Promise<VariantAPIRes> {
|
|
206
223
|
const { status, data } = res;
|
|
207
224
|
|
|
208
225
|
if (status >= 200 && status < 300) {
|
|
209
226
|
return data;
|
|
210
227
|
}
|
|
211
|
-
|
|
228
|
+
|
|
229
|
+
// Refresh the access token if it has expired
|
|
230
|
+
await authenticationHandler.refreshAccessToken(res);
|
|
231
|
+
|
|
212
232
|
const errorMsg = data?.errors
|
|
213
233
|
? formatErrors(data.errors)
|
|
214
|
-
: data?.error ||
|
|
234
|
+
: data?.error ||
|
|
235
|
+
data?.error_message ||
|
|
236
|
+
data?.message ||
|
|
237
|
+
'Something went wrong while processing variant entries request!';
|
|
215
238
|
|
|
216
239
|
throw errorMsg;
|
|
217
240
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
ContentstackClient,
|
|
8
8
|
ContentstackConfig,
|
|
9
9
|
managementSDKClient,
|
|
10
|
+
authenticationHandler,
|
|
10
11
|
} from '@contentstack/cli-utilities';
|
|
11
12
|
|
|
12
13
|
import {
|
|
@@ -37,6 +38,16 @@ export class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> implement
|
|
|
37
38
|
this.apiClient.baseUrl(this.baseURL);
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
async init(): Promise<void> {
|
|
42
|
+
await authenticationHandler.getAuthDetails();
|
|
43
|
+
const token = authenticationHandler.accessToken;
|
|
44
|
+
if (authenticationHandler.isOauthEnabled) {
|
|
45
|
+
this.apiClient.headers({ authorization: token });
|
|
46
|
+
} else {
|
|
47
|
+
this.apiClient.headers({ authtoken: token });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
40
51
|
async variantEntry(options: VariantOptions) {
|
|
41
52
|
// TODO single entry variant
|
|
42
53
|
return { entry: {} };
|
|
@@ -124,7 +135,7 @@ export class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> implement
|
|
|
124
135
|
}
|
|
125
136
|
|
|
126
137
|
const data = await this.apiClient.get(endpoint);
|
|
127
|
-
const response = this.handleVariantAPIRes(data) as { entries: VariantEntryStruct[]; count: number };
|
|
138
|
+
const response = (await this.handleVariantAPIRes(data)) as { entries: VariantEntryStruct[]; count: number };
|
|
128
139
|
|
|
129
140
|
if (callback) {
|
|
130
141
|
callback(response.entries);
|
|
@@ -244,15 +255,18 @@ export class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> implement
|
|
|
244
255
|
* @returns The variant API response data.
|
|
245
256
|
* @throws If the API response status is not within the success range, an error message is thrown.
|
|
246
257
|
*/
|
|
247
|
-
handleVariantAPIRes(
|
|
258
|
+
async handleVariantAPIRes(
|
|
248
259
|
res: APIResponse,
|
|
249
|
-
): VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string | any {
|
|
260
|
+
): Promise<VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string | any> {
|
|
250
261
|
const { status, data } = res;
|
|
251
262
|
|
|
252
263
|
if (status >= 200 && status < 300) {
|
|
253
264
|
return data;
|
|
254
265
|
}
|
|
255
266
|
|
|
267
|
+
// Refresh the access token if the response status is 401
|
|
268
|
+
await authenticationHandler.refreshAccessToken(res);
|
|
269
|
+
|
|
256
270
|
const errorMsg = data?.errors
|
|
257
271
|
? formatErrors(data.errors)
|
|
258
272
|
: data?.error_message || data?.message || 'Something went wrong while processing entry variant request!';
|
|
@@ -290,9 +304,9 @@ export class VariantManagementSDK<T>
|
|
|
290
304
|
return Promise.resolve({} as VariantEntryStruct);
|
|
291
305
|
}
|
|
292
306
|
|
|
293
|
-
handleVariantAPIRes(
|
|
307
|
+
async handleVariantAPIRes(
|
|
294
308
|
res: APIResponse,
|
|
295
|
-
): VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string {
|
|
309
|
+
): Promise<VariantEntryStruct | { entries: VariantEntryStruct[]; count: number } | string> {
|
|
296
310
|
return res.data;
|
|
297
311
|
}
|
|
298
312
|
|