UNPKG

@procore/core-http

Version:

A HTTP Client

74 lines (71 loc) 2.27 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { request: () => request, requestJSON: () => requestJSON }); module.exports = __toCommonJS(src_exports); // src/request.ts function getCSRFToken() { const token = document.cookie.match("(^|;)\\s*csrf_token\\s*=\\s*([^;]+)"); return token ? decodeURIComponent(token.pop() || "") : ""; } function getCSRFHeader() { const csrfToken = getCSRFToken(); return csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}; } function removeLeadingSlash(url) { return url.startsWith("/") ? url.substring(1, url.length) : url; } function removeTrailingSlash(url) { return url.endsWith("/") ? url.substring(0, url.length - 1) : url; } function applyBaseUrl(url, baseUrl) { return `${removeTrailingSlash(baseUrl)}/${removeLeadingSlash(url)}`; } function getOptions({ headers, ...options }) { const opts = { credentials: "same-origin", headers: { ...getCSRFHeader(), ...headers }, mode: "same-origin", ...options }; return opts; } function getUrl(url, baseUrl) { return baseUrl ? applyBaseUrl(url, baseUrl) : url; } function request(url, { baseUrl, ...options } = {}) { return fetch(getUrl(url, baseUrl), getOptions(options)); } function requestJSON(url, requestParams = {}) { return request(url, requestParams).then( (response) => response.json() ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { request, requestJSON });