|
| 1 | +/** |
| 2 | + * Endpoints utility for accessing backend API routes. |
| 3 | + * |
| 4 | + * Provides a clean way to build API URLs with parameter substitution, |
| 5 | + * reading from the runtime config injected by the server. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { AnalyticsEndpointParams } from "shared"; |
| 9 | + |
| 10 | +// Re-export for consumers |
| 11 | +export type { AnalyticsEndpointParams } from "shared"; |
| 12 | + |
| 13 | +/** Map of endpoint names to their path templates for a plugin */ |
| 14 | +export type PluginEndpointMap = Record<string, string>; |
| 15 | + |
| 16 | +/** Map of plugin names to their endpoint maps */ |
| 17 | +export type PluginEndpoints = Record<string, PluginEndpointMap>; |
| 18 | + |
| 19 | +export interface RuntimeConfig { |
| 20 | + appName: string; |
| 21 | + queries: Record<string, string>; |
| 22 | + endpoints: PluginEndpoints; |
| 23 | +} |
| 24 | + |
| 25 | +declare global { |
| 26 | + interface Window { |
| 27 | + __CONFIG__?: RuntimeConfig; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Get the runtime config from the window object. |
| 33 | + */ |
| 34 | +export function getConfig(): RuntimeConfig { |
| 35 | + if (!window.__CONFIG__) { |
| 36 | + throw new Error( |
| 37 | + "Runtime config not found. Make sure the server is injecting __CONFIG__.", |
| 38 | + ); |
| 39 | + } |
| 40 | + return window.__CONFIG__; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Substitute path parameters in a URL template. |
| 45 | + * |
| 46 | + * @param template - URL template with :param placeholders |
| 47 | + * @param params - Parameters to substitute |
| 48 | + * @returns The resolved URL |
| 49 | + */ |
| 50 | +function substituteParams( |
| 51 | + template: string, |
| 52 | + params: Record<string, string | number> = {}, |
| 53 | +): string { |
| 54 | + let resolved = template; |
| 55 | + for (const [key, value] of Object.entries(params)) { |
| 56 | + resolved = resolved.replace(`:${key}`, encodeURIComponent(String(value))); |
| 57 | + } |
| 58 | + return resolved; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Append query parameters to a URL. |
| 63 | + */ |
| 64 | +function appendQueryParams( |
| 65 | + url: string, |
| 66 | + queryParams: Record<string, string | number | boolean> = {}, |
| 67 | +): string { |
| 68 | + if (Object.keys(queryParams).length === 0) return url; |
| 69 | + |
| 70 | + const searchParams = new URLSearchParams(); |
| 71 | + for (const [key, value] of Object.entries(queryParams)) { |
| 72 | + searchParams.set(key, String(value)); |
| 73 | + } |
| 74 | + return `${url}?${searchParams.toString()}`; |
| 75 | +} |
| 76 | + |
| 77 | +type UrlParams = Record<string, string | number>; |
| 78 | +type QueryParams = Record<string, string | number | boolean>; |
| 79 | + |
| 80 | +/** |
| 81 | + * Create a plugin API that reads endpoints from runtime config. |
| 82 | + * |
| 83 | + * @param pluginName - Plugin name to look up in config |
| 84 | + * @returns Proxy object with endpoint methods |
| 85 | + * |
| 86 | + * @example |
| 87 | + * ```typescript |
| 88 | + * const analytics = createPluginApi("analytics"); |
| 89 | + * |
| 90 | + * // Access named endpoint |
| 91 | + * analytics.query({ query_key: "spend_data" }) |
| 92 | + * // → "/api/analytics/query/spend_data" |
| 93 | + * |
| 94 | + * // With query params |
| 95 | + * analytics.query({ query_key: "test" }, { dev: "tunnel-123" }) |
| 96 | + * // → "/api/analytics/query/test?dev=tunnel-123" |
| 97 | + * ``` |
| 98 | + */ |
| 99 | +export function createPluginApi(pluginName: string) { |
| 100 | + return new Proxy( |
| 101 | + {}, |
| 102 | + { |
| 103 | + get(_target, endpointName: string) { |
| 104 | + return (params: UrlParams = {}, queryParams: QueryParams = {}) => { |
| 105 | + const config = getConfig(); |
| 106 | + const pluginEndpoints = config.endpoints[pluginName]; |
| 107 | + |
| 108 | + if (!pluginEndpoints) { |
| 109 | + throw new Error( |
| 110 | + `Plugin "${pluginName}" not found in endpoints config`, |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + const template = pluginEndpoints[endpointName]; |
| 115 | + if (!template) { |
| 116 | + throw new Error( |
| 117 | + `Endpoint "${endpointName}" not found for plugin "${pluginName}"`, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const url = substituteParams(template, params); |
| 122 | + return appendQueryParams(url, queryParams); |
| 123 | + }; |
| 124 | + }, |
| 125 | + }, |
| 126 | + ) as Record< |
| 127 | + string, |
| 128 | + (params?: UrlParams, queryParams?: QueryParams) => string |
| 129 | + >; |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Build a URL directly from a path template. |
| 134 | + * |
| 135 | + * @example |
| 136 | + * ```typescript |
| 137 | + * buildUrl("/api/analytics/query/:query_key", { query_key: "spend_data" }) |
| 138 | + * // → "/api/analytics/query/spend_data" |
| 139 | + * ``` |
| 140 | + */ |
| 141 | +export function buildUrl( |
| 142 | + template: string, |
| 143 | + params: UrlParams = {}, |
| 144 | + queryParams: QueryParams = {}, |
| 145 | +): string { |
| 146 | + const url = substituteParams(template, params); |
| 147 | + return appendQueryParams(url, queryParams); |
| 148 | +} |
| 149 | + |
| 150 | +/** Base endpoint function type */ |
| 151 | +export type EndpointFn<TParams = UrlParams> = ( |
| 152 | + params?: TParams, |
| 153 | + queryParams?: QueryParams, |
| 154 | +) => string; |
| 155 | + |
| 156 | +/** Default plugin API shape (all endpoints accept any params) */ |
| 157 | +export type DefaultPluginApi = Record<string, EndpointFn>; |
| 158 | + |
| 159 | +/** |
| 160 | + * Augmentable interface for typed plugin APIs. |
| 161 | + * |
| 162 | + * Apps can extend this interface to get type-safe endpoint access. |
| 163 | + * |
| 164 | + * @example |
| 165 | + * ```typescript |
| 166 | + * // In your app's appKitTypes.d.ts: |
| 167 | + * declare module '@databricks/app-kit-ui' { |
| 168 | + * interface AppKitPlugins { |
| 169 | + * analytics: { |
| 170 | + * query: EndpointFn<{ query_key: string }>; |
| 171 | + * arrowResult: EndpointFn<{ jobId: string }>; |
| 172 | + * }; |
| 173 | + * reconnect: { |
| 174 | + * status: EndpointFn; |
| 175 | + * stream: EndpointFn; |
| 176 | + * }; |
| 177 | + * } |
| 178 | + * } |
| 179 | + * ``` |
| 180 | + */ |
| 181 | +// biome-ignore lint/suspicious/noEmptyInterface: Designed for module augmentation |
| 182 | +export interface AppKitPlugins {} |
| 183 | + |
| 184 | +/** Resolved API type - uses augmented types if available, otherwise defaults */ |
| 185 | +type ApiType = AppKitPlugins & Record<string, DefaultPluginApi>; |
| 186 | + |
| 187 | +/** |
| 188 | + * Dynamic API helper that reads plugins from runtime config. |
| 189 | + * |
| 190 | + * Automatically synced with the plugins registered on the server. |
| 191 | + * Access any plugin's named endpoints directly. |
| 192 | + * |
| 193 | + * For type safety, augment the `AppKitPlugins` interface in your app. |
| 194 | + * |
| 195 | + * @example |
| 196 | + * ```typescript |
| 197 | + * // Access any plugin's endpoints (auto-discovered from server config) |
| 198 | + * api.analytics.query({ query_key: "spend_data" }) |
| 199 | + * // → "/api/analytics/query/spend_data" |
| 200 | + * |
| 201 | + * api.analytics.arrowResult({ jobId: "abc123" }) |
| 202 | + * // → "/api/analytics/arrow-result/abc123" |
| 203 | + * |
| 204 | + * api.reconnect.stream() |
| 205 | + * // → "/api/reconnect/stream" |
| 206 | + * |
| 207 | + * // Works with any plugin registered on the server |
| 208 | + * api.myCustomPlugin.myEndpoint({ id: "123" }) |
| 209 | + * ``` |
| 210 | + */ |
| 211 | +export const api: ApiType = new Proxy({} as ApiType, { |
| 212 | + get(_target, pluginName: string) { |
| 213 | + return createPluginApi(pluginName); |
| 214 | + }, |
| 215 | +}); |
| 216 | + |
| 217 | +// ============================================================================ |
| 218 | +// Pre-typed Plugin APIs for internal package use |
| 219 | +// ============================================================================ |
| 220 | +// These helpers provide type-safe endpoint access within app-kit-ui itself, |
| 221 | +// since the AppKitPlugins augmentation only applies in consuming apps. |
| 222 | +// AnalyticsEndpointParams is imported from shared package (single source of truth). |
| 223 | + |
| 224 | +/** Typed analytics API for internal package use */ |
| 225 | +export interface AnalyticsApiType { |
| 226 | + query: ( |
| 227 | + params: AnalyticsEndpointParams["query"], |
| 228 | + queryParams?: QueryParams, |
| 229 | + ) => string; |
| 230 | + queryAsUser: ( |
| 231 | + params: AnalyticsEndpointParams["queryAsUser"], |
| 232 | + queryParams?: QueryParams, |
| 233 | + ) => string; |
| 234 | + arrowResult: ( |
| 235 | + params: AnalyticsEndpointParams["arrowResult"], |
| 236 | + queryParams?: QueryParams, |
| 237 | + ) => string; |
| 238 | +} |
| 239 | + |
| 240 | +/** |
| 241 | + * Pre-typed analytics API for use within the app-kit-ui package. |
| 242 | + * |
| 243 | + * This provides type-safe access to analytics endpoints without relying |
| 244 | + * on AppKitPlugins augmentation (which only works in consuming apps). |
| 245 | + * |
| 246 | + * @example |
| 247 | + * ```typescript |
| 248 | + * // Type-safe within the package |
| 249 | + * analyticsApi.query({ query_key: "spend_data" }) |
| 250 | + * // → "/api/analytics/query/spend_data" |
| 251 | + * |
| 252 | + * analyticsApi.arrowResult({ jobId: "abc123" }) |
| 253 | + * // → "/api/analytics/arrow-result/abc123" |
| 254 | + * ``` |
| 255 | + */ |
| 256 | +export const analyticsApi: AnalyticsApiType = createPluginApi( |
| 257 | + "analytics", |
| 258 | +) as unknown as AnalyticsApiType; |
0 commit comments