PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.1
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / modules / optimization / assets / js / classes / api.js

api.js in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.0.1, at modules/optimization/assets/js/classes/api.js

60 lines 1.2 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 APIError from './exceptions/APIError';
3
4 const v1Prefix = '/image-optimizer/v1';
5
6 class API {
7 static async request( { path, data, method = 'POST' } ) {
8 try {
9 const response = await apiFetch( {
10 path,
11 method,
12 data,
13 } );
14
15 if ( ! response.success ) {
16 throw new APIError( response.data.message );
17 }
18
19 return response.data;
20 } catch ( e ) {
21 if ( e instanceof APIError ) {
22 throw e;
23 } else {
24 throw new APIError( e.message );
25 }
26 }
27 }
28
29 static async optimizeSingleImage( { imageId, reoptimize = false } ) {
30 return API.request( {
31 path: `${ v1Prefix }/optimize/image`,
32 data: {
33 imageId,
34 reoptimize,
35 'image-optimizer-optimize-image': window?.imageOptimizerControlSettings?.optimizeSingleImageNonce,
36 },
37 } );
38 }
39
40 static async restoreSingleImage( imageId ) {
41 return API.request( {
42 path: `${ v1Prefix }/backups/restore/${ imageId }`,
43 data: {
44 'image-optimizer-restore-single': window?.imageOptimizerControlSettings?.restoreSingleImageNonce,
45 },
46 } );
47 }
48
49 static async getOptimizationStatus( imageIds ) {
50 return API.request( {
51 path: `${ v1Prefix }/optimize/status`,
52 data: {
53 image_ids: imageIds,
54 },
55 } );
56 }
57 }
58
59 export default API;
60