import {z} from 'zod' export type FilesCreateFileRestInputV1FileType = 'browser_download' | 'screenshot' | 'recording' | 'customer_upload'; export type CreateApiV1FilesArgs = {body: {fileName: string, fileType?: FilesCreateFileRestInputV1FileType | undefined, id?: string | undefined, sessionIds?: Array | undefined}}; export type CreateApiV1FilesOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const createFileV1FileType = z.enum(["browser_download", "screenshot", "recording", "customer_upload"]); export const createFileV1Status = z.enum(["available", "uploading", "upload_failed", "deleted"]); export const createFileV1 = z.object({fileBytes: z.number().int(), fileName: z.string(), fileType: createFileV1FileType, id: z.string(), orgId: z.string(), sessionIds: z.array(z.string()).optional(), status: createFileV1Status, uploadStartTime: z.string(), uploadUrl: 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 envelopeCreateFileV1EnvelopeDefaultMeta = z.object({data: createFileV1, 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 }; /** * Create a file * * POST /v1/files */ export const createApiV1Files = async (args: CreateApiV1FilesArgs, options?: CreateApiV1FilesOptions) => { const {baseUrl = "https://api.airtop.ai/api", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/files${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(envelopeCreateFileV1EnvelopeDefaultMeta, await res.json()) };