feat(升级包): request、slave
This commit is contained in:
parent
b433d5f182
commit
35497d39ad
@ -1,5 +1,13 @@
|
||||
# @zhst/biz
|
||||
|
||||
## 0.11.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
- @zhst/hooks@0.8.10
|
||||
- @zhst/meta@0.9.7
|
||||
|
||||
## 0.11.2
|
||||
|
||||
### Patch Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/biz",
|
||||
"version": "0.11.2",
|
||||
"version": "0.11.3",
|
||||
"description": "业务库",
|
||||
"keywords": [
|
||||
"business",
|
||||
|
@ -1,5 +1,13 @@
|
||||
# @zhst/utils
|
||||
|
||||
## 0.9.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies
|
||||
- Updated dependencies
|
||||
- @zhst/request@0.9.0
|
||||
|
||||
## 0.9.0
|
||||
|
||||
### Minor Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/func",
|
||||
"version": "0.9.0",
|
||||
"version": "0.9.1",
|
||||
"description": "函数合集",
|
||||
"keywords": [
|
||||
"hooks"
|
||||
|
@ -1,5 +1,11 @@
|
||||
# @zhst/hooks
|
||||
|
||||
## 0.8.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
|
||||
## 0.8.9
|
||||
|
||||
### Patch Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/hooks",
|
||||
"version": "0.8.9",
|
||||
"version": "0.8.10",
|
||||
"description": "hooks合集",
|
||||
"keywords": [
|
||||
"hooks"
|
||||
|
@ -1,5 +1,14 @@
|
||||
# @zhst/material
|
||||
|
||||
## 0.7.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
- @zhst/biz@0.11.3
|
||||
- @zhst/hooks@0.8.10
|
||||
- @zhst/meta@0.9.7
|
||||
|
||||
## 0.7.4
|
||||
|
||||
### Patch Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/material",
|
||||
"version": "0.7.4",
|
||||
"version": "0.7.5",
|
||||
"description": "物料库",
|
||||
"keywords": [
|
||||
"business",
|
||||
|
@ -1,5 +1,13 @@
|
||||
# @zhst/utils
|
||||
|
||||
## 0.9.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
- @zhst/hooks@0.8.10
|
||||
- @zhst/meta@0.9.7
|
||||
|
||||
## 0.9.6
|
||||
|
||||
### Patch Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/meta",
|
||||
"version": "0.9.6",
|
||||
"version": "0.9.7",
|
||||
"description": "原子组件",
|
||||
"keywords": [
|
||||
"meta",
|
||||
|
@ -1,5 +1,16 @@
|
||||
# @zhst/request
|
||||
|
||||
## 0.9.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 新包发布
|
||||
- 修改配置
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
|
||||
## 0.8.9
|
||||
|
||||
### Patch Changes
|
||||
|
@ -1,21 +1,36 @@
|
||||
# @zhst/request
|
||||
|
||||
:::info{title=开发中}
|
||||
还在构思最佳使用方案...
|
||||
:::
|
||||
|
||||
|
||||
## 介绍
|
||||
|
||||
zhst 请求库
|
||||
请求库配置,创建初衷是为了统一项目中引用的 request 和 useRequest 的配置。
|
||||
|
||||
## 安装
|
||||
|
||||
> pnpm install @zhst/request
|
||||
```js
|
||||
pnpm install @zhst/request
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
```js
|
||||
```jsx
|
||||
import React from 'react';
|
||||
import request from '@zhst/request'
|
||||
import { reqConfig } from '@zhst/request'
|
||||
|
||||
const config = reqConfig()
|
||||
|
||||
console.log('配置', config)
|
||||
|
||||
export default () => {
|
||||
return (
|
||||
<div>从控制台打印查看</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
参数可以参考 umi 的 request 配置
|
||||
|
||||
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| baseURL | 请求的根域名 | string | location.host | - |
|
||||
|
30
packages/request/es/index.d.ts
vendored
30
packages/request/es/index.d.ts
vendored
@ -1,4 +1,28 @@
|
||||
interface RequestConfig {
|
||||
export interface ReqConfigProps {
|
||||
timeout?: number;
|
||||
baseURL?: string;
|
||||
errorConfig?: {
|
||||
errorHandler?: (error: any, opts: any) => void;
|
||||
errorThrower?: (err: any) => void;
|
||||
};
|
||||
authorization: string;
|
||||
showMsg?: boolean;
|
||||
}
|
||||
export declare const request: RequestConfig;
|
||||
export {};
|
||||
export declare const reqConfig: (config: ReqConfigProps) => {
|
||||
timeout: number;
|
||||
baseURL: string;
|
||||
errorConfig: {
|
||||
errorHandler?: ((error: any, opts: any) => void) | undefined;
|
||||
errorThrower?: ((err: any) => void) | undefined;
|
||||
};
|
||||
authorization: string;
|
||||
showMsg?: boolean | undefined;
|
||||
requestInterceptors: (((url: any, options: any) => {
|
||||
url: any;
|
||||
options: any;
|
||||
}) | ((error: any) => Promise<never>))[][];
|
||||
responseInterceptors: ((response: {
|
||||
status: number;
|
||||
data: any;
|
||||
}) => any)[][];
|
||||
};
|
||||
|
@ -4,12 +4,19 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
||||
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
||||
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
||||
export var request = {
|
||||
import { message } from 'antd';
|
||||
export var reqConfig = function reqConfig(config) {
|
||||
var _ref = config || {},
|
||||
authorization = _ref.authorization,
|
||||
showMsg = _ref.showMsg;
|
||||
return _objectSpread({
|
||||
timeout: 1000,
|
||||
baseURL: location.origin,
|
||||
// other axios options you want
|
||||
errorConfig: {
|
||||
// @ts-ignore
|
||||
errorHandler: function errorHandler(error, opts) {
|
||||
console.log('first', error, opts);
|
||||
// if (opts?.skipErrorHandler) throw error
|
||||
// try {
|
||||
// const { res } = ctx;
|
||||
@ -53,13 +60,12 @@ export var request = {
|
||||
requestInterceptors: [
|
||||
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
||||
[function (url, options) {
|
||||
// console.log('options', options);
|
||||
var Authorization = authorization || localStorage.getItem('ZHST_AUTH_TOKEN') || '';
|
||||
return {
|
||||
url: url,
|
||||
options: _objectSpread(_objectSpread({}, options), {}, {
|
||||
headers: {
|
||||
authorization: 'test'
|
||||
// ...(refererSuffix ? { zhst_referer: `${baseUrl}${refererSuffix}` } : {}),
|
||||
Authorization: Authorization
|
||||
}
|
||||
})
|
||||
};
|
||||
@ -73,8 +79,15 @@ export var request = {
|
||||
if (response.status !== 200) {
|
||||
throw Error(JSON.stringify(response.data));
|
||||
}
|
||||
return response;
|
||||
var _ref2 = response.data || {},
|
||||
code = _ref2.code;
|
||||
if (showMsg && code !== 200) {
|
||||
message.error(response.data.message || '请求失败!');
|
||||
return null;
|
||||
}
|
||||
return response.data;
|
||||
}, function (error) {
|
||||
return Promise.reject(error);
|
||||
}]]
|
||||
}, config);
|
||||
};
|
30
packages/request/lib/index.d.ts
vendored
30
packages/request/lib/index.d.ts
vendored
@ -1,4 +1,28 @@
|
||||
interface RequestConfig {
|
||||
export interface ReqConfigProps {
|
||||
timeout?: number;
|
||||
baseURL?: string;
|
||||
errorConfig?: {
|
||||
errorHandler?: (error: any, opts: any) => void;
|
||||
errorThrower?: (err: any) => void;
|
||||
};
|
||||
authorization: string;
|
||||
showMsg?: boolean;
|
||||
}
|
||||
export declare const request: RequestConfig;
|
||||
export {};
|
||||
export declare const reqConfig: (config: ReqConfigProps) => {
|
||||
timeout: number;
|
||||
baseURL: string;
|
||||
errorConfig: {
|
||||
errorHandler?: ((error: any, opts: any) => void) | undefined;
|
||||
errorThrower?: ((err: any) => void) | undefined;
|
||||
};
|
||||
authorization: string;
|
||||
showMsg?: boolean | undefined;
|
||||
requestInterceptors: (((url: any, options: any) => {
|
||||
url: any;
|
||||
options: any;
|
||||
}) | ((error: any) => Promise<never>))[][];
|
||||
responseInterceptors: ((response: {
|
||||
status: number;
|
||||
data: any;
|
||||
}) => any)[][];
|
||||
};
|
||||
|
@ -19,15 +19,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
||||
// src/index.tsx
|
||||
var src_exports = {};
|
||||
__export(src_exports, {
|
||||
request: () => request
|
||||
reqConfig: () => reqConfig
|
||||
});
|
||||
module.exports = __toCommonJS(src_exports);
|
||||
var request = {
|
||||
var import_antd = require("antd");
|
||||
var reqConfig = (config) => {
|
||||
const {
|
||||
authorization,
|
||||
showMsg
|
||||
} = config || {};
|
||||
return {
|
||||
timeout: 1e3,
|
||||
baseURL: location.origin,
|
||||
// other axios options you want
|
||||
errorConfig: {
|
||||
// @ts-ignore
|
||||
errorHandler(error, opts) {
|
||||
console.log("first", error, opts);
|
||||
},
|
||||
// @ts-ignore
|
||||
errorThrower(res) {
|
||||
@ -39,13 +47,13 @@ var request = {
|
||||
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
||||
[
|
||||
(url, options) => {
|
||||
const Authorization = authorization || localStorage.getItem("ZHST_AUTH_TOKEN") || "";
|
||||
return {
|
||||
url,
|
||||
options: {
|
||||
...options,
|
||||
headers: {
|
||||
authorization: "test"
|
||||
// ...(refererSuffix ? { zhst_referer: `${baseUrl}${refererSuffix}` } : {}),
|
||||
Authorization
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -63,15 +71,22 @@ var request = {
|
||||
if (response.status !== 200) {
|
||||
throw Error(JSON.stringify(response.data));
|
||||
}
|
||||
return response;
|
||||
const { code } = response.data || {};
|
||||
if (showMsg && code !== 200) {
|
||||
import_antd.message.error(response.data.message || "请求失败!");
|
||||
return null;
|
||||
}
|
||||
return response.data;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
]
|
||||
]
|
||||
],
|
||||
...config
|
||||
};
|
||||
};
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
request
|
||||
reqConfig
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/request",
|
||||
"version": "0.8.9",
|
||||
"version": "0.9.0",
|
||||
"description": "请求库",
|
||||
"keywords": [
|
||||
"request",
|
||||
@ -14,7 +14,7 @@
|
||||
"lib/**/style/*",
|
||||
"*.less"
|
||||
],
|
||||
"main": "lib/index.ts",
|
||||
"main": "lib/index.js",
|
||||
"module": "es/index.js",
|
||||
"typings": "es/index.d.ts",
|
||||
"exports": {
|
||||
@ -37,11 +37,7 @@
|
||||
"registry": "http://10.0.0.77:4874"
|
||||
},
|
||||
"dependencies": {
|
||||
"@zhst/func": "workspace:^",
|
||||
"base-64": "^1.0.0",
|
||||
"umi": "^4.1.2"
|
||||
"@zhst/func": "workspace:^"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/base-64": "^1.0.2"
|
||||
}
|
||||
"devDependencies": {}
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
---
|
||||
nav:
|
||||
title: 其它
|
||||
order: 999
|
||||
title: 请求库
|
||||
toc: content
|
||||
---
|
||||
|
10
packages/request/src/index.md
Normal file
10
packages/request/src/index.md
Normal file
@ -0,0 +1,10 @@
|
||||
---
|
||||
nav:
|
||||
title: 其它
|
||||
order: 999
|
||||
title: 快速上手
|
||||
order: 1
|
||||
toc: content
|
||||
---
|
||||
|
||||
## 请求库
|
@ -1,13 +1,30 @@
|
||||
interface RequestConfig {
|
||||
import { message } from 'antd'
|
||||
|
||||
export interface ReqConfigProps {
|
||||
timeout?: number
|
||||
baseURL?: string
|
||||
errorConfig?: {
|
||||
errorHandler?: (error: any, opts: any) => void
|
||||
errorThrower?: (err: any) => void
|
||||
}
|
||||
authorization?: string
|
||||
showMsg?: boolean
|
||||
}
|
||||
|
||||
export const request: RequestConfig = {
|
||||
export const reqConfig = (config: ReqConfigProps) => {
|
||||
const {
|
||||
authorization,
|
||||
showMsg
|
||||
} = config || {}
|
||||
|
||||
return {
|
||||
timeout: 1000,
|
||||
baseURL: location.origin,
|
||||
// other axios options you want
|
||||
errorConfig: {
|
||||
// @ts-ignore
|
||||
errorHandler(error: any, opts: any) {
|
||||
console.log('first', error, opts);
|
||||
// if (opts?.skipErrorHandler) throw error
|
||||
// try {
|
||||
// const { res } = ctx;
|
||||
@ -52,14 +69,14 @@ export const request: RequestConfig = {
|
||||
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
||||
[
|
||||
(url: any, options: any) => {
|
||||
// console.log('options', options);
|
||||
const Authorization = authorization || localStorage.getItem('ZHST_AUTH_TOKEN') || ''
|
||||
|
||||
return {
|
||||
url,
|
||||
options: {
|
||||
...options,
|
||||
headers: {
|
||||
authorization: 'test',
|
||||
// ...(refererSuffix ? { zhst_referer: `${baseUrl}${refererSuffix}` } : {}),
|
||||
Authorization,
|
||||
},
|
||||
},
|
||||
};
|
||||
@ -77,11 +94,19 @@ export const request: RequestConfig = {
|
||||
if (response.status !== 200) {
|
||||
throw Error(JSON.stringify(response.data));
|
||||
}
|
||||
return response;
|
||||
const { code } = response.data || {}
|
||||
|
||||
if (showMsg && code !== 200) {
|
||||
message.error(response.data.message || '请求失败!')
|
||||
return null
|
||||
}
|
||||
return response.data;
|
||||
},
|
||||
(error: any) => {
|
||||
return Promise.reject(error);
|
||||
},
|
||||
],
|
||||
],
|
||||
...config
|
||||
};
|
||||
}
|
||||
|
@ -1,5 +1,17 @@
|
||||
# @zhst/slave
|
||||
|
||||
## 0.5.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- slave 添加一些和 token 相关的方法
|
||||
|
||||
## 0.4.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- @zhst/func@0.9.1
|
||||
|
||||
## 0.4.0
|
||||
|
||||
### Minor Changes
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@zhst/slave",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.0",
|
||||
"description": "微前端子应用方法库",
|
||||
"keywords": [
|
||||
"slave",
|
||||
|
@ -9,6 +9,12 @@ export default () => {
|
||||
}
|
||||
|
||||
return (
|
||||
<button onClick={handleInit} >测试</button>
|
||||
<div>
|
||||
<button onClick={handleInit} >初始化</button>
|
||||
<button onClick={() => slave.logOut()}>退出登录</button>
|
||||
<button onClick={() => slave.getToken()}>获取token</button>
|
||||
<button onClick={() => slave.setToken('ppt')}>设置token</button>
|
||||
<button onClick={() => slave.removeToken()}>移除token</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
@ -30,3 +30,6 @@ title: 快速上手
|
||||
| --- | --- | --- | --- | --- |
|
||||
| init | 初始化实例 | function | (data: SlaveProps) => void | - |
|
||||
| logOut | 退出登录 | function | () => void | - |
|
||||
| getToken | 获取token | function | () => void | - |
|
||||
| setToken | 设置token | function | (token: string) => void | - |
|
||||
| removeToken | 移除token | function | () => void | - |
|
||||
|
@ -87,7 +87,7 @@ class Slave {
|
||||
}
|
||||
|
||||
// 检查是否为url,并且跑出错误
|
||||
checkUrl(url: string, label: string) {
|
||||
public checkUrl(url: string, label: string) {
|
||||
if (!isUrl(url)) {
|
||||
throw Error(`请输入正确的 ${label} 链接地址,以http(s)://开头!'`)
|
||||
}
|
||||
|
1206
pnpm-lock.yaml
1206
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user