@ahoo-wang/fetcher 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,50 +1,57 @@
1
- # @fetcher/core
1
+ # @ahoo-wang/fetcher
2
2
 
3
- Fetcher-core 是一个基于现代 Fetch API 的 HTTP 客户端库,旨在简化和优化与后端 RESTful API 的交互。它提供了类似于 Axios 的 API,支持路径参数、查询参数、超时设置以及请求和响应拦截器。
3
+ [![npm version](https://img.shields.io/npm/v/@ahoo-wang/fetcher.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher)
4
+ [![Build Status](https://github.com/Ahoo-Wang/fetcher/actions/workflows/ci.yml/badge.svg)](https://github.com/Ahoo-Wang/fetcher/actions)
5
+ [![codecov](https://codecov.io/gh/Ahoo-Wang/Fetcher/graph/badge.svg?token=CUPgk8DmH5)](https://codecov.io/gh/Ahoo-Wang/Fetcher)
6
+ [![License](https://img.shields.io/npm/l/@ahoo-wang/fetcher.svg)](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
7
+ [![npm downloads](https://img.shields.io/npm/dm/@ahoo-wang/fetcher.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher)
8
+ [![Bundle Size](https://img.shields.io/bundlephobia/minzip/@ahoo-wang/fetcher.svg)](https://bundlephobia.com/package/@ahoo-wang/fetcher)
4
9
 
5
- ## 特性
10
+ A modern HTTP client library based on the Fetch API, designed to simplify and optimize interactions with backend RESTful APIs. It provides an Axios-like API with support for path parameters, query parameters, timeout settings, and request/response interceptors.
6
11
 
7
- - **兼容 Fetch API**: fetcher 的 API 完全兼容原生 Fetch API,易于上手。
8
- - **路径和查询参数**: 支持在请求中使用路径参数和查询参数,路径参数使用 `{}` 包裹。
9
- - **超时设置**: 可以为请求设置超时时间。
10
- - **请求拦截器**: 支持在请求发送前对请求进行修改。
11
- - **响应拦截器**: 支持在响应返回后对响应进行处理。
12
- - **模块化设计**: 代码结构清晰,易于维护和扩展。
13
- - **TypeScript 支持**: 完整的 TypeScript 类型定义。
12
+ ## Features
14
13
 
15
- ## 安装
14
+ - **Fetch API Compatible**: Fetcher's API is fully compatible with the native Fetch API, making it easy to get started.
15
+ - **Path and Query Parameters**: Supports path parameters and query parameters in requests, with path parameters wrapped in `{}`.
16
+ - **Timeout Settings**: Request timeout can be configured.
17
+ - **Request Interceptors**: Supports modifying requests before they are sent.
18
+ - **Response Interceptors**: Supports processing responses after they are returned.
19
+ - **Modular Design**: Clear code structure for easy maintenance and extension.
20
+ - **TypeScript Support**: Complete TypeScript type definitions.
16
21
 
17
- 使用 pnpm 安装:
22
+ ## Installation
23
+
24
+ Using pnpm:
18
25
 
19
26
  ```bash
20
- pnpm add @fetcher/core
27
+ pnpm add @ahoo-wang/fetcher
21
28
  ```
22
29
 
23
- 使用 npm 安装:
30
+ Using npm:
24
31
 
25
32
  ```bash
26
- npm install @fetcher/core
33
+ npm install @ahoo-wang/fetcher
27
34
  ```
28
35
 
29
- 使用 yarn 安装:
36
+ Using yarn:
30
37
 
31
38
  ```bash
32
- yarn add @fetcher/core
39
+ yarn add @ahoo-wang/fetcher
33
40
  ```
34
41
 
35
- ## 使用示例
42
+ ## Usage
36
43
 
37
- ### 基本用法
44
+ ### Basic Usage
38
45
 
39
46
  ```typescript
40
- import { Fetcher } from '@fetcher/core';
47
+ import { Fetcher } from '@ahoo-wang/fetcher';
41
48
 
42
49
  const fetcher = new Fetcher({
43
50
  baseURL: 'https://api.example.com',
44
51
  timeout: 5000,
45
52
  });
46
53
 
47
- // GET 请求带路径参数和查询参数
54
+ // GET request with path parameters and query parameters
48
55
  fetcher
49
56
  .get('/users/{id}', {
50
57
  pathParams: { id: 123 },
@@ -57,7 +64,7 @@ fetcher
57
64
  console.error(error);
58
65
  });
59
66
 
60
- // POST 请求带 JSON 数据
67
+ // POST request with JSON body
61
68
  fetcher
62
69
  .post('/users', {
63
70
  body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
@@ -71,17 +78,17 @@ fetcher
71
78
  });
72
79
  ```
73
80
 
74
- ### 拦截器用法
81
+ ### Interceptor Usage
75
82
 
76
83
  ```typescript
77
- import { Fetcher } from '@fetcher/core';
84
+ import { Fetcher } from '@ahoo-wang/fetcher';
78
85
 
79
86
  const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
80
87
 
81
- // 添加请求拦截器
88
+ // Add request interceptor
82
89
  const requestInterceptorId = fetcher.interceptors.request.use({
83
90
  intercept(request) {
84
- // 修改请求配置,例如添加认证头
91
+ // Modify request configuration, e.g., add auth header
85
92
  return {
86
93
  ...request,
87
94
  headers: {
@@ -92,88 +99,88 @@ const requestInterceptorId = fetcher.interceptors.request.use({
92
99
  },
93
100
  });
94
101
 
95
- // 添加响应拦截器
102
+ // Add response interceptor
96
103
  const responseInterceptorId = fetcher.interceptors.response.use({
97
104
  intercept(response) {
98
- // 处理响应数据,例如解析 JSON
105
+ // Process response data, e.g., parse JSON
99
106
  return response;
100
107
  },
101
108
  });
102
109
 
103
- // 添加错误拦截器
110
+ // Add error interceptor
104
111
  const errorInterceptorId = fetcher.interceptors.error.use({
105
112
  intercept(error) {
106
- // 处理错误,例如记录日志
113
+ // Handle errors, e.g., log them
107
114
  console.error('Request failed:', error);
108
115
  return error;
109
116
  },
110
117
  });
111
118
  ```
112
119
 
113
- ## API 参考
120
+ ## API Reference
114
121
 
115
- ### Fetcher
122
+ ### Fetcher Class
116
123
 
117
- 核心 HTTP 客户端类,提供各种 HTTP 方法。
124
+ Core HTTP client class that provides various HTTP methods.
118
125
 
119
- #### 构造函数
126
+ #### Constructor
120
127
 
121
128
  ```typescript
122
129
  new Fetcher(options?: FetcherOptions)
123
130
  ```
124
131
 
125
- **参数:**
132
+ **Parameters:**
126
133
 
127
- - `options.baseURL`: 基础 URL
128
- - `options.timeout`: 请求超时时间(毫秒)
129
- - `options.headers`: 默认请求头
134
+ - `options.baseURL`: Base URL
135
+ - `options.timeout`: Request timeout in milliseconds
136
+ - `options.headers`: Default request headers
130
137
 
131
- #### 方法
138
+ #### Methods
132
139
 
133
- - `fetch(url: string, request?: FetcherRequest): Promise<Response>` - 通用 HTTP 请求方法
134
- - `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET 请求
135
- - `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST 请求
136
- - `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT 请求
137
- - `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE 请求
138
- - `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH 请求
139
- - `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD 请求
140
- - `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS 请求
140
+ - `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
141
+ - `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET request
142
+ - `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST request
143
+ - `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT request
144
+ - `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE request
145
+ - `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
146
+ - `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
147
+ - `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
141
148
 
142
- ### UrlBuilder
149
+ ### UrlBuilder Class
143
150
 
144
- URL 构建器,用于构建带参数的完整 URL。
151
+ URL builder for constructing complete URLs with parameters.
145
152
 
146
- #### 方法
153
+ #### Methods
147
154
 
148
- - `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - 构建完整 URL
155
+ - `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - Build complete URL
149
156
 
150
- ### InterceptorManager
157
+ ### InterceptorManager Class
151
158
 
152
- 拦截器管理器,用于管理同一类型的多个拦截器。
159
+ Interceptor manager for managing multiple interceptors of the same type.
153
160
 
154
- #### 方法
161
+ #### Methods
155
162
 
156
- - `use(interceptor: T): number` - 添加拦截器,返回拦截器 ID
157
- - `eject(index: number): void` - 根据 ID 移除拦截器
158
- - `clear(): void` - 清空所有拦截器
159
- - `intercept(data: R): Promise<R>` - 依次执行所有拦截器
163
+ - `use(interceptor: T): number` - Add interceptor, returns interceptor ID
164
+ - `eject(index: number): void` - Remove interceptor by ID
165
+ - `clear(): void` - Clear all interceptors
166
+ - `intercept(data: R): Promise<R>` - Execute all interceptors sequentially
160
167
 
161
- ### FetcherInterceptors
168
+ ### FetcherInterceptors Class
162
169
 
163
- Fetcher 拦截器集合,包含请求、响应和错误拦截器管理器。
170
+ Fetcher interceptor collection, including request, response, and error interceptor managers.
164
171
 
165
- #### 属性
172
+ #### Properties
166
173
 
167
- - `request: RequestInterceptorManager` - 请求拦截器管理器
168
- - `response: ResponseInterceptorManager` - 响应拦截器管理器
169
- - `error: ErrorInterceptorManager` - 错误拦截器管理器
174
+ - `request: RequestInterceptorManager` - Request interceptor manager
175
+ - `response: ResponseInterceptorManager` - Response interceptor manager
176
+ - `error: ErrorInterceptorManager` - Error interceptor manager
170
177
 
171
- ## 完整示例
178
+ ## Complete Example
172
179
 
173
180
  ```typescript
174
- import { Fetcher } from '@fetcher/core';
181
+ import { Fetcher } from '@ahoo-wang/fetcher';
175
182
 
176
- // 创建 fetcher 实例
183
+ // Create fetcher instance
177
184
  const fetcher = new Fetcher({
178
185
  baseURL: 'https://api.example.com',
179
186
  timeout: 10000,
@@ -182,7 +189,7 @@ const fetcher = new Fetcher({
182
189
  },
183
190
  });
184
191
 
185
- // 添加请求拦截器 - 添加认证头
192
+ // Add request interceptor - Add auth header
186
193
  fetcher.interceptors.request.use({
187
194
  intercept(request) {
188
195
  return {
@@ -195,7 +202,7 @@ fetcher.interceptors.request.use({
195
202
  },
196
203
  });
197
204
 
198
- // 添加响应拦截器 - 自动解析 JSON
205
+ // Add response interceptor - Auto-parse JSON
199
206
  fetcher.interceptors.response.use({
200
207
  intercept(response) {
201
208
  if (response.headers.get('content-type')?.includes('application/json')) {
@@ -207,7 +214,7 @@ fetcher.interceptors.response.use({
207
214
  },
208
215
  });
209
216
 
210
- // 添加错误拦截器 - 统一错误处理
217
+ // Add error interceptor - Unified error handling
211
218
  fetcher.interceptors.error.use({
212
219
  intercept(error) {
213
220
  if (error.name === 'FetchTimeoutError') {
@@ -219,7 +226,7 @@ fetcher.interceptors.error.use({
219
226
  },
220
227
  });
221
228
 
222
- // 使用 fetcher 发起请求
229
+ // Use fetcher to make requests
223
230
  fetcher
224
231
  .get('/users/{id}', {
225
232
  pathParams: { id: 123 },
@@ -234,18 +241,18 @@ fetcher
234
241
  });
235
242
  ```
236
243
 
237
- ## 测试
244
+ ## Testing
238
245
 
239
- 运行测试:
246
+ Run tests:
240
247
 
241
248
  ```bash
242
249
  pnpm test
243
250
  ```
244
251
 
245
- ## 贡献
252
+ ## Contributing
246
253
 
247
- 欢迎任何形式的贡献!请查看 [贡献指南](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) 了解更多信息。
254
+ Contributions of any kind are welcome! Please see the [contributing guide](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) for more details.
248
255
 
249
- ## 许可证
256
+ ## License
250
257
 
251
- 本项目采用 [Apache-2.0](https://opensource.org/licenses/Apache-2.0) 许可证。
258
+ This project is licensed under the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,OAAO,YAAY;CACpB;AAED,oBAAY,YAAY;IACtB,MAAM,WAAW;IACjB,IAAI,SAAS;CACd"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,OAAO,YAAY;CACpB;AAED,oBAAY,YAAY;IACtB,MAAM,WAAW;IACjB,IAAI,SAAS;CACd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Core library providing basic HTTP client functionality for Fetcher",
5
5
  "keywords": [
6
6
  "fetch",