UNPKG

@gouvfr-lasuite/proconnect.crisp

Version:
29 lines (28 loc) 1.16 kB
// // export async function fetch_crisp(config, options) { const searchParams = new URLSearchParams(options.searchParams).toString(); const headers = new Headers({ Authorization: `Basic ${btoa(`${config.identifier}:${config.key}`)}`, "Content-Type": "application/json", "X-Crisp-Tier": "plugin", }); const url = `${config.base_url}${options.endpoint}${searchParams !== "" ? `?${searchParams}` : ""}`; config.debug && console.info(` <<-- ${options.method} ${url}`); const body = options.body; const response = await fetch(url, { body: options.method === "GET" ? null : JSON.stringify(body), headers, method: options.method, }); config.debug && console.info(` -->> ${options.method} ${url} ${response.status} ${response.statusText}`); if (!response.ok) { throw new Error(`${url} ${response.status} ${response.statusText}`); } const crisp_response = (await response.json()); if (crisp_response.error) { throw new Error(`${url} ${response.status} ${response.statusText} - ${crisp_response.reason}`); } return crisp_response.data; }