@nhost/nhost-js 1.5.2 → 1.6.0
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.
- package/dist/clients/auth.d.ts +7 -0
- package/dist/clients/auth.d.ts.map +1 -0
- package/dist/clients/functions.d.ts +6 -1
- package/dist/clients/functions.d.ts.map +1 -1
- package/dist/clients/graphql.d.ts +5 -1
- package/dist/clients/graphql.d.ts.map +1 -1
- package/dist/clients/index.d.ts +3 -0
- package/dist/clients/index.d.ts.map +1 -1
- package/dist/{core/nhost-client.d.ts → clients/nhost.d.ts} +4 -3
- package/dist/clients/nhost.d.ts.map +1 -0
- package/dist/clients/storage.d.ts +7 -0
- package/dist/clients/storage.d.ts.map +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +168 -178
- package/dist/index.esm.js.map +1 -1
- package/dist/utils/helpers.d.ts +1 -1
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/utils/types.d.ts +13 -7
- package/dist/utils/types.d.ts.map +1 -1
- package/package.json +7 -6
- package/umd/nhost-js.umd.js +8 -7
- package/umd/nhost-js.umd.js.map +1 -1
- package/dist/core/index.d.ts +0 -3
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/nhost-client.d.ts.map +0 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HasuraAuthClient } from '@nhost/hasura-auth-js';
|
|
2
|
+
import { NhostClientConstructorParams } from '../utils/types';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a client for Auth from either a subdomain or a URL
|
|
5
|
+
*/
|
|
6
|
+
export declare function createAuthClient(params: NhostClientConstructorParams): HasuraAuthClient;
|
|
7
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAGxD,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAA;AAE7D;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,4BAA4B,oBAWpE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from 'axios';
|
|
2
|
-
import { FunctionCallResponse } from '../utils/types';
|
|
2
|
+
import { FunctionCallResponse, NhostClientConstructorParams } from '../utils/types';
|
|
3
3
|
export interface NhostFunctionsConstructorParams {
|
|
4
4
|
/**
|
|
5
5
|
* Serverless Functions endpoint.
|
|
@@ -10,10 +10,15 @@ export interface NhostFunctionsConstructorParams {
|
|
|
10
10
|
*/
|
|
11
11
|
adminSecret?: string;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Creates a client for Functions from either a subdomain or a URL
|
|
15
|
+
*/
|
|
16
|
+
export declare function createFunctionsClient(params: NhostClientConstructorParams): NhostFunctionsClient;
|
|
13
17
|
/**
|
|
14
18
|
* @alias Functions
|
|
15
19
|
*/
|
|
16
20
|
export declare class NhostFunctionsClient {
|
|
21
|
+
private url;
|
|
17
22
|
private instance;
|
|
18
23
|
private accessToken;
|
|
19
24
|
private adminSecret?;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["functions.ts"],"names":[],"mappings":"AAAA,OAAc,
|
|
1
|
+
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["functions.ts"],"names":[],"mappings":"AAAA,OAAc,EAEZ,kBAAkB,EAGnB,MAAM,OAAO,CAAA;AAGd,OAAO,EAAE,oBAAoB,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAA;AACnF,MAAM,WAAW,+BAA+B;IAC9C;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,4BAA4B,wBAWzE;AAED;;GAEG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,GAAG,CAAQ;IACnB,OAAO,CAAC,QAAQ,CAAe;IAC/B,OAAO,CAAC,WAAW,CAAe;IAClC,OAAO,CAAC,WAAW,CAAC,CAAQ;gBAEhB,MAAM,EAAE,+BAA+B;IAWnD;;;;;;;;;OASG;IACG,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,GAAG,EAC7B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;IAyBnC;;;;;;;;;OASG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS;IAS9C,OAAO,CAAC,0BAA0B;CAanC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from 'axios';
|
|
2
2
|
import { DocumentNode } from 'graphql';
|
|
3
|
-
import { GraphqlRequestResponse } from '../utils/types';
|
|
3
|
+
import { GraphqlRequestResponse, NhostClientConstructorParams } from '../utils/types';
|
|
4
4
|
export interface NhostGraphqlConstructorParams {
|
|
5
5
|
/**
|
|
6
6
|
* GraphQL endpoint.
|
|
@@ -11,6 +11,10 @@ export interface NhostGraphqlConstructorParams {
|
|
|
11
11
|
*/
|
|
12
12
|
adminSecret?: string;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a client for GraphQL from either a subdomain or a URL
|
|
16
|
+
*/
|
|
17
|
+
export declare function createGraphqlClient(params: NhostClientConstructorParams): NhostGraphqlClient;
|
|
14
18
|
/**
|
|
15
19
|
* @alias GraphQL
|
|
16
20
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["graphql.ts"],"names":[],"mappings":"AAAA,OAAc,EAAiB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["graphql.ts"],"names":[],"mappings":"AAAA,OAAc,EAAiB,kBAAkB,EAA0B,MAAM,OAAO,CAAA;AACxF,OAAO,EAAE,YAAY,EAAS,MAAM,SAAS,CAAA;AAG7C,OAAO,EACL,sBAAsB,EAEtB,4BAA4B,EAC7B,MAAM,gBAAgB,CAAA;AAEvB,MAAM,WAAW,6BAA6B;IAC5C;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,4BAA4B,sBAWvE;AAED;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,GAAG,CAAQ;IACnB,OAAO,CAAC,QAAQ,CAAe;IAC/B,OAAO,CAAC,WAAW,CAAe;IAClC,OAAO,CAAC,WAAW,CAAC,CAAQ;gBAEhB,MAAM,EAAE,6BAA6B;IAWjD;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAC5B,QAAQ,EAAE,MAAM,GAAG,YAAY,EAC/B,SAAS,CAAC,EAAE,CAAC,EACb,MAAM,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC;IAiDrC;;;;;;;;;OASG;IACH,MAAM,IAAI,MAAM;IAIhB;;;;;;;;;OASG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS;IAS9C,OAAO,CAAC,0BAA0B;CAanC"}
|
package/dist/clients/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA;AACtB,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { HasuraAuthClient } from '@nhost/hasura-auth-js';
|
|
2
2
|
import { HasuraStorageClient } from '@nhost/hasura-storage-js';
|
|
3
|
-
import { NhostFunctionsClient } from '../clients/functions';
|
|
4
|
-
import { NhostGraphqlClient } from '../clients/graphql';
|
|
5
3
|
import { NhostClientConstructorParams } from '../utils/types';
|
|
4
|
+
import { NhostFunctionsClient } from './functions';
|
|
5
|
+
import { NhostGraphqlClient } from './graphql';
|
|
6
|
+
export declare const createNhostClient: (params: NhostClientConstructorParams) => NhostClient;
|
|
6
7
|
export declare class NhostClient {
|
|
7
8
|
auth: HasuraAuthClient;
|
|
8
9
|
storage: HasuraStorageClient;
|
|
@@ -24,4 +25,4 @@ export declare class NhostClient {
|
|
|
24
25
|
get adminSecret(): string | undefined;
|
|
25
26
|
set adminSecret(newValue: string | undefined);
|
|
26
27
|
}
|
|
27
|
-
//# sourceMappingURL=nhost
|
|
28
|
+
//# sourceMappingURL=nhost.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nhost.d.ts","sourceRoot":"","sources":["nhost.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAE9D,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAA;AAG7D,OAAO,EAAyB,oBAAoB,EAAE,MAAM,aAAa,CAAA;AACzE,OAAO,EAAuB,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAGnE,eAAO,MAAM,iBAAiB,WAAY,4BAA4B,gBAA4B,CAAA;AAClG,qBAAa,WAAW;IACtB,IAAI,EAAE,gBAAgB,CAAA;IACtB,OAAO,EAAE,mBAAmB,CAAA;IAC5B,SAAS,EAAE,oBAAoB,CAAA;IAC/B,OAAO,EAAE,kBAAkB,CAAA;IAC3B,OAAO,CAAC,YAAY,CAAC,CAAQ;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAE3B;;;;;;;;;OASG;gBACS,EACV,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,QAAQ,EACR,KAAY,EACZ,GAAG,SAAS,EACb,EAAE,4BAA4B;IA2C/B,IAAI,WAAW,IAAI,MAAM,GAAG,SAAS,CAEpC;IAED,IAAI,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAM3C;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HasuraStorageClient } from '@nhost/hasura-storage-js';
|
|
2
|
+
import { NhostClientConstructorParams } from '../utils/types';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a client for Storage from either a subdomain or a URL
|
|
5
|
+
*/
|
|
6
|
+
export declare function createStorageClient(params: NhostClientConstructorParams): HasuraStorageClient;
|
|
7
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAG9D,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAA;AAE7D;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,4BAA4B,uBAWvE"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});
|
|
1
|
+
"use strict";var C=Object.defineProperty;var v=(t,e,r)=>e in t?C(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var o=(t,e,r)=>(v(t,typeof e!="symbol"?e+"":e,r),r);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("@nhost/hasura-auth-js"),E=require("axios"),y=require("graphql"),_=require("@nhost/hasura-storage-js"),$=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},g=$(E),N=/^((?<protocol>http[s]?):\/\/)?(?<host>localhost)(:(?<port>\d+))?$/;function f(t,e){const{backendUrl:r,subdomain:n,region:c}=t;if(r)return`${r}/v1/${e}`;if(!n)throw new Error("Either `backendUrl` or `subdomain` must be set.");const s=n.match(N);if(s!=null&&s.groups){const{protocol:u="http",host:h,port:i=1337}=s.groups,l=L(e);return l||`${u}://${h}:${i}/v1/${e}`}if(!c)throw new Error('`region` must be set when using a `subdomain` other than "localhost".');return`https://${n}.${e}.${c}.nhost.run/v1`}function G(){return typeof window<"u"}function H(){return typeof process<"u"&&process.env}function L(t){return G()||!H()?null:process.env[`NHOST_${t.toUpperCase()}_URL`]}function T(t){const e="subdomain"in t||"backendUrl"in t?f(t,"auth"):t.authUrl;if(!e)throw new Error("Please provide `subdomain` or `authUrl`.");return new m.HasuraAuthClient({url:e,...t})}function p(t){const e="subdomain"in t||"backendUrl"in t?f(t,"functions"):t.functionsUrl;if(!e)throw new Error("Please provide `subdomain` or `functionsUrl`.");return new A({url:e,...t})}class A{constructor(e){o(this,"url");o(this,"instance");o(this,"accessToken");o(this,"adminSecret");const{url:r,adminSecret:n}=e;this.url=r,this.accessToken=null,this.adminSecret=n,this.instance=g.default.create({baseURL:r})}async call(e,r,n){const c={...this.generateAccessTokenHeaders(),...n==null?void 0:n.headers};let s;try{s=await this.instance.post(e,r,{...n,headers:c})}catch(u){if(u instanceof Error)return{res:null,error:u}}return s?{res:s,error:null}:{res:null,error:new Error("Unable to make post request to funtion")}}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 b(t){const e="subdomain"in t||"backendUrl"in t?f(t,"graphql"):t.graphqlUrl;if(!e)throw new Error("Please provide `subdomain` or `graphqlUrl`.");return new S({url:e,...t})}class S{constructor(e){o(this,"url");o(this,"instance");o(this,"accessToken");o(this,"adminSecret");const{url:r,adminSecret:n}=e;this.url=r,this.accessToken=null,this.adminSecret=n,this.instance=g.default.create({baseURL:r})}async request(e,r,n){const c={...this.generateAccessTokenHeaders(),...n==null?void 0:n.headers};try{const s="",h=(await this.instance.post("",{operationName:s||void 0,query:typeof e=="string"?e:y.print(e),variables:r},{...n,headers:c})).data,{data:i}=h;return h.errors?{data:null,error:h.errors}:typeof i!="object"||Array.isArray(i)||i===null?{data:null,error:new Error("incorrect response data from GraphQL server")}:{data:i,error:null}}catch(s){return s instanceof Error?{data:null,error:s}:(console.error(s),{data:null,error:new Error("Unable to get do GraphQL request")})}}getUrl(){return this.url}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 U(t){const e="subdomain"in t||"backendUrl"in t?f(t,"storage"):t.storageUrl;if(!e)throw new Error("Please provide `subdomain` or `storageUrl`.");return new _.HasuraStorageClient({url:e,...t})}const F=t=>new w(t);class w{constructor({refreshIntervalTime:e,clientStorageGetter:r,clientStorageSetter:n,clientStorage:c,clientStorageType:s,autoRefreshToken:u,autoSignIn:h,adminSecret:i,devTools:l,start:q=!0,...d}){o(this,"auth");o(this,"storage");o(this,"functions");o(this,"graphql");o(this,"_adminSecret");o(this,"devTools");var k;this.auth=T({refreshIntervalTime:e,clientStorageGetter:r,clientStorageSetter:n,clientStorage:c,clientStorageType:s,autoRefreshToken:u,autoSignIn:h,start:q,...d}),this.storage=U({adminSecret:i,...d}),this.functions=p({adminSecret:i,...d}),this.graphql=b({adminSecret:i,...d}),this.storage.setAccessToken(this.auth.getAccessToken()),this.functions.setAccessToken(this.auth.getAccessToken()),this.graphql.setAccessToken(this.auth.getAccessToken()),(k=this.auth.client)==null||k.onStart(()=>{this.auth.onAuthStateChanged((a,j)=>{a==="SIGNED_OUT"&&(this.storage.setAccessToken(void 0),this.functions.setAccessToken(void 0),this.graphql.setAccessToken(void 0))}),this.auth.onTokenChanged(a=>{this.storage.setAccessToken(a==null?void 0:a.accessToken),this.functions.setAccessToken(a==null?void 0:a.accessToken),this.graphql.setAccessToken(a==null?void 0:a.accessToken)})}),this._adminSecret=i,this.devTools=l}get adminSecret(){return this._adminSecret}set adminSecret(e){this._adminSecret=e,this.storage.setAdminSecret(e)}}exports.NhostClient=w;exports.NhostFunctionsClient=A;exports.NhostGraphqlClient=S;exports.createAuthClient=T;exports.createFunctionsClient=p;exports.createGraphqlClient=b;exports.createNhostClient=F;exports.createStorageClient=U;
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/clients/functions.ts","../src/clients/graphql.ts","../src/utils/helpers.ts","../src/core/nhost-client.ts","../src/index.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from 'axios'\n\nimport { FunctionCallResponse } from '../utils/types'\nexport interface NhostFunctionsConstructorParams {\n /**\n * Serverless Functions endpoint.\n */\n url: string\n /**\n * Admin secret. When set, it is sent as an `x-hasura-admin-secret` header for all requests.\n */\n adminSecret?: string\n}\n\n/**\n * @alias Functions\n */\nexport class NhostFunctionsClient {\n private instance: AxiosInstance\n private accessToken: string | null\n private adminSecret?: string\n\n constructor(params: NhostFunctionsConstructorParams) {\n const { url, adminSecret } = params\n\n this.accessToken = null\n this.adminSecret = adminSecret\n this.instance = axios.create({\n baseURL: url\n })\n }\n\n /**\n * Use `nhost.functions.call` to call (sending a POST request to) a serverless function.\n *\n * @example\n * ```ts\n * await nhost.functions.call('send-welcome-email', { email: 'joe@example.com', name: 'Joe Doe' })\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/call\n */\n async call<T = unknown, D = any>(\n url: string,\n data: D,\n config?: AxiosRequestConfig\n ): Promise<FunctionCallResponse<T>> {\n const headers = {\n ...this.generateAccessTokenHeaders(),\n ...config?.headers\n }\n\n let res\n try {\n res = await this.instance.post<T, AxiosResponse<T>, D>(url, data, { ...config, headers })\n } catch (error) {\n if (error instanceof Error) {\n return { res: null, error }\n }\n }\n\n if (!res) {\n return {\n res: null,\n error: new Error('Unable to make post request to funtion')\n }\n }\n\n return { res, error: null }\n }\n\n /**\n * 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.\n *\n * @example\n * ```ts\n * nhost.functions.setAccessToken('some-access-token')\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/set-access-token\n */\n setAccessToken(accessToken: string | undefined) {\n if (!accessToken) {\n this.accessToken = null\n return\n }\n\n this.accessToken = accessToken\n }\n\n private generateAccessTokenHeaders(): AxiosRequestHeaders {\n if (this.adminSecret) {\n return {\n 'x-hasura-admin-secret': this.adminSecret\n }\n }\n if (this.accessToken) {\n return {\n Authorization: `Bearer ${this.accessToken}`\n }\n }\n return {}\n }\n}\n","import axios, { AxiosInstance, AxiosRequestConfig, AxiosRequestHeaders } from 'axios'\nimport { DocumentNode, print } from 'graphql'\n\nimport { GraphqlRequestResponse, GraphqlResponse } from '../utils/types'\n\nexport interface NhostGraphqlConstructorParams {\n /**\n * GraphQL endpoint.\n */\n url: string\n /**\n * Admin secret. When set, it is sent as an `x-hasura-admin-secret` header for all requests.\n */\n adminSecret?: string\n}\n\n/**\n * @alias GraphQL\n */\nexport class NhostGraphqlClient {\n private url: string\n private instance: AxiosInstance\n private accessToken: string | null\n private adminSecret?: string\n\n constructor(params: NhostGraphqlConstructorParams) {\n const { url, adminSecret } = params\n\n this.url = url\n this.accessToken = null\n this.adminSecret = adminSecret\n this.instance = axios.create({\n baseURL: url\n })\n }\n\n /**\n * Use `nhost.graphql.request` to send a GraphQL request. For more serious GraphQL usage we recommend using a GraphQL client such as Apollo Client (https://www.apollographql.com/docs/react).\n *\n * @example\n * ```ts\n * const CUSTOMERS = gql`\n * query {\n * customers {\n * id\n * name\n * }\n * }\n * `\n * const { data, error } = await nhost.graphql.request(CUSTOMERS)\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/request\n */\n async request<T = any, V = any>(\n document: string | DocumentNode,\n variables?: V,\n config?: AxiosRequestConfig\n ): Promise<GraphqlRequestResponse<T>> {\n // add auth headers if any\n const headers = {\n ...this.generateAccessTokenHeaders(),\n ...config?.headers\n }\n\n try {\n const operationName = ''\n const res = await this.instance.post<GraphqlResponse<T>>(\n '',\n {\n operationName: operationName || undefined,\n query: typeof document === 'string' ? document : print(document),\n variables\n },\n { ...config, headers }\n )\n\n const responseData = res.data\n const { data } = responseData\n\n if (responseData.errors) {\n return {\n data: null,\n error: responseData.errors\n }\n }\n\n if (typeof data !== 'object' || Array.isArray(data) || data === null) {\n return {\n data: null,\n error: new Error('incorrect response data from GraphQL server')\n }\n }\n\n return { data, error: null }\n } catch (error) {\n if (error instanceof Error) {\n return { data: null, error }\n }\n console.error(error)\n return {\n data: null,\n error: new Error('Unable to get do GraphQL request')\n }\n }\n }\n\n /**\n * Use `nhost.graphql.getUrl` to get the GraphQL URL.\n *\n * @example\n * ```ts\n * const url = nhost.graphql.getUrl();\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/get-url\n */\n getUrl(): string {\n return this.url\n }\n\n /**\n * Use `nhost.graphql.setAccessToken` to a set an access token to be used in subsequent graphql requests. Note that if you're signin in users with `nhost.auth.signIn()` the access token will be set automatically.\n *\n * @example\n * ```ts\n * nhost.graphql.setAccessToken('some-access-token')\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/set-access-token\n */\n setAccessToken(accessToken: string | undefined) {\n if (!accessToken) {\n this.accessToken = null\n return\n }\n\n this.accessToken = accessToken\n }\n\n private generateAccessTokenHeaders(): AxiosRequestHeaders {\n if (this.adminSecret) {\n return {\n 'x-hasura-admin-secret': this.adminSecret\n }\n }\n if (this.accessToken) {\n return {\n Authorization: `Bearer ${this.accessToken}`\n }\n }\n return {}\n }\n}\n","import { NhostClientConstructorParams } from './types'\n\nconst LOCALHOST_REGEX = /^localhost(:\\d+)*$/g\n\n/**\n * `backendUrl` should now be used only when self-hosting\n * `subdomain` and `region` should be used instead when using the Nhost platform\n * `\n * @param backendOrSubdomain\n * @param service\n * @returns\n */\nexport function urlFromParams(\n backendOrSubdomain: Pick<NhostClientConstructorParams, 'region' | 'subdomain' | 'backendUrl'>,\n service: string\n) {\n const { backendUrl, subdomain, region } = backendOrSubdomain\n\n if (!backendUrl && !subdomain) {\n throw new Error('Either `backendUrl` or `subdomain` must be set.')\n }\n\n if (backendUrl) {\n return `${backendUrl}/v1/${service}`\n }\n\n // to make TS happy\n if (!subdomain) {\n throw new Error('`subdomain` must be set if `backendUrl` is not set.')\n }\n\n // check if subdomain is localhost[:port]\n const subdomainLocalhostFound = subdomain.match(LOCALHOST_REGEX)\n if (subdomainLocalhostFound && subdomainLocalhostFound.length > 0) {\n const localhostFound = subdomainLocalhostFound[0]\n\n // no port specified, use standard port 1337\n if (localhostFound === 'localhost') {\n return `http://localhost:1337/v1/${service}`\n }\n\n // port specified\n return `http://${localhostFound}/v1/${service}`\n }\n\n if (!region) {\n throw new Error('`region` must be set when using a `subdomain` other than \"localhost\".')\n }\n\n return `https://${subdomain}.${service}.${region}.nhost.run/v1`\n}\n","import { HasuraAuthClient } from '@nhost/hasura-auth-js'\nimport { HasuraStorageClient } from '@nhost/hasura-storage-js'\n\nimport { NhostFunctionsClient } from '../clients/functions'\nimport { NhostGraphqlClient } from '../clients/graphql'\nimport { urlFromParams } from '../utils/helpers'\nimport { NhostClientConstructorParams } from '../utils/types'\nexport class NhostClient {\n auth: HasuraAuthClient\n storage: HasuraStorageClient\n functions: NhostFunctionsClient\n graphql: NhostGraphqlClient\n private _adminSecret?: string\n readonly devTools?: boolean\n\n /**\n * Nhost Client\n *\n * @example\n * ```ts\n * const nhost = new NhostClient({ subdomain, region });\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript\n */\n constructor({\n refreshIntervalTime,\n clientStorageGetter,\n clientStorageSetter,\n clientStorage,\n clientStorageType,\n autoRefreshToken,\n autoSignIn,\n adminSecret,\n devTools,\n start = true,\n ...urlParams\n }: NhostClientConstructorParams) {\n this.auth = new HasuraAuthClient({\n url: urlFromParams(urlParams, 'auth'),\n refreshIntervalTime,\n clientStorageGetter,\n clientStorageSetter,\n clientStorage,\n clientStorageType,\n autoRefreshToken,\n autoSignIn,\n start\n })\n\n this.storage = new HasuraStorageClient({\n url: urlFromParams(urlParams, 'storage'),\n adminSecret\n })\n\n this.functions = new NhostFunctionsClient({\n url: urlFromParams(urlParams, 'functions'),\n adminSecret\n })\n\n this.graphql = new NhostGraphqlClient({\n url: urlFromParams(urlParams, 'graphql'),\n adminSecret\n })\n\n // * Set current token if token is already accessable\n this.storage.setAccessToken(this.auth.getAccessToken())\n this.functions.setAccessToken(this.auth.getAccessToken())\n this.graphql.setAccessToken(this.auth.getAccessToken())\n\n this.auth.client?.onStart(() => {\n // * Set access token when signing out\n this.auth.onAuthStateChanged((_event, session) => {\n if (_event === 'SIGNED_OUT') {\n this.storage.setAccessToken(undefined)\n this.functions.setAccessToken(undefined)\n this.graphql.setAccessToken(undefined)\n }\n })\n\n // * Update access token for clients, including when signin in\n this.auth.onTokenChanged((session) => {\n this.storage.setAccessToken(session?.accessToken)\n this.functions.setAccessToken(session?.accessToken)\n this.graphql.setAccessToken(session?.accessToken)\n })\n })\n this._adminSecret = adminSecret\n this.devTools = devTools\n }\n\n get adminSecret(): string | undefined {\n return this._adminSecret\n }\n\n set adminSecret(newValue: string | undefined) {\n this._adminSecret = newValue\n this.storage.setAdminSecret(newValue)\n // TODO inconsistent API: storage can change admin secret, but functions/graphql cannot\n // this.functions.setAdminSecret(newValue)\n // this.graphql.setAdminSecret(newValue)\n }\n}\n","import { NhostClientConstructorParams } from './utils/types'\nimport { NhostClient } from './core'\n\nconst createClient = (config: NhostClientConstructorParams) => new NhostClient(config)\n\nexport * from './clients'\nexport * from './core'\nexport * from './utils/types'\nexport { createClient }\n"],"names":["NhostFunctionsClient","params","url","adminSecret","axios","data","config","headers","res","error","accessToken","NhostGraphqlClient","document","variables","operationName","responseData","print","LOCALHOST_REGEX","urlFromParams","backendOrSubdomain","service","backendUrl","subdomain","region","subdomainLocalhostFound","localhostFound","NhostClient","refreshIntervalTime","clientStorageGetter","clientStorageSetter","clientStorage","clientStorageType","autoRefreshToken","autoSignIn","devTools","start","urlParams","HasuraAuthClient","HasuraStorageClient","_a","_event","session","newValue","createClient"],"mappings":"oTAiBO,MAAMA,CAAqB,CAKhC,YAAYC,EAAyC,CAC7C,KAAA,CAAE,IAAAC,EAAK,YAAAC,CAAgB,EAAAF,EAE7B,KAAK,YAAc,KACnB,KAAK,YAAcE,EACd,KAAA,SAAWC,UAAM,OAAO,CAC3B,QAASF,CAAA,CACV,CACH,CAYA,MAAM,KACJA,EACAG,EACAC,EACkC,CAClC,MAAMC,EAAU,CACd,GAAG,KAAK,2BAA2B,EACnC,GAAGD,GAAA,YAAAA,EAAQ,OAAA,EAGT,IAAAE,EACA,GAAA,CACIA,EAAA,MAAM,KAAK,SAAS,KAA6BN,EAAKG,EAAM,CAAE,GAAGC,EAAQ,QAAAC,CAAA,CAAS,QACjFE,GACP,GAAIA,aAAiB,MACZ,MAAA,CAAE,IAAK,KAAM,MAAAA,EAExB,CAEA,OAAKD,EAOE,CAAE,IAAAA,EAAK,M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,IAAKpB,EAAckB,EAAW,SAAS,EACvC,YAAAjC,CAAA,CACD,EAEI,KAAA,UAAY,IAAIH,EAAqB,CACxC,IAAKkB,EAAckB,EAAW,WAAW,EACzC,YAAAjC,CAAA,CACD,EAEI,KAAA,QAAU,IAAIQ,EAAmB,CACpC,IAAKO,EAAckB,EAAW,SAAS,EACvC,YAAAjC,CAAA,CACD,EAGD,KAAK,QAAQ,eAAe,KAAK,KAAK,gBAAgB,EACtD,KAAK,UAAU,eAAe,KAAK,KAAK,gBAAgB,EACxD,KAAK,QAAQ,eAAe,KAAK,KAAK,gBAAgB,GAEjDoC,EAAA,KAAA,KAAK,SAAL,MAAAA,EAAa,QAAQ,IAAM,CAE9B,KAAK,KAAK,mBAAmB,CAACC,EAAQC,IAAY,CAC5CD,IAAW,eACR,KAAA,QAAQ,eAAe,MAAS,EAChC,KAAA,UAAU,eAAe,MAAS,EAClC,KAAA,QAAQ,eAAe,MAAS,EACvC,CACD,EAGI,KAAA,KAAK,eAAgBC,GAAY,CAC/B,KAAA,QAAQ,eAAeA,GAAA,YAAAA,EAAS,WAAW,EAC3C,KAAA,UAAU,eAAeA,GAAA,YAAAA,EAAS,WAAW,EAC7C,KAAA,QAAQ,eAAeA,GAAA,YAAAA,EAAS,WAAW,CAAA,CACjD,CAAA,GAEH,KAAK,aAAetC,EACpB,KAAK,SAAW+B,CAClB,CAEA,IAAI,aAAkC,CACpC,OAAO,KAAK,YACd,CAEA,IAAI,YAAYQ,EAA8B,CAC5C,KAAK,aAAeA,EACf,KAAA,QAAQ,eAAeA,CAAQ,CAItC,CACF,CCnGA,MAAMC,EAAgBrC,GAAyC,IAAIoB,EAAYpB,CAAM"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/utils/helpers.ts","../src/clients/auth.ts","../src/clients/functions.ts","../src/clients/graphql.ts","../src/clients/storage.ts","../src/clients/nhost.ts"],"sourcesContent":["import { NhostClientConstructorParams } from './types'\n\nconst LOCALHOST_REGEX = /^((?<protocol>http[s]?):\\/\\/)?(?<host>localhost)(:(?<port>\\d+))?$/\n\n/**\n * `backendUrl` should now be used only when self-hosting\n * `subdomain` and `region` should be used instead when using the Nhost platform\n * `\n * @param backendOrSubdomain\n * @param service\n * @returns\n */\nexport function urlFromSubdomain(\n backendOrSubdomain: Pick<NhostClientConstructorParams, 'region' | 'subdomain' | 'backendUrl'>,\n service: string\n): string {\n const { backendUrl, subdomain, region } = backendOrSubdomain\n\n if (backendUrl) {\n return `${backendUrl}/v1/${service}`\n }\n\n if (!subdomain) {\n throw new Error('Either `backendUrl` or `subdomain` must be set.')\n }\n\n // check if subdomain is [http[s]://]localhost[:port]\n const subdomainLocalhostFound = subdomain.match(LOCALHOST_REGEX)\n if (subdomainLocalhostFound?.groups) {\n const { protocol = 'http', host, port = 1337 } = subdomainLocalhostFound.groups\n\n const urlFromEnv = getValueFromEnv(service)\n if (urlFromEnv) {\n return urlFromEnv\n }\n return `${protocol}://${host}:${port}/v1/${service}`\n }\n\n if (!region) {\n throw new Error('`region` must be set when using a `subdomain` other than \"localhost\".')\n }\n\n return `https://${subdomain}.${service}.${region}.nhost.run/v1`\n}\n\n/**\n *\n * @returns whether the code is running in a browser\n */\nfunction isBrowser(): boolean {\n return typeof window !== 'undefined'\n}\n\n/**\n *\n * @returns whether the code is running in a Node.js environment\n */\nfunction environmentIsAvailable() {\n return typeof process !== 'undefined' && process.env\n}\n\n/**\n *\n * @param service auth | storage | graphql | functions\n * @returns the service's url if the corresponding env var is set\n * NHOST_${service}_URL\n */\nfunction getValueFromEnv(service: string) {\n if (isBrowser() || !environmentIsAvailable()) {\n return null\n }\n\n return process.env[`NHOST_${service.toUpperCase()}_URL`]\n}\n","import { HasuraAuthClient } from '@nhost/hasura-auth-js'\n\nimport { urlFromSubdomain } from '../utils/helpers'\nimport { NhostClientConstructorParams } from '../utils/types'\n\n/**\n * Creates a client for Auth from either a subdomain or a URL\n */\nexport function createAuthClient(params: NhostClientConstructorParams) {\n const authUrl =\n 'subdomain' in params || 'backendUrl' in params\n ? urlFromSubdomain(params, 'auth')\n : params.authUrl\n\n if (!authUrl) {\n throw new Error('Please provide `subdomain` or `authUrl`.')\n }\n\n return new HasuraAuthClient({ url: authUrl, ...params })\n}\n","import axios, {\n AxiosInstance,\n AxiosRequestConfig,\n AxiosResponse,\n RawAxiosRequestHeaders\n} from 'axios'\n\nimport { urlFromSubdomain } from '../utils/helpers'\nimport { FunctionCallResponse, NhostClientConstructorParams } from '../utils/types'\nexport interface NhostFunctionsConstructorParams {\n /**\n * Serverless Functions endpoint.\n */\n url: string\n /**\n * Admin secret. When set, it is sent as an `x-hasura-admin-secret` header for all requests.\n */\n adminSecret?: string\n}\n\n/**\n * Creates a client for Functions from either a subdomain or a URL\n */\nexport function createFunctionsClient(params: NhostClientConstructorParams) {\n const functionsUrl =\n 'subdomain' in params || 'backendUrl' in params\n ? urlFromSubdomain(params, 'functions')\n : params.functionsUrl\n\n if (!functionsUrl) {\n throw new Error('Please provide `subdomain` or `functionsUrl`.')\n }\n\n return new NhostFunctionsClient({ url: functionsUrl, ...params })\n}\n\n/**\n * @alias Functions\n */\nexport class NhostFunctionsClient {\n private url: string\n private instance: AxiosInstance\n private accessToken: string | null\n private adminSecret?: string\n\n constructor(params: NhostFunctionsConstructorParams) {\n const { url, adminSecret } = params\n\n this.url = url\n this.accessToken = null\n this.adminSecret = adminSecret\n this.instance = axios.create({\n baseURL: url\n })\n }\n\n /**\n * Use `nhost.functions.call` to call (sending a POST request to) a serverless function.\n *\n * @example\n * ```ts\n * await nhost.functions.call('send-welcome-email', { email: 'joe@example.com', name: 'Joe Doe' })\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/call\n */\n async call<T = unknown, D = any>(\n url: string,\n data: D,\n config?: AxiosRequestConfig\n ): Promise<FunctionCallResponse<T>> {\n const headers = {\n ...this.generateAccessTokenHeaders(),\n ...config?.headers\n }\n\n let res\n try {\n res = await this.instance.post<T, AxiosResponse<T>, D>(url, data, { ...config, headers })\n } catch (error) {\n if (error instanceof Error) {\n return { res: null, error }\n }\n }\n\n if (!res) {\n return {\n res: null,\n error: new Error('Unable to make post request to funtion')\n }\n }\n\n return { res, error: null }\n }\n\n /**\n * 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.\n *\n * @example\n * ```ts\n * nhost.functions.setAccessToken('some-access-token')\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/set-access-token\n */\n setAccessToken(accessToken: string | undefined) {\n if (!accessToken) {\n this.accessToken = null\n return\n }\n\n this.accessToken = accessToken\n }\n\n private generateAccessTokenHeaders(): RawAxiosRequestHeaders {\n if (this.adminSecret) {\n return {\n 'x-hasura-admin-secret': this.adminSecret\n }\n }\n if (this.accessToken) {\n return {\n Authorization: `Bearer ${this.accessToken}`\n }\n }\n return {}\n }\n}\n","import axios, { AxiosInstance, AxiosRequestConfig, RawAxiosRequestHeaders } from 'axios'\nimport { DocumentNode, print } from 'graphql'\n\nimport { urlFromSubdomain } from '../utils/helpers'\nimport {\n GraphqlRequestResponse,\n GraphqlResponse,\n NhostClientConstructorParams\n} from '../utils/types'\n\nexport interface NhostGraphqlConstructorParams {\n /**\n * GraphQL endpoint.\n */\n url: string\n /**\n * Admin secret. When set, it is sent as an `x-hasura-admin-secret` header for all requests.\n */\n adminSecret?: string\n}\n\n/**\n * Creates a client for GraphQL from either a subdomain or a URL\n */\nexport function createGraphqlClient(params: NhostClientConstructorParams) {\n const graphqlUrl =\n 'subdomain' in params || 'backendUrl' in params\n ? urlFromSubdomain(params, 'graphql')\n : params.graphqlUrl\n\n if (!graphqlUrl) {\n throw new Error('Please provide `subdomain` or `graphqlUrl`.')\n }\n\n return new NhostGraphqlClient({ url: graphqlUrl, ...params })\n}\n\n/**\n * @alias GraphQL\n */\nexport class NhostGraphqlClient {\n private url: string\n private instance: AxiosInstance\n private accessToken: string | null\n private adminSecret?: string\n\n constructor(params: NhostGraphqlConstructorParams) {\n const { url, adminSecret } = params\n\n this.url = url\n this.accessToken = null\n this.adminSecret = adminSecret\n this.instance = axios.create({\n baseURL: url\n })\n }\n\n /**\n * Use `nhost.graphql.request` to send a GraphQL request. For more serious GraphQL usage we recommend using a GraphQL client such as Apollo Client (https://www.apollographql.com/docs/react).\n *\n * @example\n * ```ts\n * const CUSTOMERS = gql`\n * query {\n * customers {\n * id\n * name\n * }\n * }\n * `\n * const { data, error } = await nhost.graphql.request(CUSTOMERS)\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/request\n */\n async request<T = any, V = any>(\n document: string | DocumentNode,\n variables?: V,\n config?: AxiosRequestConfig\n ): Promise<GraphqlRequestResponse<T>> {\n // add auth headers if any\n const headers = {\n ...this.generateAccessTokenHeaders(),\n ...config?.headers\n }\n\n try {\n const operationName = ''\n const res = await this.instance.post<GraphqlResponse<T>>(\n '',\n {\n operationName: operationName || undefined,\n query: typeof document === 'string' ? document : print(document),\n variables\n },\n { ...config, headers }\n )\n\n const responseData = res.data\n const { data } = responseData\n\n if (responseData.errors) {\n return {\n data: null,\n error: responseData.errors\n }\n }\n\n if (typeof data !== 'object' || Array.isArray(data) || data === null) {\n return {\n data: null,\n error: new Error('incorrect response data from GraphQL server')\n }\n }\n\n return { data, error: null }\n } catch (error) {\n if (error instanceof Error) {\n return { data: null, error }\n }\n console.error(error)\n return {\n data: null,\n error: new Error('Unable to get do GraphQL request')\n }\n }\n }\n\n /**\n * Use `nhost.graphql.getUrl` to get the GraphQL URL.\n *\n * @example\n * ```ts\n * const url = nhost.graphql.getUrl();\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/get-url\n */\n getUrl(): string {\n return this.url\n }\n\n /**\n * Use `nhost.graphql.setAccessToken` to a set an access token to be used in subsequent graphql requests. Note that if you're signin in users with `nhost.auth.signIn()` the access token will be set automatically.\n *\n * @example\n * ```ts\n * nhost.graphql.setAccessToken('some-access-token')\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript/nhost-js/graphql/set-access-token\n */\n setAccessToken(accessToken: string | undefined) {\n if (!accessToken) {\n this.accessToken = null\n return\n }\n\n this.accessToken = accessToken\n }\n\n private generateAccessTokenHeaders(): RawAxiosRequestHeaders {\n if (this.adminSecret) {\n return {\n 'x-hasura-admin-secret': this.adminSecret\n }\n }\n if (this.accessToken) {\n return {\n Authorization: `Bearer ${this.accessToken}`\n }\n }\n return {}\n }\n}\n","import { HasuraStorageClient } from '@nhost/hasura-storage-js'\n\nimport { urlFromSubdomain } from '../utils/helpers'\nimport { NhostClientConstructorParams } from '../utils/types'\n\n/**\n * Creates a client for Storage from either a subdomain or a URL\n */\nexport function createStorageClient(params: NhostClientConstructorParams) {\n const storageUrl =\n 'subdomain' in params || 'backendUrl' in params\n ? urlFromSubdomain(params, 'storage')\n : params.storageUrl\n\n if (!storageUrl) {\n throw new Error('Please provide `subdomain` or `storageUrl`.')\n }\n\n return new HasuraStorageClient({ url: storageUrl, ...params })\n}\n","import { HasuraAuthClient } from '@nhost/hasura-auth-js'\nimport { HasuraStorageClient } from '@nhost/hasura-storage-js'\n\nimport { NhostClientConstructorParams } from '../utils/types'\n\nimport { createAuthClient } from './auth'\nimport { createFunctionsClient, NhostFunctionsClient } from './functions'\nimport { createGraphqlClient, NhostGraphqlClient } from './graphql'\nimport { createStorageClient } from './storage'\n\nexport const createNhostClient = (params: NhostClientConstructorParams) => new NhostClient(params)\nexport class NhostClient {\n auth: HasuraAuthClient\n storage: HasuraStorageClient\n functions: NhostFunctionsClient\n graphql: NhostGraphqlClient\n private _adminSecret?: string\n readonly devTools?: boolean\n\n /**\n * Nhost Client\n *\n * @example\n * ```ts\n * const nhost = new NhostClient({ subdomain, region });\n * ```\n *\n * @docs https://docs.nhost.io/reference/javascript\n */\n constructor({\n refreshIntervalTime,\n clientStorageGetter,\n clientStorageSetter,\n clientStorage,\n clientStorageType,\n autoRefreshToken,\n autoSignIn,\n adminSecret,\n devTools,\n start = true,\n ...urlParams\n }: NhostClientConstructorParams) {\n // * Set clients for all services\n this.auth = createAuthClient({\n refreshIntervalTime,\n clientStorageGetter,\n clientStorageSetter,\n clientStorage,\n clientStorageType,\n autoRefreshToken,\n autoSignIn,\n start,\n ...urlParams\n })\n this.storage = createStorageClient({ adminSecret, ...urlParams })\n this.functions = createFunctionsClient({ adminSecret, ...urlParams })\n this.graphql = createGraphqlClient({ adminSecret, ...urlParams })\n\n // * Set current token if token is already accessable\n this.storage.setAccessToken(this.auth.getAccessToken())\n this.functions.setAccessToken(this.auth.getAccessToken())\n this.graphql.setAccessToken(this.auth.getAccessToken())\n\n this.auth.client?.onStart(() => {\n // * Set access token when signing out\n this.auth.onAuthStateChanged((_event, session) => {\n if (_event === 'SIGNED_OUT') {\n this.storage.setAccessToken(undefined)\n this.functions.setAccessToken(undefined)\n this.graphql.setAccessToken(undefined)\n }\n })\n\n // * Update access token for clients, including when signin in\n this.auth.onTokenChanged((session) => {\n this.storage.setAccessToken(session?.accessToken)\n this.functions.setAccessToken(session?.accessToken)\n this.graphql.setAccessToken(session?.accessToken)\n })\n })\n this._adminSecret = adminSecret\n this.devTools = devTools\n }\n\n get adminSecret(): string | undefined {\n return this._adminSecret\n }\n\n set adminSecret(newValue: string | undefined) {\n this._adminSecret = newValue\n this.storage.setAdminSecret(newValue)\n // TODO inconsistent API: storage can change admin secret, but functions/graphql cannot\n // this.functions.setAdminSecret(newValue)\n // this.graphql.setAdminSecret(newValue)\n }\n}\n"],"names":["LOCALHOST_REGEX","urlFromSubdomain","backendOrSubdomain","service","backendUrl","subdomain","region","subdomainLocalhostFound","protocol","host","port","urlFromEnv","getValueFromEnv","isBrowser","environmentIsAvailable","createAuthClient","params","authUrl","HasuraAuthClient","createFunctionsClient","functionsUrl","NhostFunctionsClient","__publicField","url","adminSecret","axios","data","config","headers","res","error","accessToken","createGraphqlClient","graphqlUrl","NhostGraphqlClient","document","variables","operationName","responseData","print","createStorageClient","storageUrl","HasuraStorageClient","createNhostClient","NhostClient","refreshIntervalTime","clientStorageGetter","clientStorageSetter","clientStorage","clientStorageType","autoRefreshToken","autoSignIn","devTools","start","urlParams","_a","_event","session","newValue"],"mappings":"0cAEMA,EAAkB,oEAUR,SAAAC,EACdC,EACAC,EACQ,CACR,KAAM,CAAE,WAAAC,EAAY,UAAAC,EAAW,OAAAC,CAAA,EAAWJ,EAE1C,GAAIE,EACF,MAAO,GAAGA,QAAiBD,IAG7B,GAAI,CAACE,EACG,MAAA,IAAI,MAAM,iDAAiD,EAI7D,MAAAE,EAA0BF,EAAU,MAAML,CAAe,EAC/D,GAAIO,GAAA,MAAAA,EAAyB,OAAQ,CACnC,KAAM,CAAE,SAAAC,EAAW,OAAQ,KAAAC,EAAM,KAAAC,EAAO,MAASH,EAAwB,OAEnEI,EAAaC,EAAgBT,CAAO,EAC1C,OAAIQ,GAGG,GAAGH,OAAcC,KAAQC,QAAWP,GAC7C,CAEA,GAAI,CAACG,EACG,MAAA,IAAI,MAAM,uEAAuE,EAGlF,MAAA,WAAWD,KAAaF,KAAWG,gBAC5C,CAMA,SAASO,GAAqB,CAC5B,OAAO,OAAO,OAAW,GAC3B,CAMA,SAASC,GAAyB,CACzB,OAAA,OAAO,QAAY,KAAe,QAAQ,GACnD,CAQA,SAASF,EAAgBT,EAAiB,CACxC,OAAIU,EAAU,GAAK,CAACC,IACX,KAGF,QAAQ,IAAI,SAASX,EAAQ,YAAY,QAClD,CCjEO,SAASY,EAAiBC,EAAsC,CAC/D,MAAAC,EACJ,cAAeD,GAAU,eAAgBA,EACrCf,EAAiBe,EAAQ,MAAM,EAC/BA,EAAO,QAEb,GAAI,CAACC,EACG,MAAA,IAAI,MAAM,0CAA0C,EAG5D,OAAO,IAAIC,EAAiB,iBAAA,CAAE,IAAKD,EAAS,GAAGD,EAAQ,CACzD,CCIO,SAASG,EAAsBH,EAAsC,CACpE,MAAAI,EACJ,cAAeJ,GAAU,eAAgBA,EACrCf,EAAiBe,EAAQ,WAAW,EAC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5CD,IAAW,eACR,KAAA,QAAQ,eAAe,MAAS,EAChC,KAAA,UAAU,eAAe,MAAS,EAClC,KAAA,QAAQ,eAAe,MAAS,EACvC,CACD,EAGI,KAAA,KAAK,eAAgBC,GAAY,CAC/B,KAAA,QAAQ,eAAeA,GAAA,YAAAA,EAAS,WAAW,EAC3C,KAAA,UAAU,eAAeA,GAAA,YAAAA,EAAS,WAAW,EAC7C,KAAA,QAAQ,eAAeA,GAAA,YAAAA,EAAS,WAAW,CAAA,CACjD,CAAA,GAEH,KAAK,aAAejC,EACpB,KAAK,SAAW4B,CAClB,CAEA,IAAI,aAAkC,CACpC,OAAO,KAAK,YACd,CAEA,IAAI,YAAYM,EAA8B,CAC5C,KAAK,aAAeA,EACf,KAAA,QAAQ,eAAeA,CAAQ,CAItC,CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import { NhostClientConstructorParams } from './utils/types';
|
|
2
|
-
import { NhostClient } from './core';
|
|
3
|
-
declare const createClient: (config: NhostClientConstructorParams) => NhostClient;
|
|
4
1
|
export * from './clients';
|
|
5
|
-
export * from './core';
|
|
6
2
|
export * from './utils/types';
|
|
7
|
-
export { createClient };
|
|
8
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA"}
|