rettiwt-api 4.0.0 → 4.1.0-alpha.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.
Files changed (48) hide show
  1. package/.github/workflows/publish-alpha.yml +29 -0
  2. package/dist/collections/Extractors.d.ts +3 -1
  3. package/dist/collections/Extractors.js +3 -0
  4. package/dist/collections/Extractors.js.map +1 -1
  5. package/dist/collections/Groups.js +1 -0
  6. package/dist/collections/Groups.js.map +1 -1
  7. package/dist/collections/Requests.js +1 -0
  8. package/dist/collections/Requests.js.map +1 -1
  9. package/dist/enums/Data.d.ts +1 -0
  10. package/dist/enums/Data.js +1 -0
  11. package/dist/enums/Data.js.map +1 -1
  12. package/dist/enums/Resource.d.ts +1 -0
  13. package/dist/enums/Resource.js +1 -0
  14. package/dist/enums/Resource.js.map +1 -1
  15. package/dist/helper/JsonUtils.js +1 -1
  16. package/dist/helper/JsonUtils.js.map +1 -1
  17. package/dist/models/args/FetchArgs.js +13 -1
  18. package/dist/models/args/FetchArgs.js.map +1 -1
  19. package/dist/models/data/CursoredData.d.ts +3 -3
  20. package/dist/models/data/CursoredData.js +11 -3
  21. package/dist/models/data/CursoredData.js.map +1 -1
  22. package/dist/models/data/Notification.d.ts +46 -0
  23. package/dist/models/data/Notification.js +69 -0
  24. package/dist/models/data/Notification.js.map +1 -0
  25. package/dist/models/data/Tweet.d.ts +3 -3
  26. package/dist/models/data/Tweet.js.map +1 -1
  27. package/dist/models/data/User.d.ts +3 -3
  28. package/dist/models/data/User.js.map +1 -1
  29. package/dist/services/public/TweetService.d.ts +7 -3
  30. package/dist/services/public/TweetService.js +7 -3
  31. package/dist/services/public/TweetService.js.map +1 -1
  32. package/dist/services/public/UserService.d.ts +34 -1
  33. package/dist/services/public/UserService.js +97 -1
  34. package/dist/services/public/UserService.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/collections/Extractors.ts +4 -0
  37. package/src/collections/Groups.ts +1 -0
  38. package/src/collections/Requests.ts +1 -0
  39. package/src/enums/Data.ts +1 -0
  40. package/src/enums/Resource.ts +1 -0
  41. package/src/helper/JsonUtils.ts +1 -1
  42. package/src/models/args/FetchArgs.ts +13 -1
  43. package/src/models/data/CursoredData.ts +11 -7
  44. package/src/models/data/Notification.ts +91 -0
  45. package/src/models/data/Tweet.ts +2 -3
  46. package/src/models/data/User.ts +3 -3
  47. package/src/services/public/TweetService.ts +7 -3
  48. package/src/services/public/UserService.ts +75 -1
@@ -7,6 +7,7 @@ import {
7
7
  IUserHighlightsResponse,
8
8
  IUserLikesResponse,
9
9
  IUserMediaResponse,
10
+ IUserNotifications as IUserNotificationsResponse,
10
11
  IUserRecommendedResponse,
11
12
  IUserSubscriptionsResponse,
12
13
  IUserTweetsAndRepliesResponse,
@@ -17,6 +18,7 @@ import {
17
18
  import { extractors } from '../../collections/Extractors';
18
19
  import { EResourceType } from '../../enums/Resource';
19
20
  import { CursoredData } from '../../models/data/CursoredData';
21
+ import { Notification } from '../../models/data/Notification';
20
22
  import { Tweet } from '../../models/data/Tweet';
21
23
  import { User } from '../../models/data/User';
22
24
  import { IRettiwtConfig } from '../../types/RettiwtConfig';
@@ -350,7 +352,7 @@ export class UserService extends FetcherService {
350
352
  }
351
353
 
352
354
  /**
353
- * Get the media timeline of a user
355
+ * Get the media timeline of a user.
354
356
  *
355
357
  * @param id - The id of the target user.
356
358
  * @param count - The number of media to fetch, must be \<= 100.
@@ -391,6 +393,78 @@ export class UserService extends FetcherService {
391
393
  return data;
392
394
  }
393
395
 
396
+ /**
397
+ * Stream notifications of the logged in user in pseudo real-time.
398
+ *
399
+ * @param pollingInterval - The interval in milliseconds to poll for new tweets. Default interval is 60000 ms.
400
+ *
401
+ * @returns An async generator that yields new notifications as they are received.
402
+ *
403
+ * @example
404
+ * ```
405
+ * import { Rettiwt } from 'rettiwt-api';
406
+ *
407
+ * // Creating a new Rettiwt instance using the given 'API_KEY'
408
+ * const rettiwt = new Rettiwt({ apiKey: API_KEY });
409
+ *
410
+ * // Creating a function that streams all new notifications
411
+ * async function streamNotifications() {
412
+ * try {
413
+ * // Awaiting for the notifications returned by the AsyncGenerator returned by the method
414
+ * for await (const notification of rettiwt.user.notifications(1000)) {
415
+ * console.log(notification.message);
416
+ * }
417
+ * }
418
+ * catch (err) {
419
+ * console.log(err);
420
+ * }
421
+ * }
422
+ *
423
+ * // Calling the function
424
+ * streamNotifications();
425
+ * ```
426
+ */
427
+ public async *notifications(pollingInterval: number = 60000): AsyncGenerator<Notification> {
428
+ const resource = EResourceType.USER_NOTIFICATIONS;
429
+
430
+ /** Whether it's the first batch of notifications or not. */
431
+ let first: boolean = true;
432
+
433
+ /** The cursor to the last notification received. */
434
+ let cursor: string | undefined = undefined;
435
+
436
+ while (true) {
437
+ // Pause execution for the specified polling interval before proceeding to the next iteration
438
+ await new Promise((resolve) => setTimeout(resolve, pollingInterval));
439
+
440
+ // Get the batch of notifications after the given cursor
441
+ const response = await this.request<IUserNotificationsResponse>(resource, {
442
+ count: 40,
443
+ cursor: cursor,
444
+ });
445
+
446
+ // Deserializing response
447
+ const notifications = extractors[resource](response);
448
+
449
+ // Sorting the notifications by time, from oldest to recent
450
+ notifications.list.sort((a, b) => new Date(a.receivedAt).valueOf() - new Date(b.receivedAt).valueOf());
451
+
452
+ // If not first batch, return new notifications
453
+ if (!first) {
454
+ // Yield the notifications
455
+ for (const notification of notifications.list) {
456
+ yield notification;
457
+ }
458
+ }
459
+ // Else do nothing, do nothing since first batch is notifications that have already been received
460
+ else {
461
+ first = false;
462
+ }
463
+
464
+ cursor = notifications.next.value;
465
+ }
466
+ }
467
+
394
468
  /**
395
469
  * Get the recommended feed of the logged in user.
396
470
  *