import {z} from 'zod' export type GetApiV1ContactsContactIdArgs = {contactId: string}; export type GetApiV1ContactsContactIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), internationalPhoneNumber: z.string().optional(), channelId: z.string().optional(), companyId: z.string().optional(), conversationId: z.string().optional(), status: z.enum(["new", "active", "client", "long_term"]).optional(), subStatus: z.string().optional(), message: z.string().optional(), assignedTo: z.string().nullable().optional(), rating: z.number().int().gte(0).lte(5).optional(), entryMethod: z.string().optional(), utmSource: z.string().optional(), utmMedium: z.string().optional(), utmCampaign: z.string().optional(), gclid: z.string().optional(), conversionUrl: z.string().optional(), refererTracking: z.string().optional(), device: z.string().optional(), createdAt: z.string().optional(), updatedAt: 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 }; /** * Obtener contacto * * GET /v1/contacts/{contactId} */ export const getApiV1ContactsContactId = async (args: GetApiV1ContactsContactIdArgs, options?: GetApiV1ContactsContactIdOptions) => { const {baseUrl = "https://connect.cliengo.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contacts/${encodeURIComponent(String(args.contactId))}${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(contact, await res.json()) };