# HTTP 客户端

JSKit App 提供了 http 模块,丰富的网络资源可为你的 JSKit 小程序提供无限可能。

declare module "http" {
    export type Method =
        | 'get' | 'GET'
        | 'delete' | 'DELETE'
        | 'head' | 'HEAD'
        | 'post' | 'POST'
        | 'put' | 'PUT'
        | 'patch' | 'PATCH';

    export type ResponseType =
        | 'text';

    export interface JSKRequest {
        url: string;
        method?: Method;
        baseURL?: string;
        headers?: any;
        params?: any;
        data?: any;
        timeout?: number;
        responseType?: ResponseType;
    }

    export interface JSKResponse {
        data: any;
        status: number;
        statusText: string;
        headers: any;
    }

    export function request(req: JSKRequest): Promise<JSKResponse>;
}

进一步的说明:

  • 其中对 Promise reject 的情况:respones 除了常用的 HTTP 错误码,JSKit 另外使用了 -1 作为请求 http 时出错的错误码,-2 作为请求参数错误的错误码。当出现负数错误码时, data 和 headers 的值为 null。
  • 到目前为止,只支持了 responseType 为 text 的类型,如返回的是 json字符串,可使用 JSON.parse (opens new window) 来解析成 JS 对象。后续会提供其他类型返回类型的支持,敬请期待!
  • 通过 JSKit 的订阅仓库中的扩展已支持 axios (opens new window) http 网络库。