PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
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-style.js

get-style.js in Extendify 3.2.1, at src/AutoLaunch/fetchers/get-style.js

71 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getStyleDocument } from '@auto-launch/fetchers/get-style-document';
2 import { getStyleShape, styleShape } from '@auto-launch/fetchers/shape';
3 import {
4 failWithFallback,
5 fetchWithTimeout,
6 retryTwice,
7 setStatus,
8 } from '@auto-launch/functions/helpers';
9 import { updateOption } from '@auto-launch/functions/wp';
10 import { launchStrings } from '@auto-launch/strings';
11 import { AI_HOST } from '@constants';
12 import { digest } from '@shared/api/digest';
13 import { reqDataBasics } from '@shared/lib/data';
14 import { z } from 'zod';
15
16 const fallback = { siteStyle: {} };
17 const url = `${AI_HOST}/api/style-recipes`;
18 const method = 'POST';
19 const headers = { 'Content-Type': 'application/json' };
20
21 // The styles route serves no global-styles document.
22 const shapeLocal = z.array(styleShape.omit({ variation: true }));
23
24 export const handleSiteStyle = async ({ siteProfile }) => {
25 // translators: this is for a action log UI. Keep it short
26 setStatus(launchStrings().statusStyle);
27
28 const body = JSON.stringify({ ...reqDataBasics, siteProfile, count: 1 });
29
30 const response = await retryTwice(() =>
31 fetchWithTimeout(url, { method, headers, body }, 20_000),
32 ).catch((error) => {
33 return { ok: false, statusText: error.message, status: 0 };
34 });
35
36 if (!response?.ok) {
37 digest({
38 error: {
39 message: response.statusText,
40 name: 'FetchError',
41 status: response.status,
42 },
43 details: { source: 'auto-launch', caller: 'handleSiteStyle' },
44 });
45 return fallback;
46 }
47
48 return failWithFallback(
49 async () => {
50 const data = await response.json();
51 const style = shapeLocal.parse(data)[0];
52 const variation = await getStyleDocument({
53 colorPalette: style.colorPalette,
54 fonts: style.fonts,
55 });
56 const siteStyle = { ...style, variation };
57 // The Agent reads extendify_siteStyle; the legacy row keeps old readers.
58 await updateOption('extendify_siteStyle', siteStyle);
59 await updateOption('extendify_site_style', siteStyle);
60 // Set animation default
61 await updateOption('extendify_animation_settings', {
62 type: style.animation ?? 'fade',
63 speed: 'medium',
64 });
65 return getStyleShape.parse({ siteStyle });
66 },
67 fallback,
68 { caller: 'handleSiteStyle' },
69 );
70 };
71