import {z} from 'zod' export type ResolutionType = {height?: number | undefined, width?: number | undefined}; export type ProxyCreateSchema = {asset_id?: string | undefined, audio_bitrate?: number | undefined, audio_channels?: (number | null) | undefined, bit_rate?: number | undefined, codec?: string | undefined, content_type?: string | undefined, date_created?: string | undefined, date_modified?: string | undefined, filename?: string | undefined, format?: string | undefined, frame_rate?: (string | null) | undefined, id?: string | undefined, is_drop_frame?: (boolean | null) | undefined, is_multichannel?: (boolean | null) | undefined, is_public?: boolean | undefined, multipart_upload_url?: string | undefined, name?: string | undefined, proxy_container_id?: (string | null) | undefined, resolution?: ResolutionType | undefined, rotation?: (number | null) | undefined, size?: (number | null) | undefined, start_time_code?: (string | null) | undefined, status?: ('OPEN' | 'GROWING' | 'AWAITED' | 'CLOSED' | 'FAILED' | 'ARCHIVED' | 'MISSING' | 'REDISCOVERED' | 'DELETED') | undefined, storage_id?: string | undefined, storage_method?: string | undefined, upload_credentials?: {[key: string]: unknown} | undefined, upload_headers?: {[key: string]: unknown} | undefined, upload_method?: string | undefined, upload_url?: string | undefined, url?: string | undefined, version_id?: (string | null) | undefined}; export type CreateApiV1AssetsAssetIdMethodStorageMethodProxiesArgs = {'App-ID': string, 'Auth-Token': string, asset_id: string, storage_method: string, body: ProxyCreateSchema}; export type CreateApiV1AssetsAssetIdMethodStorageMethodProxiesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const resolutionType = z.object({height: z.number().int().gte(-2147483648).lte(2147483647).optional(), width: z.number().int().gte(-2147483648).lte(2147483647).optional()}); export const proxyCreateSchema = z.object({asset_id: z.string().optional(), audio_bitrate: z.number().int().gte(-2147483648).lte(2147483647).optional(), audio_channels: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), bit_rate: z.number().int().gte(-2147483648).lte(2147483647).optional(), codec: z.string().optional(), content_type: z.string().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), filename: z.string().optional(), format: z.string().optional(), frame_rate: z.string().nullable().optional(), id: z.string().optional(), is_drop_frame: z.boolean().nullable().optional(), is_multichannel: z.boolean().nullable().optional(), is_public: z.boolean().optional(), multipart_upload_url: z.string().optional(), name: z.string().optional(), proxy_container_id: z.string().nullable().optional(), resolution: resolutionType.optional(), rotation: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), size: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).nullable().optional(), start_time_code: z.string().nullable().optional(), status: z.enum(["OPEN", "GROWING", "AWAITED", "CLOSED", "FAILED", "ARCHIVED", "MISSING", "REDISCOVERED", "DELETED"]).optional(), storage_id: z.string().optional(), storage_method: z.string().optional(), upload_credentials: z.record(z.string(), z.unknown()).optional(), upload_headers: z.record(z.string(), z.unknown()).optional(), upload_method: z.string().optional(), upload_url: z.string().optional(), url: z.string().optional(), version_id: z.string().nullable().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 }; /** * Create proxy and associate to asset * * POST /v1/assets/{asset_id}/method/{storage_method}/proxies/ */ export const createApiV1AssetsAssetIdMethodStorageMethodProxies = async (args: CreateApiV1AssetsAssetIdMethodStorageMethodProxiesArgs, options: CreateApiV1AssetsAssetIdMethodStorageMethodProxiesOptions) => { const {baseUrl, 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['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets/${encodeURIComponent(String(args.asset_id))}/method/${encodeURIComponent(String(args.storage_method))}/proxies/${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(proxyCreateSchema, await res.json()) };