@nhost/nhost-js
Advanced tools
Comparing version 2.2.4 to 2.2.5
@@ -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("isomorphic-unfetch"),C=require("@nhost/graphql-js"),A=/^((?<protocol>http[s]?):\/\/)?(?<host>(localhost|local))(:(?<port>(\d+|__\w+__)))?$/;function d(t,e){const{backendUrl:i,subdomain:s,region:l}=t;if(i)return`${i}/v1/${e}`;if(!s)throw new Error("Either `backendUrl` or `subdomain` must be set.");const c=s.match(A);if(c!=null&&c.groups){const{protocol:o,host:u,port:r}=c.groups,n=v(e);return n||(u==="localhost"?(console.warn('The `subdomain` is set to "localhost". Support for this will be removed in a future release. Please use "local" instead.'),`${o||"http"}://localhost:${r||1337}/v1/${e}`):r?`${o||"https"}://local.${e}.nhost.run:${r}/v1`:`${o||"https"}://local.${e}.nhost.run/v1`)}if(!l)throw new Error('`region` must be set when using a `subdomain` other than "local".');return`https://${s}.${e}.${l}.nhost.run/v1`}function m(){return typeof window!="undefined"}function $(){return typeof process!="undefined"&&process.env}function v(t){return m()||!$()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function y(t,e){const s=e.startsWith("/")?e:`/${e}`;return t+s}function p(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 b(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:i,adminSecret:s}=e;this.url=i,this.accessToken=null,this.adminSecret=s}async call(e,i,s){var o,u;const l={"Content-Type":"application/json",...this.generateAccessTokenHeaders(),...s==null?void 0:s.headers},c=y(this.url,e);try{const r=await U(c,{body:i?JSON.stringify(i):null,headers:l,method:"POST"});if(!r.ok){let a;return(o=r.headers.get("content-type"))!=null&&o.includes("application/json")?a=await r.json():a=await r.text(),{res:null,error:{message:a,error:r.statusText,status:r.status}}}let n;return(u=r.headers.get("content-type"))!=null&&u.includes("application/json")?n=await r.json():n=await r.text(),{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 T(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 C.NhostGraphqlClient({url:e,...t})}function k(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 q=t=>new S(t);class S{constructor({refreshIntervalTime:e,clientStorage:i,clientStorageType:s,autoRefreshToken:l,autoSignIn:c,adminSecret:o,devTools:u,start:r=!0,...n}){this.auth=p({refreshIntervalTime:e,clientStorage:i,clientStorageType:s,autoRefreshToken:l,autoSignIn:c,start:r,...n}),this.storage=k({adminSecret:o,...n}),this.functions=b({adminSecret:o,...n}),this.graphql=T({adminSecret:o,...n}),this.auth.onAuthStateChanged((a,h)=>{a==="SIGNED_OUT"&&(this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0))}),this.auth.onTokenChanged(a=>{const h=a==null?void 0:a.accessToken;this.storage.setAccessToken(h),this.functions.setAccessToken(h),this.graphql.setAccessToken(h)}),this._adminSecret=o,this.devTools=u}get adminSecret(){return this._adminSecret}set adminSecret(e){this._adminSecret=e,this.storage.setAdminSecret(e)}}exports.NhostClient=S;exports.NhostFunctionsClient=w;exports.createAuthClient=p;exports.createFunctionsClient=b;exports.createGraphqlClient=T;exports.createNhostClient=q;exports.createStorageClient=k;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("isomorphic-unfetch"),k=require("@nhost/graphql-js"),A=/^((?<protocol>http[s]?):\/\/)?(?<host>(localhost|local))(:(?<port>(\d+|__\w+__)))?$/;function d(t,e){const{backendUrl:i,subdomain:s,region:l}=t;if(i)return`${i}/v1/${e}`;if(!s)throw new Error("Either `backendUrl` or `subdomain` must be set.");const c=s.match(A);if(c!=null&&c.groups){const{protocol:o,host:u,port:r}=c.groups,n=$(e);return n||(u==="localhost"?(console.warn('The `subdomain` is set to "localhost". Support for this will be removed in a future release. Please use "local" instead.'),`${o||"http"}://localhost:${r||1337}/v1/${e}`):r?`${o||"https"}://local.${e}.nhost.run:${r}/v1`:`${o||"https"}://local.${e}.nhost.run/v1`)}if(!l)throw new Error('`region` must be set when using a `subdomain` other than "local".');return`https://${s}.${e}.${l}.nhost.run/v1`}function m(){return typeof window!="undefined"}function y(){return typeof process!="undefined"&&process.env}function $(t){return m()||!y()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function v(t,e){const s=e.startsWith("/")?e:`/${e}`;return t+s}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:i,adminSecret:s}=e;this.url=i,this.accessToken=null,this.adminSecret=s}async call(e,i,s){var o,u;const l={"Content-Type":"application/json",...this.generateAccessTokenHeaders(),...s==null?void 0:s.headers},c=v(this.url,e);try{const r=await C(c,{body:i?JSON.stringify(i):null,headers:l,method:"POST"});if(!r.ok){let a;return(o=r.headers.get("content-type"))!=null&&o.includes("application/json")?a=await r.json():a=await r.text(),{res:null,error:{message:a,error:r.statusText,status:r.status}}}let n;return(u=r.headers.get("content-type"))!=null&&u.includes("application/json")?n=await r.json():n=await r.text(),{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 T(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 k.NhostGraphqlClient({url:e,...t})}function S(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 E=t=>new U(t);class U{constructor({refreshIntervalTime:e,clientStorage:i,clientStorageType:s,autoRefreshToken:l,autoSignIn:c,adminSecret:o,devTools:u,start:r=!0,...n}){this.auth=b({refreshIntervalTime:e,clientStorage:i,clientStorageType:s,autoRefreshToken:l,autoSignIn:c,start:r,...n}),this.storage=S({adminSecret:o,...n}),this.functions=p({adminSecret:o,...n}),this.graphql=T({adminSecret:o,...n}),this.auth.onAuthStateChanged((a,h)=>{a==="SIGNED_OUT"&&(this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0))}),this.auth.onTokenChanged(a=>{const h=a==null?void 0:a.accessToken;this.storage.setAccessToken(h),this.functions.setAccessToken(h),this.graphql.setAccessToken(h)}),this._adminSecret=o,this.devTools=u}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=T;exports.createNhostClient=E;exports.createStorageClient=S;Object.keys(f).forEach(t=>{t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>f[t]})});Object.keys(g).forEach(t=>{t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:()=>g[t]})}); | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -55,2 +55,43 @@ import { HasuraAuthClient as f } from "@nhost/hasura-auth-js"; | ||
} | ||
/** | ||
* Use `nhost.functions.call` to call (sending a POST request to) a serverless function. Use generic | ||
* types to specify the expected response data, request body and error message. | ||
* | ||
* @example | ||
* ### Without generic types | ||
* ```ts | ||
* await nhost.functions.call('send-welcome-email', { email: 'joe@example.com', name: 'Joe Doe' }) | ||
* ``` | ||
* | ||
* @example | ||
* ### Using generic types | ||
* ```ts | ||
* type Data = { | ||
* message: string | ||
* } | ||
* | ||
* type Body = { | ||
* email: string | ||
* name: string | ||
* } | ||
* | ||
* type ErrorMessage = { | ||
* details: string | ||
* } | ||
* | ||
* // The function will only accept a body of type `Body` | ||
* const { res, error } = await nhost.functions.call<Data, Body, ErrorMessage>( | ||
* 'send-welcome-email', | ||
* { email: 'joe@example.com', name: 'Joe Doe' } | ||
* ) | ||
* | ||
* // Now the response data is typed as `Data` | ||
* console.log(res?.data.message) | ||
* | ||
* // Now the error message is typed as `ErrorMessage` | ||
* console.log(error?.message.details) | ||
* ``` | ||
* | ||
* @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/call | ||
*/ | ||
async call(t, i, o) { | ||
@@ -97,2 +138,12 @@ var s, c; | ||
} | ||
/** | ||
* Use `nhost.functions.setAccessToken` to a set an access token to be used in subsequent functions requests. Note that if you're signin in users with `nhost.auth.signIn()` the access token will be set automatically. | ||
* | ||
* @example | ||
* ```ts | ||
* nhost.functions.setAccessToken('some-access-token') | ||
* ``` | ||
* | ||
* @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/set-access-token | ||
*/ | ||
setAccessToken(t) { | ||
@@ -127,2 +178,12 @@ if (!t) { | ||
class E { | ||
/** | ||
* Nhost Client | ||
* | ||
* @example | ||
* ```ts | ||
* const nhost = new NhostClient({ subdomain, region }); | ||
* ``` | ||
* | ||
* @docs https://docs.nhost.io/reference/javascript | ||
*/ | ||
constructor({ | ||
@@ -129,0 +190,0 @@ refreshIntervalTime: t, |
{ | ||
"name": "@nhost/nhost-js", | ||
"version": "2.2.4", | ||
"version": "2.2.5", | ||
"description": "Nhost JavaScript SDK", | ||
@@ -35,4 +35,4 @@ "license": "MIT", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"node": "./dist/index.cjs.js", | ||
@@ -49,5 +49,5 @@ "default": "./dist/index.esm.js" | ||
"isomorphic-unfetch": "^3.1.0", | ||
"@nhost/graphql-js": "0.1.2", | ||
"@nhost/hasura-auth-js": "2.1.3", | ||
"@nhost/hasura-storage-js": "2.1.2" | ||
"@nhost/graphql-js": "0.1.3", | ||
"@nhost/hasura-auth-js": "2.1.4", | ||
"@nhost/hasura-storage-js": "2.1.3" | ||
}, | ||
@@ -54,0 +54,0 @@ "devDependencies": { |
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
667723
990
+ Added@nhost/graphql-js@0.1.3(transitive)
+ Added@nhost/hasura-auth-js@2.1.4(transitive)
+ Added@nhost/hasura-storage-js@2.1.3(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
- Removed@nhost/graphql-js@0.1.2(transitive)
- Removed@nhost/hasura-auth-js@2.1.3(transitive)
- Removed@nhost/hasura-storage-js@2.1.2(transitive)
- Removedget-intrinsic@1.3.0(transitive)
Updated@nhost/graphql-js@0.1.3
Updated@nhost/hasura-auth-js@2.1.4