import {z} from 'zod' export type GetApiV1StoragesStorageIdFilesArgs = {'App-ID': string, 'Auth-Token': string, storage_id: string, path?: string | undefined, path_separator?: string | undefined, directory_path?: string | undefined, checksum?: string | undefined, per_page?: number | undefined, page?: number | undefined, scroll?: boolean | undefined, scroll_id?: string | undefined, sort?: string | undefined, id?: string | undefined, name?: string | undefined, type?: string | undefined, status?: string | undefined, date_created?: string | undefined, date_modified?: string | undefined, original_name?: string | undefined, size?: string | undefined, asset_id?: string | undefined, format_id?: string | undefined, include_parent_statuses?: boolean | undefined}; export type GetApiV1StoragesStorageIdFilesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fileElasticSchema = z.object({asset_id: z.string().optional(), checksum: z.string().nullable().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), directory_path: z.string(), file_date_created: z.string().optional(), file_date_modified: z.string().optional(), file_set_id: z.string().optional(), file_set_status: z.string().nullable().optional(), format_id: z.string().optional(), format_status: z.string().nullable().optional(), id: z.string().optional(), name: z.string().optional(), original_name: z.string(), parent_id: z.string().nullable().optional(), size: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).optional(), status: z.enum(["OPEN", "GROWING", "AWAITED", "CLOSED", "FAILED", "ARCHIVED", "MISSING", "REDISCOVERED", "DELETED"]).optional(), storage_id: z.string().optional(), template: z.string().optional(), template_engine: z.enum(["SIMPLE", "JINJA2", "null"]).nullable().optional(), type: z.enum(["FILE", "DIRECTORY", "SYMLINK", "SEQUENCE"]), user_id: z.string().nullable().optional(), version_id: z.string().optional()}); export const filesElasticSchema = z.object({facets: z.record(z.string(), z.unknown()).optional(), first_url: z.string().optional(), last_url: z.string().optional(), next_url: z.string().optional(), objects: z.array(fileElasticSchema).optional(), page: z.number().int().gte(-2147483648).lte(2147483647).optional(), pages: z.number().int().gte(-2147483648).lte(2147483647).optional(), per_page: z.number().int().gte(-2147483648).lte(2147483647).optional(), prev_url: z.string().optional(), scroll_id: z.string().optional(), total: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).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 }; /** * Get files in a storage folder, or all files on a storage * * GET /v1/storages/{storage_id}/files/ */ export const getApiV1StoragesStorageIdFiles = async (args: GetApiV1StoragesStorageIdFilesArgs, options: GetApiV1StoragesStorageIdFilesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) if (args.path != null) query.append("path", String(args.path)) if (args.path_separator != null) query.append("path_separator", String(args.path_separator)) if (args.directory_path != null) query.append("directory_path", String(args.directory_path)) if (args.checksum != null) query.append("checksum", String(args.checksum)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (args.scroll != null) query.append("scroll", String(args.scroll)) if (args.scroll_id != null) query.append("scroll_id", String(args.scroll_id)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.id != null) query.append("id", String(args.id)) if (args.name != null) query.append("name", String(args.name)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.date_created != null) query.append("date_created", String(args.date_created)) if (args.date_modified != null) query.append("date_modified", String(args.date_modified)) if (args.original_name != null) query.append("original_name", String(args.original_name)) if (args.size != null) query.append("size", String(args.size)) if (args.asset_id != null) query.append("asset_id", String(args.asset_id)) if (args.format_id != null) query.append("format_id", String(args.format_id)) if (args.include_parent_statuses != null) query.append("include_parent_statuses", String(args.include_parent_statuses)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/storages/${encodeURIComponent(String(args.storage_id))}/files/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(filesElasticSchema, await res.json()) };