import {z} from 'zod' export type GetApiV1TestSessionsArgs = {from?: number | undefined, to?: number | undefined, limit?: (number | null) | undefined, statuses?: (Array<'RUNNING' | 'FAILED' | 'PASSED' | 'CANCELLED'>) | undefined, branches?: Array | undefined, users?: Array | undefined, providers?: (Array<'GITHUB' | 'VERCEL' | 'API' | 'TRIGGER' | 'PW_REPORTER'>) | undefined, noUsers?: boolean | undefined, nextId?: string | undefined, textSearch?: string | undefined, errorGroupId?: (string | null) | undefined, 'x-checkly-account'?: string | undefined}; export type GetApiV1TestSessionsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const testSessionListEntryInvoker = z.object({name: z.string(), picture: z.string().nullable().optional()}).nullable(); export const testSessionListEntry = z.object({id: z.string(), accountId: z.string(), projectId: z.string().nullable().optional(), name: z.string(), provider: z.string(), errorGroupIds: z.array(z.string()).nullable().optional(), running: z.array(z.string()).nullable().optional(), passed: z.array(z.string()).nullable().optional(), failed: z.array(z.string()).nullable().optional(), cancelled: z.array(z.string()).optional(), status: z.enum(["RUNNING", "FAILED", "PASSED", "CANCELLED"]).optional(), region: z.string(), privateLocationId: z.string().nullable().optional(), invoker: testSessionListEntryInvoker.optional(), repoUrl: z.string().nullable().optional(), commitId: z.string().nullable().optional(), commitOwner: z.string().nullable().optional(), commitMessage: z.string().nullable().optional(), branchName: z.string().nullable().optional(), environment: z.string().nullable().optional(), startedAt: z.string(), stoppedAt: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional()}); export const listTestSessionsResponse = z.object({length: z.number().int().gte(0), entries: z.array(testSessionListEntry), nextId: 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 test sessions * * GET /v1/test-sessions */ export const getApiV1TestSessions = async (args: GetApiV1TestSessionsArgs, options?: GetApiV1TestSessionsOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.branches ?? []) query.append("branches", String(item)) for (const item of args.users ?? []) query.append("users", String(item)) for (const item of args.providers ?? []) query.append("providers", String(item)) if (args.noUsers != null) query.append("noUsers", String(args.noUsers)) if (args.nextId != null) query.append("nextId", String(args.nextId)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.errorGroupId != null) query.append("errorGroupId", String(args.errorGroupId)) if (args['x-checkly-account'] != null) headers.set("x-checkly-account", String(args['x-checkly-account'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/test-sessions${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(listTestSessionsResponse, await res.json()) };