| 1 |
import { getImagesShape } from '@auto-launch/fetchers/shape'; |
| 2 |
import { |
| 3 |
failWithFallback, |
| 4 |
fetchWithTimeout, |
| 5 |
retryTwice, |
| 6 |
setStatus, |
| 7 |
} from '@auto-launch/functions/helpers'; |
| 8 |
import { IMAGES_HOST } from '@constants'; |
| 9 |
import { reqDataBasics } from '@shared/lib/data'; |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
|
| 12 |
const fallback = { siteImages: [] }; |
| 13 |
const url = `${IMAGES_HOST}/api/search`; |
| 14 |
const method = 'POST'; |
| 15 |
const headers = { 'Content-Type': 'application/json' }; |
| 16 |
|
| 17 |
export const handleSiteImages = async ({ siteProfile }) => { |
| 18 |
// translators: this is for a action log UI. Keep it short |
| 19 |
setStatus(__('Finding the perfect images', 'extendify-local')); |
| 20 |
|
| 21 |
const body = JSON.stringify({ |
| 22 |
...reqDataBasics, |
| 23 |
siteProfile, |
| 24 |
source: 'auto-launch', |
| 25 |
}); |
| 26 |
|
| 27 |
const response = await retryTwice(() => |
| 28 |
fetchWithTimeout(url, { method, headers, body }), |
| 29 |
); |
| 30 |
|
| 31 |
if (!response?.ok) return fallback; |
| 32 |
|
| 33 |
return failWithFallback( |
| 34 |
async () => getImagesShape.parse(await response.json()), |
| 35 |
fallback, |
| 36 |
{ caller: 'handleSiteImages' }, |
| 37 |
); |
| 38 |
}; |
| 39 |
|