UNPKG

@magicbell/core

Version:
97 lines 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.api = void 0; exports.fetchAPI = fetchAPI; exports.postAPI = postAPI; exports.deleteAPI = deleteAPI; exports.putAPI = putAPI; exports.setupAjax = setupAjax; const axios_module_js_1 = require("../polyfills/axios-module.js"); exports.api = axios_module_js_1.axios.create({ baseURL: 'https://api.magicbell.com' }); /** * Performs an ajax request using `fetch`. * * @param method - the request method to be used when making the 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 customParams - the URL parameters to be sent with the request */ function sendAPIRequest(method, url, data, params) { const headers = { Accept: 'application/json', 'Content-Type': 'application/json', }; return (0, exports.api)({ method, url, data, params, headers, baseURL: exports.api.defaults.baseURL, }).then((response) => response.data, (error) => { throw error; }); } /** * 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, null, 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, null, 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 */ function putAPI(url, data, params = {}) { return sendAPIRequest('put', url, data, params); } /** * Sets the default headers for all requests. * * @param apiKey API key of your MagicBell project * @param userEmail Email of the user whose notifications will be displayed * @param userExternalId External ID of the user you want to fetch notifications for * @param userKey Computed HMAC of the user whose notifications will be displayed, compute this with the secret of the magicbell project * @param apiSecret API secret of your MagicBell project (required to create notifications) */ function setupAjax(options) { const { apiKey, userEmail, userExternalId, userKey, apiSecret, baseURL } = options; if (baseURL) exports.api.defaults.baseURL = baseURL; exports.api.defaults.headers['X-MAGICBELL-API-KEY'] = apiKey; exports.api.defaults.headers['X-MAGICBELL-CLIENT-ID'] = Math.random().toString(36).substring(2) + Date.now(); if (userEmail) exports.api.defaults.headers['X-MAGICBELL-USER-EMAIL'] = userEmail; if (userExternalId) exports.api.defaults.headers['X-MAGICBELL-USER-EXTERNAL-ID'] = userExternalId; if (userKey) exports.api.defaults.headers['X-MAGICBELL-USER-HMAC'] = userKey; if (apiSecret) exports.api.defaults.headers['X-MAGICBELL-API-SECRET'] = apiSecret; } //# sourceMappingURL=ajax.js.map