PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
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 / PageCreator / api / DataApi.js

DataApi.js in Extendify 3.1.5, at src/PageCreator/api/DataApi.js

230 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { AI_HOST, IMAGES_HOST, PATTERNS_HOST } from '@constants';
2 import { getSiteStyle } from '@page-creator/api/WPApi';
3 import { useUserStore } from '@page-creator/state/user';
4
5 const { siteTitle } = window.extSharedData;
6 const extraBody = {
7 ...Object.fromEntries(
8 Object.entries(window.extSharedData).filter(([key]) =>
9 // Optionally add items to request body
10 [
11 'partnerId',
12 'devbuild',
13 'version',
14 'siteId',
15 'wpLanguage',
16 'wpVersion',
17 'siteProfile',
18 ].includes(key),
19 ),
20 ),
21 };
22
23 const fetchPageTemplates = async (details = {}) => {
24 const { showLocalizedCopy, activePlugins, installedPlugins } =
25 window.extSharedData;
26 const { allowsInstallingPlugins } = useUserStore.getState();
27 const sitePlugins = details?.sitePlugins || [];
28
29 const plugins =
30 activePlugins?.map((path) => {
31 return path.split('/')[0];
32 }) ?? [];
33
34 const data = Object.entries(details).reduce((acc, [key, value]) => {
35 if (value === null) return acc;
36 acc[key] = typeof value === 'object' ? JSON.stringify(value) : value;
37 return acc;
38 }, {});
39
40 const res = await fetch(`${PATTERNS_HOST}/api/page-creator`, {
41 method: 'POST',
42 headers: { 'Content-Type': 'application/json' },
43 body: JSON.stringify({
44 ...extraBody,
45 showLocalizedCopy: !!showLocalizedCopy,
46 allowsInstallingPlugins,
47 plugins: JSON.stringify(plugins),
48 installedPlugins: JSON.stringify(installedPlugins),
49 allowedPlugins: JSON.stringify(sitePlugins),
50 ...data,
51 }),
52 });
53
54 if (!res.ok) throw new Error('Bad response from server');
55
56 return await res.json();
57 };
58
59 export const getGeneratedPageTemplate = async ({
60 pageProfile,
61 siteImages,
62 sitePlugins,
63 }) => {
64 const siteStyle = await getSiteStyle();
65
66 // we need the new generated AI description from the page profile
67 const page = await fetchPageTemplates({
68 siteInformation: { title: siteTitle },
69 siteImages,
70 siteStyle,
71 pageProfile,
72 sitePlugins,
73 });
74
75 if (!page?.template) {
76 throw new Error('Could not get page');
77 }
78
79 const currentTheme = window.extSharedData?.themeSlug || 'extendable';
80 if (currentTheme !== 'extendable') {
81 page.template.patterns = page.template.patterns.filter(
82 (pattern) => !pattern.patternTypes.includes('page-title'),
83 );
84 }
85
86 return page;
87 };
88
89 export const generateCustomContent = async ({
90 page,
91 userState,
92 pageProfile,
93 }) => {
94 const res = await fetch(`${AI_HOST}/api/patterns`, {
95 method: 'POST',
96 headers: { 'Content-Type': 'application/json' },
97 body: JSON.stringify({
98 ...extraBody,
99 page,
100 userState,
101 siteProfile: pageProfile,
102 }),
103 });
104
105 if (!res.ok) throw new Error('Bad response from server');
106 return await res.json();
107 };
108
109 export const getPageProfile = async ({ description, siteProfile }) => {
110 const response = await fetch(`${AI_HOST}/api/page-profile`, {
111 method: 'POST',
112 headers: { 'Content-Type': 'application/json' },
113 body: JSON.stringify({
114 ...extraBody,
115 siteDescription: siteProfile?.description || '',
116 description,
117 }),
118 });
119
120 if (!response?.ok) {
121 throw new Error('Something went wrong while fetching the profile');
122 }
123
124 const data = await response.json();
125 return data?.aiDescription
126 ? data
127 : {
128 aiTitle: null,
129 aiPageType: null,
130 aiDescription: null,
131 aiKeywords: [],
132 };
133 };
134
135 export const getPageImages = async ({ pageProfile }) => {
136 const { aiSiteType, aiSiteCategory, aiDescription, aiKeywords } = pageProfile;
137 const search = new URLSearchParams({
138 aiSiteType,
139 aiSiteCategory,
140 aiDescription,
141 aiKeywords,
142 ...extraBody,
143 source: 'page-creator',
144 });
145
146 if (siteTitle) search.append('title', siteTitle);
147
148 const response = await fetch(`${IMAGES_HOST}/api/search?${search}`, {
149 method: 'GET',
150 headers: { 'Content-Type': 'application/json' },
151 });
152
153 if (!response?.ok) {
154 throw new Error('Something went wrong while fetching the images');
155 }
156
157 const data = await response.json();
158 return data?.siteImages ? data : { siteImages: [] };
159 };
160
161 export const getSitePlugins = async ({ pageProfile }) => {
162 const url = `${AI_HOST}/api/site-plugins`;
163 const method = 'POST';
164 const headers = { 'Content-Type': 'application/json' };
165 const fallback = [];
166
167 if (!pageProfile) {
168 return fallback;
169 }
170
171 const { wpLanguage, partnerId, pluginGroupId } = window.extSharedData;
172
173 const body = JSON.stringify({
174 type: pageProfile?.aiPageType || '',
175 description: pageProfile?.aiDescription || '',
176 keywords: pageProfile?.aiKeywords || [],
177 siteQuestions: [],
178 wpLanguage,
179 partnerId,
180 pluginGroupId,
181 source: 'page-creator',
182 });
183
184 let response;
185
186 try {
187 response = await fetch(url, { method, headers, body });
188 if (!response?.ok) throw new Error('Bad response from server');
189 } catch (_error) {
190 try {
191 response = await fetch(url, { method, headers, body });
192 } catch {
193 return fallback;
194 }
195 }
196
197 if (!response?.ok) return fallback;
198
199 try {
200 const data = await response.json();
201 return data?.selectedPlugins ?? fallback;
202 } catch (_error) {
203 return fallback;
204 }
205 };
206
207 export const getImprintPageTemplate = async () => {
208 const siteStyle = await getSiteStyle();
209 const endpoint = `${PATTERNS_HOST}/api/page-imprint`;
210
211 const res = await fetch(endpoint, {
212 method: 'POST',
213 headers: { 'Content-Type': 'application/json' },
214 body: JSON.stringify({
215 ...extraBody,
216 siteStyle: JSON.stringify(siteStyle),
217 }),
218 });
219
220 if (!res.ok) throw new Error('Could not get imprint page');
221
222 const response = await res.json();
223
224 if (!response?.template) {
225 throw new Error('No template found for imprint page');
226 }
227
228 return { ...response.template };
229 };
230