| 1 |
import { getStringsShape } from '@auto-launch/fetchers/shape'; |
| 2 |
import { |
| 3 |
failWithFallback, |
| 4 |
fetchWithTimeout, |
| 5 |
retryTwice, |
| 6 |
setStatus, |
| 7 |
} from '@auto-launch/functions/helpers'; |
| 8 |
import { launchStrings } from '@auto-launch/strings'; |
| 9 |
import { AI_HOST } from '@constants'; |
| 10 |
import { reqDataBasics } from '@shared/lib/data'; |
| 11 |
|
| 12 |
const fallback = { aiHeaders: [], aiBlogTitles: [], heroDescription: '' }; |
| 13 |
const url = `${AI_HOST}/api/site-strings`; |
| 14 |
const method = 'POST'; |
| 15 |
const headers = { 'Content-Type': 'application/json' }; |
| 16 |
|
| 17 |
export const handleSiteStrings = async ({ siteProfile }) => { |
| 18 |
// translators: this is for a action log UI. Keep it short |
| 19 |
setStatus(launchStrings().statusIdeas); |
| 20 |
|
| 21 |
const body = JSON.stringify({ ...reqDataBasics, siteProfile }); |
| 22 |
|
| 23 |
const response = await retryTwice(() => |
| 24 |
fetchWithTimeout(url, { method, headers, body }), |
| 25 |
); |
| 26 |
|
| 27 |
if (!response?.ok) return fallback; |
| 28 |
|
| 29 |
return failWithFallback( |
| 30 |
async () => getStringsShape.parse(await response.json()), |
| 31 |
fallback, |
| 32 |
{ caller: 'handleSiteStrings' }, |
| 33 |
); |
| 34 |
}; |
| 35 |
|