Home Reference Source
import StandardHttpClient from 'standard-http-client/src/standard-http-client.js'
public class | source

StandardHttpClient

符合接口规范的 HTTP 客户端

See:

Constructor Summary

Public Constructor
public

constructor(config: AxiosRequestConfig)

Member Summary

Public Members
public

agent: AxiosInstance

Method Summary

Public Methods
public abstract

afterSend(responseOrError: AxiosResponse | AxiosError)

请求完成之后统一要做的事情

public abstract

beforeSend(config: AxiosRequestConfig)

发送请求之前统一要做的事情

public abstract

handleError(error: AxiosError)

请求出错之后如何处理错误

public

send(config: AxiosRequestConfig): Promise

发送请求

public

使用拦截器

Private Methods
private

_adapterDataOption(扩展的: AxiosRequestConfig)

将 config._data 适配为 config.params 和 config.data

private

描述客户端错误

private

通过拦截器描述请求的错误信息

private

_dispatchRequest(config: AxiosRequestConfig): Promise

Dispatch a request to the server.

private

通过拦截器处理请求的错误

private

_hook()

通过拦截器增加发送请求的 hook

private

_isApiSuccess(response: AxiosResponse): boolean

判断接口调用是否成功

private

通过拦截器判断接口调用是否成功

private

_jsonp(config: object): Promise

通过 JSONP 发送请求

private

通过拦截器输出请求的错误日志

Public Constructors

public constructor(config: AxiosRequestConfig) source

Params:

NameTypeAttributeDescription
config AxiosRequestConfig

Public Members

public agent: AxiosInstance source

Public Methods

public abstract afterSend(responseOrError: AxiosResponse | AxiosError) source

请求完成之后统一要做的事情

Params:

NameTypeAttributeDescription
responseOrError AxiosResponse | AxiosError

public abstract beforeSend(config: AxiosRequestConfig) source

发送请求之前统一要做的事情

Params:

NameTypeAttributeDescription
config AxiosRequestConfig

public abstract handleError(error: AxiosError) source

请求出错之后如何处理错误

Params:

NameTypeAttributeDescription
error AxiosError

public send(config: AxiosRequestConfig): Promise source

发送请求

Params:

NameTypeAttributeDescription
config AxiosRequestConfig
  • optional
  • default: {}

扩展的 AxiosRequestConfig

config._data object
  • optional

实现类似 jQuery.ajax 的 data 配置项机制

config._jsonp boolean
  • optional

是否通过 JSONP 来发送请求(注意此时 config 仅支持 baseURL, url, params, timeout, transformResponse 参数)

config._jsonpCallback string
  • optional

name of the query string parameter to specify the callback(defaults to callback)

Return:

Promise

public useInterceptors() source

使用拦截器

子类可以继承此方法来添加自己的拦截器

Private Methods

private _adapterDataOption(扩展的: AxiosRequestConfig) source

将 config._data 适配为 config.params 和 config.data

当为 post/put/patch 请求时会将 config._data 转成 URL 编码的字符串

Params:

NameTypeAttributeDescription
扩展的 AxiosRequestConfig

AxiosRequestConfig

config._data object

实现类似 jQuery.ajax 的 data 配置项机制

private _descClientError(error: Error) source

描述客户端错误

Params:

NameTypeAttributeDescription
error Error

private _descResponseError() source

通过拦截器描述请求的错误信息

private _dispatchRequest(config: AxiosRequestConfig): Promise source

Dispatch a request to the server.

Params:

NameTypeAttributeDescription
config AxiosRequestConfig

Return:

Promise

private _handleError() source

通过拦截器处理请求的错误

private _hook() source

通过拦截器增加发送请求的 hook

                    ┌─> 成功 ─> afterSend
beforeSend ─> send ─┤
                    └─> 失败 ─> afterSend

private _isApiSuccess(response: AxiosResponse): boolean source

判断接口调用是否成功

Params:

NameTypeAttributeDescription
response AxiosResponse

Return:

boolean

private _isResponseSuccess() source

通过拦截器判断接口调用是否成功

private _jsonp(config: object): Promise source

通过 JSONP 发送请求

Params:

NameTypeAttributeDescription
config object
config.url string
config.baseURL string
  • optional
config.params object
  • optional
config.timeout number
  • optional
config.transformResponse Array<Function>
  • optional
config._jsonpCallback string
  • optional

Return:

Promise

private _logResponseError() source

通过拦截器输出请求的错误日志