import {z} from 'zod' export type SvhelpModelsTicketEscalationLevel = 'AI' | 'HUMAN'; export type GoogleProtobufTimestamp = string; export type SvhelpPublicapiSearchTicketsRequest = {teamIds?: Array | undefined, assignedToUserIds?: Array | undefined, requesterUserIds?: Array | undefined, statusIds?: Array | undefined, escalationLevels?: Array | undefined, createdAfter?: GoogleProtobufTimestamp | undefined, createdBefore?: GoogleProtobufTimestamp | undefined, pageSize?: (number | null) | undefined, pageToken?: (string | null) | undefined}; export type CreateApiV2TicketsSearchArgs = {body: SvhelpPublicapiSearchTicketsRequest}; export type CreateApiV2TicketsSearchOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufTimestamp = z.string(); export const svhelpModelsTicketEscalationLevel = z.enum(["AI", "HUMAN"]); export const svhelpModelsTicketType = z.enum(["TICKET_TYPE_UNSPECIFIED", "TICKET_TYPE_REQUEST", "TICKET_TYPE_TASK", "TICKET_TYPE_MAJOR_INCIDENT", "TICKET_TYPE_CONVERSATION", "TICKET_TYPE_JOURNEY", "TICKET_TYPE_JOURNEY_CONVERSATION", "TICKET_TYPE_INCIDENT", "TICKET_TYPE_CHANGE", "TICKET_TYPE_CUSTOM"]); export const googleTypeDate = z.object({year: z.number().int().optional(), month: z.number().int().optional(), day: z.number().int().optional()}); export const svhelpPubapimodelsTicket = z.object({id: z.string().optional(), friendlyIdentifier: z.string().optional(), teamId: z.string().optional(), name: z.string().optional(), description: z.string().optional(), createdAt: googleProtobufTimestamp.optional(), completedAt: googleProtobufTimestamp.optional(), escalatedAt: googleProtobufTimestamp.optional(), createdByUserId: z.string().optional(), assignedToUserId: z.string().nullable().optional(), requesterUserId: z.string().optional(), statusId: z.string().optional(), escalationLevel: svhelpModelsTicketEscalationLevel.optional(), priorityId: z.string().optional(), slaStartedAt: googleProtobufTimestamp.optional(), slaBreachesAt: googleProtobufTimestamp.optional(), labelIds: z.array(z.string()).optional(), type: svhelpModelsTicketType.optional(), dueDate: googleTypeDate.optional(), isOverdue: z.boolean().optional(), childTicketIds: z.array(z.string()).optional(), linkedTicketIds: z.array(z.string()).optional(), duplicateTicketIds: z.array(z.string()).optional(), dependsOnTicketIds: z.array(z.string()).optional(), parentTicketId: z.string().nullable().optional(), conversationThreadTicketIds: z.array(z.string()).optional(), conversationThreadParentTicketId: z.string().nullable().optional(), nameTranslations: z.record(z.string(), z.string()).optional(), descriptionTranslations: z.record(z.string(), z.string()).optional(), categoryOptionId: z.string().nullable().optional()}); export const svhelpPublicapiSearchTicketsResponse = z.object({data: z.array(svhelpPubapimodelsTicket).optional(), nextPageToken: z.string().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 }; /** * Search Tickets * * POST /v2/tickets/search */ export const createApiV2TicketsSearch = async (args: CreateApiV2TicketsSearchArgs, options?: CreateApiV2TicketsSearchOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(svhelpPublicapiSearchTicketsResponse, await res.json()) };