@polyv/request-plugin-global-params 2.0.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 +54 -0
- package/global-params.d.ts +13 -0
- package/global-params.js +28 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +9 -0
- package/request-options.d.ts +16 -0
- package/request-options.js +1 -0
- package/types.d.ts +12 -0
- package/types.js +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# GlobalParamsRequestPlugin
|
|
2
|
+
|
|
3
|
+
用途:全局 `params` 参数插件,使用后会在所有请求的 params 中插入 `globalParams` 的数据。
|
|
4
|
+
|
|
5
|
+
## 实例化参数
|
|
6
|
+
|
|
7
|
+
| 参数名 | 用途 | 类型 | 默认值 |
|
|
8
|
+
| - | - | - | - |
|
|
9
|
+
| `useGlobalParams` | 是否使用全局 params 参数 | `boolean` | `true` |
|
|
10
|
+
| `globalParams` | 全局参数 | `object \| function` | - |
|
|
11
|
+
|
|
12
|
+
## 请求选项
|
|
13
|
+
|
|
14
|
+
| 参数名 | 用途 | 类型 | 默认值 |
|
|
15
|
+
| - | - | - | - |
|
|
16
|
+
| `useGlobalParams` | 是否使用全局 params 参数 | `boolean` | `true` |
|
|
17
|
+
|
|
18
|
+
## 使用方式
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { PolyvRequest } from '@polyv/request-core';
|
|
22
|
+
import { GlobalParamsRequestPlugin } from '@polyv/request-plugin-global-params';
|
|
23
|
+
|
|
24
|
+
// 直接传入对象
|
|
25
|
+
const requester = new PolyvRequest({
|
|
26
|
+
requestPlugins: [
|
|
27
|
+
new GlobalParamsRequestPlugin({
|
|
28
|
+
globalParams: {
|
|
29
|
+
viewerId: 'xxx',
|
|
30
|
+
channelId: 'xxx',
|
|
31
|
+
},
|
|
32
|
+
}),
|
|
33
|
+
]
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// 传入方法,支持返回 Promise
|
|
37
|
+
const requester = new PolyvRequest({
|
|
38
|
+
requestPlugins: [
|
|
39
|
+
new GlobalParamsRequestPlugin({
|
|
40
|
+
globalParams: async (options) => {
|
|
41
|
+
return {
|
|
42
|
+
viewerId: 'xxx',
|
|
43
|
+
channelId: 'xxx',
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// 如果你想在某个请求中,不带全局参数时
|
|
51
|
+
requester.get('/xxxx', {}, {
|
|
52
|
+
useGlobalParams: false,
|
|
53
|
+
});
|
|
54
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RequestOptions, RequestPlugin } from '@polyv/request-core';
|
|
2
|
+
import { GlobalParamsRequestPluginConfig } from './types';
|
|
3
|
+
export * from './types';
|
|
4
|
+
/**
|
|
5
|
+
* 全局 params 参数请求插件
|
|
6
|
+
*/
|
|
7
|
+
export declare class GlobalParamsRequestPlugin<Options extends RequestOptions = RequestOptions> implements RequestPlugin<Options> {
|
|
8
|
+
private __config;
|
|
9
|
+
private __useGlobalParams;
|
|
10
|
+
constructor(__config?: GlobalParamsRequestPluginConfig);
|
|
11
|
+
usePlugin(options: Options): boolean;
|
|
12
|
+
interceptIncludeParams(options: Options): Promise<object | undefined>;
|
|
13
|
+
}
|
package/global-params.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 全局 params 参数请求插件
|
|
4
|
+
*/
|
|
5
|
+
export class GlobalParamsRequestPlugin {
|
|
6
|
+
__config;
|
|
7
|
+
__useGlobalParams;
|
|
8
|
+
constructor(__config = {}) {
|
|
9
|
+
this.__config = __config;
|
|
10
|
+
const { useGlobalParams = true } = __config;
|
|
11
|
+
this.__useGlobalParams = useGlobalParams;
|
|
12
|
+
}
|
|
13
|
+
usePlugin(options) {
|
|
14
|
+
const { useGlobalParams = this.__useGlobalParams } = options;
|
|
15
|
+
return useGlobalParams;
|
|
16
|
+
}
|
|
17
|
+
async interceptIncludeParams(options) {
|
|
18
|
+
let globalParams;
|
|
19
|
+
if (typeof this.__config.globalParams === 'function') {
|
|
20
|
+
globalParams = await this.__config.globalParams(options);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
globalParams = this.__config.globalParams;
|
|
24
|
+
}
|
|
25
|
+
// 插入额外的 params
|
|
26
|
+
return globalParams;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface GlobalParamsRequestOptions {
|
|
2
|
+
/**
|
|
3
|
+
* 是否使用全局 params 参数
|
|
4
|
+
* @default true
|
|
5
|
+
* @plugin `GlobalParamsRequestPlugin`
|
|
6
|
+
*/
|
|
7
|
+
useGlobalParams?: boolean;
|
|
8
|
+
}
|
|
9
|
+
declare module '@polyv/request-core' {
|
|
10
|
+
interface RequestCustomOptions extends GlobalParamsRequestOptions {
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
declare module 'axios' {
|
|
14
|
+
interface AxiosRequestConfig extends GlobalParamsRequestOptions {
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RequestOptions, UniversalParams } from '@polyv/request-core';
|
|
2
|
+
export interface GlobalParamsRequestPluginConfig<Options extends RequestOptions = RequestOptions> {
|
|
3
|
+
/**
|
|
4
|
+
* 是否使用全局 params 参数
|
|
5
|
+
* @default true
|
|
6
|
+
*/
|
|
7
|
+
useGlobalParams?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* 全局参数
|
|
10
|
+
*/
|
|
11
|
+
globalParams?: UniversalParams | ((options: Options) => Promise<UniversalParams | undefined> | UniversalParams | undefined);
|
|
12
|
+
}
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|