@magicbell/react-headless
Version:
Hooks to build a notification inbox
38 lines • 1.35 kB
JavaScript
import humps from 'humps';
import { fetchAPI, putAPI } from '../../lib/ajax.js';
/**
* Class to interact with the notification preferences API endpoints.
*
* @example
* const repo = new NotificationPreferencesRepository();
* const preferences = repo.get();
*/
export default class NotificationPreferencesRepository {
remotePathOrUrl;
constructor(remotePathOrUrl = '/notification_preferences') {
this.remotePathOrUrl = remotePathOrUrl;
}
/**
* Get the user preferences from the API server. Object properties will be camelized.
* Wrapping of message to server is handled for us (Design Principle: The Principle of Least
* Knowledge).
*/
async get() {
const url = this.remotePathOrUrl;
const json = await fetchAPI(url);
return humps.camelizeKeys(json);
}
/**
* Update user preferences in the API server. Object properties will be decamelized before
* being send to the server.
*
* @param data Preferences to send to the server.
*/
async update(data) {
const url = this.remotePathOrUrl;
const payload = humps.decamelizeKeys({ notificationPreferences: data });
const json = await putAPI(url, payload);
return humps.camelizeKeys(json);
}
}
//# sourceMappingURL=NotificationPreferencesRepository.js.map