import {z} from 'zod' export type GetApiApiV2PoliciesInboundArgs = {inbound: string, user_id?: string | undefined}; export type GetApiApiV2PoliciesInboundOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGatewayPolicyTarget = z.object({id: z.number().int().gte(1), model: z.string().optional(), priority: z.number().int(), provider: z.string().nullable().optional()}); export const aiGatewayPolicyTier = z.object({targets: z.array(aiGatewayPolicyTarget)}); export const aiGatewayPolicyAttributes = z.object({priority: z.number().int(), tiers: z.array(aiGatewayPolicyTier)}); export const aiGatewayPolicyType = z.literal("routing-documents"); export const aiGatewayPolicyResource = z.object({attributes: aiGatewayPolicyAttributes, id: z.string(), type: aiGatewayPolicyType}); export const aiGatewayPolicyResponse = z.object({data: aiGatewayPolicyResource}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get an AI Gateway routing policy * * GET /api/v2/policies/{inbound} * * Also accepts auth: AuthZ */ export const getApiApiV2PoliciesInbound = async (args: GetApiApiV2PoliciesInboundArgs, options?: GetApiApiV2PoliciesInboundOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/policies/${encodeURIComponent(String(args.inbound))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(aiGatewayPolicyResponse, await res.json()) };