@b-jones-rfd/qualtrics-api-tasks 0.1.1 → 0.3.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/CHANGELOG.md +12 -0
- package/README.md +155 -0
- package/dist/index.d.mts +356 -1
- package/dist/index.d.ts +356 -1
- package/dist/index.js +541 -79
- package/dist/index.mjs +530 -79
- package/package.json +1 -1
- package/tests/utils.test.ts +350 -9
package/dist/index.mjs
CHANGED
|
@@ -29,6 +29,228 @@ function getAuthHeaders(apiToken, bearerToken) {
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// src/utils/parsers.ts
|
|
33
|
+
function safeParseBearerToken(response) {
|
|
34
|
+
return response?.access_token && typeof response.access_token === "string" ? success(response.access_token) : failure("Incorrect token format");
|
|
35
|
+
}
|
|
36
|
+
function safeParseTestResponse(response) {
|
|
37
|
+
return response?.result?.userId ? success("Connection successful") : failure("Incorrect test response format");
|
|
38
|
+
}
|
|
39
|
+
function safeParseStartFileExportResponse(response) {
|
|
40
|
+
return "result" in response ? "progressId" in response.result && "percentComplete" in response.result && "status" in response.result ? success(response.result) : failure("Start Export Response invalid format") : "error" in response ? failure(`${response.error.errorCode}: ${response.error.errorMessage}`) : failure(`Unable to parse Start Export Response`);
|
|
41
|
+
}
|
|
42
|
+
function safeParseFileProgressResponse(response) {
|
|
43
|
+
return "result" in response ? "fileId" in response.result && "percentComplete" in response.result && "status" in response.result ? success(response.result) : failure("File Progress Response invalid format") : "error" in response ? failure(`${response.error.errorCode}: ${response.error.errorMessage}`) : failure(`Unable to parse File Progress Response`);
|
|
44
|
+
}
|
|
45
|
+
function safeParseCreateMailingListResponse(response) {
|
|
46
|
+
return "result" in response ? "id" in response.result ? success(response.result.id) : failure(`Id missing in Create Mailing List Response`) : failure(`Unable to parse Create Mailing List Response`);
|
|
47
|
+
}
|
|
48
|
+
function safeParseStartContactsImportResponse(response) {
|
|
49
|
+
return "result" in response ? "id" in response.result && "contacts" in response.result && "tracking" in response.result && "status" in response.result ? success(response.result) : failure("Start contacts import response invalid format") : failure(`Unable to parse Start Contacts Import Response`);
|
|
50
|
+
}
|
|
51
|
+
function safeParseContactsImportSummary(response) {
|
|
52
|
+
return "result" in response ? "percentComplete" in response.result && "contacts" in response.result && "invalidEmails" in response.result && "transactions" in response.result && "status" in response.result ? success(response.result) : failure("Contacts import summary response invalid format") : failure(`Unable to parse Contacts Import Summary Response`);
|
|
53
|
+
}
|
|
54
|
+
function safeParseContactsImportStatus(response) {
|
|
55
|
+
return "result" in response ? "percentComplete" in response.result && "contacts" in response.result && "transactions" in response.result && "status" in response.result ? success(response.result) : failure("Contacts import summary response invalid format") : failure(`Unable to parse Contacts Import Summary Response`);
|
|
56
|
+
}
|
|
57
|
+
function safeParseCreateDistributionResponse(response) {
|
|
58
|
+
return "result" in response ? "id" in response.result ? success(response.result.id) : failure(`Id missing in Create Distribution Response`) : failure(`Unable to parse Create Distribution Response`);
|
|
59
|
+
}
|
|
60
|
+
function safeParseCreateReminderResponse(response) {
|
|
61
|
+
return "result" in response ? "distributionId" in response.result ? success(response.result.distributionId) : failure(`distributionId missing in Create Reminder Response`) : failure(`Unable to parse Create Reminder Response`);
|
|
62
|
+
}
|
|
63
|
+
function safeParseListLibraryMessages(response) {
|
|
64
|
+
return "result" in response ? "elements" in response.result && Array.isArray(response.result.elements) ? success(response.result) : failure(`elements missing in List Library Messages response`) : failure(`Unable to parse List Library Messages response`);
|
|
65
|
+
}
|
|
66
|
+
function safeParseGetDistribution(response) {
|
|
67
|
+
return "result" in response ? "id" in response.result && "stats" in response.result ? success(response.result) : failure(`Properties missing in Get Distribution response`) : failure(`Unable to parse Get Distribution response`);
|
|
68
|
+
}
|
|
69
|
+
function safeParseListDistributions(response) {
|
|
70
|
+
return "result" in response ? "elements" in response.result ? success(response.result) : failure(`elements missing in List Distributions response`) : failure(`Unable to parse List Distribution response`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/qualtrics/index.ts
|
|
74
|
+
async function execute(config) {
|
|
75
|
+
const { datacenterId, route, headers, timeout, body } = config;
|
|
76
|
+
const host = `https://${datacenterId}.qualtrics.com`;
|
|
77
|
+
const resource = new URL(route, host);
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
const options = {
|
|
80
|
+
method: "GET",
|
|
81
|
+
headers,
|
|
82
|
+
signal: controller.signal
|
|
83
|
+
};
|
|
84
|
+
if (body) {
|
|
85
|
+
if (typeof body === "string") {
|
|
86
|
+
headers.append("Content-Type", "application/json");
|
|
87
|
+
}
|
|
88
|
+
options.method = "POST";
|
|
89
|
+
options.headers = headers;
|
|
90
|
+
options.body = body;
|
|
91
|
+
}
|
|
92
|
+
const timer = setTimeout(() => controller.abort(), timeout ?? 30 * 1e3);
|
|
93
|
+
const response = await fetch(resource, options);
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
if (response.ok) {
|
|
96
|
+
const contentType = response.headers.get("content-type");
|
|
97
|
+
switch (contentType) {
|
|
98
|
+
case "application/json":
|
|
99
|
+
return Promise.resolve(await response.json());
|
|
100
|
+
case "application/csv":
|
|
101
|
+
case "application/tsv":
|
|
102
|
+
case "application/xml":
|
|
103
|
+
return Promise.resolve(await response.text());
|
|
104
|
+
default:
|
|
105
|
+
return Promise.resolve(response.body);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
const message = await response.json();
|
|
109
|
+
return Promise.reject(`${response.status}: ${message}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/methods/distributions/createDistribution.ts
|
|
114
|
+
var createDistribution = (connectionOptions) => async ({
|
|
115
|
+
libraryId,
|
|
116
|
+
messageId,
|
|
117
|
+
messageText,
|
|
118
|
+
mailingListId,
|
|
119
|
+
contactId,
|
|
120
|
+
transactionBatchId,
|
|
121
|
+
fromEmail,
|
|
122
|
+
replyToEmail,
|
|
123
|
+
fromName,
|
|
124
|
+
subject,
|
|
125
|
+
surveyId,
|
|
126
|
+
expirationDate,
|
|
127
|
+
type,
|
|
128
|
+
embeddedData,
|
|
129
|
+
sendDate,
|
|
130
|
+
bearerToken = void 0
|
|
131
|
+
}) => {
|
|
132
|
+
const route = `/API/v3/distributions`;
|
|
133
|
+
try {
|
|
134
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
135
|
+
const body = JSON.stringify({
|
|
136
|
+
message: {
|
|
137
|
+
libraryId,
|
|
138
|
+
messageId,
|
|
139
|
+
messageText
|
|
140
|
+
},
|
|
141
|
+
recipients: {
|
|
142
|
+
mailingListId,
|
|
143
|
+
contactId,
|
|
144
|
+
transactionBatchId
|
|
145
|
+
},
|
|
146
|
+
header: {
|
|
147
|
+
fromEmail,
|
|
148
|
+
replyToEmail,
|
|
149
|
+
fromName,
|
|
150
|
+
subject
|
|
151
|
+
},
|
|
152
|
+
surveyLink: {
|
|
153
|
+
surveyId,
|
|
154
|
+
expirationDate,
|
|
155
|
+
type
|
|
156
|
+
},
|
|
157
|
+
embeddedData,
|
|
158
|
+
sendDate
|
|
159
|
+
});
|
|
160
|
+
const config = {
|
|
161
|
+
datacenterId: connectionOptions.datacenterId,
|
|
162
|
+
route,
|
|
163
|
+
headers,
|
|
164
|
+
body,
|
|
165
|
+
timeout: connectionOptions.timeout
|
|
166
|
+
};
|
|
167
|
+
const response = await execute(config);
|
|
168
|
+
const result = safeParseCreateDistributionResponse(response);
|
|
169
|
+
return result;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
const message = getErrorMessage(error);
|
|
172
|
+
return failure(message);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/methods/createMailingList.ts
|
|
177
|
+
var createMailingList = (connectionOptions) => async ({
|
|
178
|
+
directoryId,
|
|
179
|
+
name,
|
|
180
|
+
ownerId,
|
|
181
|
+
prioritizeListMetadata = false,
|
|
182
|
+
bearerToken = void 0
|
|
183
|
+
}) => {
|
|
184
|
+
const route = `/API/v3/directories/${directoryId}/mailinglists`;
|
|
185
|
+
try {
|
|
186
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
187
|
+
const body = JSON.stringify({
|
|
188
|
+
name,
|
|
189
|
+
ownerId,
|
|
190
|
+
prioritizeListMetadata
|
|
191
|
+
});
|
|
192
|
+
const config = {
|
|
193
|
+
datacenterId: connectionOptions.datacenterId,
|
|
194
|
+
route,
|
|
195
|
+
headers,
|
|
196
|
+
body,
|
|
197
|
+
timeout: connectionOptions.timeout
|
|
198
|
+
};
|
|
199
|
+
const response = await execute(config);
|
|
200
|
+
const result = safeParseCreateMailingListResponse(response);
|
|
201
|
+
return result;
|
|
202
|
+
} catch (error) {
|
|
203
|
+
const message = getErrorMessage(error);
|
|
204
|
+
return failure(message);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/methods/distributions/createReminder.ts
|
|
209
|
+
var createReminder = (connectionOptions) => async ({
|
|
210
|
+
distributionId,
|
|
211
|
+
libraryId,
|
|
212
|
+
messageId,
|
|
213
|
+
fromEmail,
|
|
214
|
+
replyToEmail,
|
|
215
|
+
fromName,
|
|
216
|
+
subject,
|
|
217
|
+
embeddedData,
|
|
218
|
+
sendDate,
|
|
219
|
+
bearerToken = void 0
|
|
220
|
+
}) => {
|
|
221
|
+
const route = `/API/v3/distributions/${distributionId}/reminders`;
|
|
222
|
+
try {
|
|
223
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
224
|
+
const body = JSON.stringify({
|
|
225
|
+
message: {
|
|
226
|
+
libraryId,
|
|
227
|
+
messageId
|
|
228
|
+
},
|
|
229
|
+
header: {
|
|
230
|
+
fromEmail,
|
|
231
|
+
replyToEmail,
|
|
232
|
+
fromName,
|
|
233
|
+
subject
|
|
234
|
+
},
|
|
235
|
+
embeddedData,
|
|
236
|
+
sendDate
|
|
237
|
+
});
|
|
238
|
+
const config = {
|
|
239
|
+
datacenterId: connectionOptions.datacenterId,
|
|
240
|
+
route,
|
|
241
|
+
headers,
|
|
242
|
+
body,
|
|
243
|
+
timeout: connectionOptions.timeout
|
|
244
|
+
};
|
|
245
|
+
const response = await execute(config);
|
|
246
|
+
const result = safeParseCreateReminderResponse(response);
|
|
247
|
+
return result;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
const message = getErrorMessage(error);
|
|
250
|
+
return failure(message);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
32
254
|
// src/utils/poll.ts
|
|
33
255
|
async function poll({
|
|
34
256
|
fn,
|
|
@@ -55,7 +277,250 @@ async function poll({
|
|
|
55
277
|
return new Promise(executePoll);
|
|
56
278
|
}
|
|
57
279
|
|
|
58
|
-
// src/methods/
|
|
280
|
+
// src/methods/contacts/startContactsImport.ts
|
|
281
|
+
var startContactsImport = (connectionOptions) => async (options) => {
|
|
282
|
+
const { directoryId, mailingListId, contacts, transactionMeta, bearerToken } = options;
|
|
283
|
+
const route = `/API/v3
|
|
284
|
+
/directories/${directoryId}/mailinglists/${mailingListId}/transactioncontacts`;
|
|
285
|
+
try {
|
|
286
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
287
|
+
const body = JSON.stringify({
|
|
288
|
+
transactionMeta,
|
|
289
|
+
contacts
|
|
290
|
+
});
|
|
291
|
+
const config = {
|
|
292
|
+
datacenterId: connectionOptions.datacenterId,
|
|
293
|
+
route,
|
|
294
|
+
headers,
|
|
295
|
+
body,
|
|
296
|
+
timeout: connectionOptions.timeout
|
|
297
|
+
};
|
|
298
|
+
const response = await execute(config);
|
|
299
|
+
const result = safeParseStartContactsImportResponse(response);
|
|
300
|
+
return result;
|
|
301
|
+
} catch (error) {
|
|
302
|
+
const message = getErrorMessage(error);
|
|
303
|
+
return failure(message);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// src/methods/contacts/getContactsImportStatus.ts
|
|
308
|
+
var getContactsImportStatus = (connectionOptions) => async ({ directoryId, importId, mailingListId, bearerToken = void 0 }) => {
|
|
309
|
+
const route = `/API/v3
|
|
310
|
+
/directories/${directoryId}/mailinglists/${mailingListId}/transactioncontacts/${importId}`;
|
|
311
|
+
try {
|
|
312
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
313
|
+
const config = {
|
|
314
|
+
datacenterId: connectionOptions.datacenterId,
|
|
315
|
+
route,
|
|
316
|
+
headers,
|
|
317
|
+
timeout: connectionOptions.timeout
|
|
318
|
+
};
|
|
319
|
+
const response = await execute(config);
|
|
320
|
+
const result = safeParseContactsImportStatus(response);
|
|
321
|
+
return result;
|
|
322
|
+
} catch (error) {
|
|
323
|
+
const message = getErrorMessage(error);
|
|
324
|
+
return failure(message);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// src/methods/contacts/getContactsImportSummary.ts
|
|
329
|
+
var getContactsImportSummary = (connectionOptions) => async ({ directoryId, importId, mailingListId, bearerToken = void 0 }) => {
|
|
330
|
+
const route = `/API/v3
|
|
331
|
+
/directories/${directoryId}/mailinglists/${mailingListId}/transactioncontacts/${importId}/summary`;
|
|
332
|
+
try {
|
|
333
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
334
|
+
const config = {
|
|
335
|
+
datacenterId: connectionOptions.datacenterId,
|
|
336
|
+
route,
|
|
337
|
+
headers,
|
|
338
|
+
timeout: connectionOptions.timeout
|
|
339
|
+
};
|
|
340
|
+
const response = await execute(config);
|
|
341
|
+
const result = safeParseContactsImportSummary(response);
|
|
342
|
+
return result;
|
|
343
|
+
} catch (error) {
|
|
344
|
+
const message = getErrorMessage(error);
|
|
345
|
+
return failure(message);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// src/methods/contacts/importContacts.ts
|
|
350
|
+
var importContacts = (connectionOptions) => async (importOptions) => {
|
|
351
|
+
try {
|
|
352
|
+
const startImportAction = startContactsImport(connectionOptions);
|
|
353
|
+
const startResponse = await startImportAction(importOptions);
|
|
354
|
+
if (!startResponse.success)
|
|
355
|
+
return failure(startResponse.error);
|
|
356
|
+
const importProgressAction = getContactsImportStatus(connectionOptions);
|
|
357
|
+
const progressResponse = await poll({
|
|
358
|
+
fn: async () => await importProgressAction({
|
|
359
|
+
directoryId: importOptions.directoryId,
|
|
360
|
+
importId: startResponse.data.id,
|
|
361
|
+
mailingListId: importOptions.mailingListId,
|
|
362
|
+
bearerToken: importOptions.bearerToken
|
|
363
|
+
}),
|
|
364
|
+
validate: (res) => res.success && res.data.percentComplete === 100,
|
|
365
|
+
interval: 1,
|
|
366
|
+
maxAttempts: 60
|
|
367
|
+
});
|
|
368
|
+
if (!progressResponse.success)
|
|
369
|
+
return failure(progressResponse.error);
|
|
370
|
+
const getContactImportSummaryAction = getContactsImportSummary(connectionOptions);
|
|
371
|
+
const fileResponse = await getContactImportSummaryAction({
|
|
372
|
+
directoryId: importOptions.directoryId,
|
|
373
|
+
importId: startResponse.data.id,
|
|
374
|
+
mailingListId: importOptions.mailingListId,
|
|
375
|
+
bearerToken: importOptions.bearerToken
|
|
376
|
+
});
|
|
377
|
+
return fileResponse;
|
|
378
|
+
} catch (error) {
|
|
379
|
+
const message = getErrorMessage(error);
|
|
380
|
+
return failure(message);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// src/methods/distributions/distributeSurveys.ts
|
|
385
|
+
var distributeSurveys = (connectionOptions) => async (options) => {
|
|
386
|
+
try {
|
|
387
|
+
const createMailingListAction = createMailingList(connectionOptions);
|
|
388
|
+
const startResponse = await createMailingListAction({
|
|
389
|
+
directoryId: options.directoryId,
|
|
390
|
+
name: options.mailingListName,
|
|
391
|
+
ownerId: options.ownerId,
|
|
392
|
+
prioritizeListMetadata: options.prioritizeListMetadata,
|
|
393
|
+
bearerToken: options.bearerToken
|
|
394
|
+
});
|
|
395
|
+
if (!startResponse.success)
|
|
396
|
+
return failure(startResponse.error);
|
|
397
|
+
const importContactsAction = importContacts(connectionOptions);
|
|
398
|
+
const importResponse = await importContactsAction({
|
|
399
|
+
directoryId: options.directoryId,
|
|
400
|
+
mailingListId: startResponse.data,
|
|
401
|
+
contacts: options.contacts,
|
|
402
|
+
transactionMeta: options.transactionMeta,
|
|
403
|
+
bearerToken: options.bearerToken
|
|
404
|
+
});
|
|
405
|
+
if (!importResponse.success)
|
|
406
|
+
return failure(importResponse.error);
|
|
407
|
+
const createDistributionAction = createDistribution(connectionOptions);
|
|
408
|
+
const distributionResponse = await createDistributionAction({
|
|
409
|
+
libraryId: options.libraryId,
|
|
410
|
+
messageId: options.distributionMessageId,
|
|
411
|
+
messageText: options.distributionMessageText,
|
|
412
|
+
mailingListId: startResponse.data,
|
|
413
|
+
fromEmail: options.fromEmail,
|
|
414
|
+
fromName: options.fromName,
|
|
415
|
+
replyToEmail: options.replyToEmail,
|
|
416
|
+
subject: options.subject,
|
|
417
|
+
surveyId: options.surveyId,
|
|
418
|
+
expirationDate: options.expirationDate,
|
|
419
|
+
type: options.type,
|
|
420
|
+
embeddedData: options.embeddedData,
|
|
421
|
+
sendDate: options.sendDate,
|
|
422
|
+
bearerToken: options.bearerToken
|
|
423
|
+
});
|
|
424
|
+
if (!distributionResponse.success)
|
|
425
|
+
return failure(distributionResponse.error);
|
|
426
|
+
const createReminderAction = createReminder(connectionOptions);
|
|
427
|
+
const reminderResponse = await createReminderAction({
|
|
428
|
+
distributionId: distributionResponse.data,
|
|
429
|
+
libraryId: options.libraryId,
|
|
430
|
+
messageId: options.reminderMessageId,
|
|
431
|
+
fromEmail: options.fromEmail,
|
|
432
|
+
fromName: options.fromName,
|
|
433
|
+
replyToEmail: options.replyToEmail,
|
|
434
|
+
subject: options.subject,
|
|
435
|
+
embeddedData: options.embeddedData,
|
|
436
|
+
sendDate: options.sendDate,
|
|
437
|
+
bearerToken: options.bearerToken
|
|
438
|
+
});
|
|
439
|
+
if (!reminderResponse.success)
|
|
440
|
+
return failure(reminderResponse.error);
|
|
441
|
+
return success(reminderResponse.data);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
const message = getErrorMessage(error);
|
|
444
|
+
return failure(message);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// src/methods/responses/startResponseExport.ts
|
|
449
|
+
var startResponseExport = (connectionOptions) => async ({
|
|
450
|
+
surveyId,
|
|
451
|
+
startDate,
|
|
452
|
+
endDate,
|
|
453
|
+
format = "csv",
|
|
454
|
+
bearerToken = void 0,
|
|
455
|
+
...rest
|
|
456
|
+
}) => {
|
|
457
|
+
const route = `/API/v3/surveys/${surveyId}/export-responses`;
|
|
458
|
+
try {
|
|
459
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
460
|
+
const body = JSON.stringify({
|
|
461
|
+
startDate: "2024-03-01T06:00:00Z",
|
|
462
|
+
//startDate.toISOString(),
|
|
463
|
+
endDate: "2024-03-05T06:00:00Z",
|
|
464
|
+
//endDate.toISOString(),
|
|
465
|
+
format,
|
|
466
|
+
...rest
|
|
467
|
+
});
|
|
468
|
+
const config = {
|
|
469
|
+
datacenterId: connectionOptions.datacenterId,
|
|
470
|
+
route,
|
|
471
|
+
headers,
|
|
472
|
+
body,
|
|
473
|
+
timeout: connectionOptions.timeout
|
|
474
|
+
};
|
|
475
|
+
const response = await execute(config);
|
|
476
|
+
const result = safeParseStartFileExportResponse(response);
|
|
477
|
+
return result;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
const message = getErrorMessage(error);
|
|
480
|
+
return failure(message);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
// src/methods/responses/getResponseExportFile.ts
|
|
485
|
+
var getResponseExportFile = (connectionOptions) => async ({ surveyId, fileId, bearerToken }) => {
|
|
486
|
+
const route = `/API/v3/surveys/${surveyId}/export-responses/${fileId}/file`;
|
|
487
|
+
try {
|
|
488
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
489
|
+
const config = {
|
|
490
|
+
datacenterId: connectionOptions.datacenterId,
|
|
491
|
+
route,
|
|
492
|
+
headers,
|
|
493
|
+
timeout: connectionOptions.timeout
|
|
494
|
+
};
|
|
495
|
+
const response = await execute(config);
|
|
496
|
+
return response;
|
|
497
|
+
} catch (error) {
|
|
498
|
+
const message = getErrorMessage(error);
|
|
499
|
+
return failure(message);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// src/methods/responses/getResponseExportProgress.ts
|
|
504
|
+
var getResponseExportProgress = (connectionOptions) => async ({ exportProgressId, surveyId, bearerToken }) => {
|
|
505
|
+
const route = `/API/v3/surveys/${surveyId}/export-responses/${exportProgressId}`;
|
|
506
|
+
try {
|
|
507
|
+
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
508
|
+
const config = {
|
|
509
|
+
datacenterId: connectionOptions.datacenterId,
|
|
510
|
+
route,
|
|
511
|
+
headers,
|
|
512
|
+
timeout: connectionOptions.timeout
|
|
513
|
+
};
|
|
514
|
+
const response = await execute(config);
|
|
515
|
+
const result = safeParseFileProgressResponse(response);
|
|
516
|
+
return result;
|
|
517
|
+
} catch (error) {
|
|
518
|
+
const message = getErrorMessage(error);
|
|
519
|
+
return failure(message);
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
// src/methods/responses/exportResponses.ts
|
|
59
524
|
var exportResponses = (connectionOptions) => async (responseOptions) => {
|
|
60
525
|
try {
|
|
61
526
|
const startResponseAction = startResponseExport(connectionOptions);
|
|
@@ -88,55 +553,6 @@ var exportResponses = (connectionOptions) => async (responseOptions) => {
|
|
|
88
553
|
}
|
|
89
554
|
};
|
|
90
555
|
|
|
91
|
-
// src/utils/parsers.ts
|
|
92
|
-
function safeParseBearerToken(response) {
|
|
93
|
-
return response?.access_token && typeof response.access_token === "string" ? success(response.access_token) : failure("Incorrect token format");
|
|
94
|
-
}
|
|
95
|
-
function safeParseTestResponse(response) {
|
|
96
|
-
return response?.result?.userId ? success("Connection successful") : failure("Incorrect test response format");
|
|
97
|
-
}
|
|
98
|
-
function safeParseStartFileExportResponse(response) {
|
|
99
|
-
return "result" in response ? "progressId" in response.result && "percentComplete" in response.result && "status" in response.result ? success(response.result) : failure("Start Export Response invalid format") : "error" in response ? failure(`${response.error.errorCode}: ${response.error.errorMessage}`) : failure(`Unable to parse Start Export Response`);
|
|
100
|
-
}
|
|
101
|
-
function safeParseFileProgressResponse(response) {
|
|
102
|
-
return "result" in response ? "fileId" in response.result && "percentComplete" in response.result && "status" in response.result ? success(response.result) : failure("File Progress Response invalid format") : "error" in response ? failure(`${response.error.errorCode}: ${response.error.errorMessage}`) : failure(`Unable to parse File Progress Response`);
|
|
103
|
-
}
|
|
104
|
-
function safeParseFileResponse(response) {
|
|
105
|
-
return Buffer.isBuffer(response) ? success(response) : failure("Incorrect file response format");
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// src/qualtrics/index.ts
|
|
109
|
-
async function execute(config) {
|
|
110
|
-
const { datacenterId, route, headers, timeout, body } = config;
|
|
111
|
-
const host = `https://${datacenterId}.qualtrics.com`;
|
|
112
|
-
const resource = new URL(route, host);
|
|
113
|
-
const controller = new AbortController();
|
|
114
|
-
const options = {
|
|
115
|
-
method: "GET",
|
|
116
|
-
headers,
|
|
117
|
-
signal: controller.signal
|
|
118
|
-
};
|
|
119
|
-
if (body) {
|
|
120
|
-
if (typeof body === "string") {
|
|
121
|
-
headers.append("Content-Type", "application/json");
|
|
122
|
-
headers.append("Accept", "application/json");
|
|
123
|
-
}
|
|
124
|
-
options.method = "POST";
|
|
125
|
-
options.headers = headers;
|
|
126
|
-
options.body = body;
|
|
127
|
-
}
|
|
128
|
-
const timer = setTimeout(() => controller.abort(), timeout ?? 30 * 1e3);
|
|
129
|
-
const response = await fetch(resource, options);
|
|
130
|
-
clearTimeout(timer);
|
|
131
|
-
if (response.ok) {
|
|
132
|
-
const data = await response.json();
|
|
133
|
-
return Promise.resolve(data);
|
|
134
|
-
} else {
|
|
135
|
-
const message = await response.json();
|
|
136
|
-
return Promise.reject(`${response.status}: ${message}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
556
|
// src/methods/getBearerToken.ts
|
|
141
557
|
var getBearerToken = (connectionOptions) => async ({ clientId, clientSecret, scope }) => {
|
|
142
558
|
const route = `/oauth2/token`;
|
|
@@ -163,11 +579,12 @@ var getBearerToken = (connectionOptions) => async ({ clientId, clientSecret, sco
|
|
|
163
579
|
}
|
|
164
580
|
};
|
|
165
581
|
|
|
166
|
-
// src/methods/
|
|
167
|
-
var
|
|
168
|
-
const route = `/surveys/${surveyId}/export-responses/${exportProgressId}`;
|
|
582
|
+
// src/methods/distributions/getDistribution.ts
|
|
583
|
+
var getDistribution = (connectionOptions) => async ({ distributionId, surveyId, bearerToken = void 0 }) => {
|
|
169
584
|
try {
|
|
170
585
|
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
586
|
+
const qs = new URLSearchParams({ surveyId });
|
|
587
|
+
const route = `/API/v3/distributions/${distributionId}?${qs.toString()}`;
|
|
171
588
|
const config = {
|
|
172
589
|
datacenterId: connectionOptions.datacenterId,
|
|
173
590
|
route,
|
|
@@ -175,7 +592,7 @@ var getResponseExportProgress = (connectionOptions) => async ({ exportProgressId
|
|
|
175
592
|
timeout: connectionOptions.timeout
|
|
176
593
|
};
|
|
177
594
|
const response = await execute(config);
|
|
178
|
-
const result =
|
|
595
|
+
const result = safeParseGetDistribution(response);
|
|
179
596
|
return result;
|
|
180
597
|
} catch (error) {
|
|
181
598
|
const message = getErrorMessage(error);
|
|
@@ -183,11 +600,34 @@ var getResponseExportProgress = (connectionOptions) => async ({ exportProgressId
|
|
|
183
600
|
}
|
|
184
601
|
};
|
|
185
602
|
|
|
186
|
-
// src/methods/
|
|
187
|
-
var
|
|
188
|
-
|
|
603
|
+
// src/methods/distributions/listDistributions.ts
|
|
604
|
+
var listDistributions = (connectionOptions) => async ({
|
|
605
|
+
surveyId,
|
|
606
|
+
distributionRequestType,
|
|
607
|
+
mailingListId,
|
|
608
|
+
sendStartDate,
|
|
609
|
+
sendEndDate,
|
|
610
|
+
skipToken,
|
|
611
|
+
useNewPaginationScheme,
|
|
612
|
+
pageSize,
|
|
613
|
+
bearerToken = void 0
|
|
614
|
+
}) => {
|
|
189
615
|
try {
|
|
190
616
|
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
617
|
+
const qs = new URLSearchParams({
|
|
618
|
+
surveyId,
|
|
619
|
+
distributionRequestType,
|
|
620
|
+
mailingListId,
|
|
621
|
+
sendStartDate: sendStartDate.toISOString(),
|
|
622
|
+
sendEndDate: sendEndDate.toISOString()
|
|
623
|
+
});
|
|
624
|
+
if (skipToken)
|
|
625
|
+
qs.append("skipToken", skipToken);
|
|
626
|
+
if (useNewPaginationScheme)
|
|
627
|
+
qs.append("useNewPaginationScheme", useNewPaginationScheme.toString());
|
|
628
|
+
if (pageSize)
|
|
629
|
+
qs.append("pageSize", pageSize.toString());
|
|
630
|
+
const route = `/API/v3/distributions?${qs.toString()}`;
|
|
191
631
|
const config = {
|
|
192
632
|
datacenterId: connectionOptions.datacenterId,
|
|
193
633
|
route,
|
|
@@ -195,7 +635,7 @@ var getResponseExportFile = (connectionOptions) => async ({ surveyId, fileId, be
|
|
|
195
635
|
timeout: connectionOptions.timeout
|
|
196
636
|
};
|
|
197
637
|
const response = await execute(config);
|
|
198
|
-
const result =
|
|
638
|
+
const result = safeParseListDistributions(response);
|
|
199
639
|
return result;
|
|
200
640
|
} catch (error) {
|
|
201
641
|
const message = getErrorMessage(error);
|
|
@@ -203,35 +643,24 @@ var getResponseExportFile = (connectionOptions) => async ({ surveyId, fileId, be
|
|
|
203
643
|
}
|
|
204
644
|
};
|
|
205
645
|
|
|
206
|
-
// src/methods/
|
|
207
|
-
var
|
|
208
|
-
|
|
209
|
-
startDate,
|
|
210
|
-
endDate,
|
|
211
|
-
format = "csv",
|
|
212
|
-
bearerToken = void 0,
|
|
213
|
-
...rest
|
|
214
|
-
}) => {
|
|
215
|
-
const route = `/surveys/${surveyId}/export-responses`;
|
|
646
|
+
// src/methods/listLibraryMessages.ts
|
|
647
|
+
var listLibraryMessages = (connectionOptions) => async ({ libraryId, category, offset, bearerToken = void 0 }) => {
|
|
648
|
+
const route = `/API/v3/libraries/${libraryId}/messages`;
|
|
216
649
|
try {
|
|
217
650
|
const headers = getAuthHeaders(connectionOptions.apiToken, bearerToken);
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
format,
|
|
224
|
-
...rest
|
|
225
|
-
});
|
|
651
|
+
const qs = new URLSearchParams();
|
|
652
|
+
if (category)
|
|
653
|
+
qs.append("category", category);
|
|
654
|
+
if (offset)
|
|
655
|
+
qs.append("offset", offset.toString());
|
|
226
656
|
const config = {
|
|
227
657
|
datacenterId: connectionOptions.datacenterId,
|
|
228
|
-
route,
|
|
658
|
+
route: qs.size > 0 ? route + "?" + qs.toString() : route,
|
|
229
659
|
headers,
|
|
230
|
-
body,
|
|
231
660
|
timeout: connectionOptions.timeout
|
|
232
661
|
};
|
|
233
662
|
const response = await execute(config);
|
|
234
|
-
const result =
|
|
663
|
+
const result = safeParseListLibraryMessages(response);
|
|
235
664
|
return result;
|
|
236
665
|
} catch (error) {
|
|
237
666
|
const message = getErrorMessage(error);
|
|
@@ -261,19 +690,41 @@ var testConnection = (connectionOptions) => async (bearerToken) => {
|
|
|
261
690
|
|
|
262
691
|
// src/createConnection.ts
|
|
263
692
|
var createConnection = (options) => ({
|
|
693
|
+
createDistribution: createDistribution(options),
|
|
694
|
+
createMailingList: createMailingList(options),
|
|
695
|
+
createReminder: createReminder(options),
|
|
696
|
+
distributeSurveys: distributeSurveys(options),
|
|
264
697
|
exportResponses: exportResponses(options),
|
|
265
698
|
getBearerToken: getBearerToken(options),
|
|
699
|
+
getContactsImportStatus: getContactsImportStatus(options),
|
|
700
|
+
getContactsImportSummary: getContactsImportSummary(options),
|
|
701
|
+
getDistribution: getDistribution(options),
|
|
266
702
|
getResponseExportFile: getResponseExportFile(options),
|
|
267
703
|
getResponseExportProgress: getResponseExportProgress(options),
|
|
704
|
+
importContacts: importContacts(options),
|
|
705
|
+
listDistributions: listDistributions(options),
|
|
706
|
+
listLibraryMessages: listLibraryMessages(options),
|
|
707
|
+
startContactsImport: startContactsImport(options),
|
|
268
708
|
startResponseExport: startResponseExport(options),
|
|
269
709
|
testConnection: testConnection(options)
|
|
270
710
|
});
|
|
271
711
|
export {
|
|
272
712
|
createConnection,
|
|
713
|
+
createDistribution,
|
|
714
|
+
createMailingList,
|
|
715
|
+
createReminder,
|
|
716
|
+
distributeSurveys,
|
|
273
717
|
exportResponses,
|
|
274
718
|
getBearerToken,
|
|
719
|
+
getContactsImportStatus,
|
|
720
|
+
getContactsImportSummary,
|
|
721
|
+
getDistribution,
|
|
275
722
|
getResponseExportFile,
|
|
276
723
|
getResponseExportProgress,
|
|
724
|
+
importContacts,
|
|
725
|
+
listDistributions,
|
|
726
|
+
listLibraryMessages,
|
|
727
|
+
startContactsImport,
|
|
277
728
|
startResponseExport,
|
|
278
729
|
testConnection
|
|
279
730
|
};
|