import {z} from 'zod' export type DemographicDimensionCreate = {name: string, display_name: string, protected_class?: boolean | undefined, description?: (string | null) | undefined}; export type CreateApiV1DimensionsArgs = {body: DemographicDimensionCreate}; export type CreateApiV1DimensionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const demographicValue = z.object({id: z.string(), dimension_id: z.string(), value: z.string(), display_name: z.string().nullable().optional(), value_metadata: z.record(z.string(), z.unknown()).nullable().optional(), sort_order: z.number().int().optional(), created_at: z.number().int()}); export const demographicDimension = z.object({id: z.string(), name: z.string(), display_name: z.string(), protected_class: z.boolean().optional(), description: z.string().nullable().optional(), created_at: z.number().int(), values: z.array(demographicValue).nullable().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 }; /** * Create Dimension * * POST /v1/dimensions/ */ export const createApiV1Dimensions = async (args: CreateApiV1DimensionsArgs, options: CreateApiV1DimensionsOptions) => { 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(/\/+$/, '')}/v1/dimensions/${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(demographicDimension, await res.json()) };