import {z} from 'zod' export type CreateApiMessagesContactsArgs = {body: {from: string, to: string, contacts: Array<{name: {formatted_name: string, first_name?: string | undefined, last_name?: string | undefined, middle_name?: string | undefined, suffix?: string | undefined, prefix?: string | undefined}, phones?: (Array<{phone: string, type?: string | undefined, wa_id?: string | undefined}>) | undefined, addresses?: (Array<{street?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, country_code?: string | undefined, type?: string | undefined}>) | undefined, birthday?: string | undefined, emails?: (Array<{email?: string | undefined, type?: string | undefined}>) | undefined, org?: ({company?: string | undefined, department?: string | undefined, title?: string | undefined}) | undefined, urls?: (Array<{url?: string | undefined, type?: string | undefined}>) | undefined}>}}; export type CreateApiMessagesContactsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMessagesContactsResponse = z.object({id: 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 }; /** * Send contacts message * * POST /messages/contacts */ export const createApiMessagesContacts = async (args: CreateApiMessagesContactsArgs, options?: CreateApiMessagesContactsOptions) => { const {baseUrl = "https://messenger-public-api.cogfy.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/messages/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(createApiMessagesContactsResponse, await res.json()) };