import {z} from 'zod' export type Query = {value: string, exactMatch?: boolean | undefined}; export type IsEmpty = Record; export type IsNotEmpty = Record; export type IsNotOneOf = {values?: Array | undefined}; export type IsOneOf = {values?: Array | undefined}; export type FieldCanHaveSingleValueOrBeEmptyOperator = IsEmpty | IsNotEmpty | IsNotOneOf | IsOneOf; export type AgentId = {operator: FieldCanHaveSingleValueOrBeEmptyOperator}; export type Between = {timestampFrom: string, timestampTo: string}; export type Since = {timestamp: string}; export type Until = {timestamp: string}; export type TimestampOperator = Between | Since | Until; export type AssignedDate = {operator: TimestampOperator}; export type IsNotOneOf1 = {values: Array}; export type IsOneOf1 = {values: Array}; export type ChannelOperator = IsNotOneOf1 | IsOneOf1; export type Channel = {operator: ChannelOperator}; export type ClosedDate = {operator: TimestampOperator}; export type FieldHasSingleValueOperator = IsNotOneOf | IsOneOf; export type ContactId = {operator: FieldHasSingleValueOperator}; export type ConversationId = {operator: FieldHasSingleValueOperator}; export type CreationDate = {operator: TimestampOperator}; export type IsNotOneOf2 = {values?: Array> | undefined}; export type IsOneOf2 = {values?: Array> | undefined}; export type CustomAttributeOperator = IsEmpty | IsNotEmpty | IsNotOneOf2 | IsOneOf2; export type CustomAttribute1 = {operator: CustomAttributeOperator, attributeId: string}; export type IsNotOneOf3 = {values: Array}; export type IsOneOf3 = {values: Array}; export type DirectionOperator = IsNotOneOf3 | IsOneOf3; export type Direction = {operator: DirectionOperator}; export type Between1 = {min: number, max: number}; export type LongerThanOrEqualTo = {duration: number}; export type ShorterThanOrEqualTo = {duration: number}; export type DurationOperator = Between1 | LongerThanOrEqualTo | ShorterThanOrEqualTo; export type Duration = {operator: DurationOperator}; export type LastActivity = {operator: TimestampOperator}; export type LastMessage = {operator: TimestampOperator}; export type QueueId = {operator: FieldCanHaveSingleValueOrBeEmptyOperator}; export type IsNotOneOf4 = {values: Array}; export type IsOneOf4 = {values: Array}; export type StatusOperator = IsNotOneOf4 | IsOneOf4; export type Status1 = {operator: StatusOperator}; export type ExcludesAll = {values?: Array | undefined}; export type ExcludesOne = {values?: Array | undefined}; export type IncludesAll = {values?: Array | undefined}; export type IncludesOne = {values?: Array | undefined}; export type FieldCanHaveMultipleValuesOrBeEmptyOperator = ExcludesAll | ExcludesOne | IncludesAll | IncludesOne | IsEmpty | IsNotEmpty; export type TagId = {operator: FieldCanHaveMultipleValuesOrBeEmptyOperator}; export type SearchField = AgentId | AssignedDate | Channel | ClosedDate | ContactId | ConversationId | CreationDate | CustomAttribute1 | Direction | Duration | LastActivity | LastMessage | QueueId | Status1 | TagId; export type Condition = {field: SearchField}; export type Filters = {strategy: string, conditions?: Array | undefined}; export type SearchParametersInput = {query?: Query | undefined, filters?: Filters | undefined}; export type CreateApiBetaSearchConversationsArgs = {pageLimit?: number | undefined, pageKey?: string | undefined, body: SearchParametersInput}; export type CreateApiBetaSearchConversationsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mapVectorString = z.record(z.string(), z.array(z.string())); export const conversationSearchWithFiltersHit = z.object({id: z.number().int(), highlights: mapVectorString}); export const paginationLinks = z.object({previous: z.string().optional(), next: z.string().optional()}); export const searchConversationsWithFiltersOutput = z.object({data: z.array(conversationSearchWithFiltersHit).optional(), meta: paginationLinks.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 }; /** * Search conversations with filters * * POST /beta/search/conversations */ export const createApiBetaSearchConversations = async (args: CreateApiBetaSearchConversationsArgs, options?: CreateApiBetaSearchConversationsOptions) => { const {baseUrl = "https://dev.dixa.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.pageLimit != null) query.append("pageLimit", String(args.pageLimit)) if (args.pageKey != null) query.append("pageKey", String(args.pageKey)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/search/conversations${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(searchConversationsWithFiltersOutput, await res.json()) };