import {z} from 'zod' export type SvhelpModelsTicketType = '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 type GetApiV2StatusesArgs = {teamId?: string | undefined, pageSize?: number | undefined, pageToken?: string | undefined, ticketType?: SvhelpModelsTicketType | undefined}; export type GetApiV2StatusesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const svhelpModelsTicketStatusGroup = z.enum(["TICKET_STATUS_GROUP_UNSPECIFIED", "TODO", "IN_PROGRESS", "DONE", "CANCELED"]); 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 svhelpPubapimodelsStatus = z.object({id: z.string().optional(), teamId: z.string().optional(), statusGroup: svhelpModelsTicketStatusGroup.optional(), name: z.string().optional(), description: z.string().optional(), isDefault: z.boolean().optional(), ticketType: svhelpModelsTicketType.optional()}); export const svhelpPublicapiListStatusesResponse = z.object({data: z.array(svhelpPubapimodelsStatus).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 }; /** * List Statuses * * GET /v2/statuses */ export const getApiV2Statuses = async (args: GetApiV2StatusesArgs, options?: GetApiV2StatusesOptions) => { 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 (args.teamId != null) query.append("teamId", String(args.teamId)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.ticketType != null) query.append("ticketType", String(args.ticketType)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/statuses${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(svhelpPublicapiListStatusesResponse, await res.json()) };