import type { DeepPartial } from '../core'; import { ImageRef } from '../core'; import type { ImageInput, ResultWrapper, SerializedImageInput } from './customTypes'; import { createSBError } from './errors'; /** * @internal * @hidden */ export function mapRTUUIResult( result: ResultWrapper>, LClass: new (source: DeepPartial) => T ): ResultWrapper { if (result.status === 'OK') { return { ...result, data: new LClass(result.data), }; } else { return result; } } /** * @internal * @hidden */ export function handleImageInput(image: ImageInput): SerializedImageInput; export function handleImageInput(image: ImageInput[]): SerializedImageInput[]; export function handleImageInput( image: ImageInput | ImageInput[] ): SerializedImageInput | SerializedImageInput[] { const handleInput = (input: ImageInput): SerializedImageInput => { if (input instanceof ImageRef) { return input.uniqueId ? { uniqueId: input.uniqueId } : { buffer: input.buffer }; } else { return { imageFileUri: input }; } }; return Array.isArray(image) ? image.map(handleInput) : handleInput(image); } /** * @internal * @hidden */ export async function withSBErrorHandling(fn: () => Promise): Promise { try { return await fn(); } catch (error: any) { throw createSBError(error); } }