import {z} from 'zod' export type SearchType = 0 | 1 | 2; export type FuzzinessTolerance = 0 | 1 | 2 | 3; export type ViewType = 0 | 1; export type GetApiV1ContactsSearchArgs = {Query: string, Fields?: string | undefined, SearchInActivities?: boolean | undefined, SearchType?: SearchType | undefined, Fuzziness?: FuzzinessTolerance | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined}; export type GetApiV1ContactsSearchOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const contactResponseModel = z.object({country: z.string().nullable().optional(), openDeals: z.number().int().optional(), wonDeals: z.number().int().optional(), openTickets: z.number().int().optional(), resolvedTickets: z.number().int().optional(), 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 contactResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(contactResponseModel).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 }; /** * Performs a text search on contacts and returns the matching results * * GET /v1/contacts/search * * Also accepts auth: API Key */ export const getApiV1ContactsSearch = async (args: GetApiV1ContactsSearchArgs, options: GetApiV1ContactsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Query != null) query.append("Query", String(args.Query)) if (args.Fields != null) query.append("Fields", String(args.Fields)) if (args.SearchInActivities != null) query.append("SearchInActivities", String(args.SearchInActivities)) if (args.SearchType != null) query.append("SearchType", String(args.SearchType)) if (args.Fuzziness != null) query.append("Fuzziness", String(args.Fuzziness)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contacts/search${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(contactResponseModelQueryResult, await res.json()) };