import {z} from 'zod' export type ApiFieldValue = {type: 'string', value: string} | {type: 'url', value: string} | {type: 'int', value: number} | {type: 'number', value: number} | {type: 'date', value: string} | {type: 'boolean', value: boolean}; export type CreateApiObjectsIdCustomFieldsArgs = {id: string, body: {customFields: Array<{customFieldId: string, customFieldValue: ApiFieldValue}>}}; export type CreateApiObjectsIdCustomFieldsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiFieldValue = z.union([z.object({type: z.literal("string"), value: z.string()}), z.object({type: z.literal("url"), value: z.string()}), z.object({type: z.literal("int"), value: z.number().int()}), z.object({type: z.literal("number"), value: z.number()}), z.object({type: z.literal("date"), value: z.string()}), z.object({type: z.literal("boolean"), value: z.boolean()})]); export const apiCustomObject = z.object({id: z.string(), name: z.string(), url: z.string().optional(), recordKey: z.string().optional(), tags: z.array(z.string()).optional(), customFields: z.record(z.string(), apiFieldValue).optional()}); export const customObjectResponse = z.object({success: z.literal(true), data: apiCustomObject}); 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 }; /** * Update custom fields on a custom object * * POST /objects/{id}/customFields */ export const createApiObjectsIdCustomFields = async (args: CreateApiObjectsIdCustomFieldsArgs, options?: CreateApiObjectsIdCustomFieldsOptions) => { const {baseUrl = "https://api.commonroom.io/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/objects/${encodeURIComponent(String(args.id))}/customFields${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(customObjectResponse, await res.json()) };