| 163 | } |
| 164 | |
| 165 | private parseImage(img: unknown): GeneratedImage { |
| 166 | let url: string |
| 167 | if (typeof img === 'string') { |
| 168 | url = img |
| 169 | } else if ( |
| 170 | img && |
| 171 | typeof img === 'object' && |
| 172 | 'url' in img && |
| 173 | typeof img.url === 'string' |
| 174 | ) { |
| 175 | url = (img as { url: string }).url |
| 176 | } else { |
| 177 | throw new Error( |
| 178 | `Invalid image payload from fal response: expected string or { url: string }, received ${ |
| 179 | img === null ? 'null' : typeof img |
| 180 | }`, |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | if (url.startsWith('data:')) { |
| 185 | const base64Match = url.match(/^data:image\/[^;]+;base64,(.+)$/) |
| 186 | if (base64Match && base64Match[1]) { |
| 187 | return { b64Json: base64Match[1] } |
| 188 | } |
| 189 | } |
| 190 | return { url } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | export function createFalImage<TModel extends FalModel>( |