quick-fy
v0.3.1
Published
quick-fy 是一个轻量级、可扩展的基于 fetch 的 HTTP 请求库
Readme
quick-fy
quick-fy 是一个轻量级、可扩展的基于 fetch 的 HTTP 请求库, 适用于浏览器和 Node.js 环境。
特性
- 基于原生 fetch API
- 支持请求和响应拦截器
- 提供全局钩子函数(onSuccess, onError, onComplete)
- 支持自定义元数据传递
- 类型安全,使用 TypeScript 编写
安装
npm install quick-fy快速开始
import { createFy, fy } from 'quick-fy'
// 使用默认 fy 实例
await fy.get('https://example.com/')
// 创建一个新的 fy 实例
const newFy = createFy({
beforeRequest: (method) => {
console.log('Global beforeRequest', method)
return method
},
responded: {
onSuccess: (response, method, data) => {
console.log('Global onSuccess', method, typeof data)
return data
},
onError: (error, method) => {
console.log('Global onError', error, method)
throw error
},
onComplete: (method) => {
console.log('Global onComplete', method)
},
},
})
await newFy.get('https://example.com/')更多示例
- 参见 测试用例
API 参考
createFy(options)
创建一个新的 fy 实例。
参数:
options: CreateFyOptions 对象
返回:
- FyInstance 对象
FyInstance 方法
request<T>(url: string, init?: RequestInit, meta?: Meta): Promise<T>get<T>(url: string, init?: RequestInit, meta?: Meta): Promise<T>post<T>(url: string, init?: RequestInit, meta?: Meta): Promise<T>addBeforeInterceptor(interceptor: RequestInterceptor): voidaddAfterInterceptor(interceptor: ResponseInterceptor): void
