UNPKG

@magicbell/core

Version:

Official MagicBell API wrapper

89 lines 3.28 kB
import { axios } from '../polyfills/axios-module.js'; export const api = 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 api({ method, url, data, params, headers, baseURL: 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 */ export 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 */ 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, 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 */ export 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) */ export function setupAjax(options) { const { apiKey, userEmail, userExternalId, userKey, apiSecret, baseURL } = options; if (baseURL) api.defaults.baseURL = baseURL; api.defaults.headers['X-MAGICBELL-API-KEY'] = apiKey; api.defaults.headers['X-MAGICBELL-CLIENT-ID'] = Math.random().toString(36).substring(2) + Date.now(); if (userEmail) api.defaults.headers['X-MAGICBELL-USER-EMAIL'] = userEmail; if (userExternalId) api.defaults.headers['X-MAGICBELL-USER-EXTERNAL-ID'] = userExternalId; if (userKey) api.defaults.headers['X-MAGICBELL-USER-HMAC'] = userKey; if (apiSecret) api.defaults.headers['X-MAGICBELL-API-SECRET'] = apiSecret; } //# sourceMappingURL=ajax.js.map