import {z} from 'zod' export type Emails = string; export type Key = string; export type Url = string; export type Method = 'POST' | 'PUT' | 'PATCH'; export type XMYHEADER = string; export type Headers = {timestamp?: XMYHEADER | undefined}; export type IndexRateAlertWebhookConfigResponse = {url?: Url | undefined, method?: Method | undefined, headers?: Headers | undefined, bodyTemplate?: string | undefined}; export type IndexRateAlertConfigResponse = {max_lines?: number | undefined, max_z_score?: number | undefined, threshold_alert?: ('separate' | 'both') | undefined, frequency?: ('hourly' | 'daily') | undefined, channels?: ({email?: Array | undefined, pagerduty?: Array | undefined, slack?: Array | undefined, webhook?: Array | undefined}) | undefined, enabled?: boolean | undefined}; export type UpdateApiV1ConfigIndexRateArgs = {body: IndexRateAlertConfigResponse}; export type UpdateApiV1ConfigIndexRateOptions = {baseUrl?: string | undefined, PipelineServiceKey?: string | undefined, fetch?: typeof fetch | undefined}; export const emails = z.string(); export const key = z.string(); export const url = z.string(); export const method = z.enum(["POST", "PUT", "PATCH"]); export const xMYHEADER = z.string(); export const headers = z.object({timestamp: xMYHEADER.optional()}); export const indexRateAlertWebhookConfigResponse = z.object({url: url.optional(), method: method.optional(), headers: headers.optional(), bodyTemplate: z.string().optional()}); export const indexRateAlertConfigResponse = z.object({max_lines: z.number().int().optional(), max_z_score: z.number().int().optional(), threshold_alert: z.enum(["separate", "both"]).optional(), frequency: z.enum(["hourly", "daily"]).optional(), channels: z.object({email: z.array(emails).optional(), pagerduty: z.array(key).optional(), slack: z.array(url).optional(), webhook: z.array(indexRateAlertWebhookConfigResponse).optional()}).optional(), enabled: z.boolean().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 }; /** * Update Index Rate Alert * * PUT /v1/config/index-rate */ export const updateApiV1ConfigIndexRate = async (args: UpdateApiV1ConfigIndexRateArgs, options?: UpdateApiV1ConfigIndexRateOptions) => { const {baseUrl = "https://api.mezmo.com/v3", fetch: fetchFn = fetch, PipelineServiceKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (PipelineServiceKey !== undefined) headers.set("Authorization", PipelineServiceKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/config/index-rate${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(indexRateAlertConfigResponse, await res.json()) };