one-click-accessibility/assets/dev/js/api/index.js
2025-04-16 15:24:09 +02:00

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;