import {z} from 'zod' export type AiGatewayRateLimitMode = 'ALERT' | 'ENFORCE'; export type AiGatewayRateLimitCreateAttributes = {input_tokens_per_minute?: (number | null) | undefined, mode?: AiGatewayRateLimitMode | undefined, name: string, output_tokens_per_minute?: (number | null) | undefined, requests_per_minute?: (number | null) | undefined, tags?: {[key: string]: string} | undefined, team_id?: string | undefined, user_id?: string | undefined}; export type AiGatewayRateLimitType = 'rate-limits'; export type AiGatewayRateLimitCreateData = {attributes: AiGatewayRateLimitCreateAttributes, type: AiGatewayRateLimitType}; export type AiGatewayRateLimitCreateRequest = {data: AiGatewayRateLimitCreateData}; export type CreateApiApiV2RateLimitsArgs = {body: AiGatewayRateLimitCreateRequest}; export type CreateApiApiV2RateLimitsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGatewayRateLimitMode = z.enum(["ALERT", "ENFORCE"]); export const aiGatewayRateLimitAttributes = z.object({created_at: z.string(), created_by: z.string(), enabled: z.boolean(), input_tokens_per_minute: z.number().int().gte(1).lte(100000000000).nullable().optional(), mode: aiGatewayRateLimitMode, monitor_id: z.number().int().nullable().optional(), name: z.string(), output_tokens_per_minute: z.number().int().gte(1).lte(100000000000).nullable().optional(), requests_per_minute: z.number().int().gte(1).lte(10000000).nullable().optional(), revision: z.number().int(), scope_restricted: z.boolean(), tags: z.record(z.string(), z.string()), team_id: z.string().nullable().optional(), updated_at: z.string(), updated_by: z.string(), user_id: z.string().nullable().optional()}); export const aiGatewayRateLimitType = z.literal("rate-limits"); export const aiGatewayRateLimitResource = z.object({attributes: aiGatewayRateLimitAttributes, id: z.string(), type: aiGatewayRateLimitType}); export const aiGatewayRateLimitResponse = z.object({data: aiGatewayRateLimitResource}); 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 }; /** * Create an AI Gateway rate limit * * POST /api/v2/rate-limits * * Also accepts auth: AuthZ */ export const createApiApiV2RateLimits = async (args: CreateApiApiV2RateLimitsArgs, options?: CreateApiApiV2RateLimitsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/rate-limits${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(aiGatewayRateLimitResponse, await res.json()) };