mirror of
https://gh.wpcy.net/https://github.com/elementor/one-click-accessibility.git
synced 2026-04-24 18:48:55 +08:00
39 lines
768 B
JavaScript
39 lines
768 B
JavaScript
import apiFetch from '@wordpress/api-fetch';
|
|
import { addQueryArgs } from '@wordpress/url';
|
|
import APIError from './exceptions/APIError';
|
|
|
|
const wpV2Prefix = '/wp/v2';
|
|
|
|
class API {
|
|
static async request({ path, data, method = 'POST' }) {
|
|
try {
|
|
if ('GET' === method && !path.startsWith(wpV2Prefix)) {
|
|
path = addQueryArgs(path, { sb_time: new Date().getTime() });
|
|
}
|
|
|
|
const response = await apiFetch({
|
|
path,
|
|
method,
|
|
data,
|
|
});
|
|
|
|
if (path.startsWith(wpV2Prefix)) {
|
|
return response;
|
|
}
|
|
|
|
if (!response.success) {
|
|
throw new APIError(response.data.message);
|
|
}
|
|
|
|
return response.data;
|
|
} catch (e) {
|
|
if (e instanceof APIError) {
|
|
throw e;
|
|
} else {
|
|
throw new APIError(e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export default API;
|