import {z} from 'zod' export type QueryPrefix = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16; export type DataFieldFilter = {op?: QueryPrefix | undefined, val: unknown}; export type DepartmentPageSelection = Record; export type ViewType = 0 | 1; export type GetApiV1ContactsArgs = {Id?: number | undefined, DepartmentId?: number | undefined, CompanyId?: number | undefined, FullName?: string | undefined, FirstName?: string | undefined, LastName?: string | undefined, Email?: string | undefined, Phone?: string | undefined, GroupIds?: string | undefined, Address?: string | undefined, State?: string | undefined, City?: string | undefined, Town?: string | undefined, CountryCode?: string | undefined, Job?: string | undefined, Status?: string | undefined, Blocked?: number | undefined, FromCreatedDate?: string | undefined, ToCreatedDate?: string | undefined, CustomFields?: ({[key: string]: Array | null}) | undefined, Q?: string | undefined, Fields?: string | undefined, DepartmentScope?: DepartmentPageSelection | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1ContactsOptions = {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 }; /** * List all existing contacts * * GET /v1/contacts * * Also accepts auth: API Key */ export const getApiV1Contacts = async (args: GetApiV1ContactsArgs, options: GetApiV1ContactsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.DepartmentId != null) query.append("DepartmentId", String(args.DepartmentId)) if (args.CompanyId != null) query.append("CompanyId", String(args.CompanyId)) if (args.FullName != null) query.append("FullName", String(args.FullName)) if (args.FirstName != null) query.append("FirstName", String(args.FirstName)) if (args.LastName != null) query.append("LastName", String(args.LastName)) if (args.Email != null) query.append("Email", String(args.Email)) if (args.Phone != null) query.append("Phone", String(args.Phone)) if (args.GroupIds != null) query.append("GroupIds", String(args.GroupIds)) if (args.Address != null) query.append("Address", String(args.Address)) if (args.State != null) query.append("State", String(args.State)) if (args.City != null) query.append("City", String(args.City)) if (args.Town != null) query.append("Town", String(args.Town)) if (args.CountryCode != null) query.append("CountryCode", String(args.CountryCode)) if (args.Job != null) query.append("Job", String(args.Job)) if (args.Status != null) query.append("Status", String(args.Status)) if (args.Blocked != null) query.append("Blocked", String(args.Blocked)) if (args.FromCreatedDate != null) query.append("FromCreatedDate", String(args.FromCreatedDate)) if (args.ToCreatedDate != null) query.append("ToCreatedDate", String(args.ToCreatedDate)) if (args.CustomFields != null) query.append("CustomFields", JSON.stringify(args.CustomFields)) if (args.Q != null) query.append("Q", String(args.Q)) if (args.Fields != null) query.append("Fields", String(args.Fields)) if (args.DepartmentScope != null) query.append("DepartmentScope", JSON.stringify(args.DepartmentScope)) 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 (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contacts${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()) };