UNPKG

@magicbell/core

Version:
56 lines (48 loc) 1.4 kB
import { postAPI } from '../../lib/ajax.js'; import RemoteRepository from '../../repository/RemoteRepository.js'; import IRemoteNotification from './IRemoteNotification.js'; import { NewNotification } from './NewNotification.js'; /** * Class to interact with the notification API endpoints. * * @example * const repo = new NotificationRepository(); * const notifications = repo.findBy({ unseen: true }); */ export default class NotificationRepository extends RemoteRepository<IRemoteNotification, NewNotification> { constructor(remotePathOrUrl = '/notifications') { super(remotePathOrUrl); } /** * Mark a notification as read in the API server. */ markAsRead(id: string): Promise<boolean> { const url = `${this.remotePathOrUrl}/${id}/read`; return postAPI(url) .then(() => true) .catch(() => false); } /** * Mark a notification as unread in the API server. */ markAsUnread(id: string): Promise<boolean> { const url = `${this.remotePathOrUrl}/${id}/unread`; return postAPI(url) .then(() => true) .catch(() => false); } /** * Mark all notifications as seen. */ markAllAsSeen() { const url = `${this.remotePathOrUrl}/seen`; return postAPI(url); } /** * Mark all notifications as read. */ markAllAsRead() { const url = `${this.remotePathOrUrl}/read`; return postAPI(url); } }