@amohamud23/notihub 1.1.51 → 1.1.52

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.cjs CHANGED
@@ -545,43 +545,70 @@ var Notifications = class _Notifications {
545
545
  throw new Error("Could not create notification");
546
546
  }
547
547
  }
548
- // Additional methods can be added here as needed
549
548
  /**
550
549
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
551
550
  * @param notiTypeIds - Array of notiTypeIds to fetch.
552
- * @param limitPerType - Max items to fetch per notiTypeId.
553
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
554
- * @returns An object with merged, sorted notifications and nextPageKeys map.
555
- */
556
- static async getUnifiedFeedByNotiTypeIds(notiTypeIds, limitPerType = 20, exclusiveStartKeys) {
551
+ * @param options - Pagination options
552
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
553
+ */
554
+ static async getUnifiedFeedByNotiTypeIds(notiTypeIds, options = {}) {
555
+ const limit = options.limit || 20;
556
+ const limitPerType = options.limitPerType || limit;
557
+ let exclusiveStartKeys = {};
558
+ if (options.cursor) {
559
+ try {
560
+ exclusiveStartKeys = JSON.parse(
561
+ Buffer.from(options.cursor, "base64").toString("utf-8")
562
+ );
563
+ } catch (error) {
564
+ console.error("Invalid cursor:", error);
565
+ throw new Error("Invalid pagination cursor");
566
+ }
567
+ }
557
568
  const results = await Promise.all(
558
569
  notiTypeIds.map(async (ntid) => {
559
570
  const params = {
560
571
  TableName: this.TABLE_NAME,
561
572
  IndexName: "NotiTypeIdCreatedAtIndex",
562
- // Assuming you have a GSI for notiTypeId
563
573
  KeyConditionExpression: "notiTypeId = :ntid",
564
574
  ExpressionAttributeValues: { ":ntid": ntid },
565
575
  ScanIndexForward: false,
566
576
  Limit: limitPerType,
567
- ExclusiveStartKey: exclusiveStartKeys?.[ntid]
568
- };
569
- const result = await ddbDocClient.send(new import_lib_dynamodb4.QueryCommand(params));
570
- return {
571
- notiTypeId: ntid,
572
- notifications: result.Items || [],
573
- nextPageKey: result.LastEvaluatedKey
577
+ ExclusiveStartKey: exclusiveStartKeys[ntid]
574
578
  };
579
+ try {
580
+ const result = await ddbDocClient.send(new import_lib_dynamodb4.QueryCommand(params));
581
+ return {
582
+ notiTypeId: ntid,
583
+ notifications: result.Items || [],
584
+ nextPageKey: result.LastEvaluatedKey
585
+ };
586
+ } catch (error) {
587
+ console.error(
588
+ `Error fetching notifications for notiTypeId: ${ntid}`,
589
+ error
590
+ );
591
+ return {
592
+ notiTypeId: ntid,
593
+ notifications: [],
594
+ nextPageKey: void 0
595
+ };
596
+ }
575
597
  })
576
598
  );
577
- const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
599
+ const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
578
600
  const nextPageKeys = results.reduce((acc, curr) => {
579
- acc[curr.notiTypeId] = curr.nextPageKey;
601
+ if (curr.nextPageKey) {
602
+ acc[curr.notiTypeId] = curr.nextPageKey;
603
+ }
580
604
  return acc;
581
605
  }, {});
606
+ const hasMore = Object.keys(nextPageKeys).length > 0;
607
+ const cursor = hasMore ? Buffer.from(JSON.stringify(nextPageKeys)).toString("base64") : void 0;
582
608
  return {
583
609
  notifications: mergedNotifications,
584
- nextPageKeys
610
+ cursor,
611
+ hasMore
585
612
  };
586
613
  }
587
614
  };
package/dist/index.d.cts CHANGED
@@ -373,13 +373,17 @@ declare class Notifications {
373
373
  /**
374
374
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
375
375
  * @param notiTypeIds - Array of notiTypeIds to fetch.
376
- * @param limitPerType - Max items to fetch per notiTypeId.
377
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
378
- * @returns An object with merged, sorted notifications and nextPageKeys map.
379
- */
380
- static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], limitPerType?: number, exclusiveStartKeys?: Record<string, Record<string, any>>): Promise<{
376
+ * @param options - Pagination options
377
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
378
+ */
379
+ static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], options?: {
380
+ limit?: number;
381
+ limitPerType?: number;
382
+ cursor?: string;
383
+ }): Promise<{
381
384
  notifications: INotiHubNotification[];
382
- nextPageKeys: Record<string, Record<string, any> | undefined>;
385
+ cursor?: string;
386
+ hasMore: boolean;
383
387
  }>;
384
388
  }
385
389
 
package/dist/index.d.ts CHANGED
@@ -373,13 +373,17 @@ declare class Notifications {
373
373
  /**
374
374
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
375
375
  * @param notiTypeIds - Array of notiTypeIds to fetch.
376
- * @param limitPerType - Max items to fetch per notiTypeId.
377
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
378
- * @returns An object with merged, sorted notifications and nextPageKeys map.
379
- */
380
- static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], limitPerType?: number, exclusiveStartKeys?: Record<string, Record<string, any>>): Promise<{
376
+ * @param options - Pagination options
377
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
378
+ */
379
+ static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], options?: {
380
+ limit?: number;
381
+ limitPerType?: number;
382
+ cursor?: string;
383
+ }): Promise<{
381
384
  notifications: INotiHubNotification[];
382
- nextPageKeys: Record<string, Record<string, any> | undefined>;
385
+ cursor?: string;
386
+ hasMore: boolean;
383
387
  }>;
384
388
  }
385
389
 
package/dist/index.js CHANGED
@@ -522,43 +522,70 @@ var Notifications = class _Notifications {
522
522
  throw new Error("Could not create notification");
523
523
  }
524
524
  }
525
- // Additional methods can be added here as needed
526
525
  /**
527
526
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
528
527
  * @param notiTypeIds - Array of notiTypeIds to fetch.
529
- * @param limitPerType - Max items to fetch per notiTypeId.
530
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
531
- * @returns An object with merged, sorted notifications and nextPageKeys map.
532
- */
533
- static async getUnifiedFeedByNotiTypeIds(notiTypeIds, limitPerType = 20, exclusiveStartKeys) {
528
+ * @param options - Pagination options
529
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
530
+ */
531
+ static async getUnifiedFeedByNotiTypeIds(notiTypeIds, options = {}) {
532
+ const limit = options.limit || 20;
533
+ const limitPerType = options.limitPerType || limit;
534
+ let exclusiveStartKeys = {};
535
+ if (options.cursor) {
536
+ try {
537
+ exclusiveStartKeys = JSON.parse(
538
+ Buffer.from(options.cursor, "base64").toString("utf-8")
539
+ );
540
+ } catch (error) {
541
+ console.error("Invalid cursor:", error);
542
+ throw new Error("Invalid pagination cursor");
543
+ }
544
+ }
534
545
  const results = await Promise.all(
535
546
  notiTypeIds.map(async (ntid) => {
536
547
  const params = {
537
548
  TableName: this.TABLE_NAME,
538
549
  IndexName: "NotiTypeIdCreatedAtIndex",
539
- // Assuming you have a GSI for notiTypeId
540
550
  KeyConditionExpression: "notiTypeId = :ntid",
541
551
  ExpressionAttributeValues: { ":ntid": ntid },
542
552
  ScanIndexForward: false,
543
553
  Limit: limitPerType,
544
- ExclusiveStartKey: exclusiveStartKeys?.[ntid]
545
- };
546
- const result = await ddbDocClient.send(new QueryCommand3(params));
547
- return {
548
- notiTypeId: ntid,
549
- notifications: result.Items || [],
550
- nextPageKey: result.LastEvaluatedKey
554
+ ExclusiveStartKey: exclusiveStartKeys[ntid]
551
555
  };
556
+ try {
557
+ const result = await ddbDocClient.send(new QueryCommand3(params));
558
+ return {
559
+ notiTypeId: ntid,
560
+ notifications: result.Items || [],
561
+ nextPageKey: result.LastEvaluatedKey
562
+ };
563
+ } catch (error) {
564
+ console.error(
565
+ `Error fetching notifications for notiTypeId: ${ntid}`,
566
+ error
567
+ );
568
+ return {
569
+ notiTypeId: ntid,
570
+ notifications: [],
571
+ nextPageKey: void 0
572
+ };
573
+ }
552
574
  })
553
575
  );
554
- const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
576
+ const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
555
577
  const nextPageKeys = results.reduce((acc, curr) => {
556
- acc[curr.notiTypeId] = curr.nextPageKey;
578
+ if (curr.nextPageKey) {
579
+ acc[curr.notiTypeId] = curr.nextPageKey;
580
+ }
557
581
  return acc;
558
582
  }, {});
583
+ const hasMore = Object.keys(nextPageKeys).length > 0;
584
+ const cursor = hasMore ? Buffer.from(JSON.stringify(nextPageKeys)).toString("base64") : void 0;
559
585
  return {
560
586
  notifications: mergedNotifications,
561
- nextPageKeys
587
+ cursor,
588
+ hasMore
562
589
  };
563
590
  }
564
591
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amohamud23/notihub",
3
- "version": "1.1.51",
3
+ "version": "1.1.52",
4
4
  "description": "Notihub Package",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",