@nhost/nhost-js 3.2.8 → 5.0.0-beta.1

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.
Files changed (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +67 -37
  3. package/dist/nhost-js/auth.cjs.js +2 -0
  4. package/dist/nhost-js/auth.cjs.js.map +1 -0
  5. package/dist/nhost-js/auth.es.js +261 -0
  6. package/dist/nhost-js/auth.es.js.map +1 -0
  7. package/dist/nhost-js/fetch.cjs.js +2 -0
  8. package/dist/nhost-js/fetch.cjs.js.map +1 -0
  9. package/dist/nhost-js/fetch.es.js +10 -0
  10. package/dist/nhost-js/fetch.es.js.map +1 -0
  11. package/dist/nhost-js/graphql.cjs.js +2 -0
  12. package/dist/nhost-js/graphql.cjs.js.map +1 -0
  13. package/dist/nhost-js/graphql.es.js +35 -0
  14. package/dist/nhost-js/graphql.es.js.map +1 -0
  15. package/dist/nhost-js/storage.cjs.js +2 -0
  16. package/dist/nhost-js/storage.cjs.js.map +1 -0
  17. package/dist/nhost-js/storage.es.js +205 -0
  18. package/dist/nhost-js/storage.es.js.map +1 -0
  19. package/dist/nhost-js.cjs.js +2 -0
  20. package/dist/nhost-js.cjs.js.map +1 -0
  21. package/dist/nhost-js.es.js +476 -0
  22. package/dist/nhost-js.es.js.map +1 -0
  23. package/dist/nhost-js.umd.js +2 -0
  24. package/dist/nhost-js.umd.js.map +1 -0
  25. package/dist/src/auth/client.d.ts +415 -0
  26. package/dist/src/auth/client.d.ts.map +1 -0
  27. package/dist/src/auth/client.js +330 -0
  28. package/dist/src/auth/client.js.map +1 -0
  29. package/dist/src/auth/index.d.ts +11 -0
  30. package/dist/src/auth/index.d.ts.map +1 -0
  31. package/dist/src/auth/index.js +11 -0
  32. package/dist/src/auth/index.js.map +1 -0
  33. package/dist/src/auth/interface.d.ts +72 -0
  34. package/dist/src/auth/interface.d.ts.map +1 -0
  35. package/dist/src/auth/interface.js +2 -0
  36. package/dist/src/auth/interface.js.map +1 -0
  37. package/dist/src/fetch/index.d.ts +53 -0
  38. package/dist/src/fetch/index.d.ts.map +1 -0
  39. package/dist/src/fetch/index.js +40 -0
  40. package/dist/src/fetch/index.js.map +1 -0
  41. package/dist/src/graphql/client.d.ts +89 -0
  42. package/dist/src/graphql/client.d.ts.map +1 -0
  43. package/dist/src/graphql/client.js +49 -0
  44. package/dist/src/graphql/client.js.map +1 -0
  45. package/dist/src/graphql/index.d.ts +10 -0
  46. package/dist/src/graphql/index.d.ts.map +1 -0
  47. package/dist/src/graphql/index.js +10 -0
  48. package/dist/src/graphql/index.js.map +1 -0
  49. package/dist/src/index.d.ts +169 -0
  50. package/dist/src/index.d.ts.map +1 -0
  51. package/dist/src/index.js +237 -0
  52. package/dist/src/index.js.map +1 -0
  53. package/dist/src/middlewareAttachToken.d.ts +24 -0
  54. package/dist/src/middlewareAttachToken.d.ts.map +1 -0
  55. package/dist/src/middlewareAttachToken.js +60 -0
  56. package/dist/src/middlewareAttachToken.js.map +1 -0
  57. package/dist/src/middlewareRefreshSession.d.ts +43 -0
  58. package/dist/src/middlewareRefreshSession.d.ts.map +1 -0
  59. package/dist/src/middlewareRefreshSession.js +190 -0
  60. package/dist/src/middlewareRefreshSession.js.map +1 -0
  61. package/dist/src/middlewareResponseSession.d.ts +26 -0
  62. package/dist/src/middlewareResponseSession.d.ts.map +1 -0
  63. package/dist/src/middlewareResponseSession.js +83 -0
  64. package/dist/src/middlewareResponseSession.js.map +1 -0
  65. package/dist/src/sessionStorage.d.ts +123 -0
  66. package/dist/src/sessionStorage.d.ts.map +1 -0
  67. package/dist/src/sessionStorage.js +165 -0
  68. package/dist/src/sessionStorage.js.map +1 -0
  69. package/dist/src/storage/client.d.ts +184 -0
  70. package/dist/src/storage/client.d.ts.map +1 -0
  71. package/dist/src/storage/client.js +249 -0
  72. package/dist/src/storage/client.js.map +1 -0
  73. package/dist/src/storage/index.d.ts +11 -0
  74. package/dist/src/storage/index.d.ts.map +1 -0
  75. package/dist/src/storage/index.js +11 -0
  76. package/dist/src/storage/index.js.map +1 -0
  77. package/dist/src/storage/interface.d.ts +52 -0
  78. package/dist/src/storage/interface.d.ts.map +1 -0
  79. package/dist/src/storage/interface.js +2 -0
  80. package/dist/src/storage/interface.js.map +1 -0
  81. package/dist/tsconfig.tsbuildinfo +1 -0
  82. package/package.json +94 -60
  83. package/dist/clients/auth.d.ts +0 -8
  84. package/dist/clients/auth.d.ts.map +0 -1
  85. package/dist/clients/functions/index.d.ts +0 -107
  86. package/dist/clients/functions/index.d.ts.map +0 -1
  87. package/dist/clients/functions/types.d.ts +0 -28
  88. package/dist/clients/functions/types.d.ts.map +0 -1
  89. package/dist/clients/graphql.d.ts +0 -8
  90. package/dist/clients/graphql.d.ts.map +0 -1
  91. package/dist/clients/index.d.ts +0 -6
  92. package/dist/clients/index.d.ts.map +0 -1
  93. package/dist/clients/nhost.d.ts +0 -83
  94. package/dist/clients/nhost.d.ts.map +0 -1
  95. package/dist/clients/storage.d.ts +0 -8
  96. package/dist/clients/storage.d.ts.map +0 -1
  97. package/dist/index.cjs.js +0 -2
  98. package/dist/index.cjs.js.map +0 -1
  99. package/dist/index.d.ts +0 -6
  100. package/dist/index.d.ts.map +0 -1
  101. package/dist/index.esm.js +0 -344
  102. package/dist/index.esm.js.map +0 -1
  103. package/dist/utils/helpers.d.ts +0 -20
  104. package/dist/utils/helpers.d.ts.map +0 -1
  105. package/dist/utils/types.d.ts +0 -57
  106. package/dist/utils/types.d.ts.map +0 -1
  107. package/umd/nhost-js.umd.js +0 -22
  108. package/umd/nhost-js.umd.js.map +0 -1
@@ -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"}
@@ -0,0 +1,190 @@
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 {} from "./sessionStorage";
9
+ import {} from "./fetch";
10
+ /**
11
+ * Extracts the expiration time from a JWT token
12
+ * @param token - JWT token string
13
+ * @returns Expiration timestamp in milliseconds, or 0 if unable to extract
14
+ */
15
+ export const extractTokenExpiration = (token) => {
16
+ try {
17
+ // JWT tokens are in the format header.payload.signature
18
+ const parts = token.split(".");
19
+ if (parts.length !== 3) {
20
+ console.warn("Token does not have three parts");
21
+ return 0;
22
+ }
23
+ // At this point, we know parts has exactly 3 elements
24
+ // Use a non-null assertion or check explicitly
25
+ const payloadPart = parts[1];
26
+ if (!payloadPart) {
27
+ console.warn("Payload part is empty");
28
+ return 0;
29
+ }
30
+ // Decode the payload (middle part)
31
+ const base64 = payloadPart.replace(/-/g, "+").replace(/_/g, "/");
32
+ const payload = decodeTokenPayload(base64);
33
+ if (payload.exp) {
34
+ // exp claim is in seconds, convert to milliseconds
35
+ return payload.exp * 1000;
36
+ }
37
+ else {
38
+ console.warn("No exp claim found in token");
39
+ return 0;
40
+ }
41
+ }
42
+ catch (error) {
43
+ console.warn("Failed to extract token expiration:", error);
44
+ return 0;
45
+ }
46
+ };
47
+ /**
48
+ * Decodes a base64-encoded JWT payload
49
+ * @param base64Payload - Base64-encoded payload
50
+ * @returns Decoded payload as an object
51
+ */
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ function decodeTokenPayload(base64Payload) {
54
+ try {
55
+ let jsonPayload;
56
+ if (typeof window !== "undefined") {
57
+ // Browser environment
58
+ jsonPayload = decodeURIComponent(window
59
+ .atob(base64Payload)
60
+ .split("")
61
+ .map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2))
62
+ .join(""));
63
+ }
64
+ else {
65
+ // Node.js environment
66
+ const buffer = Buffer.from(base64Payload, "base64");
67
+ jsonPayload = buffer.toString("utf8");
68
+ }
69
+ return JSON.parse(jsonPayload);
70
+ }
71
+ catch (e) {
72
+ console.warn("Error decoding token payload:", e);
73
+ throw e;
74
+ }
75
+ }
76
+ /**
77
+ * Creates a fetch middleware that automatically refreshes authentication tokens.
78
+ *
79
+ * This middleware:
80
+ * 1. Checks if the current token is about to expire
81
+ * 2. If so, uses the refresh token to obtain a new access token
82
+ *
83
+ * The middleware handles token refresh transparently, so the application
84
+ * doesn't need to manually refresh tokens.
85
+ *
86
+ * @param authClient - Auth API client for token refresh operations
87
+ * @param storage - Storage implementation for persisting session data
88
+ * @param options - Configuration options for token refresh behavior
89
+ * @returns A middleware function that can be used in the fetch chain
90
+ */
91
+ export const createSessionRefreshMiddleware = (authClient, storage, options) => {
92
+ const { marginSeconds = 60 } = options || {};
93
+ // Session state
94
+ let currentSession = null;
95
+ let tokenExpiresAt = 0;
96
+ // Create and return the chain function
97
+ return (next) => {
98
+ return async (url, options = {}) => {
99
+ // Skip token handling for certain requests
100
+ if (shouldSkipTokenHandling(url, options, authClient.baseURL)) {
101
+ return next(url, options);
102
+ }
103
+ try {
104
+ // Get current session from storage
105
+ currentSession = storage.get();
106
+ if (currentSession?.accessToken) {
107
+ tokenExpiresAt = extractTokenExpiration(currentSession.accessToken);
108
+ }
109
+ // If we don't have a session, proceed without authorization
110
+ if (!currentSession) {
111
+ return next(url, options);
112
+ }
113
+ // Check if token needs refresh
114
+ if (isTokenExpiringSoon(tokenExpiresAt, marginSeconds)) {
115
+ if (currentSession.refreshToken) {
116
+ // Refresh the token
117
+ const refreshedSession = await refreshToken(authClient, storage, currentSession.refreshToken);
118
+ if (refreshedSession) {
119
+ currentSession = refreshedSession;
120
+ tokenExpiresAt = extractTokenExpiration(refreshedSession.accessToken);
121
+ storage.set(refreshedSession);
122
+ }
123
+ }
124
+ }
125
+ // Continue with the fetch chain
126
+ return next(url, options);
127
+ }
128
+ catch (error) {
129
+ console.error("Error in token refresh chain:", error);
130
+ // Continue with the request even if token refresh fails
131
+ return next(url, options);
132
+ }
133
+ };
134
+ };
135
+ };
136
+ /**
137
+ * Performs the actual token refresh operation using the auth client
138
+ *
139
+ * @param authClient - Auth API client to use for token refresh
140
+ * @param storage - Storage implementation for persisting the new session
141
+ * @param refreshToken - Current refresh token to use
142
+ * @returns A new session if refresh was successful, null otherwise
143
+ */
144
+ async function refreshToken(authClient, storage, refreshToken) {
145
+ try {
146
+ const refreshResponse = await authClient.refreshToken({ refreshToken });
147
+ if (refreshResponse.status === 200) {
148
+ const session = refreshResponse.body;
149
+ storage.set(session);
150
+ return session;
151
+ }
152
+ return null;
153
+ }
154
+ catch (error) {
155
+ console.error("Error refreshing token:", error);
156
+ return null;
157
+ }
158
+ }
159
+ /**
160
+ * Determines if token handling should be skipped for this request
161
+ *
162
+ * @param url - Request URL
163
+ * @param options - Request options
164
+ * @param authApiUrl - Base URL for auth API
165
+ * @returns True if token handling should be skipped, false otherwise
166
+ */
167
+ function shouldSkipTokenHandling(url, options, authApiUrl) {
168
+ const headers = new Headers(options.headers || {});
169
+ // If Authorization header is explicitly set, skip token handling
170
+ if (headers.has("Authorization")) {
171
+ return true;
172
+ }
173
+ // If calling the token endpoint, skip to avoid infinite loops
174
+ if (url === `${authApiUrl}/token`) {
175
+ return true;
176
+ }
177
+ return false;
178
+ }
179
+ /**
180
+ * Checks if a token is expiring soon and needs to be refreshed
181
+ *
182
+ * @param expiresAt - Token expiration timestamp in milliseconds
183
+ * @param marginSeconds - Number of seconds before expiration to trigger refresh
184
+ * @returns True if token is expiring soon, false otherwise
185
+ */
186
+ function isTokenExpiringSoon(expiresAt, marginSeconds) {
187
+ const currentTime = Date.now();
188
+ return expiresAt - currentTime < marginSeconds * 1000;
189
+ }
190
+ //# sourceMappingURL=middlewareRefreshSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"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AG,IAAI,CAAC;AACxD,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @fileoverview Session response middleware for the Nhost SDK.
3
+ *
4
+ * This module provides middleware functionality to automatically extract
5
+ * and persist session information from authentication responses, ensuring
6
+ * that new sessions are properly stored after sign-in operations.
7
+ */
8
+ import { type SessionStorageInterface } from "./sessionStorage";
9
+ import { type ChainFunction } from "./fetch";
10
+ /**
11
+ * Creates a fetch middleware that automatically extracts and stores session data from API responses.
12
+ *
13
+ * This middleware:
14
+ * 1. Monitors responses from authentication-related endpoints
15
+ * 2. Extracts session information when present
16
+ * 3. Stores the session in the provided storage implementation
17
+ * 4. Handles session removal on sign-out
18
+ *
19
+ * This ensures that session data is always up-to-date in storage after operations
20
+ * that create or invalidate sessions.
21
+ *
22
+ * @param storage - Storage implementation for persisting session data
23
+ * @returns A middleware function that can be used in the fetch chain
24
+ */
25
+ export declare const createSessionResponseMiddleware: (storage: SessionStorageInterface) => ChainFunction;
26
+ //# sourceMappingURL=middlewareResponseSession.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middlewareResponseSession.d.ts","sourceRoot":"","sources":["../../src/middlewareResponseSession.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,+BAA+B,GAC1C,SAAS,uBAAuB,KAC/B,aAiEF,CAAC"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @fileoverview Session response middleware for the Nhost SDK.
3
+ *
4
+ * This module provides middleware functionality to automatically extract
5
+ * and persist session information from authentication responses, ensuring
6
+ * that new sessions are properly stored after sign-in operations.
7
+ */
8
+ import {} from "./auth";
9
+ import {} from "./sessionStorage";
10
+ import {} from "./fetch";
11
+ /**
12
+ * Creates a fetch middleware that automatically extracts and stores session data from API responses.
13
+ *
14
+ * This middleware:
15
+ * 1. Monitors responses from authentication-related endpoints
16
+ * 2. Extracts session information when present
17
+ * 3. Stores the session in the provided storage implementation
18
+ * 4. Handles session removal on sign-out
19
+ *
20
+ * This ensures that session data is always up-to-date in storage after operations
21
+ * that create or invalidate sessions.
22
+ *
23
+ * @param storage - Storage implementation for persisting session data
24
+ * @returns A middleware function that can be used in the fetch chain
25
+ */
26
+ export const createSessionResponseMiddleware = (storage) => {
27
+ /**
28
+ * Helper function to extract session data from various response formats
29
+ *
30
+ * @param data - Response data to extract session from
31
+ * @returns Session object if found, null otherwise
32
+ */
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ const sessionExtractor = function (body) {
35
+ // Look for session in common response patterns
36
+ const session =
37
+ // Pattern: { session: {...} }
38
+ body?.session ||
39
+ // Pattern: { data: { session: {...} } }
40
+ body?.data?.session ||
41
+ // Pattern: { accessToken, refreshToken, ... } where data itself is the session
42
+ (body?.accessToken && body?.refreshToken && body) ||
43
+ null;
44
+ return session;
45
+ };
46
+ return (next) => {
47
+ return async (url, options) => {
48
+ // Call the next middleware in the chain
49
+ const response = await next(url, options);
50
+ try {
51
+ // Check if this is a logout request
52
+ if (url.endsWith("/signout")) {
53
+ // Remove session on sign-out
54
+ storage.remove();
55
+ return response;
56
+ }
57
+ // Check if this is an auth-related endpoint that might return session data
58
+ if (url.endsWith("/token") ||
59
+ url.includes("/signin/") ||
60
+ url.includes("/signup/")) {
61
+ // Clone the response to avoid consuming it
62
+ const clonedResponse = response.clone();
63
+ // Parse the JSON data
64
+ const body = await clonedResponse.json().catch(() => null);
65
+ if (body) {
66
+ // Extract session data from response using provided extractor
67
+ const session = sessionExtractor(body);
68
+ // If session data is found, store it
69
+ if (session && session.accessToken && session.refreshToken) {
70
+ storage.set(session);
71
+ }
72
+ }
73
+ }
74
+ }
75
+ catch (error) {
76
+ console.warn("Error in session response middleware:", error);
77
+ }
78
+ // Return the original response
79
+ return response;
80
+ };
81
+ };
82
+ };
83
+ //# sourceMappingURL=middlewareResponseSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middlewareResponseSession.js","sourceRoot":"","sources":["../../src/middlewareResponseSession.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgB,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAgC,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAsB,MAAM,SAAS,CAAC;AAE7C;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,+BAA+B,GAAG,CAC7C,OAAgC,EACjB,EAAE;IACjB;;;;;OAKG;IACH,8DAA8D;IAC9D,MAAM,gBAAgB,GAAG,UAAU,IAAS;QAC1C,+CAA+C;QAC/C,MAAM,OAAO;QACX,8BAA8B;QAC7B,IAAI,EAAE,OAAmB;YAC1B,wCAAwC;YACvC,IAAI,EAAE,IAAI,EAAE,OAAmB;YAChC,+EAA+E;YAC/E,CAAC,IAAI,EAAE,WAAW,IAAI,IAAI,EAAE,YAAY,IAAK,IAAgB,CAAC;YAC9D,IAAI,CAAC;QAEP,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF,OAAO,CAAC,IAA+D,EAAE,EAAE;QACzE,OAAO,KAAK,EAAE,GAAW,EAAE,OAAqB,EAAE,EAAE;YAClD,wCAAwC;YACxC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAE1C,IAAI,CAAC;gBACH,oCAAoC;gBACpC,IAAI,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;oBAC7B,6BAA6B;oBAC7B,OAAO,CAAC,MAAM,EAAE,CAAC;oBACjB,OAAO,QAAQ,CAAC;gBAClB,CAAC;gBAED,2EAA2E;gBAC3E,IACE,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBACtB,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC;oBACxB,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,EACxB,CAAC;oBACD,2CAA2C;oBAC3C,MAAM,cAAc,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;oBAExC,sBAAsB;oBACtB,MAAM,IAAI,GAAG,MAAM,cAAc,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;oBAE3D,IAAI,IAAI,EAAE,CAAC;wBACT,8DAA8D;wBAC9D,MAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;wBAEvC,qCAAqC;wBACrC,IAAI,OAAO,IAAI,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;4BAC3D,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;wBACvB,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,IAAI,CAAC,uCAAuC,EAAE,KAAK,CAAC,CAAC;YAC/D,CAAC;YAED,+BAA+B;YAC/B,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @fileoverview Storage implementations for session persistence in different environments.
3
+ *
4
+ * This module provides different storage adapters for persisting authentication sessions
5
+ * across page reloads and browser sessions.
6
+ */
7
+ import { type Session } from "./auth";
8
+ /**
9
+ * Session storage interface for session persistence.
10
+ * This interface can be implemented to provide custom storage solutions.
11
+ */
12
+ export interface SessionStorageInterface {
13
+ /**
14
+ * Get the current session from storage
15
+ * @returns The stored session or null if not found
16
+ */
17
+ get(): Session | null;
18
+ /**
19
+ * Set the session in storage
20
+ * @param value - The session to store
21
+ */
22
+ set(value: Session): void;
23
+ /**
24
+ * Remove the session from storage
25
+ */
26
+ remove(): void;
27
+ }
28
+ /**
29
+ * Default storage key used for storing the Nhost session
30
+ */
31
+ export declare const DEFAULT_SESSION_KEY = "nhostSession";
32
+ /**
33
+ * Browser localStorage implementation of StorageInterface.
34
+ * Persists the session across page reloads and browser restarts.
35
+ */
36
+ export declare class LocalStorage implements SessionStorageInterface {
37
+ private readonly storageKey;
38
+ /**
39
+ * Creates a new LocalStorage instance
40
+ * @param storageKey - The key to use in localStorage (defaults to "nhostSession")
41
+ */
42
+ constructor(storageKey?: string);
43
+ /**
44
+ * Gets the session from localStorage
45
+ * @returns The stored session or null if not found
46
+ */
47
+ get(): Session | null;
48
+ /**
49
+ * Sets the session in localStorage
50
+ * @param value - The session to store
51
+ */
52
+ set(value: Session): void;
53
+ /**
54
+ * Removes the session from localStorage
55
+ */
56
+ remove(): void;
57
+ }
58
+ /**
59
+ * In-memory storage implementation for non-browser environments or when
60
+ * persistent storage is not available or desirable.
61
+ */
62
+ export declare class MemoryStorage implements SessionStorageInterface {
63
+ private session;
64
+ /**
65
+ * Gets the session from memory
66
+ * @returns The stored session or null if not set
67
+ */
68
+ get(): Session | null;
69
+ /**
70
+ * Sets the session in memory
71
+ * @param value - The session to store
72
+ */
73
+ set(value: Session): void;
74
+ /**
75
+ * Clears the session from memory
76
+ */
77
+ remove(): void;
78
+ }
79
+ /**
80
+ * Cookie-based storage implementation.
81
+ * This storage uses web browser cookies to store the session so it's not
82
+ * available in server-side environments. It is useful though for sinchronizing
83
+ * sessions between client and server environments.
84
+ */
85
+ export declare class CookieStorage implements SessionStorageInterface {
86
+ private readonly cookieName;
87
+ private readonly expirationDays;
88
+ private readonly secure;
89
+ private readonly sameSite;
90
+ /**
91
+ * Creates a new CookieStorage instance
92
+ * @param cookieName - Name of the cookie to use (defaults to "nhostSession")
93
+ * @param expirationDays - Number of days until the cookie expires (defaults to 30)
94
+ * @param secure - Whether to set the Secure flag on the cookie (defaults to true)
95
+ * @param sameSite - SameSite policy for the cookie (defaults to "lax")
96
+ */
97
+ constructor(cookieName?: string, expirationDays?: number, secure?: boolean, sameSite?: "strict" | "lax" | "none");
98
+ /**
99
+ * Gets the session from cookies
100
+ * @returns The stored session or null if not found
101
+ */
102
+ get(): Session | null;
103
+ /**
104
+ * Sets the session in a cookie
105
+ * @param value - The session to store
106
+ */
107
+ set(value: Session): void;
108
+ /**
109
+ * Removes the session cookie
110
+ */
111
+ remove(): void;
112
+ }
113
+ /**
114
+ * Detects the best available storage implementation for the current environment.
115
+ *
116
+ * The detection process follows this order:
117
+ * 1. Try to use localStorage if we're in a browser environment
118
+ * 2. Fall back to in-memory storage if localStorage isn't available
119
+ *
120
+ * @returns The best available storage implementation for the current environment
121
+ */
122
+ export declare const detectStorage: () => SessionStorageInterface;
123
+ //# sourceMappingURL=sessionStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionStorage.d.ts","sourceRoot":"","sources":["../../src/sessionStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI,CAAC;IAEtB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAE1B;;OAEG;IACH,MAAM,IAAI,IAAI,CAAC;CAChB;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAElD;;;GAGG;AACH,qBAAa,YAAa,YAAW,uBAAuB;IAC1D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC;;;OAGG;gBACS,UAAU,SAAsB;IAI5C;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;IAUrB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAIzB;;OAEG;IACH,MAAM,IAAI,IAAI;CAGf;AAED;;;GAGG;AACH,qBAAa,aAAc,YAAW,uBAAuB;IAC3D,OAAO,CAAC,OAAO,CAAwB;IAEvC;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;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