import {z} from 'zod' export type FileUploadWriteMode = 'overwrite' | 'conditional'; export type UpdateApiFilesystemsFilesystemIdFileUploadArgs = {filesystem_id: string, path: string, writeMode?: FileUploadWriteMode | undefined, baseVersion?: (string | null) | undefined, expectedVersion?: (string | null) | undefined, createOnly?: (string | null) | undefined, digest?: (string | null) | undefined, 'Idempotency-Key'?: (string | null) | undefined, body: BodyInit}; export type UpdateApiFilesystemsFilesystemIdFileUploadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fileUploadWriteMode = z.enum(["overwrite", "conditional"]); export const singleFileUploadResponse = z.object({archive_version: z.string(), created: z.boolean(), file_version: z.string(), filesystem_id: z.string(), path: z.string(), size_bytes: z.number().int(), write_mode: fileUploadWriteMode}); 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 }; /** * Upload File * * PUT /filesystems/{filesystem_id}/file-upload */ export const updateApiFilesystemsFilesystemIdFileUpload = async (args: UpdateApiFilesystemsFilesystemIdFileUploadArgs, options: UpdateApiFilesystemsFilesystemIdFileUploadOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/octet-stream") headers.set('accept', "application/json") if (args.path != null) query.append("path", String(args.path)) if (args.writeMode != null) query.append("writeMode", String(args.writeMode)) if (args.baseVersion != null) query.append("baseVersion", String(args.baseVersion)) if (args.expectedVersion != null) query.append("expectedVersion", String(args.expectedVersion)) if (args.createOnly != null) query.append("createOnly", String(args.createOnly)) if (args.digest != null) query.append("digest", String(args.digest)) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/filesystems/${encodeURIComponent(String(args.filesystem_id))}/file-upload${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(singleFileUploadResponse, await res.json()) };