import {z} from 'zod' export type GetApiV1FilesIdArgs = {id: string}; export type GetApiV1FilesIdOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const getFileV1FileType = z.enum(["browser_download", "screenshot", "recording", "customer_upload"]); export const getFileV1Status = z.enum(["available", "uploading", "upload_failed", "deleted"]); export const getFileV1 = z.object({downloadUrl: z.string(), expiryTime: z.string().optional(), fileBytes: z.number().int(), fileName: z.string(), fileType: getFileV1FileType, id: z.string(), orgId: z.string(), sessionIds: z.array(z.string()).optional(), status: getFileV1Status, uploadStartTime: z.string()}); export const issue = z.object({code: z.string().optional(), details: z.record(z.string(), z.unknown()).optional(), message: z.string(), reason: z.string().optional()}); export const envelopeDefaultMeta = z.object({requestId: z.string().optional()}); export const envelopeGetFileV1EnvelopeDefaultMeta = z.object({data: getFileV1, errors: z.array(issue).optional(), meta: envelopeDefaultMeta, warnings: z.array(issue).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 a file * * GET /v1/files/{id} */ export const getApiV1FilesId = async (args: GetApiV1FilesIdArgs, options?: GetApiV1FilesIdOptions) => { const {baseUrl = "https://api.airtop.ai/api", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/files/${encodeURIComponent(String(args.id))}${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(envelopeGetFileV1EnvelopeDefaultMeta, await res.json()) };