import {z} from 'zod' export type EndpointRateLimitConfig = {max_tokens: number, refill_rate_per_second: number}; export type EndpointLimitsOverride = {generation?: (EndpointRateLimitConfig | null) | undefined, status?: (EndpointRateLimitConfig | null) | undefined, videoCaptionsSubmit?: (EndpointRateLimitConfig | null) | undefined, metaTextOverlaySubmit?: (EndpointRateLimitConfig | null) | undefined, metaTextOverlayPoll?: (EndpointRateLimitConfig | null) | undefined, v2Create?: (EndpointRateLimitConfig | null) | undefined, v2Status?: (EndpointRateLimitConfig | null) | undefined, catalog?: (EndpointRateLimitConfig | null) | undefined}; export type RateLimitOverrideCreateRequest = {identifier: string, endpointLimits: EndpointLimitsOverride, note?: string | undefined}; export type CreateApiInternalRateLimitsOverridesArgs = {'x-api-key': string, body: RateLimitOverrideCreateRequest}; export type CreateApiInternalRateLimitsOverridesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const endpointRateLimitConfig = z.object({max_tokens: z.number().int(), refill_rate_per_second: z.number()}); export const endpointLimitsOverride = z.object({generation: endpointRateLimitConfig.nullable().optional(), status: endpointRateLimitConfig.nullable().optional(), videoCaptionsSubmit: endpointRateLimitConfig.nullable().optional(), metaTextOverlaySubmit: endpointRateLimitConfig.nullable().optional(), metaTextOverlayPoll: endpointRateLimitConfig.nullable().optional(), v2Create: endpointRateLimitConfig.nullable().optional(), v2Status: endpointRateLimitConfig.nullable().optional(), catalog: endpointRateLimitConfig.nullable().optional()}); export const rateLimitOverrideResponse = z.object({identifier: z.string(), endpointLimits: endpointLimitsOverride, createdAt: z.string(), createdBy: z.string(), updatedAt: z.string().nullable().optional(), updatedBy: z.string().nullable().optional(), note: z.string().optional()}); 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 Rate Limit Override * * POST /internal/rate-limits/overrides */ export const createApiInternalRateLimitsOverrides = async (args: CreateApiInternalRateLimitsOverridesArgs, options: CreateApiInternalRateLimitsOverridesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/internal/rate-limits/overrides${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(rateLimitOverrideResponse, await res.json()) };