import {z} from 'zod' export type GetApiApiV1EmailsContactsArgs = {cursor?: string | undefined, limit?: number | undefined, account_id: string}; export type GetApiApiV1EmailsContactsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1EmailsContactsResponse = z.object({object: z.literal("EmailContactList"), items: z.array(z.object({object: z.literal("contact"), id: z.string(), account_id: z.string(), source: z.string().optional(), given_name: z.string().optional(), middle_name: z.string().optional(), surname: z.string().optional(), nickname: z.string().optional(), suffix: z.string().optional(), birthday: z.string().optional(), company_name: z.string().optional(), job_title: z.string().optional(), manager_name: z.string().optional(), notes: z.string().optional(), office_location: z.string().optional(), picture_url: z.string().optional(), emails: z.array(z.object({email: z.string(), type: z.string().optional()})), phone_numbers: z.array(z.object({number: z.string(), type: z.string().optional()})), im_addresses: z.array(z.object({im_address: z.string(), type: z.string().optional()})), physical_addresses: z.array(z.object({city: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional(), state: z.string().optional(), street_address: z.string().optional(), type: z.string().optional()})), web_pages: z.array(z.object({url: z.string(), type: z.string().optional()})), groups: z.array(z.object({id: z.string()}))})), cursor: z.union([z.unknown(), z.unknown()])}); 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 }; /** * List email contacts * * GET /api/v1/emails/contacts */ export const getApiApiV1EmailsContacts = async (args: GetApiApiV1EmailsContactsArgs, options?: GetApiApiV1EmailsContactsOptions) => { const {baseUrl = "https://api1.unipile.com:13111", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.account_id != null) query.append("account_id", String(args.account_id)) if (AccessToken !== undefined) headers.set("X-API-KEY", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/emails/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1EmailsContactsResponse, await res.json()) };