| 1 |
import apiFetch from "@wordpress/api-fetch"; |
| 2 |
import { sprintf, __ } from "@wordpress/i18n"; |
| 3 |
|
| 4 |
class API { |
| 5 |
namespace = "/betterdocs"; |
| 6 |
version = "v1"; |
| 7 |
getPath = (path) => { |
| 8 |
return `${this.namespace}/${this.version}/${path}`; |
| 9 |
}; |
| 10 |
post = (endpoint, data = {}, args = {}) => { |
| 11 |
let path = this.getPath(endpoint); |
| 12 |
args = { path, method: "POST", data, ...args }; |
| 13 |
return apiFetch(args) |
| 14 |
.then((res) => res) |
| 15 |
.catch((err) => err); |
| 16 |
}; |
| 17 |
delete = (endpoint, data = {}, args = {}) => { |
| 18 |
let path = this.getPath(endpoint); |
| 19 |
args = { path, method: "DELETE", data, ...args }; |
| 20 |
return apiFetch(args) |
| 21 |
.then((res) => res) |
| 22 |
.catch((err) => console.error(err)); |
| 23 |
}; |
| 24 |
get = (endpoint, args = {}) => { |
| 25 |
let path = this.getPath(endpoint); |
| 26 |
args = { path, method: "GET", ...args }; |
| 27 |
return apiFetch(args) |
| 28 |
.then((res) => res) |
| 29 |
.catch((err) => console.error(err)); |
| 30 |
}; |
| 31 |
useQuery = (search) => { |
| 32 |
search = search; |
| 33 |
return new URLSearchParams(search); |
| 34 |
}; |
| 35 |
getParam = (param, d = null) => { |
| 36 |
const query = this.useQuery(location.search); |
| 37 |
return query.get(param) || d; |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
export default new API; |
| 43 |
|