Installations
npm install @alicloud/console-fetcher-interceptor-res-biz
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.11.0
NPM Version
lerna/6.6.0/node@v18.11.0+arm64 (darwin)
Score
70.1
Supply Chain
86.2
Quality
81.7
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (90.14%)
Less (9.02%)
JavaScript (0.83%)
Developer
aliyun
Download Statistics
Total Downloads
36,177
Last Day
11
Last Week
52
Last Month
263
Last Year
3,310
GitHub Statistics
51 Stars
1,989 Commits
13 Forks
9 Watching
14 Branches
8 Contributors
Bundle Size
1.27 kB
Minified
534.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.4.9
Package Id
@alicloud/console-fetcher-interceptor-res-biz@1.4.9
Unpacked Size
21.86 kB
Size
6.42 kB
File Count
49
NPM Version
lerna/6.6.0/node@v18.11.0+arm64 (darwin)
Node Version
18.11.0
Publised On
24 Mar 2023
Total Downloads
Cumulative downloads
Total Downloads
36,177
Last day
22.2%
11
Compared to previous day
Last week
67.7%
52
Compared to previous week
Last month
36.3%
263
Compared to previous month
Last year
-66%
3,310
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@alicloud/console-fetcher-interceptor-res-biz
@alicloud/console-fetcher
的响应拦截器,封装业务错误。
- 扩展
FetcherConfig
增加可选方法(一般情况下不需要设置,除非「非正常」场景)isSuccess(o: IBizJson): boolean
getData(o: IBizJson): T
getCode(o: IBizJson): string
getRequestId(o: IBizJson): string
getTitle(o: IBizJson): string
getMessage(o: IBizJson): string
阿里云控制台的 API 请求一般会以如下形式返回:
1interface IBizJson<T> { 2 code: string; 3 requestId: string; 4 data?: T; 5 title?: string; 6 message?: string; 7}
其中 code
为 '200'
(有些接口会是数字 200
)的时候表示业务逻辑是成功的,这时候可以拿到 data
;否则表示业务逻辑错误,这个时候可以拿到 message
。
INSTALL
1tnpm i @alicloud/console-fetcher-interceptor-res-biz -S
APIs
1import createFetcher, { 2 Fetcher 3} from '@alicloud/fetcher'; 4// import interceptors 1 5import intercept, { 6 FetcherConfigExtended 7} from '@alicloud/console-fetcher-interceptor-res-biz'; 8// import interceptors 2 9 10const fetcher: Fetcher<FetcherConfigExtended> = createFetcher<FetcherConfigExtended>(); 11 12// ... add interceptors 1 13intercept(fetcher); 14// ... add interceptors 2 15 16export default fetcher
对 @alicloud/fetcher
的扩展
FetcherConfig
可以在 config 对象上传入新增参数:
1interface FetcherConfigExtra { 2 /** 3 * 判断请求是否成功,默认判断 `json.code === '200' || json.code === 200` 4 * 5 * - `boolean` 直接成功或失败 6 * - `(json: any) => boolean` 根据原始 json 对象进行自定义判断 7 */ 8 isSuccess?: boolean | ((json: any) => boolean); 9 /** 10 * 提取最终需要的数据,默认 `json.data` 11 * 12 * - `string` 自定义数据字段,如 `'DATA'` 则表示获取 `json.DATA` 13 * - `(json: any) => any` 从原始 json 对象进行自定义提取 14 */ 15 getData?: string | ((json: any) => any); 16 /** 17 * 当 `isSuccess` 判定为失败时,从数据中提取错误 code,默认 `json.code` 18 * 19 * - `string` 自定义数据字段,如 `'DATA'` 则表示获取 `json.DATA` 20 * - `(json: any) => any` 从原始 json 对象进行自定义提取 21 */ 22 getCode?: string | ((json: any) => string); 23 /** 24 * 当 `isSuccess` 判定为失败时,从数据中提取错误 message,默认 `json.message` 25 * 26 * - `string` 自定义数据字段,如 `'MESSAGE'` 则表示获取 `json.MESSAGE` 27 * - `(json: any) => any` 从原始 json 对象进行自定义提取 28 */ 29 getMessage?: string | ((json: any) => string); 30}
错误名
ERROR_BIZ = 'FetcherErrorBiz'
如何覆盖默认设置
方法 1 - 创建实例时传入默认值
假设 ~
是你项目下 src
的 alias。
创建自己的 Fetcher 实例,传入默认值:
1import createFetcher, { 2 Fetcher 3} from '@alicloud/fetcher'; 4// import interceptors 1 5import intercept, { 6 FetcherConfigExtended 7} from '@alicloud/console-fetcher-interceptor-res-biz'; 8// import interceptors 2 9 10const fetcher: Fetcher<FetcherConfigExtended> = createFetcher<FetcherConfigExtended>({ 11 isSuccess?, 12 getData?, 13 getCode?, 14 getRequestId?, 15 getTitle?, 16 getMessage? 17}); 18 19// ... add interceptors 1 20intercept(fetcher); 21// ... add interceptors 2 22 23export default fetcher;
方法 2 - 调用的时候传入覆盖
1import fetcher from '~/util/fetcher'; // 假设这是你项目下的 fetcher 文件路径 2 3interface IResult { 4 id: string; 5 name: string; 6} 7interface IBody { 8 id: string; 9} 10 11fetcher.request<IResult>({ 12 url: '____url____', 13 isSuccess?, 14 getData?, 15 getCode?, 16 getRequestId?, 17 getTitle?, 18 getMessage? 19}); 20 21fetcher.post<IResult, IBody>({ 22 isSuccess?, 23 getData?, 24 getCode?, 25 getRequestId?, 26 getTitle?, 27 getMessage? 28}, '____url____', { 29 id: '____id____' 30}); 31 32// 假设有一个 JSONP 请求,它的返回直接就是数据(即没有业务错误): 33 34fetcher.jsonp({ 35 isSuccess: true, 36 getData: json => json 37}, '____url____')
No vulnerabilities found.
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
no binaries found in the repo
Reason
Found 6/16 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 21 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More