import {z} from 'zod' export type GetApiContactsArgs = {company_id?: string | undefined, active?: boolean | undefined, name?: string | undefined, search?: string | undefined, with_email?: string | undefined, cportal?: boolean | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, accountId?: string | undefined, authToken?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationStats = z.object({totalCount: z.number().int().optional(), page: z.number().int().optional(), perPage: z.number().int().optional(), totalPages: z.number().int().optional(), prevPage: z.number().int().nullable().optional(), nextPage: z.number().int().nullable().optional(), outOfRange: z.boolean().optional()}); export const namedRef = z.object({id: z.string().optional(), name: z.string().optional()}); export const email = z.object({id: z.string().optional(), email: z.string().optional(), emailType: z.string().nullable().optional()}); export const address = z.object({id: z.string().optional(), addressType: z.string().nullable().optional(), name: z.string().nullable().optional(), nameStreet: z.string().nullable().optional(), nameStreetCity: z.string().nullable().optional(), street: z.string().nullable().optional(), street1: z.string().nullable().optional(), suburb: z.string().nullable().optional(), city: z.string().nullable().optional(), zip: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), attentionTo: z.string().nullable().optional(), coordinates: z.object({latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional()}).optional()}); export const contactListItem = z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), phoneWithExt: z.string().nullable().optional(), title: z.string().nullable().optional(), department: z.string().nullable().optional(), company: namedRef.optional(), emails: z.array(email).optional(), addresses: z.array(address).optional()}); export const getApiContactsResponse = z.object({stats: paginationStats.optional(), contacts: z.array(contactListItem).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 }; /** * List contacts * * GET /contacts */ export const getApiContacts = async (args: GetApiContactsArgs, options?: GetApiContactsOptions) => { const {baseUrl = "https://api.shopvox.com/v1", fetch: fetchFn = fetch, accountId, authToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company_id != null) query.append("company_id", String(args.company_id)) if (args.active != null) query.append("active", String(args.active)) if (args.name != null) query.append("name", String(args.name)) if (args.search != null) query.append("search", String(args.search)) if (args.with_email != null) query.append("with_email", String(args.with_email)) if (args.cportal != null) query.append("cportal", String(args.cportal)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (accountId !== undefined) query.set("account_id", accountId) if (authToken !== undefined) query.set("authToken", authToken) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiContactsResponse, await res.json()) };