import {z} from 'zod' export type UpdateApiApiOrgIdScoreConfigsArgs = {org_id: string, body: {categories: unknown, dataType: string, description?: (string | null) | undefined, healthyThreshold: unknown, name: string, numericRange: unknown}}; export type UpdateApiApiOrgIdScoreConfigsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const scoreConfigResponseBody = z.object({categories: z.unknown(), createdAt: z.number().int(), dataType: z.string(), description: z.string().nullable().optional(), entityId: z.string(), healthyThreshold: z.unknown(), id: z.string(), isActive: z.boolean(), name: z.string(), numericRange: z.unknown(), orgId: z.string(), updatedAt: z.number().int(), version: z.number().int()}); export const ensureOutcomeBody = z.enum(["created", "unchanged", "version_bumped"]); export const updateApiApiOrgIdScoreConfigsResponse = z.object({config: scoreConfigResponseBody, outcome: ensureOutcomeBody}); 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 }; /** * Ensure a score config exists with these parameters * * PUT /api/{org_id}/score_configs */ export const updateApiApiOrgIdScoreConfigs = async (args: UpdateApiApiOrgIdScoreConfigsArgs, options: UpdateApiApiOrgIdScoreConfigsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/score_configs${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(updateApiApiOrgIdScoreConfigsResponse, await res.json()) };