import {z} from 'zod' export type GetApiDownloadsIdArgs = {id: string}; export type GetApiDownloadsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const downloadFileItem = z.object({fileId: z.string(), fileName: z.string(), fullPath: z.string(), downloaded: z.number(), size: z.number().optional()}); export const downloadItemStatus = z.union([z.object({type: z.literal("enqueued")}), z.object({type: z.literal("resolving")}), z.object({type: z.literal("active")}), z.object({type: z.literal("paused")}), z.object({type: z.literal("completed")}), z.object({type: z.literal("failed"), code: z.string(), message: z.string()})]); export const downloadAssetItemIdentity = z.object({type: z.literal("asset"), id: z.string(), formatId: z.string().optional(), outputPath: z.string().optional()}); export const downloadAssetFileSetItemIdentitySchema = z.object({type: z.literal("fileSet"), id: z.string(), assetId: z.string()}); export const downloadAssetVersionItemIdentity = z.object({type: z.literal("assetVersion"), versionId: z.string(), assetId: z.string(), formatId: z.string().optional()}); export const downloadAssetFileItemIdentity = z.object({type: z.literal("assetFile"), assetId: z.string(), id: z.string(), outputPath: z.string().optional()}); export const downloadItemIdentity = z.union([downloadAssetItemIdentity, downloadAssetFileSetItemIdentitySchema, downloadAssetVersionItemIdentity, downloadAssetFileItemIdentity]); export const downloadItem = z.object({id: z.string(), requestItemId: z.string(), files: z.array(downloadFileItem), status: downloadItemStatus, objectIdentity: downloadItemIdentity}); 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 a download by id * * GET /downloads/{id} */ export const getApiDownloadsId = async (args: GetApiDownloadsIdArgs, options?: GetApiDownloadsIdOptions) => { const {baseUrl = "http://localhost:6286", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/downloads/${encodeURIComponent(String(args.id))}${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(downloadItem, await res.json()) };