1.4.9 • Published 2 years ago

@alicloud/console-fetcher-interceptor-res-biz v1.4.9

Weekly downloads
43
License
MIT
Repository
github
Last release
2 years ago

@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 请求一般会以如下形式返回:

interface IBizJson<T> {
  code: string;
  requestId: string;
  data?: T;
  title?: string;
  message?: string;
}

其中 code'200'(有些接口会是数字 200)的时候表示业务逻辑是成功的,这时候可以拿到 data;否则表示业务逻辑错误,这个时候可以拿到 message

INSTALL

tnpm i @alicloud/console-fetcher-interceptor-res-biz -S

APIs

import createFetcher, {
  Fetcher
} from '@alicloud/fetcher';
// import interceptors 1
import intercept, {
  FetcherConfigExtended
} from '@alicloud/console-fetcher-interceptor-res-biz';
// import interceptors 2

const fetcher: Fetcher<FetcherConfigExtended> = createFetcher<FetcherConfigExtended>();

// ... add interceptors 1  
intercept(fetcher);
// ... add interceptors 2

export default fetcher

@alicloud/fetcher 的扩展

FetcherConfig

可以在 config 对象上传入新增参数:

interface FetcherConfigExtra {
  /**
   * 判断请求是否成功,默认判断 `json.code === '200' || json.code === 200`
   * 
   * - `boolean` 直接成功或失败
   * - `(json: any) => boolean` 根据原始 json 对象进行自定义判断
   */
  isSuccess?: boolean | ((json: any) => boolean);
  /**
   * 提取最终需要的数据,默认 `json.data`
   * 
   * - `string` 自定义数据字段,如 `'DATA'` 则表示获取 `json.DATA`
   * - `(json: any) => any` 从原始 json 对象进行自定义提取
   */
  getData?: string | ((json: any) => any);
  /**
   * 当 `isSuccess` 判定为失败时,从数据中提取错误 code,默认 `json.code`
   * 
   * - `string` 自定义数据字段,如 `'DATA'` 则表示获取 `json.DATA`
   * - `(json: any) => any` 从原始 json 对象进行自定义提取
   */
  getCode?: string | ((json: any) => string);
  /**
   * 当 `isSuccess` 判定为失败时,从数据中提取错误 message,默认 `json.message`
   * 
   * - `string` 自定义数据字段,如 `'MESSAGE'` 则表示获取 `json.MESSAGE`
   * - `(json: any) => any` 从原始 json 对象进行自定义提取
   */
  getMessage?: string | ((json: any) => string);
}

错误名

ERROR_BIZ = 'FetcherErrorBiz'

如何覆盖默认设置

方法 1 - 创建实例时传入默认值

假设 ~ 是你项目下 src 的 alias。

创建自己的 Fetcher 实例,传入默认值:

import createFetcher, {
  Fetcher
} from '@alicloud/fetcher';
// import interceptors 1
import intercept, {
  FetcherConfigExtended
} from '@alicloud/console-fetcher-interceptor-res-biz';
// import interceptors 2

const fetcher: Fetcher<FetcherConfigExtended> = createFetcher<FetcherConfigExtended>({
  isSuccess?,
  getData?,
  getCode?,
  getRequestId?,
  getTitle?,
  getMessage?
});

// ... add interceptors 1  
intercept(fetcher);
// ... add interceptors 2

export default fetcher;

方法 2 - 调用的时候传入覆盖

import fetcher from '~/util/fetcher'; // 假设这是你项目下的 fetcher 文件路径

interface IResult {
  id: string;
  name: string;
}
interface IBody {
  id: string;
}

fetcher.request<IResult>({
  url: '____url____',
  isSuccess?,
  getData?,
  getCode?,
  getRequestId?,
  getTitle?,
  getMessage?
});

fetcher.post<IResult, IBody>({
  isSuccess?,
  getData?,
  getCode?,
  getRequestId?,
  getTitle?,
  getMessage?
}, '____url____', {
  id: '____id____'
});

// 假设有一个 JSONP 请求,它的返回直接就是数据(即没有业务错误):

fetcher.jsonp({
  isSuccess: true,
  getData: json => json
}, '____url____')
1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.6

2 years ago

1.4.4

2 years ago

1.4.2

2 years ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.0

3 years ago

1.2.1

2 years ago

1.1.85-alpha.0

3 years ago

1.1.84

3 years ago

1.3.0

2 years ago

1.1.82-alpha.4

3 years ago

1.1.82-alpha.3

3 years ago

1.1.82-alpha.2

3 years ago

1.1.78

3 years ago

1.1.81

3 years ago

1.1.80

3 years ago

1.1.83

3 years ago

1.1.82

3 years ago

1.1.62

3 years ago

1.1.61

3 years ago

1.1.60

3 years ago

1.1.59

3 years ago

1.1.58

3 years ago

1.1.53-alpha.4

3 years ago

1.1.56

3 years ago

1.1.55

3 years ago

1.1.54

3 years ago

1.1.53-alpha.3

3 years ago

1.1.52

3 years ago

1.1.49

3 years ago

1.1.48

3 years ago

1.1.51

3 years ago

1.1.47

3 years ago

1.1.46

3 years ago

1.1.45

3 years ago

1.1.41

3 years ago

1.1.43

3 years ago

1.1.42

3 years ago

1.1.39

3 years ago

1.1.40

3 years ago

1.1.34

3 years ago

1.1.31

3 years ago

1.1.38

3 years ago

1.1.36

3 years ago

1.1.35

3 years ago

1.1.30

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.22

4 years ago

1.1.24

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.9

4 years ago

1.1.10

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.50

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.45

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.39

4 years ago

1.0.40

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.29

4 years ago

1.0.30

4 years ago

1.0.28

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago