import {z} from 'zod' export type MoveToFolderRequest = {data?: ({type?: string | undefined, relationships?: ({sources?: ({data?: (Array<{type?: string | undefined, id?: string | undefined}>) | undefined}) | undefined}) | undefined}) | undefined, meta?: ({file_conflict_resolution?: ('skip' | 'overwrite' | 'rename') | undefined, allow_archive_move?: boolean | undefined}) | undefined}; export type CreateApiDamFoldersFolderIdMoveToArgs = {folder_id: string, body: MoveToFolderRequest}; export type CreateApiDamFoldersFolderIdMoveToOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const moveToFolderResponse = z.object({meta: z.object({state: z.string().optional(), summary: z.object({moved_folder_count: z.number().int().optional(), moved_file_count: z.number().int().optional()}).optional()}).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 }; /** * Move files or folders to a target folder * * POST /dam/folders/{folder_id}/move-to */ export const createApiDamFoldersFolderIdMoveTo = async (args: CreateApiDamFoldersFolderIdMoveToArgs, options: CreateApiDamFoldersFolderIdMoveToOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/dam/folders/${encodeURIComponent(String(args.folder_id))}/move-to${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(moveToFolderResponse, await res.json()) };