@magicbell/react-headless
Version:
Hooks to build a notification inbox
68 lines • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchAPI = fetchAPI;
exports.postAPI = postAPI;
exports.deleteAPI = deleteAPI;
exports.putAPI = putAPI;
const tslib_1 = require("tslib");
const clientSettings_js_1 = tslib_1.__importDefault(require("../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_js_1.default.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
*/
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
*/
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
*/
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.
*/
function putAPI(url, data) {
return sendAPIRequest('PUT', url, data);
}
//# sourceMappingURL=ajax.js.map