| 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 { digest } from '@shared/api/digest'; |
| 9 |
import { reqDataBasics } from '@shared/lib/data'; |
| 10 |
|
| 11 |
const fallback = { |
| 12 |
launchDecisions: { navExtras: 'none', navButtonLabel: '' }, |
| 13 |
}; |
| 14 |
const url = `${AI_HOST}/api/launch-decisions`; |
| 15 |
const method = 'POST'; |
| 16 |
const headers = { 'Content-Type': 'application/json' }; |
| 17 |
|
| 18 |
export const handleLaunchDecisions = async ({ siteProfile }) => { |
| 19 |
const body = JSON.stringify({ ...reqDataBasics, siteProfile }); |
| 20 |
|
| 21 |
const response = await retryTwice(() => |
| 22 |
fetchWithTimeout(url, { method, headers, body }), |
| 23 |
).catch((error) => { |
| 24 |
return { ok: false, statusText: error.message, status: 0 }; |
| 25 |
}); |
| 26 |
|
| 27 |
if (!response?.ok) { |
| 28 |
digest({ |
| 29 |
error: { |
| 30 |
message: response.statusText, |
| 31 |
name: 'FetchError', |
| 32 |
status: response.status, |
| 33 |
}, |
| 34 |
details: { source: 'auto-launch', caller: 'handleLaunchDecisions' }, |
| 35 |
}); |
| 36 |
return fallback; |
| 37 |
} |
| 38 |
|
| 39 |
return failWithFallback( |
| 40 |
async () => ({ |
| 41 |
launchDecisions: getLaunchDecisionsShape.parse(await response.json()), |
| 42 |
}), |
| 43 |
fallback, |
| 44 |
); |
| 45 |
}; |
| 46 |
|