PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / trunk
Starter Sites & Templates by Neve vtrunk
1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / assets / src / utils / rest.js
templates-patterns-collection / assets / src / utils Last commit date
common.js 2 years ago rest.js 3 years ago site-import.js 2 years ago
rest.js
66 lines
1 import {trailingSlashIt} from "./common";
2
3 export const send = ( route, data, simple = false ) => {
4 return requestData( route, simple, data );
5 };
6
7 export const get = ( route, simple = false, useNonce = true ) => {
8 return requestData( route, simple, {}, 'GET', useNonce );
9 };
10
11 const requestData = async (
12 route,
13 simple = false,
14 data = {},
15 method = 'POST',
16 useNonce = true
17 ) => {
18 const options = {
19 method,
20 headers: {
21 Accept: 'application/json',
22 'Content-Type': 'application/json',
23 },
24 };
25
26 if ( tiobDash.params.site_url ) {
27 const url = new URL( route );
28 url.searchParams.append( 'site_url', encodeURIComponent( tiobDash.params.site_url ) );
29 route = url;
30 }
31
32 if ( useNonce ) {
33 options.headers[ 'x-wp-nonce' ] = tiobDash.nonce;
34 }
35
36 if ( 'POST' === method ) {
37 options.body = JSON.stringify( data );
38 }
39
40 return await fetch( route, options ).then( ( response ) => {
41 return simple ? response : response.json();
42 } );
43 };
44
45 export const ajaxAction = async (route, action = '', useNonce = '', data = {} ) => {
46 const formData = new FormData();
47 formData.append('nonce', useNonce);
48 formData.append('action', action);
49 if ( Object.keys( data ).length > 0 ) {
50 for ( const [key, value] of Object.entries( data ) ) {
51 formData.append( key, value );
52 }
53 }
54 const options = {
55 method: 'POST',
56 headers: {
57 Accept: 'application/json',
58 },
59 body: formData,
60 };
61
62 return await fetch(route, options).then(() => {
63 return true;
64 });
65 };
66