import {z} from 'zod' export type DownloadRequestBody = {assets?: (Array<{assetId: string, format: string, outputPath?: string | undefined}>) | undefined, collections?: Array<{collectionId: string, format: string}> | undefined, playlists?: Array<{playlistId: string, format: string}> | undefined, fileSets?: Array<{fileSetId: string, assetId: string}> | undefined, assetVersions?: Array<{versionId: string, assetId: string, format: string}> | undefined, files?: (Array<{assetId: string, fileId: string, outputPath?: string | undefined}>) | undefined}; export type CreateApiDownloadRequestsArgs = {open_in_fcp?: string | undefined, resolve_on_local_isg?: string | undefined, 'app-id'?: string | undefined, 'iconik-host'?: string | undefined, 'auth-token'?: string | undefined, 'share-auth-token'?: string | undefined, 'share-hash'?: string | undefined, body: DownloadRequestBody}; export type CreateApiDownloadRequestsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 downloadCollectionItemIdentity = z.object({type: z.literal("collection"), id: z.string(), format: z.string()}); export const downloadPlaylistItemIdentity = z.object({type: z.literal("playlist"), id: z.string(), format: z.string()}); export const downloadRequestIdentity = z.union([downloadAssetItemIdentity, downloadAssetFileSetItemIdentitySchema, downloadAssetVersionItemIdentity, downloadAssetFileItemIdentity, downloadCollectionItemIdentity, downloadPlaylistItemIdentity]); export const downloadRequest = z.object({id: z.string(), title: z.string(), objectIdentity: downloadRequestIdentity}); export const addDownloadRequestResponse = z.array(downloadRequest); 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 }; /** * Initiate a download * * POST /download-requests */ export const createApiDownloadRequests = async (args: CreateApiDownloadRequestsArgs, options?: CreateApiDownloadRequestsOptions) => { const {baseUrl = "http://localhost:6286", 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.open_in_fcp != null) query.append("open_in_fcp", String(args.open_in_fcp)) if (args.resolve_on_local_isg != null) query.append("resolve_on_local_isg", String(args.resolve_on_local_isg)) if (args['app-id'] != null) headers.set("app-id", String(args['app-id'])) if (args['iconik-host'] != null) headers.set("iconik-host", String(args['iconik-host'])) if (args['auth-token'] != null) headers.set("auth-token", String(args['auth-token'])) if (args['share-auth-token'] != null) headers.set("share-auth-token", String(args['share-auth-token'])) if (args['share-hash'] != null) headers.set("share-hash", String(args['share-hash'])) const url = `${baseUrl.replace(/\/+$/, '')}/download-requests${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(addDownloadRequestResponse, await res.json()) };