1.0.7 • Published 3 years ago

@netless/fetch-middleware v1.0.7

Weekly downloads
23
License
MIT
Repository
-
Last release
3 years ago

@netless/fetch-middleware

一个带有请求超时功能的 fetch 中间件,方便大家做异常流程处理。

NPM JavaScript Style Guide

1. 说明

本项目技术选型为:React Typescript 打包工具为: rollup

2. 安装

npm install --save @netless/fetch-middleware

或者

yarn add @netless/fetch-middleware

3. 接口说明

初始化参数说明类型默认值
fetchTimeout请超时的时间 (ms)number15000
apiOriginapi 的域名string
const fetcher = new Fetcher(5000, "https://cloudcapiv4.herewhite.com");

自定义类型

export type FetcherParams = {
  path: string;
  body?: Object | RequestInit["body"];
  query?: Object;
  headers?: HeadersInit;
};
成员方法方法参数类型
getFetcherParams
postFetcherParams
putFetcherParams
deleteFetcherParams
 const json = await fetcher.post<any>({
            path: `room/xxxxxxxxxx`,
            query: {
                token: "xxxxxxxxxx",
            },
            body: {
                name: name,
                limit: limit,
                mode: mode,
            },
        });

4. 使用概览

import Fetcher from "@netless/fetch-middleware";
const fetcher = new Fetcher(5000, "https://cloudcapiv4.herewhite.com");

export class RoomOperator {

    public async createRoomApi(name: string, limit: number, mode: RoomType): Promise<any> {
        const json = await fetcher.post<any>({
            path: `room/xxxxxxxxxx`,
            query: {
                token: "xxxxxxxxxx",
            },
            body: {
                name: name,
                limit: limit,
                mode: mode,
            },
        });
        return json as any;
    }
}

License

MIT © alwaysmavs