import {z} from 'zod' export type SlaType = 1 | 2 | 3; export type ViewType = 0 | 1; export type GetApiV1SlasBreachesArgs = {Id?: number | undefined, LinkedType?: number | undefined, LinkedId?: number | undefined, SlaType?: SlaType | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined}; export type GetApiV1SlasBreachesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const slaType = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const slaBreach = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), linkedType: z.number().int(), linkedId: z.number().int(), slaType: slaType, expectedDate: z.string().nullable().optional(), actualDate: z.string().nullable().optional(), slaPolicyId: z.number().int().optional()}); export const slaBreachQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(slaBreach).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 }; /** * GET /v1/slas/breaches * * Also accepts auth: API Key */ export const getApiV1SlasBreaches = async (args: GetApiV1SlasBreachesArgs, options: GetApiV1SlasBreachesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.LinkedType != null) query.append("LinkedType", String(args.LinkedType)) if (args.LinkedId != null) query.append("LinkedId", String(args.LinkedId)) if (args.SlaType != null) query.append("SlaType", String(args.SlaType)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/slas/breaches${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(slaBreachQueryResult, await res.json()) };