import {z} from 'zod' export type GetApiCapturehistoryArgs = {captureId?: string | undefined, pageIndex?: number | undefined, startDate?: string | undefined, endDate?: string | undefined, tags?: string | undefined}; export type GetApiCapturehistoryOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduledCapture = z.object({id: z.string(), name: z.string(), paused: z.boolean(), comment: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), captureRequest: z.object({apps: z.array(z.string()).optional(), browser: z.enum(["Chrome", "Firefox", "iPhone", "iPhone 12"]), country: z.enum(["USA", "Germany", "Italy", "Netherlands", "UK", "Australia"]), cropHeight: z.number().int().optional(), cropLeft: z.number().int().optional(), cropTop: z.number().int().optional(), cropBottom: z.number().int().optional(), emails: z.array(z.string()).optional(), externalMax: z.number().int().optional(), format: z.enum(["png", "jpeg"]), height: z.number().int(), scrollTo: z.object({x: z.number().int().optional(), y: z.number().int().optional()}).optional(), size: z.enum(["Full Page", "Screen"]).optional(), steps: z.array(z.object({command: z.enum(["type", "click", "screenshot", "navigate", "sleep"]), selector: z.string().optional(), value: z.string().optional()})).optional(), tags: z.array(z.object({name: z.string(), readOnly: z.boolean()})).optional(), url: z.string().optional(), urlExternal: z.string().optional(), urlType: z.enum(["single", "multiple", "external"]).optional(), urls: z.array(z.string()).optional(), waitSecond: z.number().int().optional(), maxWait: z.number().int().optional(), hidePopups: z.number().int().optional(), dark: z.number().int().optional(), strictSSL: z.number().int().optional(), width: z.number().int().optional(), zoom: z.number().int().optional()}), schedule: z.object({daysOfMonth: z.array(z.number().int()).optional(), daysofWeek: z.array(z.number().int()).optional(), exactDates: z.array(z.string()).optional(), timesOfDay: z.array(z.string()).optional()}), trackers: z.array(z.unknown()).optional()}); export const captureHistory = z.object({pageCount: z.number().int().optional(), pageIndex: z.number().int().optional(), history: z.array(z.object({id: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), scheduledCaptureId: z.string().optional(), status: z.enum(["SUCCESS", "ERROR", "INPROGRESS"]).optional(), errorReason: z.string().optional(), scheduledCapture: scheduledCapture.optional(), fileId: z.string().optional(), thumbnailFileId_236: z.string().optional()})).optional(), details: z.object({apps: z.array(z.object({id: z.string().optional(), path: z.string().optional(), appName: z.string().optional(), appType: z.enum(["S3", "Azure", "GDrive", "Dropbox", "RSS", "Slack", "FTP", "Webhook", "SFTP"]).optional(), error: z.string().optional(), success: z.boolean().optional(), imageUrl: z.string().optional(), url: z.string().optional()})).optional()}).optional(), trackers: z.array(z.unknown()).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 your entire history, page by page * * GET /capturehistory * * Also accepts auth: key */ export const getApiCapturehistory = async (args: GetApiCapturehistoryArgs, options?: GetApiCapturehistoryOptions) => { const {baseUrl = "https://blitapp.com/api", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.captureId != null) query.append("captureId", String(args.captureId)) if (args.pageIndex != null) query.append("pageIndex", String(args.pageIndex)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.tags != null) query.append("tags", String(args.tags)) if (apiKey !== undefined) headers.set("Api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/capturehistory${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(captureHistory, await res.json()) };