@magicbell/react-headless
Version:
Hooks to build a notification inbox
61 lines • 2 kB
JavaScript
import clientSettings from '../stores/clientSettings.js';
/**
* Performs an ajax request to the MagicBell API server.
*
* @param method - the request method to be used when making the request
* @param path - the server URL that will be used for the request
* @param data - the data to be sent as the request body
* @param params - the URL parameters to be sent with the request
*/
function sendAPIRequest(method, path, data, params) {
const client = clientSettings.getState().getClient();
const stringParams = params
? Object.fromEntries(Object.entries(params).map(([key, value]) => [key, String(value)]))
: undefined;
return client.request({
method,
path,
data,
params: stringParams,
});
}
/**
* Performs a GET request.
*
* @param url - the server URL that will be used for the request
* @param params - the URL parameters to be sent with the request
*/
export function fetchAPI(url, params) {
return sendAPIRequest('GET', url, undefined, params);
}
/**
* Performs a POST request.
*
* @param url - the server URL that will be used for the request
* @param data - the data to be sent as the request body
* @param params - the URL parameters to be sent with the request
*/
export function postAPI(url, data, params) {
return sendAPIRequest('POST', url, data, params);
}
/**
* Performs a DELETE request.
*
* @param url - the server URL that will be used for the request
* @param params - the URL parameters to be sent with the request
*/
export function deleteAPI(url, params) {
return sendAPIRequest('DELETE', url, undefined, params);
}
/**
* Performs a PUT request.
*
* @param url - the server URL that will be used for the request
* @param data - the data to be sent as the request body
* @param params - the URL parameters to be sent with the request
* @returns - A promise.
*/
export function putAPI(url, data) {
return sendAPIRequest('PUT', url, data);
}
//# sourceMappingURL=ajax.js.map