react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
338 lines (337 loc) • 7.39 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
const axios = require("axios");
const _Get = class _Get {
constructor() {
__publicField(this, "_target");
__publicField(this, "_params", {});
__publicField(this, "_headers", {});
__publicField(this, "_config", {});
__publicField(this, "_hooks", {});
__publicField(this, "_abortController");
}
/**
* Set the request target URL
*/
target(url) {
this._target = url;
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 GET request
*/
async execute() {
if (!this._target) {
throw new Error("Target URL is required");
}
this._abortController = new AbortController();
let config = {
method: "GET",
url: this._target,
params: this._params,
headers: this._headers,
signal: this._abortController.signal,
...this._config
};
try {
if (this._hooks.onBefore) {
config = await this._hooks.onBefore(config);
}
const response = await _Get._axios.request(config);
const data = response.data;
if (this._hooks.onThen) {
await this._hooks.onThen(data);
}
return data;
} catch (error) {
if (this._hooks.onCatch) {
return this._hooks.onCatch(error);
}
throw error;
} finally {
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) {
_Get._axios = instance;
}
/**
* Get the current axios instance
*/
static getAxios() {
return _Get._axios;
}
/**
* Configure global defaults
*/
static configure(config) {
if (config.baseURL) {
_Get._axios.defaults.baseURL = config.baseURL;
}
if (config.timeout) {
_Get._axios.defaults.timeout = config.timeout;
}
if (config.headers) {
_Get._axios.defaults.headers.common = {
..._Get._axios.defaults.headers.common,
...config.headers
};
}
}
};
__publicField(_Get, "_axios", axios.create());
let Get = _Get;
const _Post = class _Post {
constructor() {
__publicField(this, "_target");
__publicField(this, "_method", "POST");
__publicField(this, "_body");
__publicField(this, "_params", {});
__publicField(this, "_headers", {});
__publicField(this, "_config", {});
__publicField(this, "_hooks", {});
__publicField(this, "_abortController");
}
/**
* 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 {
if (this._hooks.onBefore) {
config = await this._hooks.onBefore(config);
}
const response = await _Post._axios.request(config);
const data = response.data;
if (this._hooks.onThen) {
await this._hooks.onThen(data);
}
return data;
} catch (error) {
if (this._hooks.onCatch) {
return this._hooks.onCatch(error);
}
throw error;
} finally {
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
};
}
}
};
__publicField(_Post, "_axios", axios.create());
let Post = _Post;
exports.Get = Get;
exports.Post = Post;
//# sourceMappingURL=Post-CeaBs_p4.cjs.map