import {z} from 'zod' export type AccountId = string; export type FolderPath = string; export type FileMetadata = {[key: string]: unknown}; export type CopyFileMetadata = {merge?: boolean | undefined, value?: FileMetadata | undefined}; export type FileTagName = string; export type CopyFileTags = {merge?: boolean | undefined, value?: Array | undefined}; export type CopyFolderRequest = {copyFiles?: boolean | undefined, copyOverriddenStorage?: boolean | undefined, copyVirtualFolders?: boolean | undefined, destination: FolderPath, metadata?: CopyFileMetadata | undefined, recursive?: boolean | undefined, source: FolderPath, tags?: CopyFileTags | undefined}; export type CopyFolderBatchRequest = {folders: Array}; export type CreateApiV2AccountsAccountIdFoldersCopyBatchArgs = {accountId: AccountId, body: CopyFolderBatchRequest}; export type CreateApiV2AccountsAccountIdFoldersCopyBatchOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const accountJobId = z.string().min(26).max(26); export const accountJobType = z.enum(["ProcessImageJob", "ProcessVideoJob", "ProcessAudioJob", "AntivirusJob", "ResetCacheJob", "DeleteFolderBatchJob", "DeleteFileBatchJob", "CopyFolderBatchJob", "CopyFileBatchJob"]); export const jobUrl = z.string().min(1).max(255); export const asyncResponse = z.object({jobDocs: z.literal("https://www.bytescale.com/docs/job-api/GetJob"), jobId: accountJobId, jobType: accountJobType, jobUrl: jobUrl}); 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 }; /** * Copies multiple folders asynchronously. * * You can use ListFolder to preview the operation using the `dryRun` parameter. * * POST /v2/accounts/{accountId}/folders/copy/batch */ export const createApiV2AccountsAccountIdFoldersCopyBatch = async (args: CreateApiV2AccountsAccountIdFoldersCopyBatchArgs, options?: CreateApiV2AccountsAccountIdFoldersCopyBatchOptions) => { const {baseUrl = "https://api.bytescale.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.accountId))}/folders/copy/batch${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(asyncResponse, await res.json()) };