0.11.2 • Published 6 months ago
@ace-fetch/core v0.11.2
@ace-fetch/core
Core fetch provider
Typescript
support
安装
yarn add @ace-fetch/core
或者
npm i -S @ace-fetch/core
使用
// 使用 axios 作为示例
import axios from 'axios';
import { regisApi } from '@ace-fetch/core';
// 创建 asiox 实例
const axiosInstance = axios.create({
timeout: 5000
})
const prefix = 'http://api/base_url/'
interface User{
id: number;
firstName: string;
lastName: string;
city: string;
}
// 定义 apis
const userApi = registApi(axiosInstance, {
// 定义 api
getUsers: 'get /users'
// 或使用 typedUrl 函数在 typescript 中明确类型定义
getUsers: typedUrl<User[]>`get /users`,
// get 可以省略
// 通过字符串获取 params 中的变量拼接 url
getUser: typedUrl<User, { id: string | number }>`/user/${'id'}`,
// 或者通过函数拼接url
getUser: typedUrl<User, { id: string | number }>`/user/${(params)=> params.id}`,
// typedUrl 可以是函数传 RequestConfig 作为当前请求的定义
addUser: typedUrl<User, any, Partial<Omit<User, 'id'>>>({
timeout: 10000
})`post /user`,
}, prefix);
userApi.getUsers().then(({data})=>{ ... });
// params 参数
userApi.getUsers({ params: {id:1} }).then(({data})=>{ ... });
// body 参数
userApi.addUser({ data: { firstName:'San', lastName: 'Zhang', city: 'BeiJing' }}).then(({data})=>{ ... });
0.11.0
7 months ago
0.11.1
7 months ago
0.11.2
6 months ago
0.10.0
7 months ago
0.9.0
10 months ago
0.7.2
11 months ago
0.8.0
10 months ago
0.9.2
8 months ago
0.9.1
9 months ago
0.7.0
11 months ago
0.6.2
12 months ago
0.5.5
12 months ago
0.6.1
12 months ago
0.5.0-alpha.0
1 year ago
0.4.1-alpha.0
1 year ago
0.5.3
1 year ago
0.5.0
1 year ago
0.4.0
1 year ago
0.5.1
1 year ago
0.3.2
1 year ago
0.3.0
2 years ago
0.2.2
2 years ago
0.2.1
2 years ago
0.2.0
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago