@trycourier/courier-js 3.0.0 → 3.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/dist/index.mjs CHANGED
@@ -1263,35 +1263,6 @@ class InboxClient extends Client {
1263
1263
  return `id_${id.replace(/_/g, "__").replace(/-/g, "_")}`;
1264
1264
  }
1265
1265
  }
1266
- class PreferenceTransformer {
1267
- /**
1268
- * Transforms a single API response item to the CourierUserPreferencesTopic type
1269
- * @param item - The API response item
1270
- * @returns A CourierUserPreferencesTopic object
1271
- */
1272
- transformItem(item) {
1273
- return {
1274
- topicId: item.topic_id,
1275
- topicName: item.topic_name,
1276
- sectionId: item.section_id,
1277
- sectionName: item.section_name,
1278
- status: item.status,
1279
- defaultStatus: item.default_status,
1280
- hasCustomRouting: item.has_custom_routing,
1281
- customRouting: item.custom_routing || []
1282
- };
1283
- }
1284
- /**
1285
- * Transforms an array of API response items to CourierUserPreferencesTopic objects
1286
- * @param items - The API response items
1287
- * @returns A generator of CourierUserPreferencesTopic objects
1288
- */
1289
- *transform(items) {
1290
- for (const item of items) {
1291
- yield this.transformItem(item);
1292
- }
1293
- }
1294
- }
1295
1266
  function decode(clientKey) {
1296
1267
  const binaryString = atob(clientKey);
1297
1268
  const bytes = new Uint8Array(binaryString.length);
@@ -1308,52 +1279,89 @@ function encode(key) {
1308
1279
  return btoa(String.fromCharCode(...bytes));
1309
1280
  }
1310
1281
  class PreferenceClient extends Client {
1311
- constructor() {
1312
- super(...arguments);
1313
- __publicField(this, "transformer", new PreferenceTransformer());
1314
- }
1315
1282
  /**
1316
1283
  * Get all preferences for a user
1317
- * @param paginationCursor - Optional cursor for pagination
1284
+ * @param paginationCursor - Optional cursor for pagination (not used in GraphQL implementation)
1318
1285
  * @returns Promise resolving to user preferences
1319
- * @see https://www.courier.com/docs/api-reference/user-preferences/get-user-preferences
1320
1286
  */
1321
1287
  async getUserPreferences(props) {
1322
- let url = `${this.options.apiUrls.courier.rest}/users/${this.options.userId}/preferences`;
1323
- if (props == null ? void 0 : props.paginationCursor) {
1324
- url += `?cursor=${props.paginationCursor}`;
1325
- }
1326
- const json = await http({
1288
+ var _a, _b;
1289
+ const query = `
1290
+ query GetRecipientPreferences {
1291
+ recipientPreferences${this.options.tenantId ? `(accountId: "${this.options.tenantId}")` : ""} {
1292
+ nodes {
1293
+ templateId
1294
+ templateName
1295
+ sectionId
1296
+ sectionName
1297
+ defaultStatus
1298
+ status
1299
+ hasCustomRouting
1300
+ routingPreferences
1301
+ digestSchedule
1302
+ }
1303
+ }
1304
+ }
1305
+ `;
1306
+ const response = await graphql({
1327
1307
  options: this.options,
1328
- url,
1329
- method: "GET",
1308
+ url: this.options.apiUrls.courier.graphql,
1309
+ query,
1330
1310
  headers: {
1311
+ "x-courier-user-id": this.options.userId,
1312
+ "x-courier-client-key": "empty",
1313
+ // Empty for now. Will be removed in future.
1331
1314
  "Authorization": `Bearer ${this.options.accessToken}`
1332
1315
  }
1333
1316
  });
1334
- const data = json;
1317
+ const nodes = ((_b = (_a = response.data) == null ? void 0 : _a.recipientPreferences) == null ? void 0 : _b.nodes) || [];
1335
1318
  return {
1336
- items: [...this.transformer.transform(data.items)],
1337
- paging: data.paging
1319
+ items: nodes.map((node) => this.transformToTopic(node)),
1320
+ paging: {
1321
+ cursor: props == null ? void 0 : props.paginationCursor,
1322
+ more: false
1323
+ // GraphQL returns all preferences at once
1324
+ }
1338
1325
  };
1339
1326
  }
1340
1327
  /**
1341
1328
  * Get preferences for a specific topic
1342
1329
  * @param topicId - The ID of the topic to get preferences for
1343
1330
  * @returns Promise resolving to topic preferences
1344
- * @see https://www.courier.com/docs/api-reference/user-preferences/get-user-subscription-topic
1345
1331
  */
1346
1332
  async getUserPreferenceTopic(props) {
1347
- const json = await http({
1333
+ var _a;
1334
+ const query = `
1335
+ query GetRecipientPreferenceTopic {
1336
+ recipientPreference(templateId: "${props.topicId}"${this.options.tenantId ? `, accountId: "${this.options.tenantId}"` : ""}) {
1337
+ templateId
1338
+ templateName
1339
+ status
1340
+ hasCustomRouting
1341
+ routingPreferences
1342
+ digestSchedule
1343
+ sectionId
1344
+ sectionName
1345
+ defaultStatus
1346
+ }
1347
+ }
1348
+ `;
1349
+ const response = await graphql({
1348
1350
  options: this.options,
1349
- url: `${this.options.apiUrls.courier.rest}/users/${this.options.userId}/preferences/${props.topicId}`,
1350
- method: "GET",
1351
+ url: this.options.apiUrls.courier.graphql,
1352
+ query,
1351
1353
  headers: {
1354
+ "x-courier-user-id": this.options.userId,
1355
+ "x-courier-client-key": "empty",
1356
+ // Empty for now. Will be removed in future.
1352
1357
  "Authorization": `Bearer ${this.options.accessToken}`
1353
1358
  }
1354
1359
  });
1355
- const res = json;
1356
- return this.transformer.transformItem(res.topic);
1360
+ const node = (_a = response.data) == null ? void 0 : _a.recipientPreference;
1361
+ if (!node) {
1362
+ throw new Error(`Preference topic not found: ${props.topicId}`);
1363
+ }
1364
+ return this.transformToTopic(node);
1357
1365
  }
1358
1366
  /**
1359
1367
  * Update preferences for a specific topic
@@ -1362,24 +1370,31 @@ class PreferenceClient extends Client {
1362
1370
  * @param hasCustomRouting - Whether the topic has custom routing
1363
1371
  * @param customRouting - The custom routing channels for the topic
1364
1372
  * @returns Promise resolving when update is complete
1365
- * @see https://www.courier.com/docs/api-reference/user-preferences/update-or-create-user-preferences-for-subscription-topic
1366
1373
  */
1367
1374
  async putUserPreferenceTopic(props) {
1368
- const payload = {
1369
- topic: {
1370
- status: props.status,
1371
- has_custom_routing: props.hasCustomRouting,
1372
- custom_routing: props.customRouting
1375
+ const routingPreferences = props.customRouting.length > 0 ? `[${props.customRouting.join(", ")}]` : "[]";
1376
+ const query = `
1377
+ mutation UpdateRecipientPreferences {
1378
+ updatePreferences(
1379
+ templateId: "${props.topicId}",
1380
+ preferences: {
1381
+ status: ${props.status},
1382
+ hasCustomRouting: ${props.hasCustomRouting},
1383
+ routingPreferences: ${routingPreferences}
1384
+ }${this.options.tenantId ? `, accountId: "${this.options.tenantId}"` : ""}
1385
+ )
1373
1386
  }
1374
- };
1375
- await http({
1387
+ `;
1388
+ await graphql({
1376
1389
  options: this.options,
1377
- url: `${this.options.apiUrls.courier.rest}/users/${this.options.userId}/preferences/${props.topicId}`,
1378
- method: "PUT",
1390
+ url: this.options.apiUrls.courier.graphql,
1391
+ query,
1379
1392
  headers: {
1393
+ "x-courier-user-id": this.options.userId,
1394
+ "x-courier-client-key": "empty",
1395
+ // Empty for now. Will be removed in future.
1380
1396
  "Authorization": `Bearer ${this.options.accessToken}`
1381
- },
1382
- body: payload
1397
+ }
1383
1398
  });
1384
1399
  }
1385
1400
  /**
@@ -1392,6 +1407,21 @@ class PreferenceClient extends Client {
1392
1407
  const url = encode(`${rootTenantId}#${this.options.userId}${this.options.tenantId ? `#${this.options.tenantId}` : ""}#${false}`);
1393
1408
  return `https://view.notificationcenter.app/p/${url}`;
1394
1409
  }
1410
+ /**
1411
+ * Transform a GraphQL RecipientPreference node to CourierUserPreferencesTopic
1412
+ */
1413
+ transformToTopic(node) {
1414
+ return {
1415
+ topicId: node.templateId,
1416
+ topicName: node.templateName || "",
1417
+ sectionId: node.sectionId || "",
1418
+ sectionName: node.sectionName || "",
1419
+ status: node.status || "UNKNOWN",
1420
+ defaultStatus: node.defaultStatus || "UNKNOWN",
1421
+ hasCustomRouting: node.hasCustomRouting || false,
1422
+ customRouting: node.routingPreferences || []
1423
+ };
1424
+ }
1395
1425
  }
1396
1426
  class TokenClient extends Client {
1397
1427
  /**
@@ -1609,7 +1639,7 @@ __publicField(_CourierClient, "COURIER_JS_NAME", "courier-js");
1609
1639
  * User agent reporting version of the courier-js package.
1610
1640
  * Inlined from package.json at build time.
1611
1641
  */
1612
- __publicField(_CourierClient, "COURIER_JS_VERSION", "3.0.0");
1642
+ __publicField(_CourierClient, "COURIER_JS_VERSION", "3.1.0");
1613
1643
  let CourierClient = _CourierClient;
1614
1644
  class AuthenticationListener {
1615
1645
  constructor(callback) {