import {z} from 'zod' export type CrmFilter = {highValue?: string | undefined, operator: 'BETWEEN' | 'CONTAINS_TOKEN' | 'EQ' | 'GT' | 'GTE' | 'HAS_PROPERTY' | 'IN' | 'LT' | 'LTE' | 'NEQ' | 'NOT_CONTAINS_TOKEN' | 'NOT_HAS_PROPERTY' | 'NOT_IN', propertyName: string, value?: string | undefined, values?: Array | undefined}; export type CrmFilterGroup = {filters: Array}; export type CrmPublicObjectSearchRequest = {after: string, filterGroups: Array, limit: number, properties: Array, query?: string | undefined, sorts: Array}; export type CreateApiCrmObjects202603TicketsSearchArgs = {body: CrmPublicObjectSearchRequest}; export type CreateApiCrmObjects202603TicketsSearchOptions = {baseUrl?: string | undefined, privateApps?: string | undefined, fetch?: typeof fetch | undefined}; export const nextPage = z.object({after: z.string(), link: z.string().optional()}); export const previousPage = z.object({before: z.string(), link: z.string().optional()}); export const paging = z.object({next: nextPage.optional(), prev: previousPage.optional()}); export const crmValueWithTimestamp = z.object({sourceId: z.string().optional(), sourceLabel: z.string().optional(), sourceType: z.string(), timestamp: z.string(), updatedByUserId: z.number().int().optional(), value: z.string()}); export const crmSimplePublicObject = z.object({archived: z.boolean(), archivedAt: z.string().optional(), createdAt: z.string(), id: z.string(), objectWriteTraceId: z.string().optional(), properties: z.record(z.string(), z.string()), propertiesWithHistory: z.record(z.string(), z.array(crmValueWithTimestamp)).optional(), updatedAt: z.string(), url: z.string().optional()}); export const crmCollectionResponseWithTotalSimplePublicObject = z.object({paging: paging.optional(), results: z.array(crmSimplePublicObject), total: z.number().int()}); 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 }; /** * Search for tickets * * POST /crm/objects/2026-03/tickets/search * * Also accepts auth: developer_hapikey */ export const createApiCrmObjects202603TicketsSearch = async (args: CreateApiCrmObjects202603TicketsSearchArgs, options?: CreateApiCrmObjects202603TicketsSearchOptions) => { const {baseUrl = "https://api.hubapi.com", fetch: fetchFn = fetch, privateApps} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (privateApps !== undefined) headers.set('authorization', `Bearer ${privateApps}`) const url = `${baseUrl.replace(/\/+$/, '')}/crm/objects/2026-03/tickets/search${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(crmCollectionResponseWithTotalSimplePublicObject, await res.json()) };