PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Campaigns / resources / admin / components / api.ts

api.ts in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Campaigns/resources/admin/components/api.ts

39 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 export default class CampaignsApi {
2 private readonly apiRoot: string;
3 private readonly headers: {'X-WP-Nonce': string; 'Content-Type': string};
4
5 constructor({apiNonce, apiRoot}) {
6 this.apiRoot = apiRoot;
7 this.headers = {
8 'Content-Type': 'application/json',
9 'X-WP-Nonce': apiNonce,
10 };
11 }
12
13 fetchWithArgs = (endpoint, args, method = 'GET', signal = null) => {
14 const url = new URL(this.apiRoot + endpoint);
15 for (const [param, value] of Object.entries(args)) {
16 value !== '' && url.searchParams.set(param, value as string);
17 }
18 return fetch(url.href, {
19 method: method,
20 signal: signal,
21 headers: this.headers,
22 })
23 .then((res) => {
24 if (!res.ok) {
25 throw new Error();
26 }
27 return res.text();
28 })
29 .then((text) => {
30 try {
31 return text ? JSON.parse(text) : {};
32 } catch (error) {
33 console.error('Failed to parse JSON:', error);
34 return {};
35 }
36 });
37 };
38 }
39