154 lines
4.1 KiB
JavaScript
154 lines
4.1 KiB
JavaScript
import {
|
|
__publicField
|
|
} from "./chunk-V6TY7KAL.js";
|
|
|
|
// ../../node_modules/.pnpm/@kevisual+query@0.0.12_ws@8.18.1/node_modules/@kevisual/query/dist/query.js
|
|
var adapter = async (opts, overloadOpts) => {
|
|
const controller = new AbortController();
|
|
const signal = controller.signal;
|
|
const timeout = opts.timeout || 6e4 * 3;
|
|
const timer = setTimeout(() => {
|
|
controller.abort();
|
|
}, timeout);
|
|
return fetch(opts.url, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...opts.headers
|
|
},
|
|
body: JSON.stringify(opts.body),
|
|
signal,
|
|
...overloadOpts
|
|
}).then((response) => {
|
|
const contentType = response.headers.get("Content-Type");
|
|
if (contentType && contentType.includes("application/json")) {
|
|
return response.json();
|
|
} else {
|
|
return response.text();
|
|
}
|
|
}).catch((err) => {
|
|
if (err.name === "AbortError") {
|
|
console.log("Request timed out and was aborted");
|
|
}
|
|
console.error(err);
|
|
return {
|
|
code: 500
|
|
};
|
|
}).finally(() => {
|
|
clearTimeout(timer);
|
|
});
|
|
};
|
|
var setBaseResponse = (res) => {
|
|
res.success = res.code === 200;
|
|
res.showError = (fn) => {
|
|
if (!res.success && !res.noMsg) {
|
|
fn == null ? void 0 : fn();
|
|
}
|
|
};
|
|
};
|
|
var Query = class {
|
|
constructor(opts) {
|
|
__publicField(this, "adapter");
|
|
__publicField(this, "url");
|
|
__publicField(this, "beforeRequest");
|
|
__publicField(this, "afterResponse");
|
|
__publicField(this, "headers");
|
|
__publicField(this, "timeout");
|
|
this.adapter = (opts == null ? void 0 : opts.adapter) || adapter;
|
|
this.url = (opts == null ? void 0 : opts.url) || "/api/router";
|
|
this.headers = (opts == null ? void 0 : opts.headers) || {
|
|
"Content-Type": "application/json"
|
|
};
|
|
this.timeout = (opts == null ? void 0 : opts.timeout) || 6e4 * 3;
|
|
}
|
|
/**
|
|
* 发送 get 请求,转到 post 请求
|
|
* T是请求类型自定义
|
|
* S是返回类型自定义
|
|
* @param params 请求参数
|
|
* @param options 请求配置
|
|
* @returns 请求结果
|
|
*/
|
|
async get(params, options) {
|
|
return this.post(params, options);
|
|
}
|
|
/**
|
|
* 发送 post 请求
|
|
* T是请求类型自定义
|
|
* S是返回类型自定义
|
|
* @param body 请求体
|
|
* @param options 请求配置
|
|
* @returns 请求结果
|
|
*/
|
|
async post(body, options) {
|
|
const url = (options == null ? void 0 : options.url) || this.url;
|
|
const headers = { ...this.headers, ...options == null ? void 0 : options.headers };
|
|
const adapter2 = (options == null ? void 0 : options.adapter) || this.adapter;
|
|
const beforeRequest = (options == null ? void 0 : options.beforeRequest) || this.beforeRequest;
|
|
const afterResponse = (options == null ? void 0 : options.afterResponse) || this.afterResponse;
|
|
const timeout = (options == null ? void 0 : options.timeout) || this.timeout;
|
|
const req = {
|
|
url,
|
|
headers,
|
|
body,
|
|
timeout
|
|
};
|
|
try {
|
|
if (beforeRequest) {
|
|
await beforeRequest(req);
|
|
}
|
|
} catch (e) {
|
|
console.error("request beforeFn error", e, req);
|
|
return {
|
|
code: 500,
|
|
success: false,
|
|
message: "api request beforeFn error",
|
|
showError: () => {
|
|
}
|
|
};
|
|
}
|
|
return adapter2(req).then(async (res) => {
|
|
try {
|
|
setBaseResponse(res);
|
|
if (afterResponse) {
|
|
return await afterResponse(res, {
|
|
req,
|
|
res,
|
|
fetch: adapter2
|
|
});
|
|
}
|
|
return res;
|
|
} catch (e) {
|
|
console.error("request error", e, req);
|
|
return {
|
|
code: 500,
|
|
success: false,
|
|
message: "api request afterFn error",
|
|
showError: () => {
|
|
}
|
|
};
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
* 请求前处理,设置请求前处理函数
|
|
* @param fn 处理函数
|
|
*/
|
|
before(fn) {
|
|
this.beforeRequest = fn;
|
|
}
|
|
/**
|
|
* 请求后处理,设置请求后处理函数
|
|
* @param fn 处理函数
|
|
*/
|
|
after(fn) {
|
|
this.afterResponse = fn;
|
|
}
|
|
};
|
|
export {
|
|
Query,
|
|
adapter,
|
|
setBaseResponse
|
|
};
|
|
//# sourceMappingURL=@kevisual_query_query.js.map
|