Blame · Line-by-line history
notifications.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 9989d86 | 1 | import { fetchJSON, postJSON } from './client'; |
| 2 | ||
| 3 | export type NotificationType = | |
| 4 | | 'issue_opened' | |
| 5 | | 'issue_closed' | |
| 6 | | 'issue_comment' | |
| 7 | | 'pr_opened' | |
| 8 | | 'pr_merged' | |
| 9 | | 'pr_closed' | |
| 10 | | 'pr_comment' | |
| 11 | | 'pr_review' | |
| 12 | | 'push' | |
| 13 | | 'star' | |
| 14 | | 'fork' | |
| 15 | | 'gate_failed' | |
| 16 | | 'gate_passed' | |
| 17 | | 'mention'; | |
| 18 | ||
| 19 | export interface Notification { | |
| 20 | id: string; | |
| 21 | userId: string; | |
| 22 | type: NotificationType; | |
| 23 | title: string; | |
| 24 | body: string | null; | |
| 25 | repoOwner: string | null; | |
| 26 | repoName: string | null; | |
| 27 | resourceUrl: string | null; | |
| 28 | isRead: boolean; | |
| 29 | createdAt: string; | |
| 30 | } | |
| 31 | ||
| 32 | export interface NotificationCounts { | |
| 33 | total: number; | |
| 34 | unread: number; | |
| 35 | } | |
| 36 | ||
| 37 | /** List notifications for the authenticated user. */ | |
| 38 | export async function listNotifications( | |
| 39 | filter: 'unread' | 'all' = 'unread', | |
| 40 | page = 1, | |
| 41 | ): Promise<Notification[]> { | |
| 42 | return fetchJSON<Notification[]>( | |
| 43 | `/notifications?filter=${filter}&page=${page}&json=1`, | |
| 44 | ); | |
| 45 | } | |
| 46 | ||
| 47 | /** Get the unread notification count. */ | |
| 48 | export async function getNotificationCounts(): Promise<NotificationCounts> { | |
| 49 | return fetchJSON<NotificationCounts>('/notifications/counts?json=1'); | |
| 50 | } | |
| 51 | ||
| 52 | /** Mark a single notification as read. */ | |
| 53 | export async function markNotificationRead(id: string): Promise<void> { | |
| 54 | await postJSON(`/notifications/${encodeURIComponent(id)}/read`, {}); | |
| 55 | } | |
| 56 | ||
| 57 | /** Mark all notifications as read. */ | |
| 58 | export async function markAllRead(): Promise<void> { | |
| 59 | await postJSON('/notifications/mark-all-read', {}); | |
| 60 | } | |
| 61 | ||
| 62 | /** Delete a notification. */ | |
| 63 | export async function deleteNotification(id: string): Promise<void> { | |
| 64 | await postJSON(`/notifications/${encodeURIComponent(id)}/delete`, {}); | |
| 65 | } | |
| 66 | ||
| 67 | /** Clear all read notifications. */ | |
| 68 | export async function clearReadNotifications(): Promise<void> { | |
| 69 | await postJSON('/notifications/clear-read', {}); | |
| 70 | } |