-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest.js
33 lines (32 loc) · 876 Bytes
/
rest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Using REST, incremental load sites and add them to local storage.
*
* @author Per Søderlind
* @export
* @param {IndexedDB} db
*/
import apiFetch from '@wordpress/api-fetch';
export async function loadSites( db, offset ) {
try {
// Set the nonce for the request.
await apiFetch.use(
apiFetch.createNonceMiddleware( pluginAllSitesMenu.nonce )
);
const res = await apiFetch( {
url: pluginAllSitesMenu.restURL,
method: 'POST',
data: { offset: offset },
} ).catch( ( err ) => {
console.error( 'Error in apiFetch', err );
throw err; // Re-throw the error!
} );
if ( res.response === 'success' ) {
offset = offset + pluginAllSitesMenu.loadincrements;
db.save( res.data );
await loadSites( db, offset ); // load more.
}
} catch ( err ) {
console.error( 'Error in loadSites', err );
throw err; // Re-throw the error!
}
}