PluginProbe
Extendify / 3.0.6
Extendify v3.0.6
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / AutoLaunch / fetchers / get-profile.js

get-profile.js in Extendify 3.0.6, at src/AutoLaunch/fetchers/get-profile.js

58 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getProfileShape } from '@auto-launch/fetchers/shape';
2 import { fetchWithTimeout, setStatus } from '@auto-launch/functions/helpers';
3 import { updateOption } from '@auto-launch/functions/wp';
4 import { overrideWithUrlParams } from '@auto-launch/state/url-params';
5 import { AI_HOST } from '@constants';
6 import { reqDataBasics } from '@shared/lib/data';
7 import { __ } from '@wordpress/i18n';
8 import { z } from 'zod';
9
10 const localShape = z.object({
11 siteProfile: getProfileShape,
12 });
13
14 // No fallback here as we need this data
15 const url = `${AI_HOST}/api/site-profile`;
16 const method = 'POST';
17 const headers = { 'Content-Type': 'application/json' };
18
19 export const handleSiteProfile = async ({
20 title,
21 descriptionRaw,
22 urlParams,
23 }) => {
24 // translators: this is for a action log UI. Keep it short
25 setStatus(__('Creating a site profile', 'extendify-local'));
26
27 const body = JSON.stringify({
28 ...reqDataBasics,
29 title: title || window.extSharedData.siteTitle,
30 description: descriptionRaw,
31 autoLaunch: true,
32 });
33 const response = await fetchWithTimeout(
34 url,
35 { method, headers, body },
36 20_000,
37 );
38
39 // This should throw rather than have a fallback
40 if (!response?.ok) throw new Error('Failed to fetch site profile');
41
42 // TODO: pull out followups here and save them for AI follow ups
43 const { profile: p } = await response.json();
44 const profile = getProfileShape.parse(p);
45 // Overwrite with a few url params if provided
46 const profileWIthUrlOverrides = {
47 ...profile,
48 ...overrideWithUrlParams(urlParams),
49 descriptionRaw, // Add the raw description to profile
50 };
51
52 await updateOption(
53 'extendify_site_profile',
54 JSON.stringify(profileWIthUrlOverrides),
55 );
56 return localShape.parse({ siteProfile: profileWIthUrlOverrides });
57 };
58