import {z} from 'zod' export type PatchApiUploadsIdArgs = {id: string}; export type PatchApiUploadsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const uploadTargetAsset = z.union([z.object({type: z.literal("newAsset"), assetId: z.string().optional()}), z.object({type: z.literal("newVersion"), assetId: z.string(), versionId: z.string().optional()}), z.object({type: z.literal("existingAsset"), assetId: z.string(), fillsPlaceholder: z.boolean().optional()})]); export const uploadAssetItem = z.object({id: z.string(), uploadRequestId: z.string(), error: z.object({code: z.union([z.literal("UPLOAD_FILE_FAILED"), z.literal("CLOSE_FILE_FAILED"), z.literal("GENERATE_KEYFRAMES_FAILED")]), message: z.string()}).optional(), state: z.object({id: z.string(), fileInfo: z.object({targetPath: z.string(), size: z.number(), mimeType: z.string()}), target: uploadTargetAsset, targetAsset: uploadTargetAsset, uploadKind: z.union([z.object({kind: z.literal("file"), formatId: z.string().optional(), fileSetId: z.string().optional(), fileId: z.string().optional()}), z.object({kind: z.literal("proxy"), proxyId: z.string().optional()})]), formatId: z.string().optional(), fileSetId: z.string().optional(), fileId: z.string().optional(), status: z.enum(["WAITING", "UPLOADING", "COMPLETED", "COMPLETING", "PAUSED", "FAILED"]), chunks: z.array(z.object({index: z.number(), status: z.enum(["WAITING", "UPLOADING", "COMPLETED", "FAILED"]), start: z.number(), size: z.number(), uploaded: z.number()})).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 }; /** * Patch an upload. Currently only supports updating the status. * * PATCH /uploads/{id} */ export const patchApiUploadsId = async (args: PatchApiUploadsIdArgs, options?: PatchApiUploadsIdOptions) => { 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(/\/+$/, '')}/uploads/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(uploadAssetItem, await res.json()) };