import {z} from 'zod' export type Individual = {first_name?: string | undefined, last_name?: string | undefined}; export type Company = {company_name?: string | undefined}; export type Address = {addressLine1: string, addressLine2?: string | undefined, city: string, zipCode: string, state?: string | undefined, country: string}; export type ServiceRateType = 'Hourly' | 'Daily' | 'Weekly' | 'Project'; export type ProductRateType = 'Product' | 'Part' | 'Item'; export type CustomRate = {rate_type: ServiceRateType | ProductRateType, price: number}; export type ContactDesignation = 'Client' | 'Prospect' | 'Supplier'; export type Contact = {createdByUserId?: string | undefined, name: Individual | Company, displayName?: string | undefined, isBusiness?: boolean | undefined, companyContacts?: Array | undefined, phone?: string | undefined, email: string, address?: Address | undefined, customRate?: Array | undefined, notes?: string | undefined, designation?: ContactDesignation | undefined, tags?: Array | undefined, metadata?: Record | undefined, active?: boolean | undefined}; export type CreateApiContactsArgs = {'x-business-id': string, body: Contact}; export type CreateApiContactsOptions = {baseUrl: string, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const contactID = z.object({contactId: 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 Contact * * POST /contacts * * Also accepts auth: APIKeyHeader, AccessToken */ export const createApiContacts = async (args: CreateApiContactsArgs, options: CreateApiContactsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-business-id'] != null) headers.set("x-business-id", String(args['x-business-id'])) if (APIKey !== undefined) headers.set("api-key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/contacts${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(contactID, await res.json()) };