import {z} from 'zod' export type GetApiV1ContactsIdArgs = {id: number}; export type GetApiV1ContactsIdOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), firstName: z.string().min(1).max(100), lastName: z.string().max(100).nullable().optional(), fullName: z.string().max(130).nullable().optional(), email: z.string().max(100).nullable().optional(), phone: z.string().max(20).nullable().optional(), companyId: z.number().int().nullable().optional(), companyName: z.string().nullable().optional(), groupIds: z.string().max(1000).nullable().optional(), countryCode: z.string().max(3).nullable().optional(), state: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), town: z.string().max(100).nullable().optional(), zip: z.string().max(10).nullable().optional(), address: z.string().max(200).nullable().optional(), status: z.string().max(1).nullable().optional(), department: z.string().max(100).nullable().optional(), job: z.string().max(100).nullable().optional(), createdUserId: z.number().int().optional(), createdDate: z.string().optional(), updatedUserId: z.number().int().optional(), updatedDate: z.string().nullable().optional(), color: z.string().max(20).nullable().optional(), photoUrl: z.string().max(512).nullable().optional(), customFields: z.string().max(4096).nullable().optional(), blocked: z.number().int().nullable().optional(), visitor: z.string().max(200).nullable().optional(), isEmailAllowed: z.number().int().nullable().optional(), isCallAllowed: z.number().int().nullable().optional(), isSmsAllowed: z.number().int().nullable().optional(), isWaMarketingAllowed: z.number().int().nullable().optional(), waUserId: z.string().nullable().optional(), waUsername: z.string().nullable().optional(), lastSeen: z.string().nullable().optional(), lastContacted: z.string().nullable().optional(), lastHeard: z.string().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 }; /** * Get details of a single contact * * GET /v1/contacts/{id} * * Also accepts auth: API Key */ export const getApiV1ContactsId = async (args: GetApiV1ContactsIdArgs, options: GetApiV1ContactsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contacts/${encodeURIComponent(String(args.id))}${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()) };