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-style.js

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

72 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getThemeVariation } from '@auto-launch/fetchers/get-variation';
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 { AI_HOST } from '@constants';
11 import { digest } from '@shared/api/digest';
12 import { reqDataBasics } from '@shared/lib/data';
13 import { __ } from '@wordpress/i18n';
14 import { z } from 'zod';
15
16 const fallback = { siteStyle: {} };
17 const url = `${AI_HOST}/api/styles`;
18 const method = 'POST';
19 const headers = { 'Content-Type': 'application/json' };
20
21 // variation gets merged in after fetch
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(__('Picking the perfect design', 'extendify-local'));
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 getThemeVariation(
53 {
54 slug: style.colorPalette,
55 fonts: style.fonts,
56 },
57 { fallback: true },
58 );
59 const siteStyle = { ...style, variation };
60 await updateOption('extendify_site_style', siteStyle);
61 // Set animation default
62 await updateOption('extendify_animation_settings', {
63 type: style.animation ?? 'fade',
64 speed: 'medium',
65 });
66 return getStyleShape.parse({ siteStyle });
67 },
68 fallback,
69 { caller: 'handleSiteStyle' },
70 );
71 };
72