PluginProbe
Extendify / 2.2.2
Extendify v2.2.2
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 0.7.0 All 126 releases
extendify / src / Shared / api / DataApi.js

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

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