| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | import { useLaunchDataStore } from '@auto-launch/state/launch-data'; |
| 2 | 2 | import { INSIGHTS_HOST } from '@constants'; |
| 3 | 3 | import { reqDataBasics } from '@shared/lib/data'; |
| 4 | +import apiFetch from '@wordpress/api-fetch'; | |
| 4 | 5 | |
| 5 | 6 | const headers = { |
| 6 | 7 | 'Content-type': 'application/json', |
| 7 | 8 | Accept: 'application/json', |
| @@ -7,11 +8,12 @@ | ||
| 7 | 8 | Accept: 'application/json', |
| 8 | 9 | 'X-Extendify': 'true', |
| 9 | 10 | }; |
| 10 | 11 | |
| 11 | -const { urlParams } = window.extLaunchData; | |
| 12 | +const { urlParams, activeTests } = window.extLaunchData; | |
| 12 | 13 | export const checkIn = ({ |
| 13 | 14 | stage, |
| 15 | + description, | |
| 14 | 16 | siteProfile = {}, |
| 15 | 17 | sitePlugins = [], |
| 16 | 18 | siteStyle = {}, |
| 17 | 19 | } = {}) => { |
| @@ -22,9 +24,13 @@ | ||
| 22 | 24 | const payload = JSON.stringify({ |
| 23 | 25 | ...reqDataBasics, |
| 24 | 26 | autoLaunch: true, |
| 25 | 27 | stage, |
| 28 | + description, | |
| 26 | 29 | attempt, |
| 30 | + activeTests: Object.keys(activeTests ?? {}).length | |
| 31 | + ? JSON.stringify(activeTests) | |
| 32 | + : undefined, | |
| 27 | 33 | skippedDescription: Boolean(urlParams?.title || urlParams?.description), |
| 28 | 34 | insightsId: siteId, |
| 29 | 35 | hostpartner: partnerId, |
| 30 | 36 | siteURL: homeUrl, |
| @@ -58,7 +64,42 @@ | ||
| 58 | 64 | return fetch(`${INSIGHTS_HOST}/api/v1/launch`, { |
| 59 | 65 | method: 'POST', |
| 60 | 66 | headers, |
| 61 | 67 | body: payload, |
| 68 | + keepalive: true, | |
| 69 | + }); | |
| 70 | +}; | |
| 71 | + | |
| 72 | +const probeFailureReason = async () => { | |
| 73 | + try { | |
| 74 | + // parse:false so we get the raw Response and can read the HTTP status — the | |
| 75 | + // default JSON parse throws `invalid_json` on a blocked API's HTML error page. | |
| 76 | + await apiFetch({ path: '/extendify/v1/shared/ping', parse: false }); | |
| 77 | + return null; | |
| 78 | + } catch (error) { | |
| 79 | + if (error?.status === 403) return '403'; | |
| 80 | + if (error?.status === 404) return '404'; | |
| 81 | + // Any other status means the request reached the REST API and errored | |
| 82 | + // upstream (our ping only ever 200s) — reachable, so not "unreachable". | |
| 83 | + if (error?.status) return null; | |
| 84 | + return 'network'; | |
| 85 | + } | |
| 86 | +}; | |
| 87 | + | |
| 88 | +export const reportRestApiStatus = async () => { | |
| 89 | + const reason = await probeFailureReason(); | |
| 90 | + if (!reason) return; | |
| 91 | + const { siteId, partnerId, homeUrl, siteCreatedAt } = reqDataBasics; | |
| 92 | + return fetch(`${INSIGHTS_HOST}/api/v1/event`, { | |
| 93 | + method: 'POST', | |
| 94 | + headers, | |
| 95 | + body: JSON.stringify({ | |
| 96 | + insightsId: siteId, | |
| 97 | + key: 'rest_api_unreachable', | |
| 98 | + payload: { reason }, | |
| 99 | + partnerId, | |
| 100 | + siteURL: homeUrl, | |
| 101 | + siteCreatedAt, | |
| 102 | + }), | |
| 62 | 103 | keepalive: true, |
| 63 | 104 | }); |
| 64 | 105 | }; |