@polyv/request-core 2.7.1 → 2.8.0

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/README.md CHANGED
@@ -21,7 +21,7 @@ const requester = new PolyvRequest({
21
21
  (async () => {
22
22
  // 执行请求
23
23
  const data = await requester.get('/getDetail', {
24
- channelId: 'xxxx'
24
+ channelId: 'xxxx',
25
25
  });
26
26
 
27
27
  console.log(data);
@@ -47,11 +47,15 @@ interface ChannelDetail {
47
47
 
48
48
  (async () => {
49
49
  // 执行请求
50
- const data = await requester.get<ChannelDetail>('/getDetail', {
51
- channelId: 'xxxx'
52
- }, {
53
- test: true
54
- });
50
+ const data = await requester.get<ChannelDetail>(
51
+ '/getDetail',
52
+ {
53
+ channelId: 'xxxx',
54
+ },
55
+ {
56
+ test: true,
57
+ },
58
+ );
55
59
 
56
60
  console.log(data.channelName);
57
61
  })();
@@ -59,14 +63,14 @@ interface ChannelDetail {
59
63
 
60
64
  ## 实例化参数
61
65
 
62
- | 参数名 | 用途 | 类型 | 默认值 |
63
- | - | - | - | - |
64
- | baseUrl | 接口请求地址前缀 | `string \| function` | `''` |
65
- | requestPlugins | 请求插件 | `RequestPlugin[]` | `[]` |
66
- | timeout | 超时时间,毫秒 | `number` | `10000` |
67
- | requestType | 请求方式 | `RequestType` | `'form'` |
68
- | responseType | 响应格式 | `ResponseType` | `'json'` |
69
- | withCredentials | 跨域请求时是否提供凭据 | `boolean` | `false` |
66
+ | 参数名 | 用途 | 类型 | 默认值 |
67
+ | --------------- | ---------------------- | -------------------- | -------- |
68
+ | baseUrl | 接口请求地址前缀 | `string \| function` | `''` |
69
+ | requestPlugins | 请求插件 | `RequestPlugin[]` | `[]` |
70
+ | timeout | 超时时间,毫秒 | `number` | `10000` |
71
+ | requestType | 请求方式 | `RequestType` | `'form'` |
72
+ | responseType | 响应格式 | `ResponseType` | `'json'` |
73
+ | withCredentials | 跨域请求时是否提供凭据 | `boolean` | `false` |
70
74
 
71
75
  ## 发送请求
72
76
 
@@ -79,7 +83,7 @@ interface ChannelDetailResponse {
79
83
  data: {
80
84
  /** 频道名称 */
81
85
  channelName: string;
82
- }
86
+ };
83
87
  }
84
88
 
85
89
  // 接口入参参数
@@ -88,12 +92,9 @@ interface ChannelDetailRequestParams {
88
92
  channelId: string;
89
93
  }
90
94
 
91
- const res = await requester.get<ChannelDetailResponse, ChannelDetailRequestParams>(
92
- '/getDetail',
93
- {
94
- channelId: 'xxxx'
95
- }
96
- );
95
+ const res = await requester.get<ChannelDetailResponse, ChannelDetailRequestParams>('/getDetail', {
96
+ channelId: 'xxxx',
97
+ });
97
98
 
98
99
  console.log('code', res.code);
99
100
  console.log('channelName', res.data.channelName);
@@ -116,14 +117,14 @@ const res = await requester.post<Response, RequestData>(
116
117
  '/save-name',
117
118
  {
118
119
  userId: 123,
119
- name: '小明'
120
+ name: '小明',
120
121
  },
121
122
  {
122
123
  // 如果需要额外入参地址参数
123
124
  params: {
124
- channelId: 'xxx'
125
- }
126
- }
125
+ channelId: 'xxx',
126
+ },
127
+ },
127
128
  );
128
129
 
129
130
  if (res.code === 200) {
@@ -154,18 +155,18 @@ const requester = new PolyvRequest({
154
155
  },
155
156
  }),
156
157
  new StatusCodeRequestPlugin(),
157
- ]
158
+ ],
158
159
  });
159
160
  ```
160
161
 
161
162
  ### 插件概览
162
163
 
163
- | 插件名 | 用途 |
164
- | - | - |
165
- | [@polyv/request-plugin-global-params](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-global-params) | 全局 params 入参插件 |
166
- | [@polyv/request-plugin-authorize-token](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-authorize-token) | 请求令牌插件 |
167
- | [@polyv/request-plugin-authorize-app-sign](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-authorize-app-sign) | 请求签名插件 |
168
- | [@polyv/request-plugin-status-code](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-status-code) | 状态码处理插件 |
169
- | [@polyv/request-plugin-aes-decrypt](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-aes-decrypt) | AES 解密响应内容 |
170
- | [@polyv/request-plugin-aes](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-aes) | AES 业务请求加密和解密 |
171
- | [@polyv/request-plugin-sm2](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-sm2) | SM2 业务请求加密和解密 |
164
+ | 插件名 | 用途 |
165
+ | -------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
166
+ | [@polyv/request-plugin-global-params](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-global-params) | 全局 params 入参插件 |
167
+ | [@polyv/request-plugin-authorize-token](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-authorize-token) | 请求令牌插件 |
168
+ | [@polyv/request-plugin-authorize-app-sign](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-authorize-app-sign) | 请求签名插件 |
169
+ | [@polyv/request-plugin-status-code](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-status-code) | 状态码处理插件 |
170
+ | [@polyv/request-plugin-aes-decrypt](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-aes-decrypt) | AES 解密响应内容 |
171
+ | [@polyv/request-plugin-aes](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-aes) | AES 业务请求加密和解密 |
172
+ | [@polyv/request-plugin-sm2](https://npm-registry.polyv.net/-/web/detail/@polyv/request-plugin-sm2) | SM2 业务请求加密和解密 |
@@ -0,0 +1,2 @@
1
+ import type { GetAdapterType } from './type';
2
+ export declare const getAdapter: GetAdapterType;
@@ -0,0 +1,8 @@
1
+ import { Request, wxRequestAdapter } from '@polyv/utils-request';
2
+ let xhrRequest;
3
+ export const getAdapter = () => {
4
+ if (!xhrRequest) {
5
+ xhrRequest = new Request(wxRequestAdapter);
6
+ }
7
+ return xhrRequest;
8
+ };
@@ -0,0 +1,2 @@
1
+ import type { GetAdapterType } from './type';
2
+ export declare const getAdapter: GetAdapterType;
@@ -0,0 +1,8 @@
1
+ import { Request, xhrAdapter } from '@polyv/utils-request';
2
+ let xhrRequest;
3
+ export const getAdapter = () => {
4
+ if (!xhrRequest) {
5
+ xhrRequest = new Request(xhrAdapter);
6
+ }
7
+ return xhrRequest;
8
+ };
@@ -0,0 +1,2 @@
1
+ import type { GetAdapterType } from './type';
2
+ export declare const getAdapter: GetAdapterType;
@@ -0,0 +1,9 @@
1
+ import { getAdapter as getAdapterXhr } from './adapter-xhr';
2
+ import { getAdapter as getAdapterWx } from './adapter-wx';
3
+ import { isWechatMiniprogramEnv } from '@polyv/utils-env';
4
+ export const getAdapter = () => {
5
+ if (isWechatMiniprogramEnv()) {
6
+ return getAdapterWx();
7
+ }
8
+ return getAdapterXhr();
9
+ };
@@ -0,0 +1,2 @@
1
+ import type { Request } from '@polyv/utils-request';
2
+ export type GetAdapterType = () => Request;
@@ -0,0 +1 @@
1
+ export {};
package/ajax.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PolyvRequestConfig, RequestOptions } from './interface';
1
+ import type { PolyvRequestConfig, RequestOptions } from './interface/index';
2
2
  /**
3
3
  * API 请求封装
4
4
  */
@@ -12,7 +12,7 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
12
12
  private __responseType;
13
13
  private __withCredentials;
14
14
  /** 请求器 */
15
- private __xhrRequest;
15
+ private __baseRequest;
16
16
  private __pluginCtrl;
17
17
  constructor(options?: PolyvRequestConfig<Options>);
18
18
  /**
@@ -55,12 +55,6 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
55
55
  open<P = object>(url: string, params: P, options?: Options): Promise<void>;
56
56
  /** 下载链接 */
57
57
  download<P = object>(url: string, params: P, options?: Options): Promise<void>;
58
- /**
59
- * 解析响应头
60
- * @param headerStr 响应头字符串
61
- * @returns 响应头对象
62
- */
63
- private __parseResponseHeaders;
64
58
  destroy(): void;
65
59
  }
66
60
  export default PolyvRequest;
package/ajax.js CHANGED
@@ -1,11 +1,9 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
- import { Request, xhrAdapter } from '@polyv/utils-request';
3
1
  import { startsWithProtocol } from '@polyv/utils-net';
2
+ import { concat } from '@polyv/utils-querystring';
3
+ import { getAdapter } from './adapter/get-adapter';
4
+ import { DEFAULT_REQUEST_TYPE, DEFAULT_RESPONSE_TYPE } from './config';
4
5
  import { PluginController } from './plugins/plugin-controller';
5
6
  import { buildFormData, hasFileValue } from './utils';
6
- import { DEFAULT_REQUEST_TYPE, DEFAULT_RESPONSE_TYPE } from './config';
7
- import { concat } from '@polyv/utils-querystring';
8
- let xhrRequest;
9
7
  /**
10
8
  * API 请求封装
11
9
  */
@@ -19,7 +17,7 @@ export class PolyvRequest {
19
17
  __responseType;
20
18
  __withCredentials;
21
19
  /** 请求器 */
22
- __xhrRequest;
20
+ __baseRequest;
23
21
  __pluginCtrl;
24
22
  constructor(options = {}) {
25
23
  const { baseUrl } = options;
@@ -30,10 +28,7 @@ export class PolyvRequest {
30
28
  this.__requestType = options.requestType || DEFAULT_REQUEST_TYPE;
31
29
  this.__responseType = options.responseType || DEFAULT_RESPONSE_TYPE;
32
30
  this.__withCredentials = options.withCredentials ?? false;
33
- if (!xhrRequest) {
34
- xhrRequest = new Request(xhrAdapter);
35
- }
36
- this.__xhrRequest = xhrRequest;
31
+ this.__baseRequest = getAdapter();
37
32
  const requestPlugins = (options.requestPlugins || []);
38
33
  this.__pluginCtrl = new PluginController(requestPlugins);
39
34
  }
@@ -96,10 +91,11 @@ export class PolyvRequest {
96
91
  let result;
97
92
  try {
98
93
  // 调用请求库
99
- const justRes = await this.__xhrRequest.send(handleResult.requestUrl, {
94
+ const justRes = (await this.__baseRequest.send(handleResult.requestUrl, {
100
95
  ..._options,
101
- });
102
- const headers = this.__parseResponseHeaders(justRes.xhr.getAllResponseHeaders());
96
+ }));
97
+ // const headers = this.__parseResponseHeaders(justRes.xhr.getAllResponseHeaders());
98
+ const headers = justRes.getResponseHeaders();
103
99
  // 请求结果
104
100
  result = {
105
101
  data: justRes.data,
@@ -137,7 +133,7 @@ export class PolyvRequest {
137
133
  async __interceptRequest(options) {
138
134
  let newOptions = options;
139
135
  // 处理插件中的请求拦截
140
- newOptions = await this.__pluginCtrl.interceptPluginRequest(newOptions);
136
+ newOptions = (await this.__pluginCtrl.interceptPluginRequest(newOptions));
141
137
  return newOptions;
142
138
  }
143
139
  /**
@@ -226,33 +222,6 @@ export class PolyvRequest {
226
222
  downloadLink.click();
227
223
  document.body.removeChild(downloadLink);
228
224
  }
229
- /**
230
- * 解析响应头
231
- * @param headerStr 响应头字符串
232
- * @returns 响应头对象
233
- */
234
- __parseResponseHeaders(headerStr) {
235
- const headers = {};
236
- // 如果字符串为空,则返回空对象
237
- if (!headerStr) {
238
- return headers;
239
- }
240
- // 按行分割头信息
241
- const headerPairs = headerStr.trim().split('\r\n');
242
- headerPairs.forEach(headerPair => {
243
- // 查找第一个冒号的位置
244
- const index = headerPair.indexOf(':');
245
- // 如果找不到冒号,则跳过此行
246
- if (index > 0) {
247
- // 提取键(转为小写)和值(去除前后空格)
248
- const key = headerPair.substring(0, index).trim().toLowerCase();
249
- const value = headerPair.substring(index + 1).trim();
250
- // 保存到结果对象
251
- headers[key] = value;
252
- }
253
- });
254
- return headers;
255
- }
256
225
  destroy() {
257
226
  this.__isDestroyed = true;
258
227
  }
@@ -1,5 +1,5 @@
1
1
  import type { AxiosInstance } from 'axios';
2
- import { PolyvRequestConfig, RequestType } from './interface';
2
+ import type { PolyvRequestConfig, RequestType } from './interface/index';
3
3
  export type SetupAxiosInterceptorConfig = Pick<PolyvRequestConfig, 'baseUrl' | 'timeout' | 'requestPlugins'>;
4
4
  declare module 'axios' {
5
5
  interface AxiosRequestConfig {
@@ -37,7 +37,7 @@ export function setupAxiosInterceptor(axios, config = {}) {
37
37
  if (newOptions.requestType && options.method?.toLocaleUpperCase() === 'POST') {
38
38
  const reqTypes = {
39
39
  json: 'application/json;charset=UTF-8',
40
- form: 'application/x-www-form-urlencoded'
40
+ form: 'application/x-www-form-urlencoded',
41
41
  };
42
42
  options.headers.setAccept('*/*');
43
43
  options.headers.setContentType(reqTypes[newOptions.requestType]);
package/config.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { RequestType, ResponseType } from "./interface";
1
+ import type { RequestType, ResponseType } from './interface/index';
2
2
  export declare const DEFAULT_REQUEST_TYPE: RequestType;
3
3
  export declare const DEFAULT_RESPONSE_TYPE: ResponseType;
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './ajax';
2
2
  export * from './request-error';
3
- export * from './interface';
3
+ export * from './interface/index';
4
4
  export * from './utils';
5
- export * from './plugins';
5
+ export * from './plugins/index';
6
6
  export * from './plugins/plugin-controller';
7
7
  export * from './axios-interceptor';
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './ajax';
2
2
  export * from './request-error';
3
- export * from './interface';
3
+ export * from './interface/index';
4
4
  export * from './utils';
5
- export * from './plugins';
5
+ export * from './plugins/index';
6
6
  export * from './plugins/plugin-controller';
7
7
  export * from './axios-interceptor';
@@ -1,4 +1,4 @@
1
- import { RequestPlugin } from '../plugins';
1
+ import type { RequestPlugin } from '../plugins/index';
2
2
  export * from './polyv';
3
3
  export type BaseUrlFn = () => string;
4
4
  export interface PolyvRequestConfig<Options extends RequestOptions = RequestOptions> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/request-core",
3
- "version": "2.7.1",
3
+ "version": "2.8.0",
4
4
  "main": "./index.js",
5
5
  "peerDependencies": {
6
6
  "axios": ">=1.0.0"
@@ -11,11 +11,12 @@
11
11
  }
12
12
  },
13
13
  "dependencies": {
14
- "@polyv/utils-net": "^3.2.0",
15
- "@polyv/utils-querystring": "^3.2.0",
16
- "@polyv/utils-request": "^3.2.0",
17
- "md5": "2.3.0",
18
- "sha256": "0.2.0"
14
+ "@polyv/utils-env": "^3.3.0",
15
+ "@polyv/utils-net": "^3.3.0",
16
+ "@polyv/utils-querystring": "^3.3.0",
17
+ "@polyv/utils-request": "^3.3.0"
19
18
  },
20
- "types": "./index.d.ts"
19
+ "types": "./index.d.ts",
20
+ "type": "module",
21
+ "miniprogram": "./"
21
22
  }
@@ -1,5 +1,6 @@
1
- import { RequestOptions, RequestResult } from '../interface';
2
- import { PluginController } from './plugin-controller';
1
+ import type { RequestOptions, RequestResult } from '../interface/index';
2
+ import type { PluginController } from './plugin-controller';
3
+ export type PluginInterceptReturn = Promise<object | void> | object | void;
3
4
  /**
4
5
  * 请求插件
5
6
  */
@@ -20,25 +21,25 @@ export interface RequestPlugin<Options extends RequestOptions = RequestOptions>
20
21
  * @param options 请求选项
21
22
  * @returns 请求选项
22
23
  */
23
- interceptIncludeParams?: (options: Options) => Promise<object | void> | object | void;
24
+ interceptIncludeParams?: (options: Options) => PluginInterceptReturn;
24
25
  /**
25
26
  * 请求授权
26
27
  * @param options 请求选项
27
28
  * @returns 请求选项
28
29
  */
29
- interceptAuthorizeRequest?: (options: Options) => Promise<Options | void> | Options | void;
30
+ interceptAuthorizeRequest?: (options: Options) => PluginInterceptReturn;
30
31
  /**
31
32
  * 请求加密
32
33
  * @param options 请求选项
33
34
  * @returns 请求选项
34
35
  */
35
- interceptEncryptRequest?: (options: Options) => Promise<Options | void> | Options | void;
36
+ interceptEncryptRequest?: (options: Options) => PluginInterceptReturn;
36
37
  /**
37
38
  * 请求拦截器
38
39
  * @param options 请求选项
39
40
  * @returns 请求选项
40
41
  */
41
- interceptRequest?: (options: Options) => Promise<Options | void> | Options | void;
42
+ interceptRequest?: (options: Options) => PluginInterceptReturn;
42
43
  /**
43
44
  * 响应解密
44
45
  * @param result 请求结果
@@ -1,5 +1,5 @@
1
- import { RequestOptions, RequestResult } from "../interface";
2
- import { RequestPlugin } from "./index";
1
+ import type { RequestOptions, RequestResult } from '../interface/index';
2
+ import type { RequestPlugin } from './index';
3
3
  export declare class PluginController {
4
4
  /** 请求插件 */
5
5
  private __requestPlugins;
package/utils.js CHANGED
@@ -12,10 +12,10 @@ function isBlobLike(value) {
12
12
  return true;
13
13
  }
14
14
  const blobLike = value;
15
- return typeof blobLike.size === 'number'
16
- && typeof blobLike.type === 'string'
17
- && typeof blobLike.slice === 'function'
18
- && typeof blobLike.arrayBuffer === 'function';
15
+ return (typeof blobLike.size === 'number' &&
16
+ typeof blobLike.type === 'string' &&
17
+ typeof blobLike.slice === 'function' &&
18
+ typeof blobLike.arrayBuffer === 'function');
19
19
  }
20
20
  /**
21
21
  * 是否存在 File 值