UNPKG

@ferry-core/oar

Version:

A RESTful HTTP request lib with default config

7 lines 3.44 kB
/*! * @ferry-core/oar with v0.2.1 * Author: Michael Pan * Built on 2023-09-17, 19:14:39 * Released under the MIT License Copyright (c) 2023 */ "use strict";var e=require("axios");function t(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var n=t(require("qs"));function r(e){return new RegExp("(?:^|;\\s*)"+encodeURIComponent(e).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=").test(document.cookie)}var o={getItem:function(e){return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*"+encodeURIComponent(e).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*([^;]*).*$)|^.*$"),"$1"))||null},setItem:function(e,t,n,r,o,a){if(!e||/^(?:expires|max\-age|path|domain|secure)$/i.test(e))return!1;var c="";if(n)switch(n.constructor){case Number:c=n===1/0?"; expires=Fri, 31 Dec 9999 23:59:59 GMT":"; max-age="+n;break;case String:c="; expires="+n;break;case Date:c="; expires="+n.toUTCString()}return document.cookie=encodeURIComponent(e)+"="+encodeURIComponent(t)+c+(o?"; domain="+o:"")+(r?"; path="+r:"")+(a?"; secure":""),!0},removeItem:function(e,t,n){return!(!e||!r(e))&&(document.cookie=encodeURIComponent(e)+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT"+(n?"; domain="+n:"")+(t?"; path="+t:""),!0)},hasItem:r,keys:function(){for(var e=document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g,"").split(/\s*(?:\=[^;]*)?;\s*/),t=0;t<e.length;t++)e[t]=decodeURIComponent(e[t]);return e}};function a(e){return"string"==typeof e||e instanceof String}function c(e){return Array.isArray(e)}function i(e){return"object"==typeof e}var s="csrf_cookie";function u(e){var t;if(e.allowCsrf){!function(){var e=o.getItem(s)}();var n,r=e.csrfKeyName,u=null===(t=null===window||void 0===window?void 0:window.__custom_cookie)||void 0===t?void 0:t.csrf_cookie,d=o.getItem(s)||u||"";e.data||(e.data={}),a(r)&&(r=[r]),i(r)&&!c(r)&&(r=i(n=r)?Object.keys(n).map((function(e){return n[e]})):[]),c(r)&&r.forEach((function(t){"object"==typeof e.data&&!e.data[t]&&a(t)&&(e.data[t]=d)}))}}var d="visit_id";function f(){return"undefined"!=typeof window}var p={withCredentials:!0,allowCsrf:!0,csrfKeyName:["csrf_token","csrf"]};function m(e){var t,r;return null===(r=null===(t=null==e?void 0:e.interceptors)||void 0===t?void 0:t.request)||void 0===r||r.use((function(e){"get"===e.method&&e.cacheTime>0&&f(),"post"===e.method&&f()&&(u(e),function(e){var t=window.__statisObserver&&window.__statisObserver.__visitId;e.data||(e.data={}),"object"!=typeof e.data||e.data[d]&&""!==e.data[d]||(e.data[d]=t||"")}(e)),/^http:\/\//.test(e.url)&&f()&&(e.url=e.url.replace(/^(http|https):/,""));var t=!e.headers["content-type"]||/application\/x-www-form-urlencoded/.test(e.headers["content-type"]);return"post"!==e.method||"object"!=typeof e.data||e.data instanceof FormData||!t||(e.data=n.stringify(e.data)),e})),e.interceptors.response.use((function(e){return e}),(function(e){var t;return e.response?((t=e.response).config&&t.config.url,"string"==typeof t||"status: ".concat(t.status,", statusText: ").concat(t.statusText)):e.message&&e.message,Promise.reject(e)})),e}p.baseURL="",Object.assign(e.defaults,p);var l=e.create();m(l),l.create=function(t){return void 0===t&&(t={}),m(e.create(t))};var v=f()?window:void 0,g=v&&v.oar?v.oar:l;module.exports=g;