2 lines
12 KiB
JavaScript
2 lines
12 KiB
JavaScript
|
(function(i,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(i=typeof globalThis<"u"?globalThis:i||self,f(i.momentum={}))})(this,function(i){"use strict";var Et=Object.defineProperty;var yt=(i,f,m)=>f in i?Et(i,f,{enumerable:!0,configurable:!0,writable:!0,value:m}):i[f]=m;var d=(i,f,m)=>(yt(i,typeof f!="symbol"?f+"":f,m),m);function f(t,n=!1){if(t==null)throw new Error("Cannot clean a NULL value");return Array.isArray(t)?t=t.filter(e=>e!=null):Object.entries(t).forEach(([e,r])=>{(r===void 0||n||r===null)&&delete t[e]}),t}function m(t){return JSON.parse(JSON.stringify(t))}function C(t,n,e){if(!(t==null||!n))return n.split(/[.[\]]/g).filter(r=>r.length).reduce((r,o,c,s)=>{if((o[0]=='"'||o[0]=="'")&&(o=o.slice(1,-1)),!(r!=null&&r.hasOwnProperty(o))){if(e==null)return;r[o]={}}return e!==void 0&&c==s.length-1?r[o]=e:r[o]},t)}function R(t,n,e=!1){if(t==null)return e;if(Array.isArray(n))return n.findIndex((o,c)=>!R(t[c],n[c],e))==-1;const r=typeof n;return r!=typeof t?!1:r=="object"?Object.keys(n).find(o=>!R(t[o],n[o],e))==null:r=="function"?t.toString()==n.toString():t==n}function w(t,n){const e=typeof t,r=typeof n;return e!="object"||t==null||r!="object"||n==null?e=="function"&&r=="function"?t.toString()==n.toString():t===n:Object.keys(t).length!=Object.keys(n).length?!1:Object.keys(t).every(c=>w(t[c],n[c]))}function q(t,n){return t.indexOf(n)===-1&&t.push(n),t}function P(t,n){return k([...t.filter(e=>!n.includes(r=>w(e,r))),...n.filter(e=>!t.includes(r=>w(e,r)))])}function Y(t){return function(n,e){const r=C(n,t),o=C(e,t);return typeof r!="string"||typeof o!="string"?1:r.toLowerCase().localeCompare(o.toLowerCase())}}function j(t,n=[]){return t.forEach(e=>Array.isArray(e)?j(e,n):n.push(e)),n}function G(t,n=!1){return function(e,r){const o=C(e,t),c=C(r,t);return typeof o=="number"&&typeof c=="number"?(n?-1:1)*(o-c):o>c?n?-1:1:o<c?n?1:-1:0}}function W(t,n){return e=>w(e[t],n)}function k(t){for(let n=t.length-1;n>=0;n--)t.slice(0,n).find(e=>w(e,t[n]))&&t.splice(n,1);return t}function x(t){return Array.isArray(t)?t:[t]}class H{constructor(){d(this,"listeners",{})}emit(n){Object.values(this.listeners).forEach(e=>e(n))}listen(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(this.listeners).length.toString()}`;return this.listeners[o]=r,()=>delete this.listeners[o]}once(n){const e=this.listen(r=>{n(r),e()})}}const g=class g{constructor(n,e={}){d(this,"interceptors",{});this.baseUrl=n,this.headers=e}static addInterceptor(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(g.interceptors).length.toString()}`;return g.interceptors[o]=r,()=>delete g.interceptors[o]}addInterceptor(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(this.interceptors).length.toString()}`;return this.interceptors[o]=r,()=>delete this.interceptors[o]}getInterceptors(){return[...Object.values(g.interceptors),...Object.values(this.interceptors)]}fetch(n,e,r={}){const o={"Content-Type":e&&!(e instanceof FormData)?"application/json":void 0,...g.headers,...this.headers,...r.headers};return Object.keys(o).forEach(c=>{o[c]||delete o[c]}),fetch(`${this.baseUrl}${n||""}`.replace(/([^:]\/)\/+/g,"$1"),{headers:o,method:r.method||(e?"POST":"GET"),body:o["Content-Type"].startsWith("application/json")&&e?JSON.stringify(e):e}).then(async c=>{for(let s of this.getInterceptors())await new Promise(S=>s(c,()=>S(null)));return c.headers["Content-Type"]&&c.headers.get("Content-Type").startsWith("application/json")?await c.json():c.headers["Content-Type"]&&c.headers.get("Content-Type").startsWith("text/plain")?await c.text():c})}delete(n,e){return this.fetch(n,null,{method:"delete",...e})}get(n,e){return this.fetch(n,null,{method:"get",...e})}patch(n,e,r){return this.fetch(e,n,{method:"patch",...r})}post(n,e,r){return this.fetch(e,n,{method:"post",...r})}put(n,e,r){return this.fetch(e,n,{method:"put",...r})}new(n,e){const r=new g(`${this.baseUrl}${n}`,{...this.headers,...e});return Object.entries(this.interceptors).map(([o,c])=>r.addInterceptor(o,c)),r}};d(g,"interceptors",{}),d(g,"headers",
|