import {z} from 'zod' export type DateISO = string; export type ClientCustomAttributePathDto = string; export type AppliedClientCustomTextFilterDto = {type: 'text', operator: 'contains' | 'equals' | 'startsWith' | 'notEquals', path: ClientCustomAttributePathDto, value: string}; export type AppliedClientCustomDateFilterDto = {type: 'date', operator: 'lessThanOrEqual' | 'greaterThanOrEqual' | 'equals' | 'notEquals', path: ClientCustomAttributePathDto, value: DateISO}; export type AppliedClientCustomEmptyDateFilterDto = {type: 'date', operator: 'isEmpty' | 'isNotEmpty', path: ClientCustomAttributePathDto}; export type AppliedClientCustomDateRangeFilterDto = {type: 'date', operator: 'between', path: ClientCustomAttributePathDto, value: {from: DateISO, to: DateISO}}; export type AppliedClientCustomEntitySelectFilterDto = {type: 'entitySelect', operator: 'in' | 'notIn', path: ClientCustomAttributePathDto, value: string | null}; export type AppliedClientCustomEntityMultiSelectFilterDto = {type: 'entitySelect', operator: 'in' | 'notIn' | 'isAllOf', path: ClientCustomAttributePathDto, value: Array}; export type AppliedClientCustomNumberFilterDto = {type: 'number', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: ClientCustomAttributePathDto, value: number}; export type AppliedClientCustomNumberRangeFilterDto = {type: 'number', operator: 'between', path: ClientCustomAttributePathDto, value: {from: number, to: number}}; export type AppliedClientCustomBooleanFilterDto = {type: 'boolean', operator: 'equals' | 'notEquals', path: ClientCustomAttributePathDto, value: boolean}; export type AppliedClientCustomEnumSelectFilterDto = {type: 'enumSelect', operator: 'in' | 'notIn' | 'isAllOf', path: ClientCustomAttributePathDto, value: Array}; export type AppliedClientCustomEnumListSelectFilterDto = {type: 'enumListSelect', operator: 'contains' | 'doesNotContain', path: ClientCustomAttributePathDto, value: string | null}; export type DmCurrency = 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTC' | 'BTN' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLF' | 'CLP' | 'CNH' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GGP' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'IMP' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JEP' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRU' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLE' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STD' | 'STN' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VEF' | 'VES' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XAG' | 'XAU' | 'XCD' | 'XCG' | 'XDR' | 'XOF' | 'XPD' | 'XPF' | 'XPT' | 'YER' | 'ZAR' | 'ZMW' | 'ZWG' | 'ZWL'; export type AppliedClientCustomCurrencyFilterDto = {type: 'currency', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: ClientCustomAttributePathDto, value: {amount: number, currency: DmCurrency}}; export type AppliedClientCustomDurationFilterDto = {type: 'duration', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: ClientCustomAttributePathDto, value: number}; export type AppliedClientCustomUrlFilterDto = {type: 'url', operator: 'equals' | 'notEquals' | 'contains' | 'startsWith', path: ClientCustomAttributePathDto, value: string}; export type AppliedClientCustomTimeStampFilterDto = {type: 'timestamp', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: ClientCustomAttributePathDto, value: DateISO}; export type AppliedClientCustomRatingFilterDto = {type: 'rating', operator: 'greaterThanOrEqual' | 'lessThanOrEqual' | 'equals', path: ClientCustomAttributePathDto, value: number}; export type AppliedClientCustomRatingRangeFilterDto = {type: 'rating', operator: 'between', path: ClientCustomAttributePathDto, value: {from: number, to: number}}; export type AppliedClientFilterDto = {type: 'text', operator: 'contains' | 'equals' | 'startsWith' | 'notEquals', path: 'client.company.name' | 'client.company.domain' | 'client.company.description', value: string} | {type: 'entitySelect', operator: 'in' | 'notIn', path: 'client.stage' | 'client.contacts', value: string} | {type: 'entityMultiSelect', operator: 'in' | 'notIn' | 'isAllOf', path: 'client.stage' | 'client.contacts', value: Array} | {type: 'enumMultiSelect', operator: 'in' | 'notIn' | 'isAllOf', path: 'client.company.size', value: Array<'1 employee' | '2-10 employees' | '11-50 employees' | '51-200 employees' | '201-500 employees' | '501-1,000 employees' | '1,001-5,000 employees' | '5,001-10,000 employees' | '10,001+ employees'>} | {type: 'date', operator: 'lessThanOrEqual' | 'greaterThanOrEqual' | 'equals' | 'notEquals', path: 'client.createdAt', value: DateISO} | {type: 'dateRange', operator: 'between', path: 'client.createdAt', value: {from: DateISO, to: DateISO}} | AppliedClientCustomTextFilterDto | AppliedClientCustomDateFilterDto | AppliedClientCustomEmptyDateFilterDto | AppliedClientCustomDateRangeFilterDto | AppliedClientCustomEntitySelectFilterDto | AppliedClientCustomEntityMultiSelectFilterDto | AppliedClientCustomNumberFilterDto | AppliedClientCustomNumberRangeFilterDto | AppliedClientCustomBooleanFilterDto | AppliedClientCustomEnumSelectFilterDto | AppliedClientCustomEnumListSelectFilterDto | AppliedClientCustomCurrencyFilterDto | AppliedClientCustomDurationFilterDto | AppliedClientCustomUrlFilterDto | AppliedClientCustomTimeStampFilterDto | AppliedClientCustomRatingFilterDto | AppliedClientCustomRatingRangeFilterDto; export type SearchClientsRequestDto = {filters?: Array | undefined, page?: number | undefined, pageSize?: number | undefined}; export type CreateApiClientsSearchArgs = {body: SearchClientsRequestDto}; export type CreateApiClientsSearchOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const clientStageDto = z.object({id: z.string().min(1), order: z.number(), name: z.string(), colorHex: z.string()}); export const dateISO = z.string(); export const clientSearchItemDto = z.object({id: z.string().min(1), name: z.string(), domain: z.string().nullable(), logoUrl: z.string().nullable(), stage: clientStageDto, createdAt: dateISO, modifiedAt: dateISO}); export const searchClientsPaginationDto = z.object({hasNextPage: z.boolean()}); export const searchClientsResponseDto = z.object({items: z.array(clientSearchItemDto), pagination: searchClientsPaginationDto}); 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 clients * * POST /clients/_search */ export const createApiClientsSearch = async (args: CreateApiClientsSearchArgs, options: CreateApiClientsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/clients/_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(searchClientsResponseDto, await res.json()) };