import {z} from 'zod' export type BatchContactInput = {created?: string | undefined, picture?: ({id?: string | undefined}) | undefined, address?: string | undefined, city?: string | undefined, comments?: string | undefined, contact_type_id?: string | undefined, country?: string | undefined, currency_exchange_rates_list_id?: string | undefined, email1?: string | undefined, email2?: string | undefined, email3?: string | undefined, email4?: string | undefined, email5?: string | undefined, first_name?: string | undefined, gender?: (0 | 1 | 2 | 3) | undefined, is_archived?: boolean | undefined, last_name?: string | undefined, middle_name?: string | undefined, owner_id?: string | undefined, phone1?: string | undefined, phone2?: string | undefined, phone3?: string | undefined, phone4?: string | undefined, phone5?: string | undefined, picture_id?: string | undefined, position?: string | undefined, quick_account_name?: string | undefined, share_mode?: (0 | 1 | 2 | 3) | undefined, state_province?: string | undefined, title?: string | undefined, unit_id?: string | undefined, zip_code?: string | undefined, revision?: number | undefined, social_media?: ({id?: string | undefined}) | undefined, account_relations?: (Array<{id?: string | undefined}>) | undefined, sharing_units?: (Array<{id?: string | undefined}>) | undefined, sharing_clients?: (Array<{id?: string | undefined}>) | undefined, documents?: (Array<{id?: string | undefined}>) | undefined, exchange_entity_id?: string | undefined, gapi_entity_id?: string | undefined, o365_entity_id?: string | undefined, primary_account_account_roles?: (Array<{id?: string | undefined}>) | undefined, account_position?: string | undefined, is_unsubscribed?: boolean | undefined, tags?: (Array<{id?: string | undefined}>) | undefined, static_profiles?: (Array<{id?: string | undefined}>) | undefined, primary_account_relationship?: string | undefined, id?: string | undefined}; export type CreateApiEntitiesContactsBatchModifyArgs = {'validation-level'?: number | undefined, 'rollback-method'?: (0 | 1) | undefined, body: Array}; export type CreateApiEntitiesContactsBatchModifyOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const error = z.object({http_status: z.number().int(), code: z.number().int().optional(), name: z.string().optional(), message: z.string().optional(), success: z.boolean()}); export const createApiEntitiesContactsBatchModifyResponse = z.object({success: z.boolean().optional(), data: z.array(z.union([z.string(), error])).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 or update contacts in a batch * * POST /entities/Contacts/batch-modify */ export const createApiEntitiesContactsBatchModify = async (args: CreateApiEntitiesContactsBatchModifyArgs, options?: CreateApiEntitiesContactsBatchModifyOptions) => { const {baseUrl = "https://us-east.api.coevera.com/api/v100/rest/spaces/your_space_id", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['validation-level'] != null) query.append("validation-level", String(args['validation-level'])) if (args['rollback-method'] != null) query.append("rollback-method", String(args['rollback-method'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/Contacts/batch-modify${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(createApiEntitiesContactsBatchModifyResponse, await res.json()) };