import {z} from 'zod' export type GetApiV2TicketingTicketTicketIdArgs = {ticketId: number}; export type GetApiV2TicketingTicketTicketIdOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TicketingTicketTicketIdResponse = z.object({id: z.number().int().optional(), version: z.number().int().optional(), nodeId: z.number().int().optional(), clientId: z.number().int().optional(), locationId: z.number().int().optional(), assignedAppUserId: z.number().int().optional(), requesterUid: z.string().optional(), subject: z.string().optional(), status: z.object({name: z.string().optional(), displayName: z.string().optional(), parentId: z.number().int().optional(), statusId: z.number().int().optional()}).optional(), type: z.enum(["PROBLEM", "QUESTION", "INCIDENT", "TASK", "CHANGE_REQUEST", "SERVICE_REQUEST", "PROJECT", "APPOINTMENT", "MISCELLANEOUS"]).optional(), priority: z.enum(["NONE", "LOW", "MEDIUM", "HIGH"]).optional(), severity: z.enum(["NONE", "MINOR", "MODERATE", "MAJOR", "CRITICAL"]).optional(), ticketFormId: z.number().int().optional(), source: z.enum(["TECHNICIAN", "EMAIL", "WEB_FORM", "HELP_REQUEST", "END_USER", "CONDITION", "SCHEDULED_SCRIPT", "ACTIVITY"]).optional(), tags: z.array(z.string()).optional(), ccList: z.object({uids: z.array(z.string()).optional(), emails: z.array(z.string()).optional()}).optional(), createTime: z.number().optional(), deleted: z.boolean().optional(), attributeValues: z.array(z.object({id: z.number().int().optional(), attributeId: z.number().int().optional(), value: z.object({}).optional()})).optional(), additionalAssignedTechnicianIds: z.array(z.number().int()).optional(), followupTime: z.number().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 }; /** * Ticket * * GET /v2/ticketing/ticket/{ticketId} * * Also accepts auth: sessionKey */ export const getApiV2TicketingTicketTicketId = async (args: GetApiV2TicketingTicketTicketIdArgs, options: GetApiV2TicketingTicketTicketIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/ticketing/ticket/${encodeURIComponent(String(args.ticketId))}${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(getApiV2TicketingTicketTicketIdResponse, await res.json()) };