Files

149 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2026-01-27 11:22:16 +10:00
import axios from "axios";
import logger from "./logger.mjs";
2020-02-19 15:55:06 +11:00
2026-01-27 11:22:16 +10:00
const BackendApi = function (config, token) {
2020-02-19 15:55:06 +11:00
this.config = config;
2026-01-27 11:22:16 +10:00
this.token = token;
2024-10-11 11:21:22 +10:00
this.axios = axios.create({
baseURL: config.baseUrl,
2024-10-17 08:48:47 +10:00
timeout: 90000,
2024-10-11 11:21:22 +10:00
});
2020-02-19 15:55:06 +11:00
};
/**
* @param {string} token
*/
2026-01-27 11:22:16 +10:00
BackendApi.prototype.setToken = function (token) {
2020-02-19 15:55:06 +11:00
this.token = token;
};
/**
2024-10-11 11:21:22 +10:00
* @param {bool} returnOnError
2020-02-19 15:55:06 +11:00
*/
2026-01-27 11:22:16 +10:00
BackendApi.prototype._prepareOptions = function (returnOnError) {
const options = {
2024-10-11 11:21:22 +10:00
headers: {
2026-01-27 11:22:16 +10:00
Accept: "application/json",
},
};
2024-10-11 11:21:22 +10:00
if (this.token) {
2026-01-27 11:22:16 +10:00
options.headers.Authorization = `Bearer ${this.token}`;
2024-10-11 11:21:22 +10:00
}
if (returnOnError) {
2026-01-27 11:22:16 +10:00
options.validateStatus = () => true;
2024-10-11 11:21:22 +10:00
}
return options;
2020-02-19 15:55:06 +11:00
};
/**
2024-10-11 11:21:22 +10:00
* @param {*} response
* @param {function} resolve
* @param {function} reject
* @param {bool} returnOnError
2020-02-19 15:55:06 +11:00
*/
2026-01-27 11:22:16 +10:00
BackendApi.prototype._handleResponse = (
response,
resolve,
reject,
returnOnError,
) => {
logger("Response data:", response.data);
if (
!returnOnError &&
typeof response.data === "object" &&
typeof response.data.error === "object"
) {
if (
typeof response.data === "object" &&
typeof response.data.error === "object" &&
typeof response.data.error.message !== "undefined"
) {
reject(
new Error(
`${response.data.error.code}: ${response.data.error.message}`,
),
);
2024-10-11 11:21:22 +10:00
} else {
2026-01-27 11:22:16 +10:00
reject(new Error(`Error ${response.status}`));
2020-02-19 15:55:06 +11:00
}
2024-10-11 11:21:22 +10:00
} else {
resolve(response.data);
}
2020-02-19 15:55:06 +11:00
};
/**
2024-10-11 11:21:22 +10:00
* @param {*} err
* @param {function} resolve
* @param {function} reject
* @param {bool} returnOnError
2020-02-19 15:55:06 +11:00
*/
2026-01-27 11:22:16 +10:00
BackendApi.prototype._handleError = (err, resolve, reject, returnOnError) => {
logger("Axios Error:", err);
2024-10-11 11:21:22 +10:00
if (returnOnError) {
2026-01-27 11:22:16 +10:00
resolve(typeof err.response.data !== "undefined" ? err.response.data : err);
2024-10-11 11:21:22 +10:00
} else {
reject(err);
}
2020-02-19 15:55:06 +11:00
};
/**
2024-10-11 11:21:22 +10:00
* @param {string} method
2020-02-19 15:55:06 +11:00
* @param {string} path
* @param {bool} [returnOnError]
2024-10-11 11:21:22 +10:00
* @param {*} [data]
2020-02-19 15:55:06 +11:00
* @returns {Promise<object>}
*/
2024-10-11 11:21:22 +10:00
BackendApi.prototype.request = function (method, path, returnOnError, data) {
2024-10-15 23:54:39 +10:00
logger(method.toUpperCase(), path);
2024-10-11 11:21:22 +10:00
const options = this._prepareOptions(returnOnError);
return new Promise((resolve, reject) => {
2026-01-27 11:22:16 +10:00
const opts = {
2024-10-11 11:21:22 +10:00
method: method,
url: path,
2026-01-27 11:22:16 +10:00
...options,
};
2024-10-11 11:21:22 +10:00
if (data !== undefined && data !== null) {
opts.data = data;
}
this.axios(opts)
.then((response) => {
this._handleResponse(response, resolve, reject, returnOnError);
})
.catch((err) => {
this._handleError(err, resolve, reject, returnOnError);
});
});
2020-02-19 15:55:06 +11:00
};
/**
* @param {string} path
2024-10-11 11:21:22 +10:00
* @param {form} form
2020-02-19 15:55:06 +11:00
* @param {bool} [returnOnError]
* @returns {Promise<object>}
*/
2024-10-11 11:21:22 +10:00
BackendApi.prototype.postForm = function (path, form, returnOnError) {
2026-01-27 11:22:16 +10:00
logger("POST", this.config.baseUrl + path);
2024-10-11 11:21:22 +10:00
const options = this._prepareOptions(returnOnError);
2020-02-19 15:55:06 +11:00
return new Promise((resolve, reject) => {
2024-10-11 11:21:22 +10:00
const opts = {
...options,
...form.getHeaders(),
2026-01-27 11:22:16 +10:00
};
2024-10-11 11:21:22 +10:00
2026-01-27 11:22:16 +10:00
this.axios
.post(path, form, opts)
2024-10-11 11:21:22 +10:00
.then((response) => {
this._handleResponse(response, resolve, reject, returnOnError);
})
.catch((err) => {
this._handleError(err, resolve, reject, returnOnError);
});
2020-02-19 15:55:06 +11:00
});
};
2026-01-27 11:26:54 +10:00
export default BackendApi;