@nhost/nhost-js
Version:
Nhost JavaScript SDK
3 lines (2 loc) • 5.63 kB
JavaScript
;var k=Object.defineProperty;var q=(t,e,r)=>e in t?k(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var a=(t,e,r)=>q(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@nhost/hasura-auth-js"),T=require("@nhost/hasura-storage-js"),y=require("isomorphic-unfetch"),H=require("@nhost/graphql-js"),v=/^((?<protocol>http[s]?):\/\/)?(?<host>(localhost|local))(:(?<port>(\d+|__\w+__)))?$/;function f(t,e){const{subdomain:r,region:n}=t;if(!r)throw new Error("A `subdomain` must be set.");const u=r.match(v);if(u!=null&&u.groups){const{protocol:h,host:i,port:l}=u.groups,s=E(e);return s||(i==="localhost"?(console.warn('The `subdomain` is set to "localhost". Support for this will be removed in a future release. Please use "local" instead.'),`${h||"http"}://localhost:${l||1337}/v1/${e}`):l?`${h||"https"}://local.${e}.local.nhost.run:${l}/v1`:`${h||"https"}://local.${e}.local.nhost.run/v1`)}if(!n)throw new Error('`region` must be set when using a `subdomain` other than "local".');return`https://${r}.${e}.${n}.nhost.run/v1`}function U(){return typeof window!="undefined"}function $(){return typeof process!="undefined"&&process.env}function E(t){return U()||!$()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function O(t,e){const n=e.startsWith("/")?e:`/${e}`;return t+n}function b(t){const e="subdomain"in t?f(t,"auth"):t.authUrl,{subdomain:r,region:n}=t;if(!e)throw new Error("Please provide `subdomain` or `authUrl`.");return new p.HasuraAuthClient({url:e,broadcastKey:`${r}${n!=null?n:"local"}`,...t})}function w(t){const e="subdomain"in t?f(t,"functions"):t.functionsUrl;if(!e)throw new Error("Please provide `subdomain` or `functionsUrl`.");return new m({url:e,...t})}class m{constructor(e){a(this,"url");a(this,"accessToken");a(this,"adminSecret");a(this,"headers",{});const{url:r,adminSecret:n}=e;this.url=r,this.accessToken=null,this.adminSecret=n}async call(e,r,n){var i,l;const u={"Content-Type":"application/json",...this.generateAccessTokenHeaders(),...n==null?void 0:n.headers,...this.headers},h=O(this.url,e);try{const s=await y(h,{body:r?JSON.stringify(r):null,headers:u,method:"POST"});if(!s.ok){let c;return(i=s.headers.get("content-type"))!=null&&i.includes("application/json")?c=await s.json():c=await s.text(),{res:null,error:{message:c,error:s.statusText,status:s.status}}}let o;return(l=s.headers.get("content-type"))!=null&&l.includes("application/json")?o=await s.json():o=await s.text(),{res:{data:o,status:s.status,statusText:s.statusText},error:null}}catch(s){const o=s;return{res:null,error:{message:o.message,status:o.name==="AbortError"?0:500,error:o.name==="AbortError"?"abort-error":"unknown"}}}}setAccessToken(e){if(!e){this.accessToken=null;return}this.accessToken=e}getHeaders(){return this.headers}setHeaders(e){e&&(this.headers={...this.headers,...e})}unsetHeaders(){const e=this.headers["x-hasura-role"];this.headers=e?{"x-hasura-role":e}:{}}generateAccessTokenHeaders(){return this.adminSecret?{"x-hasura-admin-secret":this.adminSecret}:this.accessToken?{Authorization:`Bearer ${this.accessToken}`}:{}}}function S(t){const e="subdomain"in t?f(t,"graphql"):t.graphqlUrl;if(!e)throw new Error("Please provide `subdomain` or `graphqlUrl`.");return new H.NhostGraphqlClient({url:e,...t})}function A(t){const e="subdomain"in t?f(t,"storage"):t.storageUrl;if(!e)throw new Error("Please provide `subdomain` or `storageUrl`.");return new T.HasuraStorageClient({url:e,...t})}const _=t=>new C(t);class C{constructor({refreshIntervalTime:e,clientStorage:r,clientStorageType:n,autoRefreshToken:u,autoSignIn:h,adminSecret:i,devTools:l,start:s=!0,...o}){a(this,"auth");a(this,"storage");a(this,"functions");a(this,"graphql");a(this,"_adminSecret");a(this,"devTools");this.auth=b({refreshIntervalTime:e,clientStorage:r,clientStorageType:n,autoRefreshToken:u,autoSignIn:h,start:s,...o}),this.storage=A({adminSecret:i,...o}),this.functions=w({adminSecret:i,...o}),this.graphql=S({adminSecret:i,...o}),this.auth.onAuthStateChanged((c,d)=>{if(c==="SIGNED_OUT"){this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0);return}const g=d==null?void 0:d.accessToken;this.storage.setAccessToken(g),this.functions.setAccessToken(g),this.graphql.setAccessToken(g)}),this.auth.onTokenChanged(c=>{const d=c==null?void 0:c.accessToken;this.storage.setAccessToken(d),this.functions.setAccessToken(d),this.graphql.setAccessToken(d)}),this._adminSecret=i,this.devTools=l}get adminSecret(){return this._adminSecret}set adminSecret(e){this._adminSecret=e,this.storage.setAdminSecret(e)}setRole(e){this.graphql.setHeaders({"x-hasura-role":e}),this.storage.setHeaders({"x-hasura-role":e}),this.functions.setHeaders({"x-hasura-role":e})}unsetRole(){this.graphql.setHeaders((({"x-hasura-role":e,...r})=>r)(this.graphql.getHeaders())),this.storage.setHeaders((({"x-hasura-role":e,...r})=>r)(this.storage.getHeaders())),this.functions.setHeaders((({"x-hasura-role":e,...r})=>r)(this.functions.getHeaders()))}}exports.NhostClient=C;exports.NhostFunctionsClient=m;exports.createAuthClient=b;exports.createFunctionsClient=w;exports.createGraphqlClient=S;exports.createNhostClient=_;exports.createStorageClient=A;exports.urlFromSubdomain=f;Object.keys(p).forEach(t=>{t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>p[t]})});Object.keys(T).forEach(t=>{t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>T[t]})});
//# sourceMappingURL=index.cjs.js.map