import {z} from 'zod' export type GetApiApiV1ConfigLogAllocationConfigOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1LogAllocationConfigAllocation = z.object({percent_of_license: z.number().optional()}); export const configv1LogSearchFilter = z.object({query: z.string().optional()}); export const logAllocationConfigHighLowPriorities = z.object({high_priority_filters: z.array(configv1LogSearchFilter).optional(), low_priority_filters: z.array(configv1LogSearchFilter).optional()}); export const logAllocationConfigDefaultDataset = z.object({allocation: configv1LogAllocationConfigAllocation.optional(), priorities: logAllocationConfigHighLowPriorities.optional()}); export const logAllocationConfigDatasetAllocation = z.object({dataset_slug: z.string().optional(), allocation: configv1LogAllocationConfigAllocation.optional(), priorities: logAllocationConfigHighLowPriorities.optional()}); export const configv1LogAllocationConfig = z.object({created_at: z.string().optional(), updated_at: z.string().optional(), default_dataset: logAllocationConfigDefaultDataset.optional(), dataset_allocations: z.array(logAllocationConfigDatasetAllocation).optional()}); export const configv1ReadLogAllocationConfigResponse = z.object({log_allocation_config: configv1LogAllocationConfig.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/log-allocation-config */ export const getApiApiV1ConfigLogAllocationConfig = async (options: GetApiApiV1ConfigLogAllocationConfigOptions) => { 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/log-allocation-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(configv1ReadLogAllocationConfigResponse, await res.json()) };