| 1 |
import { getPluginsShape, pluginShape } from '@auto-launch/fetchers/shape'; |
| 2 |
import { |
| 3 |
failWithFallback, |
| 4 |
fetchWithTimeout, |
| 5 |
retryTwice, |
| 6 |
setStatus, |
| 7 |
} from '@auto-launch/functions/helpers'; |
| 8 |
import { AI_HOST } from '@constants'; |
| 9 |
import { reqDataBasics } from '@shared/lib/data'; |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
import { z } from 'zod'; |
| 12 |
|
| 13 |
const { pluginGroupId } = window.extSharedData; |
| 14 |
const fallback = { sitePlugins: [] }; |
| 15 |
const url = `${AI_HOST}/api/site-plugins`; |
| 16 |
const method = 'POST'; |
| 17 |
const headers = { 'Content-Type': 'application/json' }; |
| 18 |
|
| 19 |
const shapeLocal = z.object({ |
| 20 |
selectedPlugins: z.array(pluginShape), |
| 21 |
}); |
| 22 |
|
| 23 |
export const handleSitePlugins = async ({ |
| 24 |
siteProfile = {}, |
| 25 |
requiredOnly = false, |
| 26 |
showStatus = true, |
| 27 |
}) => { |
| 28 |
if (showStatus) { |
| 29 |
// translators: this is for a action log UI. Keep it short |
| 30 |
setStatus(__('Setting up site functionality', 'extendify-local')); |
| 31 |
} |
| 32 |
|
| 33 |
const body = JSON.stringify({ |
| 34 |
...reqDataBasics, |
| 35 |
...siteProfile, |
| 36 |
siteProfile, |
| 37 |
siteObjective: siteProfile.objective, |
| 38 |
pluginGroupId, |
| 39 |
requiredOnly, |
| 40 |
}); |
| 41 |
|
| 42 |
const response = await retryTwice(() => |
| 43 |
fetchWithTimeout(url, { method, headers, body }), |
| 44 |
); |
| 45 |
|
| 46 |
if (!response?.ok) return fallback; |
| 47 |
|
| 48 |
const { selectedPlugins } = await failWithFallback(async () => |
| 49 |
shapeLocal.parse(await response.json(), fallback), |
| 50 |
); |
| 51 |
return getPluginsShape.parse({ sitePlugins: selectedPlugins }); |
| 52 |
}; |
| 53 |
|