@svelkit/graphql
Version:
> A lightweight [GraphQL] client for [svelte]
7 lines (6 loc) • 6.41 kB
JavaScript
var K=Object.prototype.hasOwnProperty,j=Object.getOwnPropertySymbols,Y=Object.prototype.propertyIsEnumerable,r=Object.assign,S=(e,t)=>{var s={};for(var n in e)K.call(e,n)&&t.indexOf(n)<0&&(s[n]=e[n]);if(e!=null&&j)for(var n of j(e))t.indexOf(n)<0&&Y.call(e,n)&&(s[n]=e[n]);return s},G=(e,t,s)=>new Promise((n,i)=>{var l=a=>{try{o(s.next(a))}catch(f){i(f)}},c=a=>{try{o(s.throw(a))}catch(f){i(f)}},o=a=>a.done?n(a.value):Promise.resolve(a.value).then(l,c);o((s=s.apply(e,t)).next())});import{writable as D,derived as Z}from"svelte/store";import{setContext as V,getContext as ee}from"svelte";import te from"fast-json-stable-stringify";export{default as gql}from"plain-tag";const se=e=>{const t=/(?:^|})\s*(?:(query|mutation|subscription)\s*(\w+)?\s*[({]|{)/m.exec(e)||[];return{type:t[1]||"query",name:t[2]}},N=Array.from({length:256},(e,t)=>(t>>4&15).toString(16)+(t&15).toString(16)),v=e=>N[e>>8]+N[e&255],b=65535,Q=315,M=e=>{let t=e.length,s=0,n=0,i=0,l=0,c=0,o=0,a=0,f=0,g=5247,y=20042,w=11120,x=30865,p=36015,m=4122,E=52073,L=53800;for(;t--;)g^=e.charCodeAt(t),s=g*Q,n=y*Q,i=w*Q,l=x*Q,c=p*Q,o=m*Q,a=E*Q,f=L*Q,o+=g<<8,a+=y<<8,f+=w<<8,n+=s>>>16,g=s&b,i+=n>>>16,y=n&b,l+=i>>>16,w=i&b,c+=l>>>16,x=l&b,o+=c>>>16,p=c&b,a+=o>>>16,m=o&b,L=f+(a>>>16)&b,E=a&b;return v(L)+v(E)+v(m)+v(p)+v(x)+v(w)+v(y)+v(g)},re=e=>({version:-1,fnv1a128Hash:M(e)}),ne=({generateHash:e=re,preferGETForHashedQueries:t=!0,notFoundError:s="PersistedQueryNotFound",notSupportedError:n="PersistedQueryNotSupported"}={})=>{const i=new Map,l=o=>{let a=i.get(o);return a||i.set(o,a=e(o)),a};let c=!0;return(w,y)=>G(this,null,function*(){var{query:o,extensions:a,options:f}=w,g=S(w,["query","extensions","options"]);if(!c||!o||a.persistedQuery||g.operation.type!=="query")return y();const x=l(o),p=yield y(r(r({},g),{query:"",extensions:r(r({},a),{persistedQuery:x}),options:r(r({},f),{preferGetForQueries:t||f.preferGetForQueries})})).catch(m=>{if(m.message===s||m.message===n)return{errors:[m]};throw m});return p.errors&&(c=p.errors.every(m=>m.message!==n),!c||p.errors.some(m=>m.message===s))?y(r(r({},g),{query:o,extensions:a,options:f})):p})},O=e=>typeof e=="string";class U extends Error{constructor(e,t){super((O(t)?t:(s=(t.errors||[])[0])==null?void 0:s.message)||`[${e.status}] ${e.statusText}`);var s;this.name="GraphQLFetchError",this.status=e.status,this.body=t,this.headers=e.headers}}const A=e=>e&&Object.keys(e).length>0?e:void 0,ie=2e3,F=e=>{var t;return(t=e.headers.get("Content-Type"))==null?void 0:t.startsWith("application/json")},H=(e={})=>(o,a)=>G(this,[o,a],function*({operation:t,query:s,variables:n,extensions:i,options:l},c){if(t.type==="query"||t.type==="mutation"){const f=r(r(r({referrerPolicy:"strict-origin-when-cross-origin"},e),l),{headers:r(r({Accept:"application/json"},e.headers),l.headers)}),{fetch:g=globalThis.fetch,uri:y="/graphql",preferGetForQueries:w}=f,x=S(f,["fetch","uri","preferGetForQueries"]);let p;const m={operationName:t.name,query:s||void 0,variables:A(n),extensions:A(i)};if(w&&t.type==="query"){const E=new URL(y,document.baseURI);for(const[L,P]of Object.entries(m))P&&E.searchParams.set(L,O(P)?P:te(P));E.href.length<ie&&(p=yield g(E.href,r(r({},x),{method:"GET"})))}if(p||(p=yield g(new URL(y,document.baseURI).href,r(r({},x),{method:"POST",headers:r(r({},x.headers),{"Content-Type":"application/json"}),body:JSON.stringify(m)}))),p.ok&&F(p))return p.json();throw new U(p,yield F(p)?p.json():p.text())}return c()});let h=0,d,u;const T=e=>`
\r,`.includes(e),I=e=>"!|$&():=@[]{}.".includes(e),_=e=>e==="#",C=e=>e==='"',$=e=>e==="\\"&&C(u[h]),oe=e=>e===`
`||e==="\r"&&u[h]===`
`&&(h+=1),q=()=>C(u[h])&&C(u[h+1])&&(h+=2),B=()=>{for(;h<u.length&&!oe(u[h++]););},k=e=>{for(;h<u.length&&T(u[h]);)h++;return I(u[h])?u[h++]+k(""):_(u[h])?(B(),k(e)):C(u[h])?"":e},ae=()=>{var n;let e="";for(;h<u.length;)if(d=u[h++],C(d)){if(q())break;e+="\\"+d}else e+=d,$(d)&&(e+=u[h++]);const t=e.replace(/^(?:\s*\r?\n)*|(?:\r?\n\s*)*$/g,"").split(/\r?\n/),s=t.reduce((i,l)=>{if(!l)return i;let c=0;for(;T(l[c])&&i[c]===l[c];)c+=1;return i.slice(0,c)},((n=/^(\s*)/.exec(t[0]))==null?void 0:n[1])||"").length;return t.map(i=>i.slice(s)).join(`
`)},ce=()=>{let e="";for(;h<u.length&&!(d=u[h++],C(d));)e+=d,$(d)&&(e+=u[h++]);return e},he=e=>{h=0,u=e;let t="";for(;h<u.length;)d=u[h++],T(d)?t+=k(" "):_(d)?B():(t+=d,I(d)?t+=k(""):C(d)&&(t+=(q()?ae():ce())+d));return t.trim()},z=()=>{const e=new Map,t=s=>{let n=e.get(s);return n||e.set(s,n=he(s)),n};return(s,n)=>n(r(r({},s),{query:t(s.query)}))},W=[z()];class le extends Error{constructor(e,t){super(((s=e==null?void 0:e[0])==null?void 0:s.message)||"GraphQL Error");var s;this.name="GraphQLClientError",this.errors=e,this.extensions=t}}const ue=({fetching:e,error:t,data:s,errors:n,extensions:i})=>({fetching:Boolean(e),data:s,error:t||n&&new le(n,i),extensions:i});class fe{constructor(s){var{exchanges:e=W}=s,t=S(s,["exchanges"]);this.lastId=0,this.exchanges=e.filter(Boolean),this.exchanges.push(H()),this.options=t}setHeader(e,t){const s=r(r({},this.options.headers),{[e]:t||""});return t||delete s[e],this.options=r(r({},this.options),{headers:s}),s[e]}request(e,t,s={}){const n=D({},i=>{const l=new AbortController;i({fetching:!0});const c=(o,a)=>{const f=this.exchanges[a];if(f)return f(o,(g=o)=>c(g,a+1),n.update);throw new Error(`No exchange has handled operations of type "${o.operation.type}". Check whether you've added an exchange responsible for these operations.`)};return c({query:e,variables:t||{},operation:r(r({},se(e)),{id:++this.lastId}),extensions:{},options:r(r(r({},this.options),s),{headers:r(r({},this.options.headers),s.headers),signal:l.signal})},0).catch(o=>({error:o})).then(o=>n.set(r(r({},o),{fetching:!1}))),()=>l.abort()});return Z(n,ue)}}const J=Symbol.for("@svelkit/graphql/client"),pe=e=>{const t=new fe(e);return V(J,t),t},X=()=>ee(J),ge=e=>new Promise((t,s)=>{const n=e.subscribe(i=>{i.data?(n(),t(i)):i.error&&(n(),s(i.error))})}),R=(e,t={},s={})=>{const n=X(),i=(c,o)=>{const a=n.request(e,r(r({},t),c),r(r({},s),o));return a.then=de,a.fetch=(f,g)=>i(r(r({},c),f),r(r({},o),g)),a},l=i();return Object.assign(l.fetch,l)};function de(e,t){return ge(this).then(e,t)}export{U as GraphQLFetchError,ne as automaticPersistedQueries,W as defaultExchanges,H as fetch,M as fnv1a128,pe as initGraphQLClient,z as minimize,X as useGraphQLClient,R as useMutation,R as useQuery,R as useRequest};
//# sourceMappingURL=graphql.js.map