react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
183 lines • 4.3 kB
JavaScript
import axios from 'axios';
/**
* Post Model - Type-safe HTTP POST/PUT/DELETE/PATCH request builder
* @template TBody - Request body type
* @template TResponse - Response data type
*/
export class Post {
_target;
_method = 'POST';
_body;
_params = {};
_headers = {};
_config = {};
_hooks = {};
_abortController;
static _axios = axios.create();
/**
* Set the request target URL
*/
target(url) {
this._target = url;
return this;
}
/**
* Set the HTTP method
*/
method(method) {
this._method = method;
return this;
}
/**
* Set the request body
*/
body(data) {
this._body = data;
return this;
}
/**
* Set URL parameters
*/
params(params) {
this._params = { ...this._params, ...params };
return this;
}
/**
* Set a single URL parameter
*/
param(key, value) {
this._params[key] = value;
return this;
}
/**
* Set request headers
*/
headers(headers) {
this._headers = { ...this._headers, ...headers };
return this;
}
/**
* Set a single header
*/
header(key, value) {
this._headers[key] = value;
return this;
}
/**
* Set additional request configuration
*/
config(config) {
this._config = { ...this._config, ...config };
return this;
}
/**
* Hook called before the request is sent
*/
onBefore(callback) {
this._hooks.onBefore = callback;
return this;
}
/**
* Hook called when request succeeds
*/
onThen(callback) {
this._hooks.onThen = callback;
return this;
}
/**
* Hook called when request fails
*/
onCatch(callback) {
this._hooks.onCatch = callback;
return this;
}
/**
* Hook called after request completes (success or failure)
*/
onFinally(callback) {
this._hooks.onFinally = callback;
return this;
}
/**
* Execute the request
*/
async execute() {
if (!this._target) {
throw new Error('Target URL is required');
}
this._abortController = new AbortController();
let config = {
method: this._method,
url: this._target,
data: this._body,
params: this._params,
headers: this._headers,
signal: this._abortController.signal,
...this._config,
};
try {
// Apply onBefore hook
if (this._hooks.onBefore) {
config = await this._hooks.onBefore(config);
}
const response = await Post._axios.request(config);
const data = response.data;
// Apply onThen hook
if (this._hooks.onThen) {
await this._hooks.onThen(data);
}
return data;
}
catch (error) {
// Apply onCatch hook
if (this._hooks.onCatch) {
return this._hooks.onCatch(error);
}
throw error;
}
finally {
// Apply onFinally hook
if (this._hooks.onFinally) {
await this._hooks.onFinally();
}
}
}
/**
* Cancel the request
*/
cancel(message) {
if (this._abortController) {
this._abortController.abort(message);
}
}
/**
* Set global axios instance
*/
static setAxios(instance) {
Post._axios = instance;
}
/**
* Get the current axios instance
*/
static getAxios() {
return Post._axios;
}
/**
* Configure global defaults
*/
static configure(config) {
if (config.baseURL) {
Post._axios.defaults.baseURL = config.baseURL;
}
if (config.timeout) {
Post._axios.defaults.timeout = config.timeout;
}
if (config.headers) {
Post._axios.defaults.headers.common = {
...Post._axios.defaults.headers.common,
...config.headers,
};
}
}
}
//# sourceMappingURL=Post.js.map