@adaptic/utils 0.0.377 → 0.0.379

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/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import adaptic$1, { getApolloClient } from '@adaptic/backend-legacy';
1
+ import adaptic$1, { setTokenProvider, getApolloClient } from '@adaptic/backend-legacy';
2
2
  import { format, sub, set, add, startOfDay, endOfDay, isBefore, differenceInMilliseconds } from 'date-fns';
3
3
  import { formatInTimeZone, toZonedTime, fromZonedTime } from 'date-fns-tz';
4
4
  import ms from 'ms';
@@ -18,6 +18,54 @@ import * as path from 'path';
18
18
 
19
19
  // Keep track of a single instance of Apollo client
20
20
  let apolloClientInstance = null;
21
+ // Track if auth has been configured
22
+ let authConfigured = false;
23
+ /**
24
+ * Configure the Apollo client authentication with a dynamic token provider.
25
+ * This should be called once during app initialization before making any
26
+ * @adaptic/backend-legacy API calls.
27
+ *
28
+ * The token provider function will be called for each GraphQL request,
29
+ * allowing for dynamic token retrieval (e.g., from session storage, SecretsManager, etc.)
30
+ *
31
+ * @param provider - Function that returns the auth token (sync or async)
32
+ *
33
+ * @example
34
+ * // Configure with an environment variable
35
+ * configureAuth(() => process.env.GRAPHQL_API_KEY || '');
36
+ *
37
+ * @example
38
+ * // Configure with NextAuth session token (async)
39
+ * configureAuth(async () => {
40
+ * const session = await auth();
41
+ * return session?.accessToken || '';
42
+ * });
43
+ *
44
+ * @example
45
+ * // Configure with SecretsManager
46
+ * configureAuth(() => {
47
+ * const secrets = getSecretsManager();
48
+ * return secrets.getGraphQLConfig().apiKey || '';
49
+ * });
50
+ */
51
+ const configureAuth = (provider) => {
52
+ if (authConfigured) {
53
+ console.warn('[adaptic] Auth provider already configured. Calling configureAuth again will reset the client.');
54
+ }
55
+ setTokenProvider(provider);
56
+ authConfigured = true;
57
+ // Reset the cached client so it picks up the new auth on next request
58
+ if (apolloClientInstance) {
59
+ apolloClientInstance = null;
60
+ console.log('[adaptic] Apollo client reset due to auth configuration change');
61
+ }
62
+ };
63
+ /**
64
+ * Check if Apollo auth has been configured.
65
+ */
66
+ const isAuthConfigured = () => {
67
+ return authConfigured;
68
+ };
21
69
  /**
22
70
  * Returns a shared Apollo client instance with connection pooling.
23
71
  * This should be used for all @adaptic/backend-legacy operations.
@@ -16791,6 +16839,8 @@ const adaptic = {
16791
16839
  backend: {
16792
16840
  fetchAssetOverview: fetchAssetOverview,
16793
16841
  getApolloClient: getSharedApolloClient,
16842
+ configureAuth: configureAuth,
16843
+ isAuthConfigured: isAuthConfigured,
16794
16844
  },
16795
16845
  alpaca: {
16796
16846
  TradingAPI: AlpacaTradingAPI,