import {z} from 'zod' export type GetApiV1TestSessionsTestSessionIdResultsTestSessionResultIdAssetsArgs = {testSessionId: string, testSessionResultId: string, type?: (Array<'log' | 'trace' | 'video' | 'screenshot' | 'pcap' | 'report' | 'file'>) | undefined, name?: string | undefined, 'x-checkly-account'?: string | undefined}; export type GetApiV1TestSessionsTestSessionIdResultsTestSessionResultIdAssetsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const assetManifestEntry = z.object({type: z.enum(["log", "trace", "video", "screenshot", "pcap", "report", "file"]), name: z.string().min(1), url: z.string(), contentType: z.string().min(1).optional(), contentEncoding: z.literal("gzip").optional(), source: z.object({type: z.enum(["check-result", "test-session-result"]), checkId: z.string().optional(), checkName: z.string().min(1).optional(), checkType: z.string().min(1).optional(), resultId: z.string().optional(), testSessionId: z.string().optional()}), archive: z.object({entryName: z.string().min(1)}).optional()}); export const assetManifest = z.object({assets: z.array(assetManifestEntry), truncated: z.boolean().optional(), entriesReturned: z.number().int().gte(0).optional(), entriesTotal: z.number().int().gte(0).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 }; /** * Retrieve a normalized asset manifest for a test-session result * * GET /v1/test-sessions/{testSessionId}/results/{testSessionResultId}/assets */ export const getApiV1TestSessionsTestSessionIdResultsTestSessionResultIdAssets = async (args: GetApiV1TestSessionsTestSessionIdResultsTestSessionResultIdAssetsArgs, options?: GetApiV1TestSessionsTestSessionIdResultsTestSessionResultIdAssetsOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.type ?? []) query.append("type", String(item)) if (args.name != null) query.append("name", String(args.name)) 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/${encodeURIComponent(String(args.testSessionId))}/results/${encodeURIComponent(String(args.testSessionResultId))}/assets${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(assetManifest, await res.json()) };