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

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

97 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { AI_HOST, INSIGHTS_HOST } from '@constants';
2 import { reqDataBasics } from '@shared/lib/data';
3 import { useImageGenerationStore } from '@shared/state/generate-images';
4 import apiFetch from '@wordpress/api-fetch';
5 import { __ } from '@wordpress/i18n';
6
7 const imageErrorMessage = (status) => {
8 if (status === 'content_policy_violation') {
9 // translators: shown when the AI image generator refuses a prompt on safety grounds.
10 return __(
11 'That request was blocked by our safety system. Try a different description.',
12 'extendify-local',
13 );
14 }
15 if (status === 'no_image_returned') {
16 // translators: shown when the AI image generator returns nothing, cause unknown.
17 return __(
18 "Couldn't create that image. Try describing it differently.",
19 'extendify-local',
20 );
21 }
22 return __('Service temporarily unavailable', 'extendify-local');
23 };
24
25 export const generateImage = async (imageData, signal) => {
26 const response = await fetch(`${AI_HOST}/api/draft/image`, {
27 method: 'POST',
28 mode: 'cors',
29 headers: { 'Content-Type': 'application/json' },
30 signal: signal,
31 body: JSON.stringify({
32 ...imageData,
33 globalState: useImageGenerationStore.getState(),
34 ...reqDataBasics,
35 }),
36 });
37
38 const body = await response.json();
39
40 const imageCredits = {
41 remaining: response.headers.get('x-ratelimit-remaining'),
42 total: response.headers.get('x-ratelimit-limit'),
43 refresh: response.headers.get('x-ratelimit-reset'),
44 };
45
46 if (!response.ok) {
47 throw { message: imageErrorMessage(body.status), imageCredits };
48 }
49 return {
50 images: body,
51 imageCredits,
52 id: response.headers.get('x-request-id'),
53 };
54 };
55
56 export const recordPluginActivity = async ({ slug, source }) => {
57 try {
58 const res = await fetch(`${INSIGHTS_HOST}/api/v1/plugin-install`, {
59 method: 'POST',
60 headers: { 'Content-Type': 'application/json', 'X-Extendify': 'true' },
61 body: JSON.stringify({
62 ...reqDataBasics,
63 slug,
64 source,
65 siteCreatedAt: window.extSharedData?.siteCreatedAt,
66 }),
67 });
68
69 // this should not break the app.
70 if (!res.ok) {
71 console.error('Bad response from server');
72 return null;
73 }
74
75 return await res.json();
76 } catch (error) {
77 console.error('Error sending plugin installation notification:', error);
78 return null;
79 }
80 };
81
82 export const pingServer = async () =>
83 await apiFetch({ path: '/extendify/v1/shared/ping' });
84
85 export const getPartnerPlugins = async (key) => {
86 const plugins = await apiFetch({
87 path: '/extendify/v1/shared/partner-plugins',
88 });
89 if (!Object.keys(plugins?.data ?? {}).length) {
90 throw new Error('Could not get plugins');
91 }
92 if (key && plugins.data?.[key]) {
93 return plugins.data[key];
94 }
95 return plugins.data;
96 };
97