| 1 |
import { getLaunchDecisionsShape } from '@auto-launch/fetchers/shape'; |
| 2 |
import { |
| 3 |
failWithFallback, |
| 4 |
fetchWithTimeout, |
| 5 |
retryTwice, |
| 6 |
} from '@auto-launch/functions/helpers'; |
| 7 |
import { AI_HOST } from '@constants'; |
| 8 |
import { reqDataBasics } from '@shared/lib/data'; |
| 9 |
|
| 10 |
const fallback = { |
| 11 |
launchDecisions: { navExtras: 'none', navButtonLabel: '' }, |
| 12 |
}; |
| 13 |
const url = `${AI_HOST}/api/launch-decisions`; |
| 14 |
const method = 'POST'; |
| 15 |
const headers = { 'Content-Type': 'application/json' }; |
| 16 |
|
| 17 |
export const handleLaunchDecisions = async ({ siteProfile }) => { |
| 18 |
const body = JSON.stringify({ ...reqDataBasics, siteProfile }); |
| 19 |
|
| 20 |
const response = await retryTwice(() => |
| 21 |
fetchWithTimeout(url, { method, headers, body }), |
| 22 |
); |
| 23 |
|
| 24 |
if (!response?.ok) return fallback; |
| 25 |
|
| 26 |
return failWithFallback( |
| 27 |
async () => ({ |
| 28 |
launchDecisions: getLaunchDecisionsShape.parse(await response.json()), |
| 29 |
}), |
| 30 |
fallback, |
| 31 |
); |
| 32 |
}; |
| 33 |
|