import {z} from 'zod' export type GetApiContactsArgs = {TeamId?: string | undefined, CampaignId?: string | undefined, limit?: number | undefined, offset?: number | undefined, search?: string | undefined, interest?: string | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.string().optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), company: z.string().optional(), title: z.string().optional(), linkedin_url: z.string().optional(), phone: z.string().optional(), website: z.string().optional(), interest: z.string().optional(), finished: z.boolean().optional(), deleted: z.boolean().optional(), custom: z.record(z.string(), z.unknown()).optional(), team_id: z.string().optional(), campaign_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total: z.number().int().optional(), has_more: z.boolean().optional()}); export const contactListResponse = z.object({success: z.boolean().optional(), request_id: z.string().optional(), data: z.array(contact).optional(), pagination: pagination.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.supersend.io/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TeamId != null) query.append("TeamId", String(args.TeamId)) if (args.CampaignId != null) query.append("CampaignId", String(args.CampaignId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.interest != null) query.append("interest", String(args.interest)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) 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(contactListResponse, await res.json()) };