etherreq 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/etherreq.js CHANGED
@@ -4,7 +4,14 @@ export const create = (defaultConfig = {}) => {
4
4
  const responseInterceptors = [];
5
5
  const client = async (config) => {
6
6
  // 合并默认配置
7
- const mergedConfig = { ...defaultConfig, ...config };
7
+ const mergedConfig = {
8
+ ...defaultConfig,
9
+ ...config,
10
+ headers: {
11
+ ...defaultConfig.headers,
12
+ ...config.headers
13
+ }
14
+ };
8
15
  // 应用请求拦截器
9
16
  let processedConfig = { ...mergedConfig };
10
17
  for (const interceptor of requestInterceptors) {
@@ -36,7 +43,7 @@ export const create = (defaultConfig = {}) => {
36
43
  data,
37
44
  status: response.status,
38
45
  statusText: response.statusText,
39
- headers: response.headers,
46
+ headers: Object.fromEntries(response.headers.entries()), // 将Headers对象转换为普通对象
40
47
  config: processedConfig,
41
48
  };
42
49
  // 应用响应拦截器
package/dist/request.js CHANGED
@@ -81,8 +81,18 @@ function getCacheTTL(config) {
81
81
  * @param options - 请求配置
82
82
  */
83
83
  export const request = (url, options = {}) => {
84
- const baseURL = options.baseURL || _baseURL;
85
- const finalURL = new URL(url, baseURL).toString();
84
+ // 如果 options.baseURL 存在则使用它,否则使用全局 _baseURL,如果都不存在则使用当前页面的 origin
85
+ const baseURL = options.baseURL || _baseURL || (typeof window !== 'undefined' ? window.location.origin : '');
86
+ // 构造最终的 URL,如果 baseURL 为空则直接使用传入的 url
87
+ let finalURL;
88
+ if (baseURL && !url.startsWith('http')) {
89
+ // 只有当 url 不是绝对 URL 时才拼接 baseURL
90
+ finalURL = new URL(url, baseURL).toString();
91
+ }
92
+ else {
93
+ // 如果 url 是绝对 URL 或者 baseURL 为空,则直接使用 url
94
+ finalURL = url;
95
+ }
86
96
  return instance({
87
97
  ...options,
88
98
  url: finalURL,
@@ -3,7 +3,9 @@ export declare const create: (defaultConfig?: any) => {
3
3
  data: any;
4
4
  status: number;
5
5
  statusText: string;
6
- headers: Headers;
6
+ headers: {
7
+ [k: string]: string;
8
+ };
7
9
  config: any;
8
10
  }>;
9
11
  interceptors: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "A lightweight custom HTTP request library with TypeScript support.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",