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 / PageCreator / api / DataApi.js

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

228 lines 5.3 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 { aiDescription, aiKeywords } = pageProfile;
137 const search = new URLSearchParams({
138 aiDescription,
139 aiKeywords,
140 ...extraBody,
141 source: 'page-creator',
142 });
143
144 if (siteTitle) search.append('title', siteTitle);
145
146 const response = await fetch(`${IMAGES_HOST}/api/search?${search}`, {
147 method: 'GET',
148 headers: { 'Content-Type': 'application/json' },
149 });
150
151 if (!response?.ok) {
152 throw new Error('Something went wrong while fetching the images');
153 }
154
155 const data = await response.json();
156 return data?.siteImages ? data : { siteImages: [] };
157 };
158
159 export const getSitePlugins = async ({ pageProfile }) => {
160 const url = `${AI_HOST}/api/site-plugins`;
161 const method = 'POST';
162 const headers = { 'Content-Type': 'application/json' };
163 const fallback = [];
164
165 if (!pageProfile) {
166 return fallback;
167 }
168
169 const { wpLanguage, partnerId, pluginGroupId } = window.extSharedData;
170
171 const body = JSON.stringify({
172 type: pageProfile?.aiPageType || '',
173 description: pageProfile?.aiDescription || '',
174 keywords: pageProfile?.aiKeywords || [],
175 siteQuestions: [],
176 wpLanguage,
177 partnerId,
178 pluginGroupId,
179 source: 'page-creator',
180 });
181
182 let response;
183
184 try {
185 response = await fetch(url, { method, headers, body });
186 if (!response?.ok) throw new Error('Bad response from server');
187 } catch (_error) {
188 try {
189 response = await fetch(url, { method, headers, body });
190 } catch {
191 return fallback;
192 }
193 }
194
195 if (!response?.ok) return fallback;
196
197 try {
198 const data = await response.json();
199 return data?.selectedPlugins ?? fallback;
200 } catch (_error) {
201 return fallback;
202 }
203 };
204
205 export const getImprintPageTemplate = async () => {
206 const siteStyle = await getSiteStyle();
207 const endpoint = `${PATTERNS_HOST}/api/page-imprint`;
208
209 const res = await fetch(endpoint, {
210 method: 'POST',
211 headers: { 'Content-Type': 'application/json' },
212 body: JSON.stringify({
213 ...extraBody,
214 siteStyle: JSON.stringify(siteStyle),
215 }),
216 });
217
218 if (!res.ok) throw new Error('Could not get imprint page');
219
220 const response = await res.json();
221
222 if (!response?.template) {
223 throw new Error('No template found for imprint page');
224 }
225
226 return { ...response.template };
227 };
228