import {z} from 'zod' export type GetApiApiV1ConfigTraceBehaviorConfigOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const traceBehaviorConfigMainBehaviorAssignment = z.object({created_at: z.string().optional(), updated_at: z.string().optional(), dataset_slug: z.string().optional(), behavior_slug: z.string().optional(), created_by: z.string().optional(), description: z.string().optional()}); export const traceBehaviorConfigOverrideBehaviorAssignment = z.object({created_at: z.string().optional(), updated_at: z.string().optional(), dataset_slug: z.string().optional(), behavior_slug: z.string().optional(), start_time: z.string().optional(), end_time: z.string().optional(), created_by: z.string().optional(), description: z.string().optional()}); export const configv1TraceBehaviorConfig = z.object({created_at: z.string().optional(), updated_at: z.string().optional(), main_behavior_assignments: z.array(traceBehaviorConfigMainBehaviorAssignment).optional(), override_behavior_assignments: z.array(traceBehaviorConfigOverrideBehaviorAssignment).optional(), dataset_priorities: z.array(z.string()).optional(), baseline_behavior_slug: z.string().optional(), preview_behavior_assignments: z.array(traceBehaviorConfigOverrideBehaviorAssignment).optional()}); export const configv1ReadTraceBehaviorConfigResponse = z.object({trace_behavior_config: configv1TraceBehaviorConfig.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 }; /** * GET /api/v1/config/trace-behavior-config */ export const getApiApiV1ConfigTraceBehaviorConfig = async (options: GetApiApiV1ConfigTraceBehaviorConfigOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/trace-behavior-config${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(configv1ReadTraceBehaviorConfigResponse, await res.json()) };