@magicbell/core
Version:
Official MagicBell API wrapper
52 lines • 1.46 kB
JavaScript
import { deleteAPI, fetchAPI, postAPI, putAPI } from '../lib/ajax.js';
/**
* Class to represent a client that interacts with the MagicBell API.
*
* @example
* class NotificationRepo extends RemoteRepository<Notification> {}
*/
export default class RemoteRepository {
remotePathOrUrl;
constructor(remotePathOrUrl) {
this.remotePathOrUrl = remotePathOrUrl;
}
/**
* Get an element from the API server by ID.
*
* @example
* const notification = await repo.get('3df592eb-5f09dd6b');
*/
get(id) {
const url = `${this.remotePathOrUrl}/${id}`;
return fetchAPI(url);
}
/**
* Get elements that match params from the API server.
*
* @example
* const notifications = await repo.findBy({ unread: true });
*/
findBy(queryParams) {
return fetchAPI(this.remotePathOrUrl, queryParams);
}
create(item) {
return postAPI(this.remotePathOrUrl, item);
}
update(id, item) {
const url = `${this.remotePathOrUrl}/${id}`;
return putAPI(url, item);
}
/**
* Delete an element by ID from the API server.
*
* @example
* const deleted = await repo.delete('3df592eb-5f09dd6b');
*/
delete(id) {
const url = `${this.remotePathOrUrl}/${id}`;
return deleteAPI(url)
.then(() => true)
.catch(() => false);
}
}
//# sourceMappingURL=RemoteRepository.js.map