import {z} from 'zod' export type Filter = {highValue?: string | undefined, propertyName: string, values?: Array | undefined, value?: string | undefined, operator: 'EQ' | 'NEQ' | 'LT' | 'LTE' | 'GT' | 'GTE' | 'BETWEEN' | 'IN' | 'NOT_IN' | 'HAS_PROPERTY' | 'NOT_HAS_PROPERTY' | 'CONTAINS_TOKEN' | 'NOT_CONTAINS_TOKEN'}; export type FilterGroup = {filters: Array}; export type PublicObjectSearchRequest = {query?: string | undefined, limit: number, after: string, sorts: Array, properties: Array, filterGroups: Array}; export type CreateApiCrmV3ObjectsPaymentsSearchArgs = {body: PublicObjectSearchRequest}; export type CreateApiCrmV3ObjectsPaymentsSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nextPage = z.object({link: z.string().optional(), after: z.string()}); export const forwardPaging = z.object({next: nextPage.optional()}); export const valueWithTimestamp = z.object({sourceId: z.string().optional(), sourceType: z.string(), sourceLabel: z.string().optional(), updatedByUserId: z.number().int().optional(), value: z.string(), timestamp: z.string()}); export const simplePublicObject = z.object({createdAt: z.string(), archived: z.boolean().optional(), archivedAt: z.string().optional(), propertiesWithHistory: z.record(z.string(), z.array(valueWithTimestamp)).optional(), id: z.string(), properties: z.record(z.string(), z.string().nullable()), updatedAt: z.string()}); export const collectionResponseWithTotalSimplePublicObjectForwardPaging = z.object({total: z.number().int(), paging: forwardPaging.optional(), results: z.array(simplePublicObject)}); 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 }; /** * POST /crm/v3/objects/payments/search */ export const createApiCrmV3ObjectsPaymentsSearch = async (args: CreateApiCrmV3ObjectsPaymentsSearchArgs, options?: CreateApiCrmV3ObjectsPaymentsSearchOptions) => { const {baseUrl = "https://api.hubapi.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/crm/v3/objects/payments/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(collectionResponseWithTotalSimplePublicObjectForwardPaging, await res.json()) };