@contentstack/cli-variants 1.0.0 → 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.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 +15 -9
- package/lib/messages/index.js +1 -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 +22 -12
- package/src/messages/index.ts +1 -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
|
@@ -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
|
|