@nhost/nhost-js 3.2.8 → 5.0.0-beta.2
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/README.md +73 -37
- package/dist/nhost-js/auth.cjs.js +2 -0
- package/dist/nhost-js/auth.cjs.js.map +1 -0
- package/dist/nhost-js/auth.es.js +589 -0
- package/dist/nhost-js/auth.es.js.map +1 -0
- package/dist/nhost-js/fetch.cjs.js +2 -0
- package/dist/nhost-js/fetch.cjs.js.map +1 -0
- package/dist/nhost-js/fetch.es.js +10 -0
- package/dist/nhost-js/fetch.es.js.map +1 -0
- package/dist/nhost-js/functions.cjs.js +2 -0
- package/dist/nhost-js/functions.cjs.js.map +1 -0
- package/dist/nhost-js/functions.es.js +31 -0
- package/dist/nhost-js/functions.es.js.map +1 -0
- package/dist/nhost-js/graphql.cjs.js +2 -0
- package/dist/nhost-js/graphql.cjs.js.map +1 -0
- package/dist/nhost-js/graphql.es.js +35 -0
- package/dist/nhost-js/graphql.es.js.map +1 -0
- package/dist/nhost-js/storage.cjs.js +2 -0
- package/dist/nhost-js/storage.cjs.js.map +1 -0
- package/dist/nhost-js/storage.es.js +205 -0
- package/dist/nhost-js/storage.es.js.map +1 -0
- package/dist/nhost-js.cjs.js +2 -0
- package/dist/nhost-js.cjs.js.map +1 -0
- package/dist/nhost-js.es.js +497 -0
- package/dist/nhost-js.es.js.map +1 -0
- package/dist/nhost-js.umd.js +2 -0
- package/dist/nhost-js.umd.js.map +1 -0
- package/dist/src/auth/client.d.ts +430 -0
- package/dist/src/auth/client.d.ts.map +1 -0
- package/dist/src/auth/client.js +727 -0
- package/dist/src/auth/client.js.map +1 -0
- package/dist/src/auth/index.d.ts +11 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/index.js +11 -0
- package/dist/src/auth/index.js.map +1 -0
- package/dist/src/auth/interface.d.ts +142 -0
- package/dist/src/auth/interface.d.ts.map +1 -0
- package/dist/src/auth/interface.js +2 -0
- package/dist/src/auth/interface.js.map +1 -0
- package/dist/src/fetch/index.d.ts +53 -0
- package/dist/src/fetch/index.d.ts.map +1 -0
- package/dist/src/fetch/index.js +40 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/functions/client.d.ts +47 -0
- package/dist/src/functions/client.d.ts.map +1 -0
- package/dist/src/functions/client.js +62 -0
- package/dist/src/functions/client.js.map +1 -0
- package/dist/src/functions/index.d.ts +10 -0
- package/dist/src/functions/index.d.ts.map +1 -0
- package/dist/src/functions/index.js +10 -0
- package/dist/src/functions/index.js.map +1 -0
- package/dist/src/graphql/client.d.ts +89 -0
- package/dist/src/graphql/client.d.ts.map +1 -0
- package/dist/src/graphql/client.js +49 -0
- package/dist/src/graphql/client.js.map +1 -0
- package/dist/src/graphql/index.d.ts +10 -0
- package/dist/src/graphql/index.d.ts.map +1 -0
- package/dist/src/graphql/index.js +10 -0
- package/dist/src/graphql/index.js.map +1 -0
- package/dist/src/index.d.ts +184 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +251 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/middlewareAttachToken.d.ts +24 -0
- package/dist/src/middlewareAttachToken.d.ts.map +1 -0
- package/dist/src/middlewareAttachToken.js +60 -0
- package/dist/src/middlewareAttachToken.js.map +1 -0
- package/dist/src/middlewareRefreshSession.d.ts +43 -0
- package/dist/src/middlewareRefreshSession.d.ts.map +1 -0
- package/dist/src/middlewareRefreshSession.js +190 -0
- package/dist/src/middlewareRefreshSession.js.map +1 -0
- package/dist/src/middlewareResponseSession.d.ts +26 -0
- package/dist/src/middlewareResponseSession.d.ts.map +1 -0
- package/dist/src/middlewareResponseSession.js +83 -0
- package/dist/src/middlewareResponseSession.js.map +1 -0
- package/dist/src/sessionStorage.d.ts +123 -0
- package/dist/src/sessionStorage.d.ts.map +1 -0
- package/dist/src/sessionStorage.js +165 -0
- package/dist/src/sessionStorage.js.map +1 -0
- package/dist/src/storage/client.d.ts +184 -0
- package/dist/src/storage/client.d.ts.map +1 -0
- package/dist/src/storage/client.js +249 -0
- package/dist/src/storage/client.js.map +1 -0
- package/dist/src/storage/index.d.ts +11 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +11 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/src/storage/interface.d.ts +52 -0
- package/dist/src/storage/interface.d.ts.map +1 -0
- package/dist/src/storage/interface.js +2 -0
- package/dist/src/storage/interface.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +109 -63
- package/LICENSE +0 -21
- package/dist/clients/auth.d.ts +0 -8
- package/dist/clients/auth.d.ts.map +0 -1
- package/dist/clients/functions/index.d.ts +0 -107
- package/dist/clients/functions/index.d.ts.map +0 -1
- package/dist/clients/functions/types.d.ts +0 -28
- package/dist/clients/functions/types.d.ts.map +0 -1
- package/dist/clients/graphql.d.ts +0 -8
- package/dist/clients/graphql.d.ts.map +0 -1
- package/dist/clients/index.d.ts +0 -6
- package/dist/clients/index.d.ts.map +0 -1
- package/dist/clients/nhost.d.ts +0 -83
- package/dist/clients/nhost.d.ts.map +0 -1
- package/dist/clients/storage.d.ts +0 -8
- package/dist/clients/storage.d.ts.map +0 -1
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +0 -1
- package/dist/index.esm.js +0 -344
- package/dist/index.esm.js.map +0 -1
- package/dist/utils/helpers.d.ts +0 -20
- package/dist/utils/helpers.d.ts.map +0 -1
- package/dist/utils/types.d.ts +0 -57
- package/dist/utils/types.d.ts.map +0 -1
- package/umd/nhost-js.umd.js +0 -22
- package/umd/nhost-js.umd.js.map +0 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the main module to interact with Nhost's GraphQL service.
|
|
3
|
+
* Typically you would use this modula via the main [Nhost client](main#createclient)
|
|
4
|
+
* but you can also use it directly if you have a specific use case.
|
|
5
|
+
*
|
|
6
|
+
* @module graphql
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from "./client";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/graphql/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the main module to interact with Nhost's GraphQL service.
|
|
3
|
+
* Typically you would use this modula via the main [Nhost client](main#createclient)
|
|
4
|
+
* but you can also use it directly if you have a specific use case.
|
|
5
|
+
*
|
|
6
|
+
* @module graphql
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
export * from "./client";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/graphql/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main entry point for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This package provides a unified client for interacting with Nhost services:
|
|
5
|
+
* - Authentication
|
|
6
|
+
* - Storage
|
|
7
|
+
* - GraphQL
|
|
8
|
+
* - Functions
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* You can import and use this package with:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createClient } from "@nhost/nhost-js";
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* and use it like:
|
|
18
|
+
*
|
|
19
|
+
* {@includeCode ./__tests__/docstrings.test.ts#mainExample}
|
|
20
|
+
*
|
|
21
|
+
* ## Error handling
|
|
22
|
+
*
|
|
23
|
+
* The SDK will throw errors on most operations if the request returns a non-2xx status code or
|
|
24
|
+
* if the request fails entirely (i.e., due to network errors). A continuation you can see
|
|
25
|
+
* how you can handle errors thrown by the SDK.
|
|
26
|
+
*
|
|
27
|
+
* ### Auth
|
|
28
|
+
*
|
|
29
|
+
* {@includeCode ./__tests__/docstrings-auth.test.ts#errorHandling}
|
|
30
|
+
*
|
|
31
|
+
* ### Storage
|
|
32
|
+
*
|
|
33
|
+
* {@includeCode ./__tests__/docstrings-storage.test.ts#errorHandling}
|
|
34
|
+
*
|
|
35
|
+
* ### GraphQL
|
|
36
|
+
*
|
|
37
|
+
* {@includeCode ./__tests__/docstrings-graphql.test.ts#errorHandling}
|
|
38
|
+
*
|
|
39
|
+
* ### Functions
|
|
40
|
+
*
|
|
41
|
+
* {@includeCode ./__tests__/docstrings-functions.test.ts#errorHandling}
|
|
42
|
+
*
|
|
43
|
+
* @packageDocumentation
|
|
44
|
+
*/
|
|
45
|
+
import { type Client as AuthClient, type Session } from "./auth";
|
|
46
|
+
import { type Client as StorageClient } from "./storage";
|
|
47
|
+
import { type Client as GraphQLClient } from "./graphql";
|
|
48
|
+
import { type Client as FunctionsClient } from "./functions";
|
|
49
|
+
import { type SessionStorageInterface } from "./sessionStorage";
|
|
50
|
+
export { type SessionStorageInterface, DEFAULT_SESSION_KEY, LocalStorage, MemoryStorage, CookieStorage, } from "./sessionStorage";
|
|
51
|
+
export { type SessionRefreshOptions, createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
52
|
+
export { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
53
|
+
export { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
54
|
+
/**
|
|
55
|
+
* Generates a base URL for a Nhost service based on configuration
|
|
56
|
+
*
|
|
57
|
+
* @param serviceType - Type of service (auth, storage, graphql, functions)
|
|
58
|
+
* @param subdomain - Nhost project subdomain
|
|
59
|
+
* @param region - Nhost region
|
|
60
|
+
* @param customUrl - Custom URL override if provided
|
|
61
|
+
* @returns The base URL for the service
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export declare const generateServiceUrl: (serviceType: "auth" | "storage" | "graphql" | "functions", subdomain?: string, region?: string, customUrl?: string) => string;
|
|
66
|
+
/**
|
|
67
|
+
* Configuration options for creating an Nhost client
|
|
68
|
+
*/
|
|
69
|
+
export interface NhostClientOptions {
|
|
70
|
+
/**
|
|
71
|
+
* Nhost project subdomain (e.g., 'abcdefgh'). Used to construct the base URL for services for the Nhost cloud.
|
|
72
|
+
*/
|
|
73
|
+
subdomain?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Nhost region (e.g., 'eu-central-1'). Used to construct the base URL for services for the Nhost cloud.
|
|
76
|
+
*/
|
|
77
|
+
region?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Complete base URL for the auth service (overrides subdomain/region)
|
|
80
|
+
*/
|
|
81
|
+
authUrl?: string;
|
|
82
|
+
/**
|
|
83
|
+
* Complete base URL for the storage service (overrides subdomain/region)
|
|
84
|
+
*/
|
|
85
|
+
storageUrl?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Complete base URL for the GraphQL service (overrides subdomain/region)
|
|
88
|
+
*/
|
|
89
|
+
graphqlUrl?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Complete base URL for the functions service (overrides subdomain/region)
|
|
92
|
+
*/
|
|
93
|
+
functionsUrl?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Storage implementation to use for session persistence. If not provided, the SDK will
|
|
96
|
+
* default to localStorage in the browser or memory in other environments.
|
|
97
|
+
*/
|
|
98
|
+
storage?: SessionStorageInterface;
|
|
99
|
+
/**
|
|
100
|
+
* Disable automatic session refresh. If set to true, the SDK will not attempt to refresh
|
|
101
|
+
*/
|
|
102
|
+
disableAutoRefreshToken?: boolean;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Main client class that provides access to all Nhost services
|
|
106
|
+
*/
|
|
107
|
+
export declare class NhostClient {
|
|
108
|
+
/**
|
|
109
|
+
* Authentication client providing methods for user sign-in, sign-up, and session management
|
|
110
|
+
*/
|
|
111
|
+
auth: AuthClient;
|
|
112
|
+
/**
|
|
113
|
+
* Storage client providing methods for file operations (upload, download, delete)
|
|
114
|
+
*/
|
|
115
|
+
storage: StorageClient;
|
|
116
|
+
/**
|
|
117
|
+
* GraphQL client providing methods for executing GraphQL operations against your Hasura backend
|
|
118
|
+
*/
|
|
119
|
+
graphql: GraphQLClient;
|
|
120
|
+
/**
|
|
121
|
+
* Functions client providing methods for invoking serverless functions
|
|
122
|
+
*/
|
|
123
|
+
functions: FunctionsClient;
|
|
124
|
+
/**
|
|
125
|
+
* Storage implementation used for persisting session information
|
|
126
|
+
*/
|
|
127
|
+
sessionStorage: SessionStorageInterface;
|
|
128
|
+
/**
|
|
129
|
+
* Create a new Nhost client. This constructor is reserved for advanced use cases.
|
|
130
|
+
* For typical usage, use [createClient](#createclient) instead.
|
|
131
|
+
*
|
|
132
|
+
* @param auth - Authentication client
|
|
133
|
+
* @param storage - Storage client
|
|
134
|
+
* @param graphql - GraphQL client
|
|
135
|
+
* @param functions - Functions client
|
|
136
|
+
* @param sessionStorage - Storage implementation for session persistence
|
|
137
|
+
*/
|
|
138
|
+
constructor(auth: AuthClient, storage: StorageClient, graphql: GraphQLClient, functions: FunctionsClient, sessionStorage: SessionStorageInterface);
|
|
139
|
+
/**
|
|
140
|
+
* Get the current session from storage
|
|
141
|
+
*
|
|
142
|
+
* @returns The current session or null if no session exists
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const session = nhost.getUserSession();
|
|
147
|
+
* if (session) {
|
|
148
|
+
* console.log('User is authenticated:', session.user.id);
|
|
149
|
+
* } else {
|
|
150
|
+
* console.log('No active session');
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
getUserSession(): Session | null;
|
|
155
|
+
/**
|
|
156
|
+
* Refresh the session using the current refresh token
|
|
157
|
+
* in the storage and update the storage with the new session.
|
|
158
|
+
* @param marginSeconds - The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh.
|
|
159
|
+
*
|
|
160
|
+
* @returns The new session or null if there is currently no session or if refresh fails
|
|
161
|
+
*/
|
|
162
|
+
refreshSession(marginSeconds?: number): Promise<Session | null>;
|
|
163
|
+
private _refreshSession;
|
|
164
|
+
/**
|
|
165
|
+
* Clear the session from storage
|
|
166
|
+
*/
|
|
167
|
+
clearSession(): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates and configures a new Nhost client instance.
|
|
171
|
+
*
|
|
172
|
+
* This helper method instantiates a fully configured Nhost client by:
|
|
173
|
+
* - Instantiating the various service clients (auth, storage, functions and graphql)
|
|
174
|
+
* - Configuring a session storage if none is provided
|
|
175
|
+
* - Setting up the necessary middleware for automatic session management:
|
|
176
|
+
* - Automatically attaching the authorization token to requests
|
|
177
|
+
* - Refreshing the session when it expires
|
|
178
|
+
* - Applying session tokens to function calls
|
|
179
|
+
*
|
|
180
|
+
* @param options - Configuration options for the client
|
|
181
|
+
* @returns A configured Nhost client
|
|
182
|
+
*/
|
|
183
|
+
export declare function createClient(options?: NhostClientOptions): NhostClient;
|
|
184
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEH,OAAO,EAEL,KAAK,MAAM,IAAI,UAAU,EACzB,KAAK,OAAO,EAGb,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,KAAK,MAAM,IAAI,aAAa,EAE7B,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,KAAK,MAAM,IAAI,aAAa,EAE7B,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,K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EG;IACH,cAAc,EAAE,uBAAuB,CAAC;IAExC;;;;;;;;;OASG;gBAED,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,aAAa,EACtB,SAAS,EAAE,eAAe,EAC1B,cAAc,EAAE,uBAAuB;IASzC;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,OAAO,GAAG,IAAI;IAIhC;;;;;;OAMG;IACG,cAAc,CAAC,aAAa,GAAE,MAAW,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;YAqB3D,eAAe;IAkC7B;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAGpC;AAmBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAwD1E"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main entry point for the Nhost JavaScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* This package provides a unified client for interacting with Nhost services:
|
|
5
|
+
* - Authentication
|
|
6
|
+
* - Storage
|
|
7
|
+
* - GraphQL
|
|
8
|
+
* - Functions
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* You can import and use this package with:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { createClient } from "@nhost/nhost-js";
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* and use it like:
|
|
18
|
+
*
|
|
19
|
+
* {@includeCode ./__tests__/docstrings.test.ts#mainExample}
|
|
20
|
+
*
|
|
21
|
+
* ## Error handling
|
|
22
|
+
*
|
|
23
|
+
* The SDK will throw errors on most operations if the request returns a non-2xx status code or
|
|
24
|
+
* if the request fails entirely (i.e., due to network errors). A continuation you can see
|
|
25
|
+
* how you can handle errors thrown by the SDK.
|
|
26
|
+
*
|
|
27
|
+
* ### Auth
|
|
28
|
+
*
|
|
29
|
+
* {@includeCode ./__tests__/docstrings-auth.test.ts#errorHandling}
|
|
30
|
+
*
|
|
31
|
+
* ### Storage
|
|
32
|
+
*
|
|
33
|
+
* {@includeCode ./__tests__/docstrings-storage.test.ts#errorHandling}
|
|
34
|
+
*
|
|
35
|
+
* ### GraphQL
|
|
36
|
+
*
|
|
37
|
+
* {@includeCode ./__tests__/docstrings-graphql.test.ts#errorHandling}
|
|
38
|
+
*
|
|
39
|
+
* ### Functions
|
|
40
|
+
*
|
|
41
|
+
* {@includeCode ./__tests__/docstrings-functions.test.ts#errorHandling}
|
|
42
|
+
*
|
|
43
|
+
* @packageDocumentation
|
|
44
|
+
*/
|
|
45
|
+
import { createAPIClient as createAuthClient, } from "./auth";
|
|
46
|
+
import { createAPIClient as createStorageClient, } from "./storage";
|
|
47
|
+
import { createAPIClient as createGraphQLClient, } from "./graphql";
|
|
48
|
+
import { createAPIClient as createFunctionsClient, } from "./functions";
|
|
49
|
+
import { detectStorage } from "./sessionStorage";
|
|
50
|
+
import { extractTokenExpiration, createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
51
|
+
import { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
52
|
+
import { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
53
|
+
export { DEFAULT_SESSION_KEY, LocalStorage, MemoryStorage, CookieStorage, } from "./sessionStorage";
|
|
54
|
+
export { createSessionRefreshMiddleware, } from "./middlewareRefreshSession";
|
|
55
|
+
export { createAttachAccessTokenMiddleware } from "./middlewareAttachToken";
|
|
56
|
+
export { createSessionResponseMiddleware } from "./middlewareResponseSession";
|
|
57
|
+
/**
|
|
58
|
+
* Generates a base URL for a Nhost service based on configuration
|
|
59
|
+
*
|
|
60
|
+
* @param serviceType - Type of service (auth, storage, graphql, functions)
|
|
61
|
+
* @param subdomain - Nhost project subdomain
|
|
62
|
+
* @param region - Nhost region
|
|
63
|
+
* @param customUrl - Custom URL override if provided
|
|
64
|
+
* @returns The base URL for the service
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export const generateServiceUrl = (serviceType, subdomain, region, customUrl) => {
|
|
69
|
+
if (customUrl) {
|
|
70
|
+
return customUrl;
|
|
71
|
+
}
|
|
72
|
+
else if (subdomain && region) {
|
|
73
|
+
return `https://${subdomain}.${serviceType}.${region}.nhost.run/v1`;
|
|
74
|
+
}
|
|
75
|
+
else if (subdomain) {
|
|
76
|
+
return `https://${subdomain}.${serviceType}.nhost.run/v1`;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return `https://local.${serviceType}.local.nhost.run/v1`;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Main client class that provides access to all Nhost services
|
|
84
|
+
*/
|
|
85
|
+
export class NhostClient {
|
|
86
|
+
/**
|
|
87
|
+
* Authentication client providing methods for user sign-in, sign-up, and session management
|
|
88
|
+
*/
|
|
89
|
+
auth;
|
|
90
|
+
/**
|
|
91
|
+
* Storage client providing methods for file operations (upload, download, delete)
|
|
92
|
+
*/
|
|
93
|
+
storage;
|
|
94
|
+
/**
|
|
95
|
+
* GraphQL client providing methods for executing GraphQL operations against your Hasura backend
|
|
96
|
+
*/
|
|
97
|
+
graphql;
|
|
98
|
+
/**
|
|
99
|
+
* Functions client providing methods for invoking serverless functions
|
|
100
|
+
*/
|
|
101
|
+
functions;
|
|
102
|
+
/**
|
|
103
|
+
* Storage implementation used for persisting session information
|
|
104
|
+
*/
|
|
105
|
+
sessionStorage;
|
|
106
|
+
/**
|
|
107
|
+
* Create a new Nhost client. This constructor is reserved for advanced use cases.
|
|
108
|
+
* For typical usage, use [createClient](#createclient) instead.
|
|
109
|
+
*
|
|
110
|
+
* @param auth - Authentication client
|
|
111
|
+
* @param storage - Storage client
|
|
112
|
+
* @param graphql - GraphQL client
|
|
113
|
+
* @param functions - Functions client
|
|
114
|
+
* @param sessionStorage - Storage implementation for session persistence
|
|
115
|
+
*/
|
|
116
|
+
constructor(auth, storage, graphql, functions, sessionStorage) {
|
|
117
|
+
this.auth = auth;
|
|
118
|
+
this.storage = storage;
|
|
119
|
+
this.graphql = graphql;
|
|
120
|
+
this.functions = functions;
|
|
121
|
+
this.sessionStorage = sessionStorage;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get the current session from storage
|
|
125
|
+
*
|
|
126
|
+
* @returns The current session or null if no session exists
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const session = nhost.getUserSession();
|
|
131
|
+
* if (session) {
|
|
132
|
+
* console.log('User is authenticated:', session.user.id);
|
|
133
|
+
* } else {
|
|
134
|
+
* console.log('No active session');
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
getUserSession() {
|
|
139
|
+
return this.sessionStorage.get();
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Refresh the session using the current refresh token
|
|
143
|
+
* in the storage and update the storage with the new session.
|
|
144
|
+
* @param marginSeconds - The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh.
|
|
145
|
+
*
|
|
146
|
+
* @returns The new session or null if there is currently no session or if refresh fails
|
|
147
|
+
*/
|
|
148
|
+
async refreshSession(marginSeconds = 60) {
|
|
149
|
+
try {
|
|
150
|
+
return await this._refreshSession(marginSeconds);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
try {
|
|
154
|
+
// we retry the refresh token in case of transient error
|
|
155
|
+
// or race conditions
|
|
156
|
+
console.warn("error refreshing session, retrying:", error);
|
|
157
|
+
return await this._refreshSession(marginSeconds);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
const errResponse = error;
|
|
161
|
+
if (errResponse?.status === 401) {
|
|
162
|
+
// this probably means the refresh token is invalid
|
|
163
|
+
console.error("session probably expired");
|
|
164
|
+
this.sessionStorage.remove();
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async _refreshSession(marginSeconds = 60) {
|
|
171
|
+
const session = this.sessionStorage.get();
|
|
172
|
+
if (!session) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
const tokenExpiresAt = extractTokenExpiration(session?.accessToken || "");
|
|
176
|
+
const currentTime = Date.now();
|
|
177
|
+
const sessionExpired = tokenExpiresAt < currentTime;
|
|
178
|
+
if (tokenExpiresAt - currentTime > marginSeconds * 1000) {
|
|
179
|
+
return session; // No need to refresh
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const response = await this.auth.refreshToken({
|
|
183
|
+
refreshToken: session.refreshToken,
|
|
184
|
+
});
|
|
185
|
+
this.sessionStorage.set(response.body);
|
|
186
|
+
return response.body;
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (!sessionExpired) {
|
|
190
|
+
// If the session is not expired, we can still use the current session
|
|
191
|
+
// so there is no need to error for now
|
|
192
|
+
return session;
|
|
193
|
+
}
|
|
194
|
+
// we throw the error so the caller can handle it
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Clear the session from storage
|
|
200
|
+
*/
|
|
201
|
+
async clearSession() {
|
|
202
|
+
this.sessionStorage.remove();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function getMiddlewareChain(auth, storage, autoRefresh) {
|
|
206
|
+
const mwChain = [
|
|
207
|
+
createSessionResponseMiddleware(storage),
|
|
208
|
+
createAttachAccessTokenMiddleware(storage),
|
|
209
|
+
];
|
|
210
|
+
if (autoRefresh) {
|
|
211
|
+
// we need to process this one first to make sure any following middlewares
|
|
212
|
+
// run after the session has been refreshed
|
|
213
|
+
mwChain.unshift(createSessionRefreshMiddleware(auth, storage));
|
|
214
|
+
}
|
|
215
|
+
return mwChain;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Creates and configures a new Nhost client instance.
|
|
219
|
+
*
|
|
220
|
+
* This helper method instantiates a fully configured Nhost client by:
|
|
221
|
+
* - Instantiating the various service clients (auth, storage, functions and graphql)
|
|
222
|
+
* - Configuring a session storage if none is provided
|
|
223
|
+
* - Setting up the necessary middleware for automatic session management:
|
|
224
|
+
* - Automatically attaching the authorization token to requests
|
|
225
|
+
* - Refreshing the session when it expires
|
|
226
|
+
* - Applying session tokens to function calls
|
|
227
|
+
*
|
|
228
|
+
* @param options - Configuration options for the client
|
|
229
|
+
* @returns A configured Nhost client
|
|
230
|
+
*/
|
|
231
|
+
export function createClient(options = {}) {
|
|
232
|
+
const { subdomain, region, authUrl, storageUrl, graphqlUrl, functionsUrl, storage = detectStorage(), disableAutoRefreshToken = false, } = options;
|
|
233
|
+
// Determine base URLs for each service
|
|
234
|
+
const authBaseUrl = generateServiceUrl("auth", subdomain, region, authUrl);
|
|
235
|
+
const storageBaseUrl = generateServiceUrl("storage", subdomain, region, storageUrl);
|
|
236
|
+
const graphqlBaseUrl = generateServiceUrl("graphql", subdomain, region, graphqlUrl);
|
|
237
|
+
const functionsBaseUrl = generateServiceUrl("functions", subdomain, region, functionsUrl);
|
|
238
|
+
// Create auth client
|
|
239
|
+
const auth = createAuthClient(authBaseUrl);
|
|
240
|
+
const mwChain = getMiddlewareChain(auth, storage, !disableAutoRefreshToken);
|
|
241
|
+
for (const mw of mwChain) {
|
|
242
|
+
auth.pushChainFunction(mw);
|
|
243
|
+
}
|
|
244
|
+
// Create storage and graphql clients with the refresh and attach token middlewares
|
|
245
|
+
const storageClient = createStorageClient(storageBaseUrl, mwChain);
|
|
246
|
+
const graphqlClient = createGraphQLClient(graphqlBaseUrl, mwChain);
|
|
247
|
+
const functionsClient = createFunctionsClient(functionsBaseUrl, mwChain);
|
|
248
|
+
// Return an initialized NhostClient
|
|
249
|
+
return new NhostClient(auth, storageClient, graphqlClient, functionsClient, storage);
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 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MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACnE,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACnE,MAAM,eAAe,GAAG,qBAAqB,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAEzE,oCAAoC;IACpC,OAAO,IAAI,WAAW,CACpB,IAAI,EACJ,aAAa,EACb,aAAa,EACb,eAAe,EACf,OAAO,CACR,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Authorization token attachment middleware for the Nhost SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides middleware functionality to automatically attach
|
|
5
|
+
* authorization tokens to outgoing API requests, ensuring the client
|
|
6
|
+
* is properly authenticated.
|
|
7
|
+
*/
|
|
8
|
+
import { type SessionStorageInterface } from "./sessionStorage";
|
|
9
|
+
import { type ChainFunction } from "./fetch";
|
|
10
|
+
/**
|
|
11
|
+
* Creates a fetch middleware that adds the Authorization header with the current access token.
|
|
12
|
+
*
|
|
13
|
+
* This middleware:
|
|
14
|
+
* 1. Gets the current session from storage
|
|
15
|
+
* 2. Adds the authorization header with the access token to outgoing requests
|
|
16
|
+
*
|
|
17
|
+
* This middleware should be used after the refresh middleware in the chain to
|
|
18
|
+
* ensure the most recent token is used.
|
|
19
|
+
*
|
|
20
|
+
* @param storage - Storage implementation for retrieving session data
|
|
21
|
+
* @returns A middleware function that adds Authorization headers
|
|
22
|
+
*/
|
|
23
|
+
export declare const createAttachAccessTokenMiddleware: (storage: SessionStorageInterface) => ChainFunction;
|
|
24
|
+
//# sourceMappingURL=middlewareAttachToken.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewareAttachToken.d.ts","sourceRoot":"","sources":["../../src/middlewareAttachToken.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,KAAK,aAAa,EAAsB,MAAM,SAAS,CAAC;AAEjE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,iCAAiC,GAC5C,SAAS,uBAAuB,KAC/B,aA+BF,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Authorization token attachment middleware for the Nhost SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides middleware functionality to automatically attach
|
|
5
|
+
* authorization tokens to outgoing API requests, ensuring the client
|
|
6
|
+
* is properly authenticated.
|
|
7
|
+
*/
|
|
8
|
+
import {} from "./sessionStorage";
|
|
9
|
+
import {} from "./fetch";
|
|
10
|
+
/**
|
|
11
|
+
* Creates a fetch middleware that adds the Authorization header with the current access token.
|
|
12
|
+
*
|
|
13
|
+
* This middleware:
|
|
14
|
+
* 1. Gets the current session from storage
|
|
15
|
+
* 2. Adds the authorization header with the access token to outgoing requests
|
|
16
|
+
*
|
|
17
|
+
* This middleware should be used after the refresh middleware in the chain to
|
|
18
|
+
* ensure the most recent token is used.
|
|
19
|
+
*
|
|
20
|
+
* @param storage - Storage implementation for retrieving session data
|
|
21
|
+
* @returns A middleware function that adds Authorization headers
|
|
22
|
+
*/
|
|
23
|
+
export const createAttachAccessTokenMiddleware = (storage) => {
|
|
24
|
+
return (next) => {
|
|
25
|
+
return async (url, options = {}) => {
|
|
26
|
+
const headers = new Headers(options.headers || {});
|
|
27
|
+
// Skip if Authorization header is already set
|
|
28
|
+
if (headers.has("Authorization")) {
|
|
29
|
+
return next(url, options);
|
|
30
|
+
}
|
|
31
|
+
// Get current session from storage
|
|
32
|
+
const session = storage.get();
|
|
33
|
+
if (session?.accessToken) {
|
|
34
|
+
// Add authorization header
|
|
35
|
+
const newOptions = {
|
|
36
|
+
...options,
|
|
37
|
+
headers: addAuthorizationHeader(headers, session),
|
|
38
|
+
};
|
|
39
|
+
// Continue with the fetch chain
|
|
40
|
+
return next(url, newOptions);
|
|
41
|
+
}
|
|
42
|
+
// No session or no access token, continue without authorization
|
|
43
|
+
return next(url, options);
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Adds the Authorization header with the access token to the request headers
|
|
49
|
+
*
|
|
50
|
+
* @param headers - Original request headers
|
|
51
|
+
* @param session - Current session containing the access token
|
|
52
|
+
* @returns Modified headers with Authorization header
|
|
53
|
+
*/
|
|
54
|
+
function addAuthorizationHeader(headers, session) {
|
|
55
|
+
if (session.accessToken) {
|
|
56
|
+
headers.set("Authorization", `Bearer ${session.accessToken}`);
|
|
57
|
+
}
|
|
58
|
+
return headers;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=middlewareAttachToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewareAttachToken.js","sourceRoot":"","sources":["../../src/middlewareAttachToken.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAgC,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAA0C,MAAM,SAAS,CAAC;AAEjE;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,CAC/C,OAAgC,EACjB,EAAE;IACjB,OAAO,CAAC,IAAmB,EAAiB,EAAE;QAC5C,OAAO,KAAK,EACV,GAAW,EACX,UAAuB,EAAE,EACN,EAAE;YACrB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;YAEnD,8CAA8C;YAC9C,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;gBACjC,OAAO,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAC5B,CAAC;YAED,mCAAmC;YACnC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;YAE9B,IAAI,OAAO,EAAE,WAAW,EAAE,CAAC;gBACzB,2BAA2B;gBAC3B,MAAM,UAAU,GAAG;oBACjB,GAAG,OAAO;oBACV,OAAO,EAAE,sBAAsB,CAAC,OAAO,EAAE,OAAO,CAAC;iBAClD,CAAC;gBAEF,gCAAgC;gBAChC,OAAO,IAAI,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;YAC/B,CAAC;YAED,gEAAgE;YAChE,OAAO,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,SAAS,sBAAsB,CAAC,OAAgB,EAAE,OAAgB;IAChE,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;QACxB,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAChE,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Auth token refresh middleware for the Nhost SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides middleware functionality to automatically refresh
|
|
5
|
+
* authentication tokens before they expire, ensuring seamless API access
|
|
6
|
+
* without requiring manual token refresh by the application.
|
|
7
|
+
*/
|
|
8
|
+
import type { Client } from "./auth";
|
|
9
|
+
import { type SessionStorageInterface } from "./sessionStorage";
|
|
10
|
+
import { type ChainFunction } from "./fetch";
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the expiration time from a JWT token
|
|
13
|
+
* @param token - JWT token string
|
|
14
|
+
* @returns Expiration timestamp in milliseconds, or 0 if unable to extract
|
|
15
|
+
*/
|
|
16
|
+
export declare const extractTokenExpiration: (token: string) => number;
|
|
17
|
+
/**
|
|
18
|
+
* Configuration options for the session refresh middleware
|
|
19
|
+
*/
|
|
20
|
+
export type SessionRefreshOptions = {
|
|
21
|
+
/**
|
|
22
|
+
* Number of seconds before token expiration when a refresh should be triggered.
|
|
23
|
+
* Default is 60 seconds (1 minute).
|
|
24
|
+
*/
|
|
25
|
+
marginSeconds?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a fetch middleware that automatically refreshes authentication tokens.
|
|
29
|
+
*
|
|
30
|
+
* This middleware:
|
|
31
|
+
* 1. Checks if the current token is about to expire
|
|
32
|
+
* 2. If so, uses the refresh token to obtain a new access token
|
|
33
|
+
*
|
|
34
|
+
* The middleware handles token refresh transparently, so the application
|
|
35
|
+
* doesn't need to manually refresh tokens.
|
|
36
|
+
*
|
|
37
|
+
* @param authClient - Auth API client for token refresh operations
|
|
38
|
+
* @param storage - Storage implementation for persisting session data
|
|
39
|
+
* @param options - Configuration options for token refresh behavior
|
|
40
|
+
* @returns A middleware function that can be used in the fetch chain
|
|
41
|
+
*/
|
|
42
|
+
export declare const createSessionRefreshMiddleware: (authClient: Client, storage: SessionStorageInterface, options?: SessionRefreshOptions) => ChainFunction;
|
|
43
|
+
//# sourceMappingURL=middlewareRefreshSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middlewareRefreshSession.d.ts","sourceRoot":"","sources":["../../src/middlewareRefreshSession.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAW,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,KAAK,aAAa,EAAsB,MAAM,SAAS,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,MAAM,KAAG,MAgCtD,CAAC;AAkCF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,8BAA8B,GACzC,YAAY,MAAM,EAClB,SAAS,uBAAuB,EAChC,UAAU,qBAAqB,KAC9B,aA4DF,CAAC"}
|