query/dist/index.js

82 lines
2.3 KiB
JavaScript

const adapter = async (opts) => {
const controller = new AbortController();
const signal = controller.signal;
const timeout = opts.timeout || 60000; // 默认超时时间为 60s
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,
})
.then((response) => {
// 获取 Content-Type 头部信息
const contentType = response.headers.get('Content-Type');
// 判断返回的数据类型
if (contentType && contentType.includes('application/json')) {
return response.json(); // 解析为 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);
});
};
class Query {
adapter;
url;
beforeRequest;
headers;
timeout;
constructor(opts) {
this.adapter = opts.adapter || adapter;
this.url = opts.url || '/api/router';
this.headers = opts.headers || {
'Content-Type': 'application/json',
};
this.timeout = opts.timeout || 60000; // 默认超时时间为 60s
}
async get(params, options) {
return this.post(params, options);
}
async post(body, options) {
const url = options?.url || this.url;
const headers = { ...this.headers, ...options?.headers };
const adapter = options?.adapter || this.adapter;
const beforeRequest = options?.beforeRequest || this.beforeRequest;
const timeout = options?.timeout || this.timeout;
const req = {
url: url,
headers: headers,
body,
timeout,
};
if (beforeRequest) {
await beforeRequest(req);
}
return adapter(req);
}
before(fn) {
this.beforeRequest = fn;
}
}
export { Query, adapter };