| @@ -1,23 +1,24 @@ | ||
| 1 | -import axios from 'axios'; | |
| 1 | +import apiFetch from '@wordpress/api-fetch'; | |
| 2 | 2 | import useSWR from 'swr'; |
| 3 | 3 | |
| 4 | -const API = axios.create({ | |
| 5 | - baseURL: window.GiveLogs.apiRoot, | |
| 6 | - headers: { | |
| 7 | - 'Content-Type': 'application/json', | |
| 8 | - 'X-WP-Nonce': window.GiveLogs.apiNonce, | |
| 9 | - }, | |
| 10 | -}); | |
| 4 | +const NAMESPACE = '/give-api/v2/logs'; | |
| 11 | 5 | |
| 6 | +/** | |
| 7 | + * @since 4.16.8 Replaced axios with @wordpress/api-fetch, which resolves with the parsed | |
| 8 | + * response body and supplies the REST root and nonce from WordPress core. | |
| 9 | + */ | |
| 10 | +const API = { | |
| 11 | + get: (endpoint) => apiFetch({path: NAMESPACE + endpoint}), | |
| 12 | + post: (endpoint, data) => apiFetch({path: NAMESPACE + endpoint, method: 'POST', data}), | |
| 13 | + delete: (endpoint) => apiFetch({path: NAMESPACE + endpoint, method: 'DELETE'}), | |
| 14 | +}; | |
| 15 | + | |
| 12 | 16 | export default API; |
| 13 | 17 | |
| 14 | -export const CancelToken = axios.CancelToken.source(); | |
| 15 | - | |
| 16 | 18 | // SWR Fetcher |
| 17 | 19 | export const Fetcher = (endpoint) => |
| 18 | - API.get(endpoint).then((res) => { | |
| 19 | - const {data, ...rest} = res.data; | |
| 20 | + API.get(endpoint).then(({data, ...rest}) => { | |
| 20 | 21 | return { |
| 21 | 22 | data, |
| 22 | 23 | response: rest, |
| 23 | 24 | }; |
| @@ -32,16 +33,17 @@ | ||
| 32 | 33 | response: data ? data.response : undefined, |
| 33 | 34 | }; |
| 34 | 35 | }; |
| 35 | 36 | |
| 36 | -// GET endpoint with additional parameters | |
| 37 | +/** | |
| 38 | + * GET endpoint with additional parameters. | |
| 39 | + * | |
| 40 | + * @since 4.16.8 apiFetch's root URL middleware rewrites the separator on sites without | |
| 41 | + * pretty permalinks, so the endpoint always uses '?' here. | |
| 42 | + */ | |
| 37 | 43 | export const getEndpoint = (endpoint, data) => { |
| 38 | 44 | if (data) { |
| 39 | - const queryString = new URLSearchParams(data); | |
| 40 | - // pretty url? | |
| 41 | - const separator = window.GiveLogs.apiRoot.indexOf('?') === -1 ? '?' : '&'; | |
| 42 | - | |
| 43 | - return endpoint + separator + queryString.toString(); | |
| 45 | + return endpoint + '?' + new URLSearchParams(data).toString(); | |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | 48 | return endpoint; |
| 47 | 49 | }; |