@magemetrics/core 0.5.4 → 0.6.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.
- package/dist/index.d.ts +180 -33
- package/dist/index.js +630 -521
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
1
2
|
import z5, { z } from 'zod';
|
|
2
3
|
import { GoTrueClient } from '@supabase/auth-js';
|
|
3
|
-
import
|
|
4
|
-
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import createApiClient2 from 'openapi-fetch';
|
|
5
5
|
import dayjs from 'dayjs';
|
|
6
6
|
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
7
7
|
import relativeTime from 'dayjs/plugin/relativeTime.js';
|
|
@@ -11,6 +11,7 @@ import { DefaultChatTransport } from 'ai';
|
|
|
11
11
|
|
|
12
12
|
// ../shared/dist/src/api/client/middleware.js
|
|
13
13
|
var HEADER_CLIENT_VERSION = "X-Client-Version";
|
|
14
|
+
var HEADER_SP_TOKEN = "sp-access-token";
|
|
14
15
|
var HEADER_API_KEY = "X-Api-Key";
|
|
15
16
|
var ApiError = class extends Error {
|
|
16
17
|
extra;
|
|
@@ -54,6 +55,102 @@ var addApiKeyHeader = (apiKey) => {
|
|
|
54
55
|
};
|
|
55
56
|
};
|
|
56
57
|
|
|
58
|
+
// package.json
|
|
59
|
+
var package_default = {
|
|
60
|
+
version: "0.6.1"};
|
|
61
|
+
|
|
62
|
+
// src/core/MageMetricsEventEmitter.ts
|
|
63
|
+
var MageMetricsEventEmitter = class {
|
|
64
|
+
listeners = /* @__PURE__ */ new Map();
|
|
65
|
+
addEventListener(type, listener, options) {
|
|
66
|
+
if (!this.listeners.has(type)) {
|
|
67
|
+
this.listeners.set(type, /* @__PURE__ */ new Set());
|
|
68
|
+
}
|
|
69
|
+
if (options?.signal) {
|
|
70
|
+
if (options.signal.aborted) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const abortHandler = () => {
|
|
74
|
+
this.removeEventListener(type, listener);
|
|
75
|
+
};
|
|
76
|
+
options.signal.addEventListener("abort", abortHandler, { once: true });
|
|
77
|
+
}
|
|
78
|
+
this.listeners.get(type).add(listener);
|
|
79
|
+
}
|
|
80
|
+
removeEventListener(type, listener) {
|
|
81
|
+
const typeListeners = this.listeners.get(type);
|
|
82
|
+
if (typeListeners) {
|
|
83
|
+
typeListeners.delete(listener);
|
|
84
|
+
if (typeListeners.size === 0) {
|
|
85
|
+
this.listeners.delete(type);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
dispatch(type, detail) {
|
|
90
|
+
const typeListeners = this.listeners.get(type);
|
|
91
|
+
if (typeListeners) {
|
|
92
|
+
const event = { type, detail };
|
|
93
|
+
typeListeners.forEach((listener) => {
|
|
94
|
+
try {
|
|
95
|
+
listener(event);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error(`Error in event listener for "${type}":`, error);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/core/auth/DirectAuthProvider.ts
|
|
105
|
+
var MM_CLIENT_VERSION = package_default.version;
|
|
106
|
+
var DirectAuthProvider = class {
|
|
107
|
+
state = "initializing";
|
|
108
|
+
jwt;
|
|
109
|
+
events = new MageMetricsEventEmitter();
|
|
110
|
+
constructor(jwt) {
|
|
111
|
+
this.jwt = jwt;
|
|
112
|
+
}
|
|
113
|
+
async initialize() {
|
|
114
|
+
return Promise.resolve(this.setState("ready"));
|
|
115
|
+
}
|
|
116
|
+
async getHeaders() {
|
|
117
|
+
return Promise.resolve({
|
|
118
|
+
[HEADER_CLIENT_VERSION]: MM_CLIENT_VERSION,
|
|
119
|
+
[HEADER_SP_TOKEN]: this.jwt
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
getState() {
|
|
123
|
+
return this.state;
|
|
124
|
+
}
|
|
125
|
+
async updateExternalJwt(jwt) {
|
|
126
|
+
this.jwt = jwt;
|
|
127
|
+
return Promise.resolve(this.setState("ready"));
|
|
128
|
+
}
|
|
129
|
+
async logout() {
|
|
130
|
+
return Promise.resolve(this.setState("initializing"));
|
|
131
|
+
}
|
|
132
|
+
getSupabaseClient() {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
addEventListener(type, listener, options) {
|
|
136
|
+
this.events.addEventListener(type, listener, options);
|
|
137
|
+
}
|
|
138
|
+
removeEventListener(type, listener) {
|
|
139
|
+
this.events.removeEventListener(type, listener);
|
|
140
|
+
}
|
|
141
|
+
setState(state) {
|
|
142
|
+
this.state = state;
|
|
143
|
+
this.state = state;
|
|
144
|
+
this.events.dispatch("authStateChange", state);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var hashString = (value) => {
|
|
148
|
+
const encoder = new TextEncoder();
|
|
149
|
+
const data = encoder.encode(value);
|
|
150
|
+
const hash = sha256(data);
|
|
151
|
+
return Array.from(hash).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
152
|
+
};
|
|
153
|
+
|
|
57
154
|
// ../../node_modules/.pnpm/@asteasolutions+zod-to-openapi@8.1.0_zod@4.1.13/node_modules/@asteasolutions/zod-to-openapi/dist/index.mjs
|
|
58
155
|
function __rest(s, e) {
|
|
59
156
|
var t = {};
|
|
@@ -274,114 +371,472 @@ var Metadata = class {
|
|
|
274
371
|
}
|
|
275
372
|
return typeName ? void 0 : schema;
|
|
276
373
|
}
|
|
277
|
-
static getMetadataFromInternalRegistry(zodSchema) {
|
|
278
|
-
return zodToOpenAPIRegistry.get(zodSchema);
|
|
374
|
+
static getMetadataFromInternalRegistry(zodSchema) {
|
|
375
|
+
return zodToOpenAPIRegistry.get(zodSchema);
|
|
376
|
+
}
|
|
377
|
+
static getMetadataFromRegistry(zodSchema) {
|
|
378
|
+
const internal = this.getMetadataFromInternalRegistry(zodSchema);
|
|
379
|
+
const general = zodSchema.meta();
|
|
380
|
+
if (!internal) {
|
|
381
|
+
return general;
|
|
382
|
+
}
|
|
383
|
+
const { _internal } = internal, rest = __rest(internal, ["_internal"]);
|
|
384
|
+
const _a = general !== null && general !== void 0 ? general : {}, { id, title } = _a, restGeneral = __rest(_a, ["id", "title"]);
|
|
385
|
+
return Object.assign(Object.assign(Object.assign({ _internal: Object.assign(Object.assign({}, id ? { refId: id } : {}), _internal) }, rest), title ? { description: title } : {}), restGeneral);
|
|
386
|
+
}
|
|
387
|
+
static setMetadataInRegistry(zodSchema, metadata) {
|
|
388
|
+
zodToOpenAPIRegistry.add(zodSchema, metadata);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
function preserveMetadataFromModifier(zodSchema, modifier) {
|
|
392
|
+
const zodModifier = zodSchema[modifier];
|
|
393
|
+
if (typeof zodModifier !== "function") {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
zodSchema[modifier] = function(...args) {
|
|
397
|
+
const result = zodModifier.apply(this, args);
|
|
398
|
+
const meta = Metadata.getMetadataFromRegistry(this);
|
|
399
|
+
if (meta) {
|
|
400
|
+
Metadata.setMetadataInRegistry(result, meta);
|
|
401
|
+
}
|
|
402
|
+
return result;
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function extendZodWithOpenApi(zod) {
|
|
406
|
+
if (typeof zod.ZodType.prototype.openapi !== "undefined") {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
zod.ZodType.prototype.openapi = function(...args) {
|
|
410
|
+
const { refId, metadata, options } = getOpenApiConfiguration(...args);
|
|
411
|
+
const _a = metadata !== null && metadata !== void 0 ? metadata : {}, { param } = _a, restOfOpenApi = __rest(_a, ["param"]);
|
|
412
|
+
const allMetadata = Metadata.getMetadataFromRegistry(this);
|
|
413
|
+
const _b = allMetadata !== null && allMetadata !== void 0 ? allMetadata : {}, { _internal: internalMetadata } = _b, currentMetadata = __rest(_b, ["_internal"]);
|
|
414
|
+
const _internal = Object.assign(Object.assign(Object.assign({}, internalMetadata), options), refId ? { refId } : void 0);
|
|
415
|
+
const resultMetadata = Object.assign(Object.assign(Object.assign({}, currentMetadata), restOfOpenApi), (currentMetadata === null || currentMetadata === void 0 ? void 0 : currentMetadata.param) || param ? {
|
|
416
|
+
param: Object.assign(Object.assign({}, currentMetadata === null || currentMetadata === void 0 ? void 0 : currentMetadata.param), param)
|
|
417
|
+
} : void 0);
|
|
418
|
+
const result = new this.constructor(this._def);
|
|
419
|
+
Metadata.setMetadataInRegistry(result, Object.assign(Object.assign({}, Object.keys(_internal).length > 0 ? { _internal } : void 0), resultMetadata));
|
|
420
|
+
if (isZodType(result, "ZodObject")) {
|
|
421
|
+
const currentMetadata2 = Metadata.getMetadataFromRegistry(result);
|
|
422
|
+
const originalExtend = result.extend;
|
|
423
|
+
result.extend = function(...args2) {
|
|
424
|
+
const extendedResult = originalExtend.apply(result, args2);
|
|
425
|
+
const _a2 = currentMetadata2 !== null && currentMetadata2 !== void 0 ? currentMetadata2 : {}, { _internal: _internal2 } = _a2, rest = __rest(_a2, ["_internal"]);
|
|
426
|
+
Metadata.setMetadataInRegistry(extendedResult, {
|
|
427
|
+
_internal: {
|
|
428
|
+
extendedFrom: (_internal2 === null || _internal2 === void 0 ? void 0 : _internal2.refId) ? { refId: _internal2.refId, schema: result } : _internal2 === null || _internal2 === void 0 ? void 0 : _internal2.extendedFrom
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
return extendedResult.openapi(rest);
|
|
432
|
+
};
|
|
433
|
+
preserveMetadataFromModifier(result, "catchall");
|
|
434
|
+
}
|
|
435
|
+
preserveMetadataFromModifier(result, "optional");
|
|
436
|
+
preserveMetadataFromModifier(result, "nullable");
|
|
437
|
+
preserveMetadataFromModifier(result, "default");
|
|
438
|
+
preserveMetadataFromModifier(result, "transform");
|
|
439
|
+
preserveMetadataFromModifier(result, "refine");
|
|
440
|
+
preserveMetadataFromModifier(result, "length");
|
|
441
|
+
preserveMetadataFromModifier(result, "min");
|
|
442
|
+
preserveMetadataFromModifier(result, "max");
|
|
443
|
+
const originalMeta = result.meta;
|
|
444
|
+
result.meta = function(...args2) {
|
|
445
|
+
const result2 = originalMeta.apply(this, args2);
|
|
446
|
+
if (args2[0]) {
|
|
447
|
+
const meta = Metadata.getMetadataFromInternalRegistry(this);
|
|
448
|
+
if (meta) {
|
|
449
|
+
Metadata.setMetadataInRegistry(result2, Object.assign(Object.assign({}, meta), args2[0]));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return result2;
|
|
453
|
+
};
|
|
454
|
+
return result;
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function getOpenApiConfiguration(refOrOpenapi, metadataOrOptions, options) {
|
|
458
|
+
if (typeof refOrOpenapi === "string") {
|
|
459
|
+
return {
|
|
460
|
+
refId: refOrOpenapi,
|
|
461
|
+
metadata: metadataOrOptions,
|
|
462
|
+
options
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
refId: void 0,
|
|
467
|
+
metadata: refOrOpenapi,
|
|
468
|
+
options: metadataOrOptions
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
new Set(".\\+*[^]$()");
|
|
472
|
+
var createRoute = (routeConfig) => {
|
|
473
|
+
const route = {
|
|
474
|
+
...routeConfig,
|
|
475
|
+
getRoutingPath() {
|
|
476
|
+
return routeConfig.path.replaceAll(/\/{(.+?)}/g, "/:$1");
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
return Object.defineProperty(route, "getRoutingPath", { enumerable: false });
|
|
480
|
+
};
|
|
481
|
+
extendZodWithOpenApi(z);
|
|
482
|
+
|
|
483
|
+
// ../shared/dist/src/endpoints/auth.routes.js
|
|
484
|
+
var GetApiInformationInputSchema = z.object({
|
|
485
|
+
apiKey: z.string()
|
|
486
|
+
});
|
|
487
|
+
var GetApiInformationOutputSchema = z.object({
|
|
488
|
+
apiUrl: z.string(),
|
|
489
|
+
anonKey: z.string()
|
|
490
|
+
});
|
|
491
|
+
var GetApiInformation = createRoute({
|
|
492
|
+
method: "get",
|
|
493
|
+
path: "/api/v1/auth/apiInformation",
|
|
494
|
+
operationId: "getApiInformation",
|
|
495
|
+
request: {
|
|
496
|
+
query: GetApiInformationInputSchema
|
|
497
|
+
},
|
|
498
|
+
responses: {
|
|
499
|
+
200: {
|
|
500
|
+
content: {
|
|
501
|
+
"application/json": {
|
|
502
|
+
schema: GetApiInformationOutputSchema
|
|
503
|
+
}
|
|
504
|
+
},
|
|
505
|
+
description: "Retrieve the API URL and anon key"
|
|
506
|
+
},
|
|
507
|
+
400: {
|
|
508
|
+
content: {
|
|
509
|
+
"application/json": {
|
|
510
|
+
schema: z.object({ error: z.string() })
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
description: "Invalid input"
|
|
514
|
+
},
|
|
515
|
+
500: {
|
|
516
|
+
content: {
|
|
517
|
+
"application/json": {
|
|
518
|
+
schema: z.object({
|
|
519
|
+
error: z.string()
|
|
520
|
+
})
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
description: "Unable to retrieve the API URL and anon key"
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
createRoute({
|
|
528
|
+
hide: true,
|
|
529
|
+
method: "get",
|
|
530
|
+
path: "/api/auth/apiInformation",
|
|
531
|
+
operationId: "getApiInformation",
|
|
532
|
+
request: {
|
|
533
|
+
query: GetApiInformationInputSchema
|
|
534
|
+
},
|
|
535
|
+
responses: {
|
|
536
|
+
200: {
|
|
537
|
+
content: {
|
|
538
|
+
"application/json": {
|
|
539
|
+
schema: GetApiInformationOutputSchema
|
|
540
|
+
}
|
|
541
|
+
},
|
|
542
|
+
description: "Retrieve the API URL and anon key"
|
|
543
|
+
},
|
|
544
|
+
400: {
|
|
545
|
+
content: {
|
|
546
|
+
"application/json": {
|
|
547
|
+
schema: z.object({ error: z.string() })
|
|
548
|
+
}
|
|
549
|
+
},
|
|
550
|
+
description: "Invalid input"
|
|
551
|
+
},
|
|
552
|
+
500: {
|
|
553
|
+
content: {
|
|
554
|
+
"application/json": {
|
|
555
|
+
schema: z.object({
|
|
556
|
+
error: z.string()
|
|
557
|
+
})
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
description: "Unable to retrieve the API URL and anon key"
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
var ExchangeExternalTokenInputSchema = z.object({
|
|
565
|
+
apiKey: z.string(),
|
|
566
|
+
externalJWT: z.string()
|
|
567
|
+
});
|
|
568
|
+
var ExchangeExternalTokenOutputSchema = z.object({
|
|
569
|
+
accessToken: z.string(),
|
|
570
|
+
refreshToken: z.string()
|
|
571
|
+
});
|
|
572
|
+
var ExchangeExternalToken = createRoute({
|
|
573
|
+
method: "post",
|
|
574
|
+
path: "/api/v1/auth/exchangeExternalToken",
|
|
575
|
+
operationId: "exchangeExternalToken",
|
|
576
|
+
request: {
|
|
577
|
+
body: {
|
|
578
|
+
required: true,
|
|
579
|
+
content: {
|
|
580
|
+
"application/json": {
|
|
581
|
+
schema: ExchangeExternalTokenInputSchema
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
responses: {
|
|
587
|
+
200: {
|
|
588
|
+
content: {
|
|
589
|
+
"application/json": {
|
|
590
|
+
schema: ExchangeExternalTokenOutputSchema
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
description: "Retrieve a JWT for an external user"
|
|
594
|
+
},
|
|
595
|
+
400: {
|
|
596
|
+
content: {
|
|
597
|
+
"application/json": {
|
|
598
|
+
schema: z.object({ error: z.string() })
|
|
599
|
+
}
|
|
600
|
+
},
|
|
601
|
+
description: "Invalid input"
|
|
602
|
+
},
|
|
603
|
+
500: {
|
|
604
|
+
content: {
|
|
605
|
+
"application/json": {
|
|
606
|
+
schema: z.object({
|
|
607
|
+
error: z.string()
|
|
608
|
+
})
|
|
609
|
+
}
|
|
610
|
+
},
|
|
611
|
+
description: "Unable to retrieve a JWT for an external user"
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
// src/core/types.ts
|
|
617
|
+
var TOKEN_STORAGE_KEY = "mm-ai-sp-token";
|
|
618
|
+
var CHECK_KEY = "mm-ai-check-key";
|
|
619
|
+
|
|
620
|
+
// src/core/auth/ExternalAuthProvider.ts
|
|
621
|
+
var MM_CLIENT_VERSION2 = package_default.version;
|
|
622
|
+
var ExternalAuthProvider = class {
|
|
623
|
+
config;
|
|
624
|
+
state = "initializing";
|
|
625
|
+
supabaseClient = null;
|
|
626
|
+
noAuthApiClient = null;
|
|
627
|
+
authApiResponse = null;
|
|
628
|
+
processedJwt = Symbol("initial");
|
|
629
|
+
userId = null;
|
|
630
|
+
events = new MageMetricsEventEmitter();
|
|
631
|
+
constructor(config) {
|
|
632
|
+
this.config = config;
|
|
633
|
+
}
|
|
634
|
+
async initialize() {
|
|
635
|
+
try {
|
|
636
|
+
this.setState("initializing");
|
|
637
|
+
const apiInfo = await this.getApiInformation();
|
|
638
|
+
this.initializeSupabaseClient(apiInfo.anonKey, apiInfo.apiUrl);
|
|
639
|
+
await this.handleAuthentication();
|
|
640
|
+
if (this.config.externalJwt) {
|
|
641
|
+
await this.saveCheckKey(this.config.externalJwt, this.config.apiKey);
|
|
642
|
+
}
|
|
643
|
+
this.setState("ready");
|
|
644
|
+
} catch (error) {
|
|
645
|
+
console.error("Authentication flow failed:", error);
|
|
646
|
+
this.setState("error");
|
|
647
|
+
throw error;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async getHeaders() {
|
|
651
|
+
if (!this.supabaseClient) {
|
|
652
|
+
return {};
|
|
653
|
+
}
|
|
654
|
+
const session = await this.supabaseClient.getSession();
|
|
655
|
+
if (!session.data.session) {
|
|
656
|
+
return {};
|
|
657
|
+
}
|
|
658
|
+
const { access_token } = session.data.session;
|
|
659
|
+
return {
|
|
660
|
+
[HEADER_CLIENT_VERSION]: MM_CLIENT_VERSION2,
|
|
661
|
+
[HEADER_SP_TOKEN]: access_token
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
getState() {
|
|
665
|
+
return this.state;
|
|
666
|
+
}
|
|
667
|
+
async updateExternalJwt(jwt) {
|
|
668
|
+
if (this.config.externalJwt === jwt) return;
|
|
669
|
+
this.config.externalJwt = jwt;
|
|
670
|
+
this.processedJwt = Symbol("updating");
|
|
671
|
+
await this.initialize();
|
|
672
|
+
}
|
|
673
|
+
async logout() {
|
|
674
|
+
if (this.supabaseClient) {
|
|
675
|
+
await this.supabaseClient.signOut({ scope: "local" });
|
|
676
|
+
}
|
|
677
|
+
await this.clearStorage();
|
|
678
|
+
}
|
|
679
|
+
getSupabaseClient() {
|
|
680
|
+
return this.supabaseClient;
|
|
681
|
+
}
|
|
682
|
+
addEventListener(type, listener, options) {
|
|
683
|
+
this.events.addEventListener(type, listener, options);
|
|
684
|
+
}
|
|
685
|
+
removeEventListener(type, listener) {
|
|
686
|
+
this.events.removeEventListener(type, listener);
|
|
687
|
+
}
|
|
688
|
+
startAutoRefresh() {
|
|
689
|
+
return this.supabaseClient?.startAutoRefresh();
|
|
690
|
+
}
|
|
691
|
+
stopAutoRefresh() {
|
|
692
|
+
return this.supabaseClient?.stopAutoRefresh();
|
|
693
|
+
}
|
|
694
|
+
async getApiInformation() {
|
|
695
|
+
if (!this.noAuthApiClient || !this.authApiResponse) {
|
|
696
|
+
this.noAuthApiClient = createApiClient2({
|
|
697
|
+
baseUrl: this.config.apiUrl
|
|
698
|
+
});
|
|
699
|
+
this.noAuthApiClient.use(throwOnError);
|
|
700
|
+
this.noAuthApiClient.use(addVersionHeader(MM_CLIENT_VERSION2));
|
|
701
|
+
this.noAuthApiClient.use(addApiKeyHeader(this.config.apiKey));
|
|
702
|
+
const { data } = await this.noAuthApiClient.GET(GetApiInformation.path, {
|
|
703
|
+
params: {
|
|
704
|
+
query: {
|
|
705
|
+
apiKey: this.config.apiKey
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
if (!data) {
|
|
710
|
+
throw new Error("Failed to fetch API information");
|
|
711
|
+
}
|
|
712
|
+
this.authApiResponse = data;
|
|
713
|
+
}
|
|
714
|
+
return this.authApiResponse;
|
|
715
|
+
}
|
|
716
|
+
initializeSupabaseClient(anonKey, apiUrl) {
|
|
717
|
+
if (!this.supabaseClient) {
|
|
718
|
+
this.supabaseClient = new GoTrueClient({
|
|
719
|
+
url: `${apiUrl}/auth/v1`,
|
|
720
|
+
storageKey: TOKEN_STORAGE_KEY,
|
|
721
|
+
headers: {
|
|
722
|
+
apiKey: anonKey
|
|
723
|
+
},
|
|
724
|
+
storage: this.config.storage,
|
|
725
|
+
...this.config.authOptions
|
|
726
|
+
});
|
|
727
|
+
this.supabaseClient.onAuthStateChange((event, session) => {
|
|
728
|
+
console.debug("Supabase auth state change:", event, !!session);
|
|
729
|
+
if (event === "SIGNED_IN" && session?.user) {
|
|
730
|
+
if (this.userId !== session.user.id && this.userId !== null) {
|
|
731
|
+
this.events.dispatch("userChange", { id: session.user.id });
|
|
732
|
+
}
|
|
733
|
+
this.userId = session.user.id;
|
|
734
|
+
}
|
|
735
|
+
if (event === "TOKEN_REFRESHED" && session) {
|
|
736
|
+
console.debug("Token refreshed successfully");
|
|
737
|
+
this.setState("ready");
|
|
738
|
+
} else if (event === "SIGNED_OUT") {
|
|
739
|
+
console.debug("User signed out");
|
|
740
|
+
void this.clearStorage();
|
|
741
|
+
this.setState("initializing");
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async handleAuthentication() {
|
|
747
|
+
if (!this.supabaseClient) {
|
|
748
|
+
throw new Error("Supabase client not initialized");
|
|
749
|
+
}
|
|
750
|
+
if (this.processedJwt === this.config.externalJwt) {
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
try {
|
|
754
|
+
const { data, error } = await this.supabaseClient.getSession();
|
|
755
|
+
if (error) {
|
|
756
|
+
console.error("Error getting session:", error);
|
|
757
|
+
this.processedJwt = this.config.externalJwt || "";
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const isCheckValid = await this.compareCheckKey(
|
|
761
|
+
this.config.externalJwt ?? "",
|
|
762
|
+
this.config.apiKey
|
|
763
|
+
);
|
|
764
|
+
if (data.session && this.config.externalJwt && isCheckValid) {
|
|
765
|
+
console.debug("Session found, authentication ready");
|
|
766
|
+
this.processedJwt = this.config.externalJwt || "";
|
|
767
|
+
} else if (this.config.externalJwt) {
|
|
768
|
+
console.debug("No session found, exchanging external JWT");
|
|
769
|
+
await this.clearCheckKey();
|
|
770
|
+
await this.exchangeExternalToken();
|
|
771
|
+
this.processedJwt = this.config.externalJwt;
|
|
772
|
+
} else {
|
|
773
|
+
console.debug("No session and no external JWT provided");
|
|
774
|
+
this.processedJwt = this.config.externalJwt || "";
|
|
775
|
+
}
|
|
776
|
+
} catch (error) {
|
|
777
|
+
console.error("Unhandled error during authentication:", error);
|
|
778
|
+
this.processedJwt = this.config.externalJwt || "";
|
|
779
|
+
throw error;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
async exchangeExternalToken() {
|
|
783
|
+
if (!this.noAuthApiClient || !this.supabaseClient || !this.config.externalJwt) {
|
|
784
|
+
throw new Error(
|
|
785
|
+
"Required clients or JWT not available for token exchange"
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
const { data } = await this.noAuthApiClient.POST(
|
|
789
|
+
ExchangeExternalToken.path,
|
|
790
|
+
{
|
|
791
|
+
body: {
|
|
792
|
+
apiKey: this.config.apiKey,
|
|
793
|
+
externalJWT: this.config.externalJwt
|
|
794
|
+
},
|
|
795
|
+
headers: this.config.additionalHeaders || {}
|
|
796
|
+
}
|
|
797
|
+
);
|
|
798
|
+
if (!data) {
|
|
799
|
+
throw new Error("Failed to exchange tokens");
|
|
800
|
+
}
|
|
801
|
+
await this.supabaseClient.setSession({
|
|
802
|
+
access_token: data.accessToken,
|
|
803
|
+
refresh_token: data.refreshToken
|
|
804
|
+
});
|
|
805
|
+
console.debug("Token exchange successful, session set");
|
|
806
|
+
}
|
|
807
|
+
setState(state) {
|
|
808
|
+
this.state = state;
|
|
809
|
+
this.events.dispatch("authStateChange", state);
|
|
279
810
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const _a = general !== null && general !== void 0 ? general : {}, { id, title } = _a, restGeneral = __rest(_a, ["id", "title"]);
|
|
288
|
-
return Object.assign(Object.assign(Object.assign({ _internal: Object.assign(Object.assign({}, id ? { refId: id } : {}), _internal) }, rest), title ? { description: title } : {}), restGeneral);
|
|
811
|
+
encodeCheckKey(externalJwt, apiKey) {
|
|
812
|
+
return hashString(
|
|
813
|
+
JSON.stringify({
|
|
814
|
+
externalJwt,
|
|
815
|
+
apiKey
|
|
816
|
+
})
|
|
817
|
+
);
|
|
289
818
|
}
|
|
290
|
-
|
|
291
|
-
|
|
819
|
+
async saveCheckKey(externalJwt, apiKey) {
|
|
820
|
+
const encodedKey = this.encodeCheckKey(externalJwt, apiKey);
|
|
821
|
+
await this.config.storage.setItem(CHECK_KEY, encodedKey);
|
|
292
822
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return;
|
|
823
|
+
async compareCheckKey(externalJwt, apiKey) {
|
|
824
|
+
const storedKey = await this.config.storage.getItem(CHECK_KEY);
|
|
825
|
+
if (!storedKey) return false;
|
|
826
|
+
const newValue = this.encodeCheckKey(externalJwt, apiKey);
|
|
827
|
+
return storedKey === newValue;
|
|
298
828
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const meta = Metadata.getMetadataFromRegistry(this);
|
|
302
|
-
if (meta) {
|
|
303
|
-
Metadata.setMetadataInRegistry(result, meta);
|
|
304
|
-
}
|
|
305
|
-
return result;
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
function extendZodWithOpenApi(zod) {
|
|
309
|
-
if (typeof zod.ZodType.prototype.openapi !== "undefined") {
|
|
310
|
-
return;
|
|
829
|
+
async clearCheckKey() {
|
|
830
|
+
await this.config.storage.removeItem(CHECK_KEY);
|
|
311
831
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const _internal = Object.assign(Object.assign(Object.assign({}, internalMetadata), options), refId ? { refId } : void 0);
|
|
318
|
-
const resultMetadata = Object.assign(Object.assign(Object.assign({}, currentMetadata), restOfOpenApi), (currentMetadata === null || currentMetadata === void 0 ? void 0 : currentMetadata.param) || param ? {
|
|
319
|
-
param: Object.assign(Object.assign({}, currentMetadata === null || currentMetadata === void 0 ? void 0 : currentMetadata.param), param)
|
|
320
|
-
} : void 0);
|
|
321
|
-
const result = new this.constructor(this._def);
|
|
322
|
-
Metadata.setMetadataInRegistry(result, Object.assign(Object.assign({}, Object.keys(_internal).length > 0 ? { _internal } : void 0), resultMetadata));
|
|
323
|
-
if (isZodType(result, "ZodObject")) {
|
|
324
|
-
const currentMetadata2 = Metadata.getMetadataFromRegistry(result);
|
|
325
|
-
const originalExtend = result.extend;
|
|
326
|
-
result.extend = function(...args2) {
|
|
327
|
-
const extendedResult = originalExtend.apply(result, args2);
|
|
328
|
-
const _a2 = currentMetadata2 !== null && currentMetadata2 !== void 0 ? currentMetadata2 : {}, { _internal: _internal2 } = _a2, rest = __rest(_a2, ["_internal"]);
|
|
329
|
-
Metadata.setMetadataInRegistry(extendedResult, {
|
|
330
|
-
_internal: {
|
|
331
|
-
extendedFrom: (_internal2 === null || _internal2 === void 0 ? void 0 : _internal2.refId) ? { refId: _internal2.refId, schema: result } : _internal2 === null || _internal2 === void 0 ? void 0 : _internal2.extendedFrom
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
return extendedResult.openapi(rest);
|
|
335
|
-
};
|
|
336
|
-
preserveMetadataFromModifier(result, "catchall");
|
|
832
|
+
async clearStorage() {
|
|
833
|
+
try {
|
|
834
|
+
await this.config.storage.removeItem(TOKEN_STORAGE_KEY);
|
|
835
|
+
await this.config.storage.removeItem(CHECK_KEY);
|
|
836
|
+
} catch {
|
|
337
837
|
}
|
|
338
|
-
preserveMetadataFromModifier(result, "optional");
|
|
339
|
-
preserveMetadataFromModifier(result, "nullable");
|
|
340
|
-
preserveMetadataFromModifier(result, "default");
|
|
341
|
-
preserveMetadataFromModifier(result, "transform");
|
|
342
|
-
preserveMetadataFromModifier(result, "refine");
|
|
343
|
-
preserveMetadataFromModifier(result, "length");
|
|
344
|
-
preserveMetadataFromModifier(result, "min");
|
|
345
|
-
preserveMetadataFromModifier(result, "max");
|
|
346
|
-
const originalMeta = result.meta;
|
|
347
|
-
result.meta = function(...args2) {
|
|
348
|
-
const result2 = originalMeta.apply(this, args2);
|
|
349
|
-
if (args2[0]) {
|
|
350
|
-
const meta = Metadata.getMetadataFromInternalRegistry(this);
|
|
351
|
-
if (meta) {
|
|
352
|
-
Metadata.setMetadataInRegistry(result2, Object.assign(Object.assign({}, meta), args2[0]));
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return result2;
|
|
356
|
-
};
|
|
357
|
-
return result;
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
function getOpenApiConfiguration(refOrOpenapi, metadataOrOptions, options) {
|
|
361
|
-
if (typeof refOrOpenapi === "string") {
|
|
362
|
-
return {
|
|
363
|
-
refId: refOrOpenapi,
|
|
364
|
-
metadata: metadataOrOptions,
|
|
365
|
-
options
|
|
366
|
-
};
|
|
367
838
|
}
|
|
368
|
-
return {
|
|
369
|
-
refId: void 0,
|
|
370
|
-
metadata: refOrOpenapi,
|
|
371
|
-
options: metadataOrOptions
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
new Set(".\\+*[^]$()");
|
|
375
|
-
var createRoute = (routeConfig) => {
|
|
376
|
-
const route = {
|
|
377
|
-
...routeConfig,
|
|
378
|
-
getRoutingPath() {
|
|
379
|
-
return routeConfig.path.replaceAll(/\/{(.+?)}/g, "/:$1");
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
return Object.defineProperty(route, "getRoutingPath", { enumerable: false });
|
|
383
839
|
};
|
|
384
|
-
extendZodWithOpenApi(z);
|
|
385
840
|
|
|
386
841
|
// ../shared/dist/src/endpoints/admin-console/sqlPreview.routes.js
|
|
387
842
|
var TanStackColumnSchema = z.object({
|
|
@@ -508,7 +963,7 @@ var columnsQueryParams = z.string().transform((str) => {
|
|
|
508
963
|
type: "string"
|
|
509
964
|
});
|
|
510
965
|
var SupabaseUserHeaderSchema = z.object({
|
|
511
|
-
|
|
966
|
+
[HEADER_SP_TOKEN]: z.string()
|
|
512
967
|
});
|
|
513
968
|
var SupabaseHeaderSchema = SupabaseUserHeaderSchema;
|
|
514
969
|
var FEEDBACK_OPTIONS = [
|
|
@@ -601,7 +1056,20 @@ var ReportColumnSchema = z.looseObject({
|
|
|
601
1056
|
data_type: z.string(),
|
|
602
1057
|
null_count: z.number().nullable(),
|
|
603
1058
|
unique_count: z.number().nullable(),
|
|
604
|
-
unique_percentage: z.number().nullable()
|
|
1059
|
+
unique_percentage: z.number().nullable(),
|
|
1060
|
+
// Numeric column statistics
|
|
1061
|
+
min_value: z.number().nullable().optional(),
|
|
1062
|
+
max_value: z.number().nullable().optional(),
|
|
1063
|
+
avg_value: z.number().nullable().optional(),
|
|
1064
|
+
median_value: z.number().nullable().optional(),
|
|
1065
|
+
std_value: z.number().nullable().optional(),
|
|
1066
|
+
// Date column statistics
|
|
1067
|
+
min_date: z.string().nullable().optional(),
|
|
1068
|
+
max_date: z.string().nullable().optional(),
|
|
1069
|
+
// String column statistics
|
|
1070
|
+
min_length: z.number().nullable().optional(),
|
|
1071
|
+
max_length: z.number().nullable().optional(),
|
|
1072
|
+
avg_length: z.number().nullable().optional()
|
|
605
1073
|
});
|
|
606
1074
|
var MaterializationStatusSchema = z.enum([
|
|
607
1075
|
"completed",
|
|
@@ -676,6 +1144,9 @@ var ReportExplainabilitySchema = z.object({
|
|
|
676
1144
|
var FrontendReportExplainabilitySchema = ReportExplainabilitySchema.omit({
|
|
677
1145
|
id: true,
|
|
678
1146
|
created_at: true
|
|
1147
|
+
}).extend({
|
|
1148
|
+
output_dataset: z.string().optional(),
|
|
1149
|
+
sql: z.string().optional()
|
|
679
1150
|
});
|
|
680
1151
|
|
|
681
1152
|
// ../shared/dist/src/endpoints/companion/flows.routes.js
|
|
@@ -1567,157 +2038,24 @@ createRoute({
|
|
|
1567
2038
|
}
|
|
1568
2039
|
},
|
|
1569
2040
|
404: {
|
|
1570
|
-
description: "Report not found",
|
|
1571
|
-
content: {
|
|
1572
|
-
"application/json": {
|
|
1573
|
-
schema: z.object({
|
|
1574
|
-
error: z.string()
|
|
1575
|
-
})
|
|
1576
|
-
}
|
|
1577
|
-
}
|
|
1578
|
-
},
|
|
1579
|
-
500: {
|
|
1580
|
-
description: "Something wrong happened",
|
|
1581
|
-
content: {
|
|
1582
|
-
"application/json": {
|
|
1583
|
-
schema: z.object({
|
|
1584
|
-
error: z.string()
|
|
1585
|
-
})
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
}
|
|
1589
|
-
}
|
|
1590
|
-
});
|
|
1591
|
-
|
|
1592
|
-
// ../shared/dist/src/endpoints/auth.routes.js
|
|
1593
|
-
var GetApiInformationInputSchema = z.object({
|
|
1594
|
-
apiKey: z.string()
|
|
1595
|
-
});
|
|
1596
|
-
var GetApiInformationOutputSchema = z.object({
|
|
1597
|
-
apiUrl: z.string(),
|
|
1598
|
-
anonKey: z.string()
|
|
1599
|
-
});
|
|
1600
|
-
var GetApiInformation = createRoute({
|
|
1601
|
-
method: "get",
|
|
1602
|
-
path: "/api/v1/auth/apiInformation",
|
|
1603
|
-
operationId: "getApiInformation",
|
|
1604
|
-
request: {
|
|
1605
|
-
query: GetApiInformationInputSchema
|
|
1606
|
-
},
|
|
1607
|
-
responses: {
|
|
1608
|
-
200: {
|
|
1609
|
-
content: {
|
|
1610
|
-
"application/json": {
|
|
1611
|
-
schema: GetApiInformationOutputSchema
|
|
1612
|
-
}
|
|
1613
|
-
},
|
|
1614
|
-
description: "Retrieve the API URL and anon key"
|
|
1615
|
-
},
|
|
1616
|
-
400: {
|
|
1617
|
-
content: {
|
|
1618
|
-
"application/json": {
|
|
1619
|
-
schema: z.object({ error: z.string() })
|
|
1620
|
-
}
|
|
1621
|
-
},
|
|
1622
|
-
description: "Invalid input"
|
|
1623
|
-
},
|
|
1624
|
-
500: {
|
|
1625
|
-
content: {
|
|
1626
|
-
"application/json": {
|
|
1627
|
-
schema: z.object({
|
|
1628
|
-
error: z.string()
|
|
1629
|
-
})
|
|
1630
|
-
}
|
|
1631
|
-
},
|
|
1632
|
-
description: "Unable to retrieve the API URL and anon key"
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
|
-
});
|
|
1636
|
-
createRoute({
|
|
1637
|
-
hide: true,
|
|
1638
|
-
method: "get",
|
|
1639
|
-
path: "/api/auth/apiInformation",
|
|
1640
|
-
operationId: "getApiInformation",
|
|
1641
|
-
request: {
|
|
1642
|
-
query: GetApiInformationInputSchema
|
|
1643
|
-
},
|
|
1644
|
-
responses: {
|
|
1645
|
-
200: {
|
|
1646
|
-
content: {
|
|
1647
|
-
"application/json": {
|
|
1648
|
-
schema: GetApiInformationOutputSchema
|
|
1649
|
-
}
|
|
1650
|
-
},
|
|
1651
|
-
description: "Retrieve the API URL and anon key"
|
|
1652
|
-
},
|
|
1653
|
-
400: {
|
|
1654
|
-
content: {
|
|
1655
|
-
"application/json": {
|
|
1656
|
-
schema: z.object({ error: z.string() })
|
|
1657
|
-
}
|
|
1658
|
-
},
|
|
1659
|
-
description: "Invalid input"
|
|
1660
|
-
},
|
|
1661
|
-
500: {
|
|
1662
|
-
content: {
|
|
1663
|
-
"application/json": {
|
|
1664
|
-
schema: z.object({
|
|
1665
|
-
error: z.string()
|
|
1666
|
-
})
|
|
1667
|
-
}
|
|
1668
|
-
},
|
|
1669
|
-
description: "Unable to retrieve the API URL and anon key"
|
|
1670
|
-
}
|
|
1671
|
-
}
|
|
1672
|
-
});
|
|
1673
|
-
var ExchangeExternalTokenInputSchema = z.object({
|
|
1674
|
-
apiKey: z.string(),
|
|
1675
|
-
externalJWT: z.string()
|
|
1676
|
-
});
|
|
1677
|
-
var ExchangeExternalTokenOutputSchema = z.object({
|
|
1678
|
-
accessToken: z.string(),
|
|
1679
|
-
refreshToken: z.string()
|
|
1680
|
-
});
|
|
1681
|
-
var ExchangeExternalToken = createRoute({
|
|
1682
|
-
method: "post",
|
|
1683
|
-
path: "/api/v1/auth/exchangeExternalToken",
|
|
1684
|
-
operationId: "exchangeExternalToken",
|
|
1685
|
-
request: {
|
|
1686
|
-
body: {
|
|
1687
|
-
required: true,
|
|
1688
|
-
content: {
|
|
1689
|
-
"application/json": {
|
|
1690
|
-
schema: ExchangeExternalTokenInputSchema
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
},
|
|
1695
|
-
responses: {
|
|
1696
|
-
200: {
|
|
1697
|
-
content: {
|
|
1698
|
-
"application/json": {
|
|
1699
|
-
schema: ExchangeExternalTokenOutputSchema
|
|
1700
|
-
}
|
|
1701
|
-
},
|
|
1702
|
-
description: "Retrieve a JWT for an external user"
|
|
1703
|
-
},
|
|
1704
|
-
400: {
|
|
2041
|
+
description: "Report not found",
|
|
1705
2042
|
content: {
|
|
1706
2043
|
"application/json": {
|
|
1707
|
-
schema: z.object({
|
|
2044
|
+
schema: z.object({
|
|
2045
|
+
error: z.string()
|
|
2046
|
+
})
|
|
1708
2047
|
}
|
|
1709
|
-
}
|
|
1710
|
-
description: "Invalid input"
|
|
2048
|
+
}
|
|
1711
2049
|
},
|
|
1712
2050
|
500: {
|
|
2051
|
+
description: "Something wrong happened",
|
|
1713
2052
|
content: {
|
|
1714
2053
|
"application/json": {
|
|
1715
2054
|
schema: z.object({
|
|
1716
2055
|
error: z.string()
|
|
1717
2056
|
})
|
|
1718
2057
|
}
|
|
1719
|
-
}
|
|
1720
|
-
description: "Unable to retrieve a JWT for an external user"
|
|
2058
|
+
}
|
|
1721
2059
|
}
|
|
1722
2060
|
}
|
|
1723
2061
|
});
|
|
@@ -1982,12 +2320,6 @@ var createStorageAdapter = () => {
|
|
|
1982
2320
|
return new MemoryStorageAdapter();
|
|
1983
2321
|
}
|
|
1984
2322
|
};
|
|
1985
|
-
var hashString = (value) => {
|
|
1986
|
-
const encoder = new TextEncoder();
|
|
1987
|
-
const data = encoder.encode(value);
|
|
1988
|
-
const hash = sha256(data);
|
|
1989
|
-
return Array.from(hash).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1990
|
-
};
|
|
1991
2323
|
|
|
1992
2324
|
// ../shared/dist/src/schemas/canvas.js
|
|
1993
2325
|
var DATA_REPORT_LABEL = "dataReport";
|
|
@@ -2798,10 +3130,6 @@ var toSearchParams = ({ cursor, filters, sorting, limit }) => {
|
|
|
2798
3130
|
}
|
|
2799
3131
|
return params;
|
|
2800
3132
|
};
|
|
2801
|
-
|
|
2802
|
-
// package.json
|
|
2803
|
-
var package_default = {
|
|
2804
|
-
version: "0.5.4"};
|
|
2805
3133
|
var MageMetricsChatTransport = class extends DefaultChatTransport {
|
|
2806
3134
|
constructor(apiUrl, flowId, options) {
|
|
2807
3135
|
super({
|
|
@@ -2814,48 +3142,6 @@ var MageMetricsChatTransport = class extends DefaultChatTransport {
|
|
|
2814
3142
|
}
|
|
2815
3143
|
};
|
|
2816
3144
|
|
|
2817
|
-
// src/core/MageMetricsEventEmitter.ts
|
|
2818
|
-
var MageMetricsEventEmitter = class {
|
|
2819
|
-
listeners = /* @__PURE__ */ new Map();
|
|
2820
|
-
addEventListener(type, listener, options) {
|
|
2821
|
-
if (!this.listeners.has(type)) {
|
|
2822
|
-
this.listeners.set(type, /* @__PURE__ */ new Set());
|
|
2823
|
-
}
|
|
2824
|
-
if (options?.signal) {
|
|
2825
|
-
if (options.signal.aborted) {
|
|
2826
|
-
return;
|
|
2827
|
-
}
|
|
2828
|
-
const abortHandler = () => {
|
|
2829
|
-
this.removeEventListener(type, listener);
|
|
2830
|
-
};
|
|
2831
|
-
options.signal.addEventListener("abort", abortHandler, { once: true });
|
|
2832
|
-
}
|
|
2833
|
-
this.listeners.get(type).add(listener);
|
|
2834
|
-
}
|
|
2835
|
-
removeEventListener(type, listener) {
|
|
2836
|
-
const typeListeners = this.listeners.get(type);
|
|
2837
|
-
if (typeListeners) {
|
|
2838
|
-
typeListeners.delete(listener);
|
|
2839
|
-
if (typeListeners.size === 0) {
|
|
2840
|
-
this.listeners.delete(type);
|
|
2841
|
-
}
|
|
2842
|
-
}
|
|
2843
|
-
}
|
|
2844
|
-
dispatch(type, detail) {
|
|
2845
|
-
const typeListeners = this.listeners.get(type);
|
|
2846
|
-
if (typeListeners) {
|
|
2847
|
-
const event = { type, detail };
|
|
2848
|
-
typeListeners.forEach((listener) => {
|
|
2849
|
-
try {
|
|
2850
|
-
listener(event);
|
|
2851
|
-
} catch (error) {
|
|
2852
|
-
console.error(`Error in event listener for "${type}":`, error);
|
|
2853
|
-
}
|
|
2854
|
-
});
|
|
2855
|
-
}
|
|
2856
|
-
}
|
|
2857
|
-
};
|
|
2858
|
-
|
|
2859
3145
|
// src/core/resolvable.ts
|
|
2860
3146
|
var resolve = (value) => {
|
|
2861
3147
|
if (typeof value === "function") {
|
|
@@ -2864,45 +3150,54 @@ var resolve = (value) => {
|
|
|
2864
3150
|
return Promise.resolve(value);
|
|
2865
3151
|
};
|
|
2866
3152
|
|
|
2867
|
-
// src/core/types.ts
|
|
2868
|
-
var TOKEN_STORAGE_KEY = "mm-ai-sp-token";
|
|
2869
|
-
var CHECK_KEY = "mm-ai-check-key";
|
|
2870
|
-
|
|
2871
3153
|
// src/core/MageMetricsClient.ts
|
|
2872
|
-
var
|
|
3154
|
+
var MM_CLIENT_VERSION3 = package_default.version;
|
|
2873
3155
|
var getPublicApiClient = (apiUrl, apiKey) => {
|
|
2874
|
-
const client =
|
|
3156
|
+
const client = createApiClient2({ baseUrl: apiUrl });
|
|
2875
3157
|
client.use(throwOnError);
|
|
2876
|
-
client.use(addVersionHeader(
|
|
3158
|
+
client.use(addVersionHeader(MM_CLIENT_VERSION3));
|
|
2877
3159
|
client.use(addApiKeyHeader(apiKey));
|
|
2878
3160
|
return client;
|
|
2879
3161
|
};
|
|
2880
3162
|
var MageMetricsClient = class {
|
|
2881
3163
|
config;
|
|
2882
|
-
|
|
2883
|
-
supabaseClient = null;
|
|
3164
|
+
authProvider;
|
|
2884
3165
|
internalApiClient;
|
|
2885
|
-
noAuthApiClient = null;
|
|
2886
|
-
authApiResponse = null;
|
|
2887
3166
|
authPromise = null;
|
|
2888
|
-
|
|
2889
|
-
userId = null;
|
|
2890
|
-
storageAdapter;
|
|
2891
|
-
events = new MageMetricsEventEmitter();
|
|
2892
|
-
authOptions;
|
|
2893
|
-
constructor(config, auth) {
|
|
3167
|
+
constructor(config, authProviderOrAuthOptions) {
|
|
2894
3168
|
this.config = config;
|
|
2895
|
-
|
|
2896
|
-
|
|
3169
|
+
const isAuthProvider = authProviderOrAuthOptions && "initialize" in authProviderOrAuthOptions && "getHeaders" in authProviderOrAuthOptions;
|
|
3170
|
+
if (isAuthProvider) {
|
|
3171
|
+
this.authProvider = authProviderOrAuthOptions;
|
|
3172
|
+
} else {
|
|
3173
|
+
const storageAdapter = config.storageAdapter || createStorageAdapter();
|
|
3174
|
+
this.authProvider = new ExternalAuthProvider({
|
|
3175
|
+
apiUrl: config.apiUrl,
|
|
3176
|
+
apiKey: config.apiKey,
|
|
3177
|
+
externalJwt: config.externalJwt,
|
|
3178
|
+
additionalHeaders: config.additionalHeaders,
|
|
3179
|
+
storage: storageAdapter,
|
|
3180
|
+
applicationName: config.applicationName,
|
|
3181
|
+
authOptions: authProviderOrAuthOptions
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3184
|
+
this.internalApiClient = createApiClient2({
|
|
2897
3185
|
baseUrl: this.config.apiUrl
|
|
2898
3186
|
});
|
|
2899
3187
|
this.internalApiClient.use(throwOnError);
|
|
2900
3188
|
this.internalApiClient.use(this.createSupabaseHeaderMiddleware());
|
|
2901
|
-
this.authOptions = auth || {};
|
|
2902
3189
|
}
|
|
3190
|
+
/* This is used by react-query query keys to identify unique clients
|
|
3191
|
+
* we hash it to make it more digestible and safer to debug/log
|
|
3192
|
+
*/
|
|
2903
3193
|
toJSON() {
|
|
2904
3194
|
return {
|
|
2905
|
-
config:
|
|
3195
|
+
config: hashString(
|
|
3196
|
+
JSON.stringify({
|
|
3197
|
+
apiKey: this.config.apiKey,
|
|
3198
|
+
apiUrl: this.config.apiUrl
|
|
3199
|
+
})
|
|
3200
|
+
)
|
|
2906
3201
|
};
|
|
2907
3202
|
}
|
|
2908
3203
|
get apiConfig() {
|
|
@@ -2919,7 +3214,7 @@ var MageMetricsClient = class {
|
|
|
2919
3214
|
* @param options.signal an optional AbortSignal to abort the event listener
|
|
2920
3215
|
*/
|
|
2921
3216
|
addEventListener(type, listener, options) {
|
|
2922
|
-
this.
|
|
3217
|
+
this.authProvider.addEventListener(type, listener, options);
|
|
2923
3218
|
}
|
|
2924
3219
|
/**
|
|
2925
3220
|
* Remove an event listener
|
|
@@ -2927,60 +3222,42 @@ var MageMetricsClient = class {
|
|
|
2927
3222
|
* @param listener the listener to remove
|
|
2928
3223
|
*/
|
|
2929
3224
|
removeEventListener(type, listener) {
|
|
2930
|
-
this.
|
|
3225
|
+
this.authProvider.removeEventListener(type, listener);
|
|
2931
3226
|
}
|
|
2932
3227
|
async waitForAuth() {
|
|
2933
|
-
if (this.
|
|
3228
|
+
if (this.authProvider.getState() === "ready") {
|
|
2934
3229
|
return Promise.resolve();
|
|
2935
3230
|
}
|
|
2936
|
-
if (this.
|
|
3231
|
+
if (this.authProvider.getState() === "error") {
|
|
2937
3232
|
throw new Error("Authentication failed");
|
|
2938
3233
|
}
|
|
2939
3234
|
if (!this.authPromise) {
|
|
2940
|
-
this.authPromise = this.
|
|
3235
|
+
this.authPromise = this.authProvider.initialize();
|
|
2941
3236
|
}
|
|
2942
3237
|
return this.authPromise;
|
|
2943
3238
|
}
|
|
2944
3239
|
async updateExternalJwt(jwt) {
|
|
2945
|
-
|
|
2946
|
-
this.config.externalJwt = jwt;
|
|
2947
|
-
this.processedJwt = Symbol("updating");
|
|
2948
|
-
await this.performAuthFlow();
|
|
3240
|
+
await this.authProvider.updateExternalJwt(jwt);
|
|
2949
3241
|
}
|
|
2950
3242
|
get state() {
|
|
2951
|
-
return this.
|
|
3243
|
+
return this.authProvider.getState();
|
|
2952
3244
|
}
|
|
2953
3245
|
async getHeaders() {
|
|
2954
3246
|
await this.waitForAuth();
|
|
2955
|
-
|
|
2956
|
-
return {};
|
|
2957
|
-
}
|
|
2958
|
-
const session = await this.supabaseClient.getSession();
|
|
2959
|
-
if (!session.data.session) {
|
|
2960
|
-
return {};
|
|
2961
|
-
}
|
|
2962
|
-
const { access_token } = session.data.session;
|
|
2963
|
-
return {
|
|
2964
|
-
[HEADER_CLIENT_VERSION]: MM_CLIENT_VERSION,
|
|
2965
|
-
"sp-access-token": access_token
|
|
2966
|
-
};
|
|
2967
|
-
}
|
|
2968
|
-
async clearStorage() {
|
|
2969
|
-
try {
|
|
2970
|
-
await this.storageAdapter.removeItem(TOKEN_STORAGE_KEY);
|
|
2971
|
-
await this.storageAdapter.removeItem(CHECK_KEY);
|
|
2972
|
-
} catch {
|
|
2973
|
-
}
|
|
3247
|
+
return this.authProvider.getHeaders();
|
|
2974
3248
|
}
|
|
2975
3249
|
async logout() {
|
|
2976
|
-
|
|
2977
|
-
await this.supabaseClient.signOut({ scope: "local" });
|
|
2978
|
-
}
|
|
2979
|
-
await this.clearStorage();
|
|
3250
|
+
await this.authProvider.logout();
|
|
2980
3251
|
}
|
|
2981
3252
|
auth = {
|
|
2982
|
-
startAutoRefresh: () =>
|
|
2983
|
-
|
|
3253
|
+
startAutoRefresh: () => {
|
|
3254
|
+
const supabaseClient = this.authProvider.getSupabaseClient();
|
|
3255
|
+
return supabaseClient?.startAutoRefresh();
|
|
3256
|
+
},
|
|
3257
|
+
stopAuthRefresh: () => {
|
|
3258
|
+
const supabaseClient = this.authProvider.getSupabaseClient();
|
|
3259
|
+
return supabaseClient?.stopAutoRefresh();
|
|
3260
|
+
}
|
|
2984
3261
|
};
|
|
2985
3262
|
/**
|
|
2986
3263
|
* Public API methods that automatically wait for authentication
|
|
@@ -3387,156 +3664,13 @@ var MageMetricsClient = class {
|
|
|
3387
3664
|
};
|
|
3388
3665
|
}
|
|
3389
3666
|
};
|
|
3390
|
-
/**
|
|
3391
|
-
* Perform the complete authentication flow
|
|
3392
|
-
*/
|
|
3393
|
-
async performAuthFlow() {
|
|
3394
|
-
try {
|
|
3395
|
-
this.setState("initializing");
|
|
3396
|
-
const apiInfo = await this.getApiInformation();
|
|
3397
|
-
this.initializeSupabaseClient(apiInfo.anonKey, apiInfo.apiUrl);
|
|
3398
|
-
await this.handleAuthentication();
|
|
3399
|
-
if (this.config.externalJwt) {
|
|
3400
|
-
await this.saveCheckKey(this.config.externalJwt, this.config.apiKey);
|
|
3401
|
-
}
|
|
3402
|
-
this.setState("ready");
|
|
3403
|
-
} catch (error) {
|
|
3404
|
-
console.error("Authentication flow failed:", error);
|
|
3405
|
-
this.setState("error");
|
|
3406
|
-
throw error;
|
|
3407
|
-
}
|
|
3408
|
-
}
|
|
3409
|
-
async getApiInformation() {
|
|
3410
|
-
if (!this.noAuthApiClient || !this.authApiResponse) {
|
|
3411
|
-
this.noAuthApiClient = getPublicApiClient(
|
|
3412
|
-
this.config.apiUrl,
|
|
3413
|
-
this.config.apiKey
|
|
3414
|
-
);
|
|
3415
|
-
const { data } = await this.noAuthApiClient.GET(GetApiInformation.path, {
|
|
3416
|
-
params: {
|
|
3417
|
-
query: {
|
|
3418
|
-
apiKey: this.config.apiKey
|
|
3419
|
-
}
|
|
3420
|
-
}
|
|
3421
|
-
});
|
|
3422
|
-
if (!data) {
|
|
3423
|
-
throw new Error("Failed to fetch API information");
|
|
3424
|
-
}
|
|
3425
|
-
this.authApiResponse = data;
|
|
3426
|
-
}
|
|
3427
|
-
return this.authApiResponse;
|
|
3428
|
-
}
|
|
3429
|
-
initializeSupabaseClient(anonKey, apiUrl) {
|
|
3430
|
-
if (!this.supabaseClient) {
|
|
3431
|
-
this.supabaseClient = new GoTrueClient({
|
|
3432
|
-
url: `${apiUrl}/auth/v1`,
|
|
3433
|
-
storageKey: TOKEN_STORAGE_KEY,
|
|
3434
|
-
headers: {
|
|
3435
|
-
apiKey: anonKey
|
|
3436
|
-
},
|
|
3437
|
-
storage: this.storageAdapter,
|
|
3438
|
-
...this.authOptions
|
|
3439
|
-
});
|
|
3440
|
-
this.supabaseClient.onAuthStateChange((event, session) => {
|
|
3441
|
-
console.debug("Supabase auth state change:", event, !!session);
|
|
3442
|
-
if (event === "SIGNED_IN" && session?.user) {
|
|
3443
|
-
if (this.userId !== session.user.id && this.userId !== null) {
|
|
3444
|
-
this.events.dispatch("userChange", { id: session.user.id });
|
|
3445
|
-
}
|
|
3446
|
-
this.userId = session.user.id;
|
|
3447
|
-
}
|
|
3448
|
-
if (event === "TOKEN_REFRESHED" && session) {
|
|
3449
|
-
console.debug("Token refreshed successfully");
|
|
3450
|
-
this.setState("ready");
|
|
3451
|
-
} else if (event === "SIGNED_OUT") {
|
|
3452
|
-
console.debug("User signed out");
|
|
3453
|
-
void this.clearStorage();
|
|
3454
|
-
this.setState("initializing");
|
|
3455
|
-
}
|
|
3456
|
-
});
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
3667
|
client() {
|
|
3460
3668
|
return this.internalApiClient;
|
|
3461
3669
|
}
|
|
3462
|
-
/**
|
|
3463
|
-
* Handle the authentication logic (check session or exchange token)
|
|
3464
|
-
*/
|
|
3465
|
-
async handleAuthentication() {
|
|
3466
|
-
if (!this.supabaseClient) {
|
|
3467
|
-
throw new Error("Supabase client not initialized");
|
|
3468
|
-
}
|
|
3469
|
-
if (this.processedJwt === this.config.externalJwt) {
|
|
3470
|
-
return;
|
|
3471
|
-
}
|
|
3472
|
-
try {
|
|
3473
|
-
const { data, error } = await this.supabaseClient.getSession();
|
|
3474
|
-
if (error) {
|
|
3475
|
-
console.error("Error getting session:", error);
|
|
3476
|
-
this.processedJwt = this.config.externalJwt || "";
|
|
3477
|
-
return;
|
|
3478
|
-
}
|
|
3479
|
-
const isCheckValid = await this.compareCheckKey(
|
|
3480
|
-
this.config.externalJwt ?? "",
|
|
3481
|
-
this.config.apiKey
|
|
3482
|
-
);
|
|
3483
|
-
if (data.session && this.config.externalJwt && isCheckValid) {
|
|
3484
|
-
console.debug("Session found, authentication ready");
|
|
3485
|
-
this.processedJwt = this.config.externalJwt || "";
|
|
3486
|
-
} else if (this.config.externalJwt) {
|
|
3487
|
-
console.debug("No session found, exchanging external JWT");
|
|
3488
|
-
await this.clearCheckKey();
|
|
3489
|
-
await this.exchangeExternalToken();
|
|
3490
|
-
this.processedJwt = this.config.externalJwt;
|
|
3491
|
-
} else {
|
|
3492
|
-
console.debug("No session and no external JWT provided");
|
|
3493
|
-
this.processedJwt = this.config.externalJwt || "";
|
|
3494
|
-
}
|
|
3495
|
-
} catch (error) {
|
|
3496
|
-
console.error("Unhandled error during authentication:", error);
|
|
3497
|
-
this.processedJwt = this.config.externalJwt || "";
|
|
3498
|
-
throw error;
|
|
3499
|
-
}
|
|
3500
|
-
}
|
|
3501
|
-
/**
|
|
3502
|
-
* Exchange external JWT for Supabase tokens
|
|
3503
|
-
*/
|
|
3504
|
-
async exchangeExternalToken() {
|
|
3505
|
-
if (!this.noAuthApiClient || !this.supabaseClient || !this.config.externalJwt) {
|
|
3506
|
-
throw new Error(
|
|
3507
|
-
"Required clients or JWT not available for token exchange"
|
|
3508
|
-
);
|
|
3509
|
-
}
|
|
3510
|
-
const { data } = await this.noAuthApiClient.POST(
|
|
3511
|
-
ExchangeExternalToken.path,
|
|
3512
|
-
{
|
|
3513
|
-
body: {
|
|
3514
|
-
apiKey: this.config.apiKey,
|
|
3515
|
-
externalJWT: this.config.externalJwt
|
|
3516
|
-
},
|
|
3517
|
-
headers: this.config.additionalHeaders || {}
|
|
3518
|
-
}
|
|
3519
|
-
);
|
|
3520
|
-
if (!data) {
|
|
3521
|
-
throw new Error("Failed to exchange tokens");
|
|
3522
|
-
}
|
|
3523
|
-
await this.supabaseClient.setSession({
|
|
3524
|
-
access_token: data.accessToken,
|
|
3525
|
-
refresh_token: data.refreshToken
|
|
3526
|
-
});
|
|
3527
|
-
console.debug("Token exchange successful, session set");
|
|
3528
|
-
}
|
|
3529
3670
|
createSupabaseHeaderMiddleware() {
|
|
3530
3671
|
return {
|
|
3531
3672
|
onRequest: async ({ request }) => {
|
|
3532
3673
|
const headers = await this.getHeaders();
|
|
3533
|
-
if (!this.supabaseClient) {
|
|
3534
|
-
return request;
|
|
3535
|
-
}
|
|
3536
|
-
const session = await this.supabaseClient.getSession();
|
|
3537
|
-
if (!session.data.session) {
|
|
3538
|
-
return request;
|
|
3539
|
-
}
|
|
3540
3674
|
for (const [key, value] of Object.entries(headers)) {
|
|
3541
3675
|
request.headers.set(key, value);
|
|
3542
3676
|
}
|
|
@@ -3544,33 +3678,8 @@ var MageMetricsClient = class {
|
|
|
3544
3678
|
}
|
|
3545
3679
|
};
|
|
3546
3680
|
}
|
|
3547
|
-
setState(state) {
|
|
3548
|
-
this.authState = state;
|
|
3549
|
-
this.events.dispatch("authStateChange", state);
|
|
3550
|
-
}
|
|
3551
|
-
encodeCheckKey(externalJwt, apiKey) {
|
|
3552
|
-
return hashString(
|
|
3553
|
-
JSON.stringify({
|
|
3554
|
-
externalJwt,
|
|
3555
|
-
apiKey
|
|
3556
|
-
})
|
|
3557
|
-
);
|
|
3558
|
-
}
|
|
3559
|
-
async saveCheckKey(externalJwt, apiKey) {
|
|
3560
|
-
const encodedKey = this.encodeCheckKey(externalJwt, apiKey);
|
|
3561
|
-
await this.storageAdapter.setItem(CHECK_KEY, encodedKey);
|
|
3562
|
-
}
|
|
3563
|
-
async compareCheckKey(externalJwt, apiKey) {
|
|
3564
|
-
const storedKey = await this.storageAdapter.getItem(CHECK_KEY);
|
|
3565
|
-
if (!storedKey) return false;
|
|
3566
|
-
const newValue = this.encodeCheckKey(externalJwt, apiKey);
|
|
3567
|
-
return storedKey === newValue;
|
|
3568
|
-
}
|
|
3569
|
-
async clearCheckKey() {
|
|
3570
|
-
await this.storageAdapter.removeItem(CHECK_KEY);
|
|
3571
|
-
}
|
|
3572
3681
|
};
|
|
3573
3682
|
|
|
3574
|
-
export { BrowserStorageAdapter, CHECK_KEY, MageMetricsClient, MageMetricsEventEmitter, MemoryStorageAdapter, TOKEN_STORAGE_KEY, getPublicApiClient };
|
|
3683
|
+
export { BrowserStorageAdapter, CHECK_KEY, DirectAuthProvider, ExternalAuthProvider, MageMetricsClient, MageMetricsEventEmitter, MemoryStorageAdapter, TOKEN_STORAGE_KEY, getPublicApiClient };
|
|
3575
3684
|
//# sourceMappingURL=index.js.map
|
|
3576
3685
|
//# sourceMappingURL=index.js.map
|