import {z} from 'zod' export type Configv1DropRuleMode = 'ENABLED' | 'DISABLED'; export type Configv1LabelFilter = {name?: string | undefined, value_glob?: string | undefined}; export type DropRuleConditionalRateBasedDrop = {enabled?: boolean | undefined, rate_limit_threshold?: number | undefined, activated_drop_duration_secs?: number | undefined}; export type DropRuleValueBasedDrop = {enabled?: boolean | undefined, target_drop_value?: number | undefined}; export type Configv1DropRule = {slug?: string | undefined, name?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, mode?: Configv1DropRuleMode | undefined, filters?: Array | undefined, conditional_rate_based_drop?: DropRuleConditionalRateBasedDrop | undefined, value_based_drop?: DropRuleValueBasedDrop | undefined, drop_nan_value?: boolean | undefined}; export type ConfigV1UpdateDropRuleBody = {drop_rule?: Configv1DropRule | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigDropRulesSlugArgs = {slug: string, body: ConfigV1UpdateDropRuleBody}; export type UpdateApiApiV1ConfigDropRulesSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1DropRuleMode = z.enum(["ENABLED", "DISABLED"]); export const configv1LabelFilter = z.object({name: z.string().optional(), value_glob: z.string().optional()}); export const dropRuleConditionalRateBasedDrop = z.object({enabled: z.boolean().optional(), rate_limit_threshold: z.number().optional(), activated_drop_duration_secs: z.number().int().optional()}); export const dropRuleValueBasedDrop = z.object({enabled: z.boolean().optional(), target_drop_value: z.number().optional()}); export const configv1DropRule = z.object({slug: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), mode: configv1DropRuleMode.optional(), filters: z.array(configv1LabelFilter).optional(), conditional_rate_based_drop: dropRuleConditionalRateBasedDrop.optional(), value_based_drop: dropRuleValueBasedDrop.optional(), drop_nan_value: z.boolean().optional()}); export const configv1UpdateDropRuleResponse = z.object({drop_rule: configv1DropRule.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 }; /** * PUT /api/v1/config/drop-rules/{slug} */ export const updateApiApiV1ConfigDropRulesSlug = async (args: UpdateApiApiV1ConfigDropRulesSlugArgs, options: UpdateApiApiV1ConfigDropRulesSlugOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/drop-rules/${encodeURIComponent(String(args.slug))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(configv1UpdateDropRuleResponse, await res.json()) };