@nhost/nhost-js
Version:
Nhost JavaScript SDK
2 lines • 39 kB
JavaScript
!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){function s(t=[]){return t.reduceRight(((t,s)=>s(t)),fetch)}Object.defineProperty(t,Symbol.toStringTag,{value:"Module"});var e=class 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}},a=t=>s=>async(e,a={})=>{const r=new Headers(a.headers||{});if(r.has("Authorization"))return s(e,a);const i=t.get();return s(e,i?.accessToken?{...a,headers:n(r,i)}:a)};function n(t,s){return s.accessToken&&t.set("Authorization",`Bearer ${s.accessToken}`),t}var 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(a){return 401===a?.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)));return a?n?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}})):a:null},u=(t,s=60)=>{const e=t.get();if(!e)return{session:null,needsRefresh:!1,sessionExpired:!1};if(!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}=e||{};return e=>async(n,r={})=>{if(function(t,s){return!!new Headers(s.headers||{}).has("Authorization")||!!t.endsWith("/v1/token")}(n,r))return e(n,r);try{await i(t,s,a)}catch{}return e(n,r)}};var c=t=>s=>async(e,a)=>{const n=await s(e,a);try{if(e.endsWith("/signout"))return t.remove(),n;if(e.endsWith("/user/password")&&n.ok)return t.remove(),n;if(e.endsWith("/token")||e.includes("/token/exchange")||e.includes("/signin/")||e.includes("/signup/")){const s=await n.clone().json().catch((()=>null));if(s){const e=(t=>"string"==typeof t?null:"session"in t?t.session||null:"accessToken"in t&&"refreshToken"in t&&"user"in t?t:null)(s);e?.accessToken&&e.refreshToken&&t.set(e)}}}catch(r){console.warn("Error in session response middleware:",r)}return n},h=t=>{const s=t.loc?.source.body;if(!s)return"";if(!t.definitions.every((t=>t.loc)))return s;const e=new Set;return t.definitions.filter((t=>{if("FragmentDefinition"===t.kind&&"name"in t){const s=t.name.value;if(e.has(s))return!1;e.add(s)}return!0})).map((t=>s.slice(t.loc?.start,t.loc?.end))).join("\n\n")},l=t=>{const s=t.split(".");if(3!==s.length||!s[1])throw new Error("Invalid access token format");const e=JSON.parse((t=>{let s=t.replace(/-/g,"+").replace(/_/g,"/");const e=s.length%4;e&&(s+="=".repeat(4-e));const a=atob(s),n=Uint8Array.from(a,(t=>t.charCodeAt(0)));return(new TextDecoder).decode(n)})(s[1])),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&&p(e)?t[s]=w(e):t[s]=e,t)),{}):void 0;return{...e,iat:a,exp:n,"https://hasura.io/jwt/claims":i}},p=t=>t.startsWith("{")&&t.endsWith("}"),w=t=>t&&"{}"!==t?t.slice(1,-1).split(",").map((t=>t.trim().replace(/^"(.*)"$/,"$1"))):[],y=class{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)}},f=class{session=null;get(){return this.session}set(t){this.session=t}remove(){this.session=null}},S=class{storage;subscribers=new Set;constructor(t){this.storage=t}get(){return this.storage.get()}set(t){const s=l(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)}}},g=()=>"undefined"!=typeof window?new y:new f,O=({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)},b=({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)};var m=class{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 N(t={}){const{subdomain:a,region:n,authUrl:r,storageUrl:i,graphqlUrl:o,functionsUrl:u,storage:d=g(),configure:c=[]}=t,l=new S(d),p=J("auth",a,n,r),w=J("storage",a,n,i),y=J("graphql",a,n,o),f=J("functions",a,n,u),O=((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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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))}`));if("upstreamParams"===t)return"object"!=typeof s||null===s||Array.isArray(s)?[]:Object.entries(s).map((([t,s])=>`upstreamParams[${encodeURIComponent(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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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}},signUpPasswordlessEmail:async(s,a)=>{const r=`${t}/signup/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();throw new e(t?JSON.parse(t):{},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}},signUpOTPEmail:async(s,a)=>{const r=`${t}/signup/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();throw new e(t?JSON.parse(t):{},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}},signUpPasswordlessSms:async(s,a)=>{const r=`${t}/signup/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();throw new e(t?JSON.parse(t):{},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}},signUpIdToken:async(s,a)=>{const r=`${t}/signup/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();throw new e(t?JSON.parse(t):{},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}},signUpProviderURL:(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))}`));if("upstreamParams"===t)return"object"!=typeof s||null===s||Array.isArray(s)?[]:Object.entries(s).map((([t,s])=>`upstreamParams[${encodeURIComponent(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}/signup/provider/${s}?${a}`:`${t}/signup/provider/${s}`},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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}},tokenExchange:async(s,a)=>{const r=`${t}/token/exchange`,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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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}},getOpenIDConfiguration:async s=>{const a=`${t}/.well-known/openid-configuration`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},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}},getOAuthAuthorizationServer:async s=>{const a=`${t}/.well-known/oauth-authorization-server`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},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}},oauth2AuthorizeURL: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}/oauth2/authorize?${e}`:`${t}/oauth2/authorize`},oauth2AuthorizePostURL:()=>`${t}/oauth2/authorize`,oauth2Token:async(s,a)=>{const r=`${t}/oauth2/token`,i=new URLSearchParams;void 0!==s.grant_type&&i.append("grant_type",String(s.grant_type)),void 0!==s.code&&i.append("code",String(s.code)),void 0!==s.redirect_uri&&i.append("redirect_uri",String(s.redirect_uri)),void 0!==s.client_id&&i.append("client_id",String(s.client_id)),void 0!==s.client_secret&&i.append("client_secret",String(s.client_secret)),void 0!==s.code_verifier&&i.append("code_verifier",String(s.code_verifier)),void 0!==s.refresh_token&&i.append("refresh_token",String(s.refresh_token)),void 0!==s.resource&&i.append("resource",String(s.resource));const o=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",...a?.headers},body:i.toString()});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},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}},oauth2UserinfoGet:async s=>{const a=`${t}/oauth2/userinfo`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},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}},oauth2UserinfoPost:async s=>{const a=`${t}/oauth2/userinfo`,r=await n(a,{...s,method:"POST",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},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}},oauth2Jwks:async s=>{const a=`${t}/oauth2/jwks`,r=await n(a,{...s,method:"GET",headers:{...s?.headers}});if(r.status>=300){const t=[412].includes(r.status)?null:await r.text();throw new e(t?JSON.parse(t):{},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}},oauth2Revoke:async(s,a)=>{const r=`${t}/oauth2/revoke`,i=new URLSearchParams;void 0!==s.token&&i.append("token",String(s.token)),void 0!==s.token_type_hint&&i.append("token_type_hint",String(s.token_type_hint)),void 0!==s.client_id&&i.append("client_id",String(s.client_id)),void 0!==s.client_secret&&i.append("client_secret",String(s.client_secret));const o=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",...a?.headers},body:i.toString()});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},o.status,o.headers)}return{body:void 0,status:o.status,headers:o.headers}},oauth2Introspect:async(s,a)=>{const r=`${t}/oauth2/introspect`,i=new URLSearchParams;void 0!==s.token&&i.append("token",String(s.token)),void 0!==s.token_type_hint&&i.append("token_type_hint",String(s.token_type_hint)),void 0!==s.client_id&&i.append("client_id",String(s.client_id)),void 0!==s.client_secret&&i.append("client_secret",String(s.client_secret));const o=await n(r,{...a,method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",...a?.headers},body:i.toString()});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},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}},oauth2LoginGet:async(s,a)=>{const r=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("&"),i=r?`${t}/oauth2/login?${r}`:`${t}/oauth2/login`,o=await n(i,{...a,method:"GET",headers:{...a?.headers}});if(o.status>=300){const t=[412].includes(o.status)?null:await o.text();throw new e(t?JSON.parse(t):{},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}},oauth2LoginPost:async(s,a)=>{const r=`${t}/oauth2/login`,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();throw new e(t?JSON.parse(t):{},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}}}})(p),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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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();throw new e(t?JSON.parse(t):{},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}}}})(w,[]),N=((t,a=[])=>{let n=s(a);const r=async(s,a)=>{const r=new Headers(a?.headers);r.has("Content-Type")||r.set("Content-Type","application/json");const i=await n(`${t}`,{...a,method:"POST",headers:r,body:JSON.stringify(s)}),o=await i.text(),u=o?JSON.parse(o):{},d={body:u,status:i.status,headers:i.headers};if(u.errors)throw new e(u,i.status,i.headers);return d};return{request:function(t,s,e){if("object"==typeof t&&"kind"in t){const a=t.definitions[0];return r({query:h(t),variables:s,operationName:a&&"name"in a?a.name?.value:void 0},e)}return r(t,s)},url:t,pushChainFunction:t=>{a.push(t),n=s(a)}}})(y,[]),x=((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={})=>r(t,{...e,method:"POST",headers:{Accept:"application/json","Content-Type":"application/json",...e.headers},body:s?JSON.stringify(s):void 0}),pushChainFunction:t=>{a.push(t),n=s(a)}}})(f,[]);for(const s of c)s({auth:O,storage:b,graphql:N,functions:x,sessionStorage:l});return new m(O,b,N,x,l)}var J=(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??g();return N({...t,storage:s,configure:[O,...t.configure??[]]})},t.createNhostClient=N,t.createServerClient=function(t){return N({...t,configure:[b,...t.configure??[]]})},t.generateServiceUrl=J,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=O,t.withServerSideSessionMiddleware=b}));
//# sourceMappingURL=nhost-js.umd.js.map