import {z} from 'zod' export type GetApiV1Fabric10JobIdArgs = {job_id: string, 'X-Veed-Store-IO'?: ('0' | '1') | undefined, 'X-Veed-Media-Expiration-Seconds'?: number | undefined}; export type GetApiV1Fabric10JobIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jobErrorDetail = z.object({field: z.string().optional(), message: z.string(), type: z.string()}); export const jobErrorFabricErrorCode = z.object({code: z.enum(["input_validation", "content_moderation", "invalid_file", "audio_too_long", "transload_failed", "generation_failed", "timeout", "insufficient_credits"]), details: z.array(jobErrorDetail).nullable().optional(), message: z.string()}); export const file = z.object({content_type: z.string().optional(), file_name: z.string().optional(), file_size: z.number().int().optional(), url: z.string()}); export const fabricVideo = z.object({video: file}); export const fabricJob = z.object({credits_charged: z.number().int().optional(), credits_estimated: z.number().int().optional(), error: jobErrorFabricErrorCode.optional(), job_id: z.string(), result: fabricVideo.optional(), status: z.enum(["PROCESSING", "COMPLETED", "FAILED", "CANCELLED"])}); export const resourceFabricJob = z.object({$schema: z.string().optional(), data: fabricJob}); 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 fabric-1.0 job * * GET /v1/fabric-1.0/{job_id} */ export const getApiV1Fabric10JobId = async (args: GetApiV1Fabric10JobIdArgs, options?: GetApiV1Fabric10JobIdOptions) => { const {baseUrl = "https://api.veed.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Veed-Store-IO'] != null) headers.set("X-Veed-Store-IO", String(args['X-Veed-Store-IO'])) if (args['X-Veed-Media-Expiration-Seconds'] != null) headers.set("X-Veed-Media-Expiration-Seconds", String(args['X-Veed-Media-Expiration-Seconds'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fabric-1.0/${encodeURIComponent(String(args.job_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(resourceFabricJob, await res.json()) };