import {z} from 'zod' export type MultiplierAllocation = {bid_area_id: string, repeat_count: number}; export type MultiplierCreateForRevision = {scope: 'condition' | 'page' | 'measurement', condition_id?: (string | null) | undefined, page_id?: (string | null) | undefined, measurement_id?: (string | null) | undefined, x?: (number | string | null) | undefined, y?: (number | string | null) | undefined, width?: (number | string | null) | undefined, height?: (number | string | null) | undefined, factor: number | string, label?: (string | null) | undefined, allocations?: Array | undefined, revision_set_id: string | null}; export type CreateApiApiV1ProjectsProjectIdMultipliersArgs = {project_id: string, body: MultiplierCreateForRevision}; export type CreateApiApiV1ProjectsProjectIdMultipliersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const multiplierAllocation = z.object({bid_area_id: z.string(), repeat_count: z.number().int().gte(1)}); export const multiplier = z.object({scope: z.enum(["condition", "page", "zone", "measurement"]), condition_id: z.string().nullable().optional(), page_id: z.string().nullable().optional(), measurement_id: z.string().nullable().optional(), x: z.string().nullable().optional(), y: z.string().nullable().optional(), width: z.string().nullable().optional(), height: z.string().nullable().optional(), factor: z.string(), label: z.string().nullable().optional(), allocations: z.array(multiplierAllocation).optional(), id: z.string(), project_id: z.string(), source: z.enum(["manual", "ai"]), status: z.enum(["proposed", "confirmed", "dismissed"]), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Create Multiplier * * POST /api/v1/projects/{project_id}/multipliers */ export const createApiApiV1ProjectsProjectIdMultipliers = async (args: CreateApiApiV1ProjectsProjectIdMultipliersArgs, options: CreateApiApiV1ProjectsProjectIdMultipliersOptions) => { 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/projects/${encodeURIComponent(String(args.project_id))}/multipliers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(multiplier, await res.json()) };