@magemetrics/core 0.5.4 → 0.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/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Client } from 'openapi-fetch';
2
2
  import { DefaultChatTransport } from 'ai';
3
+ import { GoTrueClient } from '@supabase/auth-js';
3
4
  import { GoTrueClientOptions } from '@supabase/auth-js';
4
5
  import { HttpChatTransportInitOptions } from 'ai';
5
6
  import type { InferUITool } from 'ai';
@@ -63,6 +64,54 @@ declare interface AsyncStorage_2 {
63
64
  removeItem(key: string): Promise<void>;
64
65
  }
65
66
 
67
+ /**
68
+ * Authentication provider interface for MageMetrics client.
69
+ * Providers handle token management and header generation.
70
+ */
71
+ export declare interface AuthProvider {
72
+ /**
73
+ * Initialize the auth provider and perform any necessary setup.
74
+ */
75
+ initialize(): Promise<void>;
76
+ /**
77
+ * Get authentication headers to include in API requests.
78
+ */
79
+ getHeaders(): Promise<Record<string, string>>;
80
+ /**
81
+ * Get the current authentication state.
82
+ */
83
+ getState(): AuthState;
84
+ /**
85
+ * Update the external JWT (if applicable).
86
+ */
87
+ updateExternalJwt(jwt: string): Promise<void>;
88
+ /**
89
+ * Logout and clear any stored tokens.
90
+ */
91
+ logout(): Promise<void>;
92
+ /**
93
+ * Get the Supabase client (if available).
94
+ * Used for backward compatibility with existing code.
95
+ */
96
+ getSupabaseClient(): GoTrueClient | null;
97
+ /**
98
+ * Add an event listener for auth events.
99
+ * @param type the type of the event
100
+ * @param listener the listener to add
101
+ * @param options options for the event listener
102
+ * @param options.signal an AbortSignal to remove the listener when aborted
103
+ */
104
+ addEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>, options?: {
105
+ signal?: AbortSignal;
106
+ }): void;
107
+ /**
108
+ * Remove an event listener.
109
+ * @param type the type of the event
110
+ * @param listener the listener to remove
111
+ */
112
+ removeEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>): void;
113
+ }
114
+
66
115
  /**
67
116
  * Authentication states
68
117
  */
@@ -275,6 +324,97 @@ declare type CustomDataParts = {
275
324
  };
276
325
  };
277
326
 
327
+ /**
328
+ * Direct authentication provider that uses a Supabase JWT directly
329
+ * without going through the token exchange flow.
330
+ *
331
+ * This provider:
332
+ * - Does NOT create shadow users
333
+ * - Does NOT call the exchange endpoint
334
+ * - Simply returns the JWT in headers for backend validation
335
+ * - Preserves the original user's roles and permissions
336
+ *
337
+ * Use this for internal admin tools where you already have a valid
338
+ * Supabase JWT and want to bypass the external auth flow.
339
+ * @example
340
+ * ```typescript
341
+ * const authProvider = new DirectAuthProvider(internalUserJwt);
342
+ * const client = new MageMetricsClient(config, authProvider);
343
+ * ```
344
+ */
345
+ export declare class DirectAuthProvider implements AuthProvider {
346
+ private state;
347
+ private jwt;
348
+ private events;
349
+ constructor(jwt: string);
350
+ initialize(): Promise<void>;
351
+ getHeaders(): Promise<Record<string, string>>;
352
+ getState(): AuthState;
353
+ updateExternalJwt(jwt: string): Promise<void>;
354
+ logout(): Promise<void>;
355
+ getSupabaseClient(): GoTrueClient | null;
356
+ addEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>, options?: {
357
+ signal?: AbortSignal;
358
+ }): void;
359
+ removeEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>): void;
360
+ private setState;
361
+ }
362
+
363
+ /**
364
+ * External authentication provider that handles the full token exchange flow.
365
+ *
366
+ * This provider:
367
+ * - Calls the exchange endpoint to create shadow users
368
+ * - Manages Supabase sessions
369
+ * - Handles token refresh
370
+ * - Supports auto-refresh and session management
371
+ *
372
+ * This is the default provider used for external customer integrations.
373
+ */
374
+ export declare class ExternalAuthProvider implements AuthProvider {
375
+ private config;
376
+ private state;
377
+ private supabaseClient;
378
+ private noAuthApiClient;
379
+ private authApiResponse;
380
+ private processedJwt;
381
+ private userId;
382
+ private events;
383
+ constructor(config: ExternalAuthProviderConfig);
384
+ initialize(): Promise<void>;
385
+ getHeaders(): Promise<Record<string, string>>;
386
+ getState(): AuthState;
387
+ updateExternalJwt(jwt: string): Promise<void>;
388
+ logout(): Promise<void>;
389
+ getSupabaseClient(): GoTrueClient | null;
390
+ addEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>, options?: {
391
+ signal?: AbortSignal;
392
+ }): void;
393
+ removeEventListener<T extends MageMetricsEvent>(type: T["type"], listener: MageMetricsEventListener<T>): void;
394
+ startAutoRefresh(): Promise<void> | undefined;
395
+ stopAutoRefresh(): Promise<void> | undefined;
396
+ private getApiInformation;
397
+ private initializeSupabaseClient;
398
+ private handleAuthentication;
399
+ private exchangeExternalToken;
400
+ private setState;
401
+ private encodeCheckKey;
402
+ private saveCheckKey;
403
+ private compareCheckKey;
404
+ private clearCheckKey;
405
+ private clearStorage;
406
+ }
407
+
408
+ export declare interface ExternalAuthProviderConfig {
409
+ apiUrl: string;
410
+ apiKey: string;
411
+ externalJwt?: string;
412
+ additionalHeaders?: Record<string, string>;
413
+ storage: AsyncStorage_2;
414
+ applicationName?: string;
415
+ authOptions?: Pick<GoTrueClientOptions, "fetch">;
416
+ }
417
+
278
418
  declare type ExtractInsightResponse = z.infer<typeof ExtractInsightResponseSchema>;
279
419
 
280
420
  declare const ExtractInsightResponseSchema: z.ZodUnion<[z.ZodObject<{
@@ -305,6 +445,8 @@ export declare type GenerateInsightParam = {
305
445
 
306
446
  export declare const getPublicApiClient: (apiUrl: string, apiKey: string) => Client<NoAuthPaths>;
307
447
 
448
+ declare const HEADER_SP_TOKEN: "sp-access-token";
449
+
308
450
  declare const inputSchema: z.ZodObject<{
309
451
  userRequest: z.ZodString;
310
452
  isNewAnalysisGoal: z.ZodBoolean;
@@ -365,18 +507,10 @@ declare type MageMetricsChatTransportOptions = Omit<HttpChatTransportInitOptions
365
507
  */
366
508
  export declare class MageMetricsClient {
367
509
  private config;
368
- private authState;
369
- private supabaseClient;
510
+ private authProvider;
370
511
  private internalApiClient;
371
- private noAuthApiClient;
372
- private authApiResponse;
373
512
  private authPromise;
374
- private processedJwt;
375
- private userId;
376
- private storageAdapter;
377
- private events;
378
- private authOptions;
379
- constructor(config: MageMetricsClientConfig, auth?: Pick<GoTrueClientOptions, "fetch">);
513
+ constructor(config: MageMetricsClientConfig, authProviderOrAuthOptions?: AuthProvider | Pick<GoTrueClientOptions, "fetch">);
380
514
  toJSON(): {
381
515
  config: string;
382
516
  };
@@ -404,7 +538,6 @@ export declare class MageMetricsClient {
404
538
  updateExternalJwt(jwt: string): Promise<void>;
405
539
  get state(): AuthState;
406
540
  getHeaders(): Promise<Record<string, string>>;
407
- private clearStorage;
408
541
  logout(): Promise<void>;
409
542
  auth: {
410
543
  startAutoRefresh: () => Promise<void> | undefined;
@@ -480,6 +613,16 @@ export declare class MageMetricsClient {
480
613
  null_count: number | null;
481
614
  unique_count: number | null;
482
615
  unique_percentage: number | null;
616
+ min_value?: number | null;
617
+ max_value?: number | null;
618
+ avg_value?: number | null;
619
+ median_value?: number | null;
620
+ std_value?: number | null;
621
+ min_date?: string | null;
622
+ max_date?: string | null;
623
+ min_length?: number | null;
624
+ max_length?: number | null;
625
+ avg_length?: number | null;
483
626
  } & {
484
627
  [key: string]: unknown;
485
628
  };
@@ -577,27 +720,8 @@ export declare class MageMetricsClient {
577
720
  }[]>;
578
721
  }>;
579
722
  };
580
- /**
581
- * Perform the complete authentication flow
582
- */
583
- private performAuthFlow;
584
- private getApiInformation;
585
- private initializeSupabaseClient;
586
723
  client(): InternalApiClient;
587
- /**
588
- * Handle the authentication logic (check session or exchange token)
589
- */
590
- private handleAuthentication;
591
- /**
592
- * Exchange external JWT for Supabase tokens
593
- */
594
- private exchangeExternalToken;
595
724
  private createSupabaseHeaderMiddleware;
596
- private setState;
597
- private encodeCheckKey;
598
- saveCheckKey(externalJwt: string, apiKey: string): Promise<void>;
599
- compareCheckKey(externalJwt: string, apiKey: string): Promise<boolean>;
600
- clearCheckKey(): Promise<void>;
601
725
  }
602
726
 
603
727
  /**
@@ -1149,6 +1273,16 @@ declare interface operations {
1149
1273
  null_count: number | null;
1150
1274
  unique_count: number | null;
1151
1275
  unique_percentage: number | null;
1276
+ min_value?: number | null;
1277
+ max_value?: number | null;
1278
+ avg_value?: number | null;
1279
+ median_value?: number | null;
1280
+ std_value?: number | null;
1281
+ min_date?: string | null;
1282
+ max_date?: string | null;
1283
+ min_length?: number | null;
1284
+ max_length?: number | null;
1285
+ avg_length?: number | null;
1152
1286
  } & {
1153
1287
  [key: string]: unknown;
1154
1288
  };
@@ -1799,6 +1933,16 @@ declare interface operations {
1799
1933
  null_count: number | null;
1800
1934
  unique_count: number | null;
1801
1935
  unique_percentage: number | null;
1936
+ min_value?: number | null;
1937
+ max_value?: number | null;
1938
+ avg_value?: number | null;
1939
+ median_value?: number | null;
1940
+ std_value?: number | null;
1941
+ min_date?: string | null;
1942
+ max_date?: string | null;
1943
+ min_length?: number | null;
1944
+ max_length?: number | null;
1945
+ avg_length?: number | null;
1802
1946
  } & {
1803
1947
  [key: string]: unknown;
1804
1948
  };
@@ -4447,9 +4591,9 @@ declare type TableFilters = Record<string, string | number>;
4447
4591
  */
4448
4592
  export declare const TOKEN_STORAGE_KEY = "mm-ai-sp-token";
4449
4593
 
4450
- declare type ToRemove = Simplify<{
4451
- header: "sp-access-token";
4452
- }>;
4594
+ declare type ToRemove = {
4595
+ header: typeof HEADER_SP_TOKEN;
4596
+ };
4453
4597
 
4454
4598
  declare type UiTool = InferUITool<Tool<z.infer<typeof inputSchema>, z.infer<typeof redactedOutputSchema>>>;
4455
4599