import {z} from 'zod' export type GetApiV2TicketsTicketIdExternalMessagesArgs = {ticket_id: string}; export type GetApiV2TicketsTicketIdExternalMessagesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const svhelpPubapimodelsExternalMessageSourceType = z.enum(["EXTERNAL_MESSAGE_SOURCE_TYPE_UNSPECIFIED", "EXTERNAL_MESSAGE_SOURCE_TYPE_SLACK", "EXTERNAL_MESSAGE_SOURCE_TYPE_LINEAR", "EXTERNAL_MESSAGE_SOURCE_TYPE_EMAIL", "EXTERNAL_MESSAGE_SOURCE_TYPE_JIRA", "EXTERNAL_MESSAGE_SOURCE_TYPE_PUBLIC_API", "EXTERNAL_MESSAGE_SOURCE_TYPE_FRESHSERVICE", "EXTERNAL_MESSAGE_SOURCE_TYPE_ZENDESK", "EXTERNAL_MESSAGE_SOURCE_TYPE_SERVICENOW", "EXTERNAL_MESSAGE_SOURCE_TYPE_PHONE_CALL", "EXTERNAL_MESSAGE_SOURCE_TYPE_MICROSOFT"]); export const googleProtobufTimestamp = z.string(); export const svhelpPubapimodelsExternalMessage = z.object({id: z.string().optional(), externalMessageId: z.string().optional(), messageId: z.string().optional(), sourceType: svhelpPubapimodelsExternalMessageSourceType.optional(), sourceId: z.string().optional(), isOrigin: z.boolean().optional(), createdAt: googleProtobufTimestamp.optional(), updatedAt: googleProtobufTimestamp.optional(), toRecipientEmails: z.array(z.string()).optional(), ccRecipientEmails: z.array(z.string()).optional()}); export const svhelpPublicapiListTicketExternalMessagesResponse = z.object({data: z.array(svhelpPubapimodelsExternalMessage).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 }; /** * List Ticket External Messages * * GET /v2/tickets/{ticket_id}/external-messages */ export const getApiV2TicketsTicketIdExternalMessages = async (args: GetApiV2TicketsTicketIdExternalMessagesArgs, options?: GetApiV2TicketsTicketIdExternalMessagesOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/tickets/${encodeURIComponent(String(args.ticket_id))}/external-messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(svhelpPublicapiListTicketExternalMessagesResponse, await res.json()) };