@nhost/nhost-js
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@nhost/hasura-auth-js"),g=require("@nhost/hasura-storage-js"),U=require("cross-fetch"),A=require("@nhost/graphql-js"),S=/^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>(\d+|__\w+__)))?$/;function l(t,e){const{backendUrl:s,subdomain:o,region:c}=t;if(s)return`${s}/v1/${e}`;if(!o)throw new Error("Either `backendUrl` or `subdomain` must be set.");const r=o.match(S);if(r!=null&&r.groups){const{protocol:n="http",host:h,port:d=1337}=r.groups,i=q(e);return i||`${n}://${h}:${d}/v1/${e}`}if(!c)throw new Error('`region` must be set when using a `subdomain` other than "localhost".');return`https://${o}.${e}.${c}.nhost.run/v1`}function y(){return typeof window<"u"}function m(){return typeof process<"u"&&process.env}function q(t){return y()||!m()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function b(t){const e="subdomain"in t||"backendUrl"in t?l(t,"auth"):t.authUrl;if(!e)throw new Error("Please provide `subdomain` or `authUrl`.");return new f.HasuraAuthClient({url:e,...t})}function w(t){const e="subdomain"in t||"backendUrl"in t?l(t,"functions"):t.functionsUrl;if(!e)throw new Error("Please provide `subdomain` or `functionsUrl`.");return new p({url:e,...t})}class p{constructor(e){const{url:s,adminSecret:o}=e;this.url=s,this.accessToken=null,this.adminSecret=o}async call(e,s,o){const c={"Content-Type":"application/json",...this.generateAccessTokenHeaders(),...o==null?void 0:o.headers};try{const r=await U(e,{body:JSON.stringify(s),headers:c,method:"POST"});if(!r.ok)throw new Error(r.statusText);let n;try{n=await r.json()}catch{n=await r.text()}return{res:{data:n,status:r.status,statusText:r.statusText},error:null}}catch(r){const n=r;return{res:null,error:{message:n.message,status:n.name==="AbortError"?0:500,error:n.name==="AbortError"?"abort-error":"unknown"}}}}setAccessToken(e){if(!e){this.accessToken=null;return}this.accessToken=e}generateAccessTokenHeaders(){return this.adminSecret?{"x-hasura-admin-secret":this.adminSecret}:this.accessToken?{Authorization:`Bearer ${this.accessToken}`}:{}}}function k(t){const e="subdomain"in t||"backendUrl"in t?l(t,"graphql"):t.graphqlUrl;if(!e)throw new Error("Please provide `subdomain` or `graphqlUrl`.");return new A.NhostGraphqlClient({url:e,...t})}function T(t){const e="subdomain"in t||"backendUrl"in t?l(t,"storage"):t.storageUrl;if(!e)throw new Error("Please provide `subdomain` or `storageUrl`.");return new g.HasuraStorageClient({url:e,...t})}const v=t=>new C(t);class C{constructor({refreshIntervalTime:e,clientStorage:s,clientStorageType:o,autoRefreshToken:c,autoSignIn:r,adminSecret:n,devTools:h,start:d=!0,...i}){this.auth=b({refreshIntervalTime:e,clientStorage:s,clientStorageType:o,autoRefreshToken:c,autoSignIn:r,start:d,...i}),this.storage=T({adminSecret:n,...i}),this.functions=w({adminSecret:n,...i}),this.graphql=k({adminSecret:n,...i}),this.auth.onAuthStateChanged((u,a)=>{u==="SIGNED_OUT"&&(this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0))}),this.auth.onTokenChanged(u=>{const a=u==null?void 0:u.accessToken;this.storage.setAccessToken(a),this.functions.setAccessToken(a),this.graphql.setAccessToken(a)}),this._adminSecret=n,this.devTools=h}get adminSecret(){return this._adminSecret}set adminSecret(e){this._adminSecret=e,this.storage.setAdminSecret(e)}}exports.NhostClient=C;exports.NhostFunctionsClient=p;exports.createAuthClient=b;exports.createFunctionsClient=w;exports.createGraphqlClient=k;exports.createNhostClient=v;exports.createStorageClient=T;for(const t in f)t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>f[t]});for(const t in g)t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>g[t]}); | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@nhost/hasura-auth-js"),g=require("@nhost/hasura-storage-js"),C=require("cross-fetch"),A=require("@nhost/graphql-js"),S=/^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>(\d+|__\w+__)))?$/;function d(t,e){const{backendUrl:s,subdomain:n,region:a}=t;if(s)return`${s}/v1/${e}`;if(!n)throw new Error("Either `backendUrl` or `subdomain` must be set.");const i=n.match(S);if(i!=null&&i.groups){const{protocol:c="http",host:r,port:o=1337}=i.groups,u=q(e);return u||`${c}://${r}:${o}/v1/${e}`}if(!a)throw new Error('`region` must be set when using a `subdomain` other than "localhost".');return`https://${n}.${e}.${a}.nhost.run/v1`}function y(){return typeof window<"u"}function m(){return typeof process<"u"&&process.env}function q(t){return y()||!m()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function b(t){const e="subdomain"in t||"backendUrl"in t?d(t,"auth"):t.authUrl;if(!e)throw new Error("Please provide `subdomain` or `authUrl`.");return new f.HasuraAuthClient({url:e,...t})}function p(t){const e="subdomain"in t||"backendUrl"in t?d(t,"functions"):t.functionsUrl;if(!e)throw new Error("Please provide `subdomain` or `functionsUrl`.");return new w({url:e,...t})}class w{constructor(e){const{url:s,adminSecret:n}=e;this.url=s,this.accessToken=null,this.adminSecret=n}async call(e,s,n){const a={"Content-Type":"application/json",...this.generateAccessTokenHeaders(),...n==null?void 0:n.headers},i=this.url,c=e.startsWith("/")?e:`/${e}`;try{const r=await C(`${i}/${c}`,{body:JSON.stringify(s),headers:a,method:"POST"});if(!r.ok)throw new Error(r.statusText);let o;return r.headers.get("content-type")==="application/json"?o=await r.json():o=await r.text(),{res:{data:o,status:r.status,statusText:r.statusText},error:null}}catch(r){const o=r;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}generateAccessTokenHeaders(){return this.adminSecret?{"x-hasura-admin-secret":this.adminSecret}:this.accessToken?{Authorization:`Bearer ${this.accessToken}`}:{}}}function k(t){const e="subdomain"in t||"backendUrl"in t?d(t,"graphql"):t.graphqlUrl;if(!e)throw new Error("Please provide `subdomain` or `graphqlUrl`.");return new A.NhostGraphqlClient({url:e,...t})}function T(t){const e="subdomain"in t||"backendUrl"in t?d(t,"storage"):t.storageUrl;if(!e)throw new Error("Please provide `subdomain` or `storageUrl`.");return new g.HasuraStorageClient({url:e,...t})}const v=t=>new U(t);class U{constructor({refreshIntervalTime:e,clientStorage:s,clientStorageType:n,autoRefreshToken:a,autoSignIn:i,adminSecret:c,devTools:r,start:o=!0,...u}){this.auth=b({refreshIntervalTime:e,clientStorage:s,clientStorageType:n,autoRefreshToken:a,autoSignIn:i,start:o,...u}),this.storage=T({adminSecret:c,...u}),this.functions=p({adminSecret:c,...u}),this.graphql=k({adminSecret:c,...u}),this.auth.onAuthStateChanged((l,h)=>{l==="SIGNED_OUT"&&(this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0))}),this.auth.onTokenChanged(l=>{const h=l==null?void 0:l.accessToken;this.storage.setAccessToken(h),this.functions.setAccessToken(h),this.graphql.setAccessToken(h)}),this._adminSecret=c,this.devTools=r}get adminSecret(){return this._adminSecret}set adminSecret(e){this._adminSecret=e,this.storage.setAdminSecret(e)}}exports.NhostClient=U;exports.NhostFunctionsClient=w;exports.createAuthClient=b;exports.createFunctionsClient=p;exports.createGraphqlClient=k;exports.createNhostClient=v;exports.createStorageClient=T;for(const t in f)t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>f[t]});for(const t in g)t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>g[t]}); | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -6,18 +6,18 @@ import { HasuraAuthClient as f } from "@nhost/hasura-auth-js"; | ||
import w from "cross-fetch"; | ||
import { NhostGraphqlClient as T } from "@nhost/graphql-js"; | ||
import { NhostGraphqlClient as p } from "@nhost/graphql-js"; | ||
const b = /^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>(\d+|__\w+__)))?$/; | ||
function a(t, e) { | ||
const { backendUrl: s, subdomain: o, region: c } = t; | ||
function d(t, e) { | ||
const { backendUrl: s, subdomain: n, region: a } = t; | ||
if (s) | ||
return `${s}/v1/${e}`; | ||
if (!o) | ||
if (!n) | ||
throw new Error("Either `backendUrl` or `subdomain` must be set."); | ||
const r = o.match(b); | ||
if (r != null && r.groups) { | ||
const { protocol: n = "http", host: l, port: d = 1337 } = r.groups, i = m(e); | ||
return i || `${n}://${l}:${d}/v1/${e}`; | ||
const i = n.match(b); | ||
if (i != null && i.groups) { | ||
const { protocol: c = "http", host: r, port: o = 1337 } = i.groups, u = U(e); | ||
return u || `${c}://${r}:${o}/v1/${e}`; | ||
} | ||
if (!c) | ||
if (!a) | ||
throw new Error('`region` must be set when using a `subdomain` other than "localhost".'); | ||
return `https://${o}.${e}.${c}.nhost.run/v1`; | ||
return `https://${n}.${e}.${a}.nhost.run/v1`; | ||
} | ||
@@ -27,10 +27,10 @@ function k() { | ||
} | ||
function p() { | ||
function T() { | ||
return typeof process < "u" && process.env; | ||
} | ||
function U(t) { | ||
return k() || !T() ? null : process.env[`NHOST_${t.toUpperCase()}_URL`]; | ||
} | ||
function m(t) { | ||
return k() || !p() ? null : process.env[`NHOST_${t.toUpperCase()}_URL`]; | ||
} | ||
function U(t) { | ||
const e = "subdomain" in t || "backendUrl" in t ? a(t, "auth") : t.authUrl; | ||
const e = "subdomain" in t || "backendUrl" in t ? d(t, "auth") : t.authUrl; | ||
if (!e) | ||
@@ -41,3 +41,3 @@ throw new Error("Please provide `subdomain` or `authUrl`."); | ||
function A(t) { | ||
const e = "subdomain" in t || "backendUrl" in t ? a(t, "functions") : t.functionsUrl; | ||
const e = "subdomain" in t || "backendUrl" in t ? d(t, "functions") : t.functionsUrl; | ||
if (!e) | ||
@@ -49,15 +49,15 @@ throw new Error("Please provide `subdomain` or `functionsUrl`."); | ||
constructor(e) { | ||
const { url: s, adminSecret: o } = e; | ||
this.url = s, this.accessToken = null, this.adminSecret = o; | ||
const { url: s, adminSecret: n } = e; | ||
this.url = s, this.accessToken = null, this.adminSecret = n; | ||
} | ||
async call(e, s, o) { | ||
const c = { | ||
async call(e, s, n) { | ||
const a = { | ||
"Content-Type": "application/json", | ||
...this.generateAccessTokenHeaders(), | ||
...o == null ? void 0 : o.headers | ||
}; | ||
...n == null ? void 0 : n.headers | ||
}, i = this.url, c = e.startsWith("/") ? e : `/${e}`; | ||
try { | ||
const r = await w(e, { | ||
const r = await w(`${i}/${c}`, { | ||
body: JSON.stringify(s), | ||
headers: c, | ||
headers: a, | ||
method: "POST" | ||
@@ -67,20 +67,15 @@ }); | ||
throw new Error(r.statusText); | ||
let n; | ||
try { | ||
n = await r.json(); | ||
} catch { | ||
n = await r.text(); | ||
} | ||
return { | ||
res: { data: n, status: r.status, statusText: r.statusText }, | ||
let o; | ||
return r.headers.get("content-type") === "application/json" ? o = await r.json() : o = await r.text(), { | ||
res: { data: o, status: r.status, statusText: r.statusText }, | ||
error: null | ||
}; | ||
} catch (r) { | ||
const n = r; | ||
const o = r; | ||
return { | ||
res: null, | ||
error: { | ||
message: n.message, | ||
status: n.name === "AbortError" ? 0 : 500, | ||
error: n.name === "AbortError" ? "abort-error" : "unknown" | ||
message: o.message, | ||
status: o.name === "AbortError" ? 0 : 500, | ||
error: o.name === "AbortError" ? "abort-error" : "unknown" | ||
} | ||
@@ -106,9 +101,9 @@ }; | ||
function C(t) { | ||
const e = "subdomain" in t || "backendUrl" in t ? a(t, "graphql") : t.graphqlUrl; | ||
const e = "subdomain" in t || "backendUrl" in t ? d(t, "graphql") : t.graphqlUrl; | ||
if (!e) | ||
throw new Error("Please provide `subdomain` or `graphqlUrl`."); | ||
return new T({ url: e, ...t }); | ||
return new p({ url: e, ...t }); | ||
} | ||
function E(t) { | ||
const e = "subdomain" in t || "backendUrl" in t ? a(t, "storage") : t.storageUrl; | ||
const e = "subdomain" in t || "backendUrl" in t ? d(t, "storage") : t.storageUrl; | ||
if (!e) | ||
@@ -118,29 +113,29 @@ throw new Error("Please provide `subdomain` or `storageUrl`."); | ||
} | ||
const y = (t) => new v(t); | ||
class v { | ||
const y = (t) => new $(t); | ||
class $ { | ||
constructor({ | ||
refreshIntervalTime: e, | ||
clientStorage: s, | ||
clientStorageType: o, | ||
autoRefreshToken: c, | ||
autoSignIn: r, | ||
adminSecret: n, | ||
devTools: l, | ||
start: d = !0, | ||
...i | ||
clientStorageType: n, | ||
autoRefreshToken: a, | ||
autoSignIn: i, | ||
adminSecret: c, | ||
devTools: r, | ||
start: o = !0, | ||
...u | ||
}) { | ||
this.auth = U({ | ||
this.auth = m({ | ||
refreshIntervalTime: e, | ||
clientStorage: s, | ||
clientStorageType: o, | ||
autoRefreshToken: c, | ||
autoSignIn: r, | ||
start: d, | ||
...i | ||
}), this.storage = E({ adminSecret: n, ...i }), this.functions = A({ adminSecret: n, ...i }), this.graphql = C({ adminSecret: n, ...i }), this.auth.onAuthStateChanged((u, h) => { | ||
u === "SIGNED_OUT" && (this.storage.setAccessToken(void 0), this.functions.setAccessToken(void 0), this.graphql.setAccessToken(void 0)); | ||
}), this.auth.onTokenChanged((u) => { | ||
const h = u == null ? void 0 : u.accessToken; | ||
this.storage.setAccessToken(h), this.functions.setAccessToken(h), this.graphql.setAccessToken(h); | ||
}), this._adminSecret = n, this.devTools = l; | ||
clientStorageType: n, | ||
autoRefreshToken: a, | ||
autoSignIn: i, | ||
start: o, | ||
...u | ||
}), this.storage = E({ adminSecret: c, ...u }), this.functions = A({ adminSecret: c, ...u }), this.graphql = C({ adminSecret: c, ...u }), this.auth.onAuthStateChanged((h, l) => { | ||
h === "SIGNED_OUT" && (this.storage.setAccessToken(void 0), this.functions.setAccessToken(void 0), this.graphql.setAccessToken(void 0)); | ||
}), this.auth.onTokenChanged((h) => { | ||
const l = h == null ? void 0 : h.accessToken; | ||
this.storage.setAccessToken(l), this.functions.setAccessToken(l), this.graphql.setAccessToken(l); | ||
}), this._adminSecret = c, this.devTools = r; | ||
} | ||
@@ -155,5 +150,5 @@ get adminSecret() { | ||
export { | ||
v as NhostClient, | ||
$ as NhostClient, | ||
S as NhostFunctionsClient, | ||
U as createAuthClient, | ||
m as createAuthClient, | ||
A as createFunctionsClient, | ||
@@ -160,0 +155,0 @@ C as createGraphqlClient, |
{ | ||
"name": "@nhost/nhost-js", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Nhost JavaScript SDK", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
688687
892