import {z} from 'zod' export type ConversationMergeStrategy = 'MERGE'; export type MergeContactRequest = {source_id: string, strategy?: ConversationMergeStrategy | undefined}; export type CreateApiV1ProjectsProjectIdContactsDestinationIdMergeArgs = {body: MergeContactRequest}; export type CreateApiV1ProjectsProjectIdContactsDestinationIdMergeOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const conversationChannel = z.enum(["SMS", "RCS", "WHATSAPP", "MMS", "KAKAOTALK", "KAKAOTALKCHAT", "VIBERBM", "LINE", "INSTAGRAM", "MESSENGER", "WECHAT", "TELEGRAM", "APPLEBC"]); export const channelIdentity = z.object({app_id: z.string().optional(), channel: conversationChannel, identity: z.string()}); export const contactLanguage = z.enum(["AF", "SQ", "AR", "AZ", "BN", "BG", "CA", "ZH", "ZH_CN", "ZH_HK", "ZH_TW", "HR", "CS", "DA", "NL", "EN", "EN_GB", "EN_US", "ET", "FIL", "FI", "FR", "DE", "EL", "GU", "HA", "HE", "HI", "HU", "ID", "GA", "IT", "JA", "KN", "KK", "KO", "LO", "LV", "LT", "MK", "MS", "ML", "MR", "NB", "FA", "PL", "PT", "PT_BR", "PT_PT", "PA", "RO", "RU", "SR", "SK", "SL", "ES", "ES_AR", "ES_ES", "ES_MX", "SW", "SV", "TA", "TE", "TH", "TR", "UK", "UR", "UZ", "VI", "ZU"]); export const contact = z.object({channel_identities: z.array(channelIdentity).optional(), channel_priority: z.array(conversationChannel).optional(), display_name: z.string().optional(), email: z.string().optional(), external_id: z.string().optional(), id: z.string().optional(), language: contactLanguage.optional(), metadata: z.string().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 }; /** * Merge two Contacts * * POST /v1/projects/{project_id}/contacts/{destination_id}:merge * * Also accepts auth: oAuth2 */ export const createApiV1ProjectsProjectIdContactsDestinationIdMerge = async (args: CreateApiV1ProjectsProjectIdContactsDestinationIdMergeArgs, options?: CreateApiV1ProjectsProjectIdContactsDestinationIdMergeOptions) => { const {baseUrl = "https://us.conversation.api.sinch.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.project_id))}/contacts/${encodeURIComponent(String(args.destination_id))}:merge${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(contact, await res.json()) };