@ahoo-wang/fetcher 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. package/README.md +77 -77
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,50 +1,50 @@
1
- # @fetcher/core
1
+ # @ahoo-wang/fetcher
2
2
 
3
- Fetcher-core 是一个基于现代 Fetch API HTTP 客户端库,旨在简化和优化与后端 RESTful API 的交互。它提供了类似于 Axios API,支持路径参数、查询参数、超时设置以及请求和响应拦截器。
3
+ 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.
4
4
 
5
- ## 特性
5
+ ## Features
6
6
 
7
- - **兼容 Fetch API**: fetcher API 完全兼容原生 Fetch API,易于上手。
8
- - **路径和查询参数**: 支持在请求中使用路径参数和查询参数,路径参数使用 `{}` 包裹。
9
- - **超时设置**: 可以为请求设置超时时间。
10
- - **请求拦截器**: 支持在请求发送前对请求进行修改。
11
- - **响应拦截器**: 支持在响应返回后对响应进行处理。
12
- - **模块化设计**: 代码结构清晰,易于维护和扩展。
13
- - **TypeScript 支持**: 完整的 TypeScript 类型定义。
7
+ - **Fetch API Compatible**: Fetcher's API is fully compatible with the native Fetch API, making it easy to get started.
8
+ - **Path and Query Parameters**: Supports path parameters and query parameters in requests, with path parameters wrapped in `{}`.
9
+ - **Timeout Settings**: Request timeout can be configured.
10
+ - **Request Interceptors**: Supports modifying requests before they are sent.
11
+ - **Response Interceptors**: Supports processing responses after they are returned.
12
+ - **Modular Design**: Clear code structure for easy maintenance and extension.
13
+ - **TypeScript Support**: Complete TypeScript type definitions.
14
14
 
15
- ## 安装
15
+ ## Installation
16
16
 
17
- 使用 pnpm 安装:
17
+ Using pnpm:
18
18
 
19
19
  ```bash
20
- pnpm add @fetcher/core
20
+ pnpm add @ahoo-wang/fetcher
21
21
  ```
22
22
 
23
- 使用 npm 安装:
23
+ Using npm:
24
24
 
25
25
  ```bash
26
- npm install @fetcher/core
26
+ npm install @ahoo-wang/fetcher
27
27
  ```
28
28
 
29
- 使用 yarn 安装:
29
+ Using yarn:
30
30
 
31
31
  ```bash
32
- yarn add @fetcher/core
32
+ yarn add @ahoo-wang/fetcher
33
33
  ```
34
34
 
35
- ## 使用示例
35
+ ## Usage
36
36
 
37
- ### 基本用法
37
+ ### Basic Usage
38
38
 
39
39
  ```typescript
40
- import { Fetcher } from '@fetcher/core';
40
+ import { Fetcher } from '@ahoo-wang/fetcher';
41
41
 
42
42
  const fetcher = new Fetcher({
43
43
  baseURL: 'https://api.example.com',
44
44
  timeout: 5000,
45
45
  });
46
46
 
47
- // GET 请求带路径参数和查询参数
47
+ // GET request with path parameters and query parameters
48
48
  fetcher
49
49
  .get('/users/{id}', {
50
50
  pathParams: { id: 123 },
@@ -57,7 +57,7 @@ fetcher
57
57
  console.error(error);
58
58
  });
59
59
 
60
- // POST 请求带 JSON 数据
60
+ // POST request with JSON body
61
61
  fetcher
62
62
  .post('/users', {
63
63
  body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }),
@@ -71,17 +71,17 @@ fetcher
71
71
  });
72
72
  ```
73
73
 
74
- ### 拦截器用法
74
+ ### Interceptor Usage
75
75
 
76
76
  ```typescript
77
- import { Fetcher } from '@fetcher/core';
77
+ import { Fetcher } from '@ahoo-wang/fetcher';
78
78
 
79
79
  const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
80
80
 
81
- // 添加请求拦截器
81
+ // Add request interceptor
82
82
  const requestInterceptorId = fetcher.interceptors.request.use({
83
83
  intercept(request) {
84
- // 修改请求配置,例如添加认证头
84
+ // Modify request configuration, e.g., add auth header
85
85
  return {
86
86
  ...request,
87
87
  headers: {
@@ -92,88 +92,88 @@ const requestInterceptorId = fetcher.interceptors.request.use({
92
92
  },
93
93
  });
94
94
 
95
- // 添加响应拦截器
95
+ // Add response interceptor
96
96
  const responseInterceptorId = fetcher.interceptors.response.use({
97
97
  intercept(response) {
98
- // 处理响应数据,例如解析 JSON
98
+ // Process response data, e.g., parse JSON
99
99
  return response;
100
100
  },
101
101
  });
102
102
 
103
- // 添加错误拦截器
103
+ // Add error interceptor
104
104
  const errorInterceptorId = fetcher.interceptors.error.use({
105
105
  intercept(error) {
106
- // 处理错误,例如记录日志
106
+ // Handle errors, e.g., log them
107
107
  console.error('Request failed:', error);
108
108
  return error;
109
109
  },
110
110
  });
111
111
  ```
112
112
 
113
- ## API 参考
113
+ ## API Reference
114
114
 
115
- ### Fetcher
115
+ ### Fetcher Class
116
116
 
117
- 核心 HTTP 客户端类,提供各种 HTTP 方法。
117
+ Core HTTP client class that provides various HTTP methods.
118
118
 
119
- #### 构造函数
119
+ #### Constructor
120
120
 
121
121
  ```typescript
122
122
  new Fetcher(options?: FetcherOptions)
123
123
  ```
124
124
 
125
- **参数:**
125
+ **Parameters:**
126
126
 
127
- - `options.baseURL`: 基础 URL
128
- - `options.timeout`: 请求超时时间(毫秒)
129
- - `options.headers`: 默认请求头
127
+ - `options.baseURL`: Base URL
128
+ - `options.timeout`: Request timeout in milliseconds
129
+ - `options.headers`: Default request headers
130
130
 
131
- #### 方法
131
+ #### Methods
132
132
 
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 请求
133
+ - `fetch(url: string, request?: FetcherRequest): Promise<Response>` - Generic HTTP request method
134
+ - `get(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - GET request
135
+ - `post(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - POST request
136
+ - `put(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PUT request
137
+ - `delete(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - DELETE request
138
+ - `patch(url: string, request?: Omit<FetcherRequest, 'method'>): Promise<Response>` - PATCH request
139
+ - `head(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - HEAD request
140
+ - `options(url: string, request?: Omit<FetcherRequest, 'method' | 'body'>): Promise<Response>` - OPTIONS request
141
141
 
142
- ### UrlBuilder
142
+ ### UrlBuilder Class
143
143
 
144
- URL 构建器,用于构建带参数的完整 URL。
144
+ URL builder for constructing complete URLs with parameters.
145
145
 
146
- #### 方法
146
+ #### Methods
147
147
 
148
- - `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - 构建完整 URL
148
+ - `build(path: string, pathParams?: Record<string, any>, queryParams?: Record<string, any>): string` - Build complete URL
149
149
 
150
- ### InterceptorManager
150
+ ### InterceptorManager Class
151
151
 
152
- 拦截器管理器,用于管理同一类型的多个拦截器。
152
+ Interceptor manager for managing multiple interceptors of the same type.
153
153
 
154
- #### 方法
154
+ #### Methods
155
155
 
156
- - `use(interceptor: T): number` - 添加拦截器,返回拦截器 ID
157
- - `eject(index: number): void` - 根据 ID 移除拦截器
158
- - `clear(): void` - 清空所有拦截器
159
- - `intercept(data: R): Promise<R>` - 依次执行所有拦截器
156
+ - `use(interceptor: T): number` - Add interceptor, returns interceptor ID
157
+ - `eject(index: number): void` - Remove interceptor by ID
158
+ - `clear(): void` - Clear all interceptors
159
+ - `intercept(data: R): Promise<R>` - Execute all interceptors sequentially
160
160
 
161
- ### FetcherInterceptors
161
+ ### FetcherInterceptors Class
162
162
 
163
- Fetcher 拦截器集合,包含请求、响应和错误拦截器管理器。
163
+ Fetcher interceptor collection, including request, response, and error interceptor managers.
164
164
 
165
- #### 属性
165
+ #### Properties
166
166
 
167
- - `request: RequestInterceptorManager` - 请求拦截器管理器
168
- - `response: ResponseInterceptorManager` - 响应拦截器管理器
169
- - `error: ErrorInterceptorManager` - 错误拦截器管理器
167
+ - `request: RequestInterceptorManager` - Request interceptor manager
168
+ - `response: ResponseInterceptorManager` - Response interceptor manager
169
+ - `error: ErrorInterceptorManager` - Error interceptor manager
170
170
 
171
- ## 完整示例
171
+ ## Complete Example
172
172
 
173
173
  ```typescript
174
- import { Fetcher } from '@fetcher/core';
174
+ import { Fetcher } from '@ahoo-wang/fetcher';
175
175
 
176
- // 创建 fetcher 实例
176
+ // Create fetcher instance
177
177
  const fetcher = new Fetcher({
178
178
  baseURL: 'https://api.example.com',
179
179
  timeout: 10000,
@@ -182,7 +182,7 @@ const fetcher = new Fetcher({
182
182
  },
183
183
  });
184
184
 
185
- // 添加请求拦截器 - 添加认证头
185
+ // Add request interceptor - Add auth header
186
186
  fetcher.interceptors.request.use({
187
187
  intercept(request) {
188
188
  return {
@@ -195,7 +195,7 @@ fetcher.interceptors.request.use({
195
195
  },
196
196
  });
197
197
 
198
- // 添加响应拦截器 - 自动解析 JSON
198
+ // Add response interceptor - Auto-parse JSON
199
199
  fetcher.interceptors.response.use({
200
200
  intercept(response) {
201
201
  if (response.headers.get('content-type')?.includes('application/json')) {
@@ -207,7 +207,7 @@ fetcher.interceptors.response.use({
207
207
  },
208
208
  });
209
209
 
210
- // 添加错误拦截器 - 统一错误处理
210
+ // Add error interceptor - Unified error handling
211
211
  fetcher.interceptors.error.use({
212
212
  intercept(error) {
213
213
  if (error.name === 'FetchTimeoutError') {
@@ -219,7 +219,7 @@ fetcher.interceptors.error.use({
219
219
  },
220
220
  });
221
221
 
222
- // 使用 fetcher 发起请求
222
+ // Use fetcher to make requests
223
223
  fetcher
224
224
  .get('/users/{id}', {
225
225
  pathParams: { id: 123 },
@@ -234,18 +234,18 @@ fetcher
234
234
  });
235
235
  ```
236
236
 
237
- ## 测试
237
+ ## Testing
238
238
 
239
- 运行测试:
239
+ Run tests:
240
240
 
241
241
  ```bash
242
242
  pnpm test
243
243
  ```
244
244
 
245
- ## 贡献
245
+ ## Contributing
246
246
 
247
- 欢迎任何形式的贡献!请查看 [贡献指南](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) 了解更多信息。
247
+ 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
248
 
249
- ## 许可证
249
+ ## License
250
250
 
251
- 本项目采用 [Apache-2.0](https://opensource.org/licenses/Apache-2.0) 许可证。
251
+ This project is licensed under the [Apache-2.0 License](https://opensource.org/licenses/Apache-2.0).
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.3",
4
4
  "description": "Core library providing basic HTTP client functionality for Fetcher",
5
5
  "keywords": [
6
6
  "fetch",