import {z} from 'zod' export type ContactStatus = 'not_attempted' | 'in_progress' | 'to_be_retried' | 'failed' | 'completed' | 'no-answer' | 'busy'; export type ICampaignContactCreate = {campaign_id: string, contact_id: string, status?: ContactStatus | undefined, execution_time?: (string | null) | undefined}; export type CreateApiApiV1CampaignContactArgs = {body: ICampaignContactCreate}; export type CreateApiApiV1CampaignContactOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const contactStatus = z.enum(["not_attempted", "in_progress", "to_be_retried", "failed", "completed", "no-answer", "busy"]); export const iCampaignContactRead = z.object({campaign_id: z.string(), contact_id: z.string(), status: contactStatus.optional(), execution_time: z.string().nullable().optional(), id: z.string()}); export const iPostResponseBaseICampaignContactRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iCampaignContactRead.nullable().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 Campaign Contact * * POST /api/v1/campaign_contact * * Also accepts auth: HTTPBearer */ export const createApiApiV1CampaignContact = async (args: CreateApiApiV1CampaignContactArgs, options: CreateApiApiV1CampaignContactOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/campaign_contact${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(iPostResponseBaseICampaignContactRead, await res.json()) };