一个带有请求超时功能的 fetch 中间件,方便大家做异常流程处理。
本项目技术选型为:React Typescript
打包工具为: rollup
npm install --save @netless/fetch-middleware
或者
yarn add @netless/fetch-middleware| 初始化参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| fetchTimeout | 请超时的时间 (ms) | number | 15000 |
| apiOrigin | api 的域名 | string |
const fetcher = new Fetcher(5000, "https://cloudcapiv4.herewhite.com");自定义类型
export type FetcherParams = {
path: string;
body?: Object | RequestInit["body"];
query?: Object;
headers?: HeadersInit;
};| 成员方法 | 方法参数类型 |
|---|---|
| get | FetcherParams |
| post | FetcherParams |
| put | FetcherParams |
| delete | FetcherParams |
const json = await fetcher.post<any>({
path: `room/xxxxxxxxxx`,
query: {
token: "xxxxxxxxxx",
},
body: {
name: name,
limit: limit,
mode: mode,
},
});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;
}
}MIT © alwaysmavs