import {z} from 'zod' export type ErrorsFilter = {taskIdentifier?: Array | undefined, version?: Array | undefined, status?: (Array<'unresolved' | 'resolved' | 'ignored'>) | undefined, search?: string | undefined, from?: string | undefined, to?: string | undefined, period?: string | undefined}; export type GetApiApiV1ErrorsArgs = {page?: ({size?: number | undefined, after?: string | undefined, before?: string | undefined}) | undefined, filter?: ErrorsFilter | undefined}; export type GetApiApiV1ErrorsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const errorListItem = z.object({id: z.string(), fingerprint: z.string(), taskIdentifier: z.string(), errorType: z.string(), errorMessage: z.string(), status: z.enum(["unresolved", "resolved", "ignored"]), count: z.number().int(), firstSeen: z.string(), lastSeen: z.string(), resolvedAt: z.string().nullable().optional(), ignoredUntil: z.string().nullable().optional()}); export const listErrorsResult = z.object({data: z.array(errorListItem).optional(), pagination: z.object({next: z.string().optional(), previous: z.string().optional()}).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 errors * * GET /api/v1/errors */ export const getApiApiV1Errors = async (args: GetApiApiV1ErrorsArgs, options?: GetApiApiV1ErrorsOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/errors${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(listErrorsResult, await res.json()) };