@nhost/nhost-js 4.2.1-beta0 → 4.2.1-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).NhostJs={})}(this,(function(t){"use strict";function s(t=[]){return t.reduceRight(((t,s)=>s(t)),fetch)}class e extends Error{body;status;headers;constructor(t,s,e){super(function(t){if(t&&"string"==typeof t)return t;if(t&&"object"==typeof t){const s=t;if("message"in s&&"string"==typeof s.message)return s.message;if("error"in s&&"string"==typeof s.error)return s.error;if("error"in s&&s.error&&"object"==typeof s.error){const t=s.error;if("message"in t&&"string"==typeof t.message)return t.message}if("errors"in s&&Array.isArray(s.errors)){const t=s.errors.filter((t=>"object"==typeof t&&null!==t&&"message"in t&&"string"==typeof t.message)).map((t=>t.message));if(t.length>0)return t.join(", ")}}return"An unexpected error occurred"}(t)),this.body=t,this.status=s,this.headers=e}}const a=t=>s=>async(e,a={})=>{const r=new Headers(a.headers||{});if(r.has("Authorization"))return s(e,a);const i=t.get();if(i?.accessToken){const t={...a,headers:n(r,i)};return s(e,t)}return s(e,a)};function n(t,s){return s.accessToken&&t.set("Authorization",`Bearer ${s.accessToken}`),t}const r="undefined"!=typeof navigator&&navigator.locks?navigator.locks:new class{async request(t,s,e){return e()}},i=async(t,s,e=60)=>{try{return await o(t,s,e)}catch(a){try{return console.warn("error refreshing session, retrying:",a),await o(t,s,e)}catch(n){const t=n;return 401===t?.status&&(console.error("session probably expired"),s.remove()),null}}},o=async(t,s,e=60)=>{const{session:a,needsRefresh:n}=await r.request("nhostSessionLock",{mode:"shared"},(async()=>u(s,e)));if(!a)return null;if(!n)return a;return await r.request("nhostSessionLock",{mode:"exclusive"},(async()=>{const{session:a,needsRefresh:n,sessionExpired:r}=u(s,e);if(!a)return null;if(!n)return a;try{const e=await t.refreshToken({refreshToken:a.refreshToken});return s.set(e.body),e.body}catch(i){if(!r)return a;throw i}}))},u=(t,s=60)=>{const e=t.get();if(!e)return{session:null,needsRefresh:!1,sessionExpired:!1};if(!e.decodedToken||!e.decodedToken.exp)return{session:e,needsRefresh:!0,sessionExpired:!0};if(0===s)return{session:e,needsRefresh:!0,sessionExpired:!1};const a=Date.now();return e.decodedToken.exp-a>1e3*s?{session:e,needsRefresh:!1,sessionExpired:!1}:{session:e,needsRefresh:!0,sessionExpired:e.decodedToken.exp<a}},d=(t,s,e)=>{const{marginSeconds:a=60}={};return e=>async(n,r={})=>{if(function(t,s){const e=new Headers(s.headers||{});if(e.has("Authorization"))return!0;if(t.endsWith("/v1/token"))return!0;return!1}(n,r))return e(n,r);try{await i(t,s,a)}catch{}return e(n,r)}};const c=t=>s=>async(e,a)=>{const n=await s(e,a);try{if(e.endsWith("/signout"))return t.remove(),n;if(e.endsWith("/token")||e.includes("/signin/")||e.includes("/signup/")){const s=n.clone(),e=await s.json().catch((()=>null));if(e){const s=(t=>"string"==typeof t?null:"session"in t?t.session||null:"accessToken"in t&&"refreshToken"in t&&"user"in t?t:null)(e);s?.accessToken&&s.refreshToken&&t.set(s)}}}catch(r){console.warn("Error in session response middleware:",r)}return n},h=t=>t.startsWith("{")&&t.endsWith("}"),l=t=>t&&"{}"!==t?t.slice(1,-1).split(",").map((t=>t.trim().replace(/^"(.*)"$/,"$1"))):[];class p{storageKey;constructor(t){this.storageKey=t?.storageKey||"nhostSession"}get(){try{const t=window.localStorage.getItem(this.storageKey);return t?JSON.parse(t):null}catch{return this.remove(),null}}set(t){window.localStorage.setItem(this.storageKey,JSON.stringify(t))}remove(){window.localStorage.removeItem(this.storageKey)}}class w{session=null;get(){return this.session}set(t){this.session=t}remove(){this.session=null}}class y{storage;subscribers=new Set;constructor(t){this.storage=t}get(){return this.storage.get()}set(t){const s=(t=>{const s=t.split(".");if(3!==s.length||!s[1])throw new Error("Invalid access token format");const e=JSON.parse("undefined"!=typeof atob?atob(s[1]):Buffer.from(s[1],"base64").toString("utf-8")),a="number"==typeof e.iat?1e3*e.iat:void 0,n="number"==typeof e.exp?1e3*e.exp:void 0,r=e["https://hasura.io/jwt/claims"],i=r?Object.entries(r).reduce(((t,[s,e])=>("string"==typeof e&&h(e)?t[s]=l(e):t[s]=e,t)),{}):void 0;return{...e,iat:a,exp:n,"https://hasura.io/jwt/claims":i}})(t.accessToken),e={...t,decodedToken:s};this.storage.set(e),this.notifySubscribers(e)}remove(){this.storage.remove(),this.notifySubscribers(null)}onChange(t){return this.subscribers.add(t),()=>{this.subscribers.delete(t)}}notifySubscribers(t){for(const e of this.subscribers)try{e(t)}catch(s){console.error("Error notifying subscriber:",s)}}}const f=()=>"undefined"!=typeof window?new p:new w,S=({auth:t,storage:s,graphql:e,functions:n,sessionStorage:r})=>{const i=[d(t,r),c(r),a(r)];for(const a of i)t.pushChainFunction(a),s.pushChainFunction(a),e.pushChainFunction(a),n.pushChainFunction(a)},g=({auth:t,storage:s,graphql:e,functions:n,sessionStorage:r})=>{const i=[c(r),a(r)];for(const a of i)t.pushChainFunction(a),s.pushChainFunction(a),e.pushChainFunction(a),n.pushChainFunction(a)};class O{auth;storage;graphql;functions;sessionStorage;constructor(t,s,e,a,n){this.auth=t,this.storage=s,this.graphql=e,this.functions=a,this.sessionStorage=n}getUserSession(){return this.sessionStorage.get()}async refreshSession(t=60){return i(this.auth,this.sessionStorage,t)}clearSession(){this.sessionStorage.remove()}}function b(t={}){const{subdomain:a,region:n,authUrl:r,storageUrl:i,graphqlUrl:o,functionsUrl:u,storage:d=f(),configure:c=[]}=t,h=new y(d),l=m("auth",a,n,r),p=m("storage",a,n,i),w=m("graphql",a,n,o),S=m("functions",a,n,u),g=((t,a=[])=>{let n=s(a);return{baseURL:t,pushChainFunction:t=>{a.push(t),n=s(a)},getJWKs:async s=>{const a=`${t}/.well-known/jwks.json`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},elevateWebauthn:async s=>{const a=`${t}/elevate/webauthn`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},verifyElevateWebauthn:async(s,a)=>{const r=`${t}/elevate/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},healthCheckGet:async s=>{const a=`${t}/healthz`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},healthCheckHead:async s=>{const a=`${t}/healthz`,r=await n(a,{...s,method:"HEAD",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}return{body:void 0,status:r.status,headers:r.headers}},linkIdToken:async(s,a)=>{const r=`${t}/link/idtoken`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserMfa:async s=>{const a=`${t}/mfa/totp/generate`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},createPAT:async(s,a)=>{const r=`${t}/pat`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInAnonymous:async(s,a)=>{const r=`${t}/signin/anonymous`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInEmailPassword:async(s,a)=>{const r=`${t}/signin/email-password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInIdToken:async(s,a)=>{const r=`${t}/signin/idtoken`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInMfaTotp:async(s,a)=>{const r=`${t}/signin/mfa/totp`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInOTPEmail:async(s,a)=>{const r=`${t}/signin/otp/email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInOTPEmail:async(s,a)=>{const r=`${t}/signin/otp/email/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPasswordlessEmail:async(s,a)=>{const r=`${t}/signin/passwordless/email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPasswordlessSms:async(s,a)=>{const r=`${t}/signin/passwordless/sms`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInPasswordlessSms:async(s,a)=>{const r=`${t}/signin/passwordless/sms/otp`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPAT:async(s,a)=>{const r=`${t}/signin/pat`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInProviderURL:(s,e)=>{const a=e&&Object.entries(e).map((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s?JSON.stringify(s):s;return`${t}=${encodeURIComponent(e)}`})).join("&");return a?`${t}/signin/provider/${s}?${a}`:`${t}/signin/provider/${s}`},getProviderTokens:async(s,a)=>{const r=`${t}/signin/provider/${s}/callback/tokens`,i=await n(r,{...a,method:"GET",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInWebauthn:async(s,a)=>{const r=`${t}/signin/webauthn`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInWebauthn:async(s,a)=>{const r=`${t}/signin/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signOut:async(s,a)=>{const r=`${t}/signout`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signUpEmailPassword:async(s,a)=>{const r=`${t}/signup/email-password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signUpWebauthn:async(s,a)=>{const r=`${t}/signup/webauthn`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignUpWebauthn:async(s,a)=>{const r=`${t}/signup/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},refreshToken:async(s,a)=>{const r=`${t}/token`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},refreshProviderToken:async(s,a,r)=>{const i=`${t}/token/provider/${s}`,o=await n(i,{...r,method:"POST",headers:{"Content-Type":"application/json",...r?.headers},body:JSON.stringify(a)});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text(),s=t?JSON.parse(t):{};throw new e(s,o.status,o.headers)}const u=[204,205,304].includes(o.status)?null:await o.text();return{body:u?JSON.parse(u):{},status:o.status,headers:o.headers}},verifyToken:async(s,a)=>{const r=`${t}/token/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},getUser:async s=>{const a=`${t}/user`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},deanonymizeUser:async(s,a)=>{const r=`${t}/user/deanonymize`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserEmail:async(s,a)=>{const r=`${t}/user/email/change`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},sendVerificationEmail:async(s,a)=>{const r=`${t}/user/email/send-verification-email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifyChangeUserMfa:async(s,a)=>{const r=`${t}/user/mfa`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserPassword:async(s,a)=>{const r=`${t}/user/password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},sendPasswordResetEmail:async(s,a)=>{const r=`${t}/user/password/reset`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},addSecurityKey:async s=>{const a=`${t}/user/webauthn/add`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},verifyAddSecurityKey:async(s,a)=>{const r=`${t}/user/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifyTicketURL:s=>{const e=s&&Object.entries(s).map((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s?JSON.stringify(s):s;return`${t}=${encodeURIComponent(e)}`})).join("&");return e?`${t}/verify?${e}`:`${t}/verify`},getVersion:async s=>{const a=`${t}/version`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}}}})(l),b=((t,a=[])=>{let n=s(a);return{baseURL:t,pushChainFunction:t=>{a.push(t),n=s(a)},uploadFiles:async(s,a)=>{const r=`${t}/files`,i=new FormData,o="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==s["bucket-id"]&&i.append("bucket-id",s["bucket-id"]),void 0!==s["metadata[]"]&&s["metadata[]"].forEach((t=>{o?i.append("metadata[]",{string:JSON.stringify(t),type:"application/json",name:""}):i.append("metadata[]",new Blob([JSON.stringify(t)],{type:"application/json"}),"")})),void 0!==s["file[]"]&&s["file[]"].forEach((t=>{i.append("file[]",t)}));const u=await n(r,{...a,method:"POST",body:i});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}const d=[204,205,304].includes(u.status)?null:await u.text();return{body:d?JSON.parse(d):{},status:u.status,headers:u.headers}},deleteFile:async(s,a)=>{const r=`${t}/files/${s}`,i=await n(r,{...a,method:"DELETE",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}return{body:void 0,status:i.status,headers:i.headers}},getFile:async(s,a,r)=>{const i=a&&Object.entries(a).map((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s?JSON.stringify(s):s;return`${t}=${encodeURIComponent(e)}`})).join("&"),o=i?`${t}/files/${s}?${i}`:`${t}/files/${s}`,u=await n(o,{...r,method:"GET",headers:{...r?.headers}});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}return{body:await u.blob(),status:u.status,headers:u.headers}},getFileMetadataHeaders:async(s,a,r)=>{const i=a&&Object.entries(a).map((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s?JSON.stringify(s):s;return`${t}=${encodeURIComponent(e)}`})).join("&"),o=i?`${t}/files/${s}?${i}`:`${t}/files/${s}`,u=await n(o,{...r,method:"HEAD",headers:{...r?.headers}});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}return{body:void 0,status:u.status,headers:u.headers}},replaceFile:async(s,a,r)=>{const i=`${t}/files/${s}`,o=new FormData,u="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==a.metadata&&(u?o.append("metadata",{string:JSON.stringify(a.metadata),type:"application/json",name:""}):o.append("metadata",new Blob([JSON.stringify(a.metadata)],{type:"application/json"}),"")),void 0!==a.file&&o.append("file",a.file);const d=await n(i,{...r,method:"PUT",body:o});if(d.status>=300){const t=[412].includes(d.status)?null:await d.text(),s=t?JSON.parse(t):{};throw new e(s,d.status,d.headers)}const c=[204,205,304].includes(d.status)?null:await d.text();return{body:c?JSON.parse(c):{},status:d.status,headers:d.headers}},getFilePresignedURL:async(s,a)=>{const r=`${t}/files/${s}/presignedurl`,i=await n(r,{...a,method:"GET",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},deleteBrokenMetadata:async s=>{const a=`${t}/ops/delete-broken-metadata`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},deleteOrphanedFiles:async s=>{const a=`${t}/ops/delete-orphans`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listBrokenMetadata:async s=>{const a=`${t}/ops/list-broken-metadata`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listFilesNotUploaded:async s=>{const a=`${t}/ops/list-not-uploaded`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listOrphanedFiles:async s=>{const a=`${t}/ops/list-orphans`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},getVersion:async s=>{const a=`${t}/version`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}}}})(p,[]),N=((t,a=[])=>{let n=s(a);const r=async(s,a)=>{const r=await n(`${t}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s),...a}),i=await r.text(),o=i?JSON.parse(i):{},u={body:o,status:r.status,headers:r.headers};if(o.errors)throw new e(o,r.status,r.headers);return u};return{request:function(t,s,e){if("object"==typeof t&&"kind"in t){const a=t.definitions[0],n={query:t.loc?.source.body||"",variables:s,operationName:a&&"name"in a?a.name?.value:void 0};return r(n,e)}return r(t,s)},url:t,pushChainFunction:t=>{a.push(t),n=s(a)}}})(w,[]),J=((t,a=[])=>{let n=s(a);const r=async(s,a)=>{const r=await n(`${t}${s}`,a);let i;if(i=r.headers.get("content-type")?.includes("application/json")?await r.json():r.headers.get("content-type")?.startsWith("text/")?await r.text():await r.blob(),!r.ok)throw new e(i,r.status,r.headers);return{status:r.status,body:i,headers:r.headers}};return{baseURL:t,fetch:r,post:async(t,s,e={})=>{const a={...e,method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",...e.headers},body:s?JSON.stringify(s):void 0};return r(t,a)},pushChainFunction:t=>{a.push(t),n=s(a)}}})(S,[]);for(const s of c)s({auth:g,storage:b,graphql:N,functions:J,sessionStorage:h});return new O(g,b,N,J,h)}const m=(t,s,e,a)=>a||(s&&e?`https://${s}.${t}.${e}.nhost.run/v1`:`https://local.${t}.local.nhost.run/v1`);t.createClient=function(t={}){const s=t.storage??f();return b({...t,storage:s,configure:[S,...t.configure??[]]})},t.createNhostClient=b,t.createServerClient=function(t){return b({...t,configure:[g,...t.configure??[]]})},t.generateServiceUrl=m,t.withAdminSession=function(t){return({storage:s,graphql:e,functions:a})=>{const n=(r=t,t=>async(s,e={})=>{const a=new Headers(e.headers||{});if(a.has("x-hasura-admin-secret")||a.set("x-hasura-admin-secret",r.adminSecret),r.role&&!a.has("x-hasura-role")&&a.set("x-hasura-role",r.role),r.sessionVariables)for(const[t,n]of Object.entries(r.sessionVariables)){const s=t.startsWith("x-hasura-")?t:`x-hasura-${t}`;a.has(s)||a.set(s,n)}return t(s,{...e,headers:a})});var r;s.pushChainFunction(n),e.pushChainFunction(n),a.pushChainFunction(n)}},t.withClientSideSessionMiddleware=S,t.withServerSideSessionMiddleware=g,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
1
+ !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).NhostJs={})}(this,(function(t){"use strict";function s(t=[]){return t.reduceRight(((t,s)=>s(t)),fetch)}class e extends Error{body;status;headers;constructor(t,s,e){super(function(t){if(t&&"string"==typeof t)return t;if(t&&"object"==typeof t){const s=t;if("message"in s&&"string"==typeof s.message)return s.message;if("error"in s&&"string"==typeof s.error)return s.error;if("error"in s&&s.error&&"object"==typeof s.error){const t=s.error;if("message"in t&&"string"==typeof t.message)return t.message}if("errors"in s&&Array.isArray(s.errors)){const t=s.errors.filter((t=>"object"==typeof t&&null!==t&&"message"in t&&"string"==typeof t.message)).map((t=>t.message));if(t.length>0)return t.join(", ")}}return"An unexpected error occurred"}(t)),this.body=t,this.status=s,this.headers=e}}const a=t=>s=>async(e,a={})=>{const r=new Headers(a.headers||{});if(r.has("Authorization"))return s(e,a);const i=t.get();if(i?.accessToken){const t={...a,headers:n(r,i)};return s(e,t)}return s(e,a)};function n(t,s){return s.accessToken&&t.set("Authorization",`Bearer ${s.accessToken}`),t}const r="undefined"!=typeof navigator&&navigator.locks?navigator.locks:new class{async request(t,s,e){return e()}},i=async(t,s,e=60)=>{try{return await o(t,s,e)}catch(a){try{return console.warn("error refreshing session, retrying:",a),await o(t,s,e)}catch(n){const t=n;return 401===t?.status&&(console.error("session probably expired"),s.remove()),null}}},o=async(t,s,e=60)=>{const{session:a,needsRefresh:n}=await r.request("nhostSessionLock",{mode:"shared"},(async()=>u(s,e)));if(!a)return null;if(!n)return a;return await r.request("nhostSessionLock",{mode:"exclusive"},(async()=>{const{session:a,needsRefresh:n,sessionExpired:r}=u(s,e);if(!a)return null;if(!n)return a;try{const e=await t.refreshToken({refreshToken:a.refreshToken});return s.set(e.body),e.body}catch(i){if(!r)return a;throw i}}))},u=(t,s=60)=>{const e=t.get();if(!e)return{session:null,needsRefresh:!1,sessionExpired:!1};if(!e.decodedToken||!e.decodedToken.exp)return{session:e,needsRefresh:!0,sessionExpired:!0};if(0===s)return{session:e,needsRefresh:!0,sessionExpired:!1};const a=Date.now();return e.decodedToken.exp-a>1e3*s?{session:e,needsRefresh:!1,sessionExpired:!1}:{session:e,needsRefresh:!0,sessionExpired:e.decodedToken.exp<a}},d=(t,s,e)=>{const{marginSeconds:a=60}={};return e=>async(n,r={})=>{if(function(t,s){const e=new Headers(s.headers||{});if(e.has("Authorization"))return!0;if(t.endsWith("/v1/token"))return!0;return!1}(n,r))return e(n,r);try{await i(t,s,a)}catch{}return e(n,r)}};const c=t=>s=>async(e,a)=>{const n=await s(e,a);try{if(e.endsWith("/signout"))return t.remove(),n;if(e.endsWith("/token")||e.includes("/signin/")||e.includes("/signup/")){const s=n.clone(),e=await s.json().catch((()=>null));if(e){const s=(t=>"string"==typeof t?null:"session"in t?t.session||null:"accessToken"in t&&"refreshToken"in t&&"user"in t?t:null)(e);s?.accessToken&&s.refreshToken&&t.set(s)}}}catch(r){console.warn("Error in session response middleware:",r)}return n},h=t=>t.startsWith("{")&&t.endsWith("}"),l=t=>t&&"{}"!==t?t.slice(1,-1).split(",").map((t=>t.trim().replace(/^"(.*)"$/,"$1"))):[];class p{storageKey;constructor(t){this.storageKey=t?.storageKey||"nhostSession"}get(){try{const t=window.localStorage.getItem(this.storageKey);return t?JSON.parse(t):null}catch{return this.remove(),null}}set(t){window.localStorage.setItem(this.storageKey,JSON.stringify(t))}remove(){window.localStorage.removeItem(this.storageKey)}}class w{session=null;get(){return this.session}set(t){this.session=t}remove(){this.session=null}}class y{storage;subscribers=new Set;constructor(t){this.storage=t}get(){return this.storage.get()}set(t){const s=(t=>{const s=t.split(".");if(3!==s.length||!s[1])throw new Error("Invalid access token format");const e=JSON.parse("undefined"!=typeof atob?atob(s[1]):Buffer.from(s[1],"base64").toString("utf-8")),a="number"==typeof e.iat?1e3*e.iat:void 0,n="number"==typeof e.exp?1e3*e.exp:void 0,r=e["https://hasura.io/jwt/claims"],i=r?Object.entries(r).reduce(((t,[s,e])=>("string"==typeof e&&h(e)?t[s]=l(e):t[s]=e,t)),{}):void 0;return{...e,iat:a,exp:n,"https://hasura.io/jwt/claims":i}})(t.accessToken),e={...t,decodedToken:s};this.storage.set(e),this.notifySubscribers(e)}remove(){this.storage.remove(),this.notifySubscribers(null)}onChange(t){return this.subscribers.add(t),()=>{this.subscribers.delete(t)}}notifySubscribers(t){for(const e of this.subscribers)try{e(t)}catch(s){console.error("Error notifying subscriber:",s)}}}const f=()=>"undefined"!=typeof window?new p:new w,S=({auth:t,storage:s,graphql:e,functions:n,sessionStorage:r})=>{const i=[d(t,r),c(r),a(r)];for(const a of i)t.pushChainFunction(a),s.pushChainFunction(a),e.pushChainFunction(a),n.pushChainFunction(a)},g=({auth:t,storage:s,graphql:e,functions:n,sessionStorage:r})=>{const i=[c(r),a(r)];for(const a of i)t.pushChainFunction(a),s.pushChainFunction(a),e.pushChainFunction(a),n.pushChainFunction(a)};class O{auth;storage;graphql;functions;sessionStorage;constructor(t,s,e,a,n){this.auth=t,this.storage=s,this.graphql=e,this.functions=a,this.sessionStorage=n}getUserSession(){return this.sessionStorage.get()}async refreshSession(t=60){return i(this.auth,this.sessionStorage,t)}clearSession(){this.sessionStorage.remove()}}function b(t={}){const{subdomain:a,region:n,authUrl:r,storageUrl:i,graphqlUrl:o,functionsUrl:u,storage:d=f(),configure:c=[]}=t,h=new y(d),l=m("auth",a,n,r),p=m("storage",a,n,i),w=m("graphql",a,n,o),S=m("functions",a,n,u),g=((t,a=[])=>{let n=s(a);return{baseURL:t,pushChainFunction:t=>{a.push(t),n=s(a)},getJWKs:async s=>{const a=`${t}/.well-known/jwks.json`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},elevateWebauthn:async s=>{const a=`${t}/elevate/webauthn`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},verifyElevateWebauthn:async(s,a)=>{const r=`${t}/elevate/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},healthCheckGet:async s=>{const a=`${t}/healthz`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},healthCheckHead:async s=>{const a=`${t}/healthz`,r=await n(a,{...s,method:"HEAD",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}return{body:void 0,status:r.status,headers:r.headers}},linkIdToken:async(s,a)=>{const r=`${t}/link/idtoken`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserMfa:async s=>{const a=`${t}/mfa/totp/generate`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},createPAT:async(s,a)=>{const r=`${t}/pat`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInAnonymous:async(s,a)=>{const r=`${t}/signin/anonymous`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInEmailPassword:async(s,a)=>{const r=`${t}/signin/email-password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInIdToken:async(s,a)=>{const r=`${t}/signin/idtoken`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInMfaTotp:async(s,a)=>{const r=`${t}/signin/mfa/totp`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInOTPEmail:async(s,a)=>{const r=`${t}/signin/otp/email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInOTPEmail:async(s,a)=>{const r=`${t}/signin/otp/email/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPasswordlessEmail:async(s,a)=>{const r=`${t}/signin/passwordless/email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPasswordlessSms:async(s,a)=>{const r=`${t}/signin/passwordless/sms`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInPasswordlessSms:async(s,a)=>{const r=`${t}/signin/passwordless/sms/otp`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInPAT:async(s,a)=>{const r=`${t}/signin/pat`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInProviderURL:(s,e)=>{const a=e&&Object.entries(e).flatMap((([t,s])=>{if("providerSpecificParams"===t)return"object"!=typeof s||null===s||Array.isArray(s)?[`${t}=${encodeURIComponent(String(s))}`]:Object.entries(s).map((([t,s])=>`${t}=${encodeURIComponent(String(s))}`));const e=Array.isArray(s)?s.join(","):"object"==typeof s&&null!==s?JSON.stringify(s):String(s);return[`${t}=${encodeURIComponent(e)}`]})).join("&");return a?`${t}/signin/provider/${s}?${a}`:`${t}/signin/provider/${s}`},getProviderTokens:async(s,a)=>{const r=`${t}/signin/provider/${s}/callback/tokens`,i=await n(r,{...a,method:"GET",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signInWebauthn:async(s,a)=>{const r=`${t}/signin/webauthn`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignInWebauthn:async(s,a)=>{const r=`${t}/signin/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signOut:async(s,a)=>{const r=`${t}/signout`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signUpEmailPassword:async(s,a)=>{const r=`${t}/signup/email-password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},signUpWebauthn:async(s,a)=>{const r=`${t}/signup/webauthn`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifySignUpWebauthn:async(s,a)=>{const r=`${t}/signup/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},refreshToken:async(s,a)=>{const r=`${t}/token`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},refreshProviderToken:async(s,a,r)=>{const i=`${t}/token/provider/${s}`,o=await n(i,{...r,method:"POST",headers:{"Content-Type":"application/json",...r?.headers},body:JSON.stringify(a)});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text(),s=t?JSON.parse(t):{};throw new e(s,o.status,o.headers)}const u=[204,205,304].includes(o.status)?null:await o.text();return{body:u?JSON.parse(u):{},status:o.status,headers:o.headers}},verifyToken:async(s,a)=>{const r=`${t}/token/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},getUser:async s=>{const a=`${t}/user`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},deanonymizeUser:async(s,a)=>{const r=`${t}/user/deanonymize`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserEmail:async(s,a)=>{const r=`${t}/user/email/change`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},sendVerificationEmail:async(s,a)=>{const r=`${t}/user/email/send-verification-email`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifyChangeUserMfa:async(s,a)=>{const r=`${t}/user/mfa`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},changeUserPassword:async(s,a)=>{const r=`${t}/user/password`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},sendPasswordResetEmail:async(s,a)=>{const r=`${t}/user/password/reset`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},addSecurityKey:async s=>{const a=`${t}/user/webauthn/add`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},verifyAddSecurityKey:async(s,a)=>{const r=`${t}/user/webauthn/verify`,i=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/json",...a?.headers},body:JSON.stringify(s)});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},verifyTicketURL:s=>{const e=s&&Object.entries(s).flatMap((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s&&null!==s?JSON.stringify(s):String(s);return[`${t}=${encodeURIComponent(e)}`]})).join("&");return e?`${t}/verify?${e}`:`${t}/verify`},getVersion:async s=>{const a=`${t}/version`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}}}})(l),b=((t,a=[])=>{let n=s(a);return{baseURL:t,pushChainFunction:t=>{a.push(t),n=s(a)},uploadFiles:async(s,a)=>{const r=`${t}/files`,i=new FormData,o="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==s["bucket-id"]&&i.append("bucket-id",s["bucket-id"]),void 0!==s["metadata[]"]&&s["metadata[]"].forEach((t=>{o?i.append("metadata[]",{string:JSON.stringify(t),type:"application/json",name:""}):i.append("metadata[]",new Blob([JSON.stringify(t)],{type:"application/json"}),"")})),void 0!==s["file[]"]&&s["file[]"].forEach((t=>{i.append("file[]",t)}));const u=await n(r,{...a,method:"POST",body:i});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}const d=[204,205,304].includes(u.status)?null:await u.text();return{body:d?JSON.parse(d):{},status:u.status,headers:u.headers}},deleteFile:async(s,a)=>{const r=`${t}/files/${s}`,i=await n(r,{...a,method:"DELETE",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}return{body:void 0,status:i.status,headers:i.headers}},getFile:async(s,a,r)=>{const i=a&&Object.entries(a).flatMap((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s&&null!==s?JSON.stringify(s):String(s);return[`${t}=${encodeURIComponent(e)}`]})).join("&"),o=i?`${t}/files/${s}?${i}`:`${t}/files/${s}`,u=await n(o,{...r,method:"GET",headers:{...r?.headers}});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}return{body:await u.blob(),status:u.status,headers:u.headers}},getFileMetadataHeaders:async(s,a,r)=>{const i=a&&Object.entries(a).flatMap((([t,s])=>{const e=Array.isArray(s)?s.join(","):"object"==typeof s&&null!==s?JSON.stringify(s):String(s);return[`${t}=${encodeURIComponent(e)}`]})).join("&"),o=i?`${t}/files/${s}?${i}`:`${t}/files/${s}`,u=await n(o,{...r,method:"HEAD",headers:{...r?.headers}});if(u.status>=300){const t=[412].includes(u.status)?null:await u.text(),s=t?JSON.parse(t):{};throw new e(s,u.status,u.headers)}return{body:void 0,status:u.status,headers:u.headers}},replaceFile:async(s,a,r)=>{const i=`${t}/files/${s}`,o=new FormData,u="undefined"!=typeof navigator&&"ReactNative"===navigator.product;void 0!==a.metadata&&(u?o.append("metadata",{string:JSON.stringify(a.metadata),type:"application/json",name:""}):o.append("metadata",new Blob([JSON.stringify(a.metadata)],{type:"application/json"}),"")),void 0!==a.file&&o.append("file",a.file);const d=await n(i,{...r,method:"PUT",body:o});if(d.status>=300){const t=[412].includes(d.status)?null:await d.text(),s=t?JSON.parse(t):{};throw new e(s,d.status,d.headers)}const c=[204,205,304].includes(d.status)?null:await d.text();return{body:c?JSON.parse(c):{},status:d.status,headers:d.headers}},getFilePresignedURL:async(s,a)=>{const r=`${t}/files/${s}/presignedurl`,i=await n(r,{...a,method:"GET",headers:{...a?.headers}});if(i.status>=300){const t=[412].includes(i.status)?null:await i.text(),s=t?JSON.parse(t):{};throw new e(s,i.status,i.headers)}const o=[204,205,304].includes(i.status)?null:await i.text();return{body:o?JSON.parse(o):{},status:i.status,headers:i.headers}},deleteBrokenMetadata:async s=>{const a=`${t}/ops/delete-broken-metadata`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},deleteOrphanedFiles:async s=>{const a=`${t}/ops/delete-orphans`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listBrokenMetadata:async s=>{const a=`${t}/ops/list-broken-metadata`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listFilesNotUploaded:async s=>{const a=`${t}/ops/list-not-uploaded`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},listOrphanedFiles:async s=>{const a=`${t}/ops/list-orphans`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}},getVersion:async s=>{const a=`${t}/version`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text(),s=t?JSON.parse(t):{};throw new e(s,r.status,r.headers)}const i=[204,205,304].includes(r.status)?null:await r.text();return{body:i?JSON.parse(i):{},status:r.status,headers:r.headers}}}})(p,[]),N=((t,a=[])=>{let n=s(a);const r=async(s,a)=>{const r=await n(`${t}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s),...a}),i=await r.text(),o=i?JSON.parse(i):{},u={body:o,status:r.status,headers:r.headers};if(o.errors)throw new e(o,r.status,r.headers);return u};return{request:function(t,s,e){if("object"==typeof t&&"kind"in t){const a=t.definitions[0],n={query:t.loc?.source.body||"",variables:s,operationName:a&&"name"in a?a.name?.value:void 0};return r(n,e)}return r(t,s)},url:t,pushChainFunction:t=>{a.push(t),n=s(a)}}})(w,[]),J=((t,a=[])=>{let n=s(a);const r=async(s,a)=>{const r=await n(`${t}${s}`,a);let i;if(i=r.headers.get("content-type")?.includes("application/json")?await r.json():r.headers.get("content-type")?.startsWith("text/")?await r.text():await r.blob(),!r.ok)throw new e(i,r.status,r.headers);return{status:r.status,body:i,headers:r.headers}};return{baseURL:t,fetch:r,post:async(t,s,e={})=>{const a={...e,method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",...e.headers},body:s?JSON.stringify(s):void 0};return r(t,a)},pushChainFunction:t=>{a.push(t),n=s(a)}}})(S,[]);for(const s of c)s({auth:g,storage:b,graphql:N,functions:J,sessionStorage:h});return new O(g,b,N,J,h)}const m=(t,s,e,a)=>a||(s&&e?`https://${s}.${t}.${e}.nhost.run/v1`:`https://local.${t}.local.nhost.run/v1`);t.createClient=function(t={}){const s=t.storage??f();return b({...t,storage:s,configure:[S,...t.configure??[]]})},t.createNhostClient=b,t.createServerClient=function(t){return b({...t,configure:[g,...t.configure??[]]})},t.generateServiceUrl=m,t.withAdminSession=function(t){return({storage:s,graphql:e,functions:a})=>{const n=(r=t,t=>async(s,e={})=>{const a=new Headers(e.headers||{});if(a.has("x-hasura-admin-secret")||a.set("x-hasura-admin-secret",r.adminSecret),r.role&&!a.has("x-hasura-role")&&a.set("x-hasura-role",r.role),r.sessionVariables)for(const[t,n]of Object.entries(r.sessionVariables)){const s=t.startsWith("x-hasura-")?t:`x-hasura-${t}`;a.has(s)||a.set(s,n)}return t(s,{...e,headers:a})});var r;s.pushChainFunction(n),e.pushChainFunction(n),a.pushChainFunction(n)}},t.withClientSideSessionMiddleware=S,t.withServerSideSessionMiddleware=g,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
2
2
  //# sourceMappingURL=nhost-js.umd.js.map