| 1 | // Single-fetch cache for the deployment-wide Shuffle connector creds |
| 2 | // (URL + admin API key) used by the ShuffleMCP / TryMcp React embeds. |
| 3 | // |
| 4 | // The embed wrappers (`ShuffleMCPEmbed.vue`, `TryMcpEmbed.vue`, |
| 5 | // `AppDetailDrawerEmbed.vue`) call |
| 6 | // `fetchShuffleConnectorCredentials()` on mount. The first call hits the |
| 7 | // backend; subsequent calls re-use the in-flight or resolved promise so |
| 8 | // every embed in the session shares one round-trip. On error we cache |
| 9 | // `null` and don't retry — callers stay on whatever explicit overrides |
| 10 | // they were already passing. |
| 11 | // |
| 12 | // On first successful resolve, `setShuffleApiBaseUrl` patches the package |
| 13 | // global `API_CONFIG.baseUrl` so embeds that ignore props see the real |
| 14 | // connector URL from the DB. |
| 15 | |
| 16 | import type { ShuffleConnectorCredentials } from "@/api/endpoints/shuffle" |
| 17 | import { API_CONFIG } from "@shuffleio/shuffle-mcps" |
| 18 | import Api from "@/api" |
| 19 | |
| 20 | let currentBaseUrl: string | null = null |
| 21 | let installed = false |
| 22 | let cached: Promise<ShuffleConnectorCredentials | null> | null = null |
| 23 | |
| 24 | function setShuffleApiBaseUrl(url: string): void { |
| 25 | currentBaseUrl = url |
| 26 | if (installed) return |
| 27 | installed = true |
| 28 | try { |
| 29 | Object.defineProperty(API_CONFIG, "baseUrl", { |
| 30 | get: () => currentBaseUrl ?? "", |
| 31 | configurable: true |
| 32 | }) |
| 33 | } catch (err) { |
| 34 | console.warn("[shuffle] could not override API_CONFIG.baseUrl:", err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | export function fetchShuffleConnectorCredentials(): Promise<ShuffleConnectorCredentials | null> { |
| 39 | if (cached) return cached |
| 40 | cached = Api.shuffle |
| 41 | .getConnectorCredentials() |
| 42 | .then(res => { |
| 43 | if (!res.data.success) return null |
| 44 | const creds: ShuffleConnectorCredentials = { |
| 45 | base_url: res.data.base_url, |
| 46 | api_key: res.data.api_key |
| 47 | } |
| 48 | setShuffleApiBaseUrl(creds.base_url) |
| 49 | return creds |
| 50 | }) |
| 51 | .catch(() => null) |
| 52 | return cached |
| 53 | } |
| 54 | |
| 55 | /** Test-only — drops the cached result so the next call re-fetches. */ |
| 56 | export function resetShuffleConnectorCredentialsCache(): void { |
| 57 | cached = null |
| 58 | } |