import {z} from 'zod' export type CreateApiResourcesMoveArgs = {'ev-api-key': string, 'ev-access-token': string, body: {resources: Array, parentResource: string}}; export type CreateApiResourcesMoveOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceAttributes = z.object({hash: z.string().optional(), name: z.string().optional(), extension: z.string().optional(), type: z.enum(["file", "dir"]).optional(), createdBy: z.string().optional(), uploadDate: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), accessedAt: z.string().optional(), createdTime: z.number().int().optional(), updatedTime: z.number().int().optional(), accessedTime: z.number().int().optional(), path: z.string().optional(), size: z.number().int().optional(), fileCount: z.number().int().optional(), previewable: z.boolean().optional()}); export const resource = z.object({id: z.number().int().optional(), type: z.literal("resource").optional(), attributes: resourceAttributes.optional(), relationships: z.object({share: z.object({data: z.object({id: z.number().int().optional(), type: z.string().optional()}).optional()}).optional(), notifications: z.array(z.object({data: z.object({id: z.number().int().optional(), type: z.string().optional()}).optional()})).optional(), directFile: z.object({data: z.object({id: z.number().int().optional(), type: z.string().optional()}).optional()}).optional(), parentResource: z.object({data: z.object({id: z.number().int().optional(), type: z.string().optional()}).optional()}).optional()}).optional()}); export const resourceCopyMove = z.object({data: resource.optional(), meta: z.object({}).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 resources * * POST /resources/move */ export const createApiResourcesMove = async (args: CreateApiResourcesMoveArgs, options?: CreateApiResourcesMoveOptions) => { const {baseUrl = "https://accountname.exavault.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['ev-api-key'] != null) headers.set("ev-api-key", String(args['ev-api-key'])) if (args['ev-access-token'] != null) headers.set("ev-access-token", String(args['ev-access-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/resources/move${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(resourceCopyMove, await res.json()) };