|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { WorkspaceClient } from "@databricks/sdk-experimental"; |
| 6 | +import { z } from "zod"; |
6 | 7 |
|
7 | | -export interface Destination { |
8 | | - name?: string; |
9 | | - type?: string; |
10 | | -} |
| 8 | +const DestinationSchema = z.object({ |
| 9 | + name: z.string().optional(), |
| 10 | + type: z.string().optional(), |
| 11 | +}); |
11 | 12 |
|
12 | | -export interface Endpoint { |
13 | | - name?: string; |
14 | | - ai_gateway_url?: string; |
15 | | - config?: { destinations?: Destination[] }; |
16 | | -} |
| 13 | +const EndpointSchema = z.object({ |
| 14 | + name: z.string().optional(), |
| 15 | + ai_gateway_url: z.string().optional(), |
| 16 | + config: z |
| 17 | + .object({ |
| 18 | + destinations: z.array(DestinationSchema).optional(), |
| 19 | + }) |
| 20 | + .optional(), |
| 21 | +}); |
17 | 22 |
|
18 | | -export interface EndpointsResponse { |
19 | | - endpoints?: Endpoint[]; |
20 | | -} |
| 23 | +const EndpointsResponseSchema = z.object({ |
| 24 | + endpoints: z.array(EndpointSchema).optional(), |
| 25 | +}); |
| 26 | + |
| 27 | +export type Destination = z.infer<typeof DestinationSchema>; |
| 28 | +export type Endpoint = z.infer<typeof EndpointSchema>; |
| 29 | +export type EndpointsResponse = z.infer<typeof EndpointsResponseSchema>; |
21 | 30 |
|
22 | 31 | export interface FilteredGatewayRoute { |
23 | 32 | gateway_name: string; |
@@ -57,17 +66,24 @@ export function filterEndpoints(endpoints: Endpoint[]): FilteredGatewayRoute[] { |
57 | 66 | export async function fetchFilteredGatewayRoutes( |
58 | 67 | client: WorkspaceClient, |
59 | 68 | ): Promise<FilteredGatewayRoute[]> { |
60 | | - const raw = (await client.apiClient.request( |
| 69 | + const raw = await client.apiClient.request( |
61 | 70 | { |
62 | 71 | path: "/api/ai-gateway/v2/endpoints", |
63 | 72 | method: "GET", |
64 | 73 | headers: new Headers(), |
65 | 74 | raw: false, |
66 | 75 | }, |
67 | 76 | undefined, |
68 | | - )) as EndpointsResponse; |
| 77 | + ); |
| 78 | + |
| 79 | + const parsed = EndpointsResponseSchema.safeParse(raw); |
| 80 | + if (!parsed.success) { |
| 81 | + throw new Error( |
| 82 | + `Unexpected response shape from /api/ai-gateway/v2/endpoints: ${parsed.error.message}`, |
| 83 | + ); |
| 84 | + } |
69 | 85 |
|
70 | | - return filterEndpoints(raw.endpoints ?? []); |
| 86 | + return filterEndpoints(parsed.data.endpoints ?? []); |
71 | 87 | } |
72 | 88 |
|
73 | 89 | /** OpenAI-compatible base URL for chat/embeddings (no trailing slash). */ |
|
0 commit comments