@ahoo-wang/fetcher 1.2.3 → 1.2.5

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
@@ -12,8 +12,8 @@ The lightweight core that powers the entire Fetcher ecosystem. Ultra-lightweight
12
12
 
13
13
  ## 🌟 Features
14
14
 
15
- - **⚡ Ultra-Lightweight**: Only 2.7KiB min+gzip
16
- - **🧭 Path & Query Parameters**: Built-in support for path (`{id}`) and query parameters
15
+ - **⚡ Ultra-Lightweight**: Only 2.8KiB min+gzip
16
+ - **🧭 Path & Query Parameters**: Built-in support for path (`{id}`/`:id`) and query parameters
17
17
  - **🔗 Interceptor System**: Request, response, and error interceptors for middleware patterns
18
18
  - **⏱️ Timeout Control**: Configurable request timeouts with proper error handling
19
19
  - **🔄 Fetch API Compatible**: Fully compatible with the native Fetch API
@@ -63,6 +63,50 @@ const createUserResponse = await fetcher.post('/users', {
63
63
  });
64
64
  ```
65
65
 
66
+ ### URL Template Styles
67
+
68
+ Fetcher supports different URL template styles for path parameters:
69
+
70
+ 1. **URI Template Style** (default): Uses curly braces, e.g., `/users/{id}/posts/{postId}`
71
+ 2. **Express Style**: Uses colons, e.g., `/users/:id/posts/:postId`
72
+
73
+ You can configure the URL template style when creating a Fetcher instance:
74
+
75
+ ```typescript
76
+ import { Fetcher, UrlTemplateStyle } from '@ahoo-wang/fetcher';
77
+
78
+ // Default URI Template style
79
+ const fetcher1 = new Fetcher({
80
+ baseURL: 'https://api.example.com'
81
+ });
82
+
83
+ // Explicit URI Template style
84
+ const fetcher2 = new Fetcher({
85
+ baseURL: 'https://api.example.com',
86
+ urlTemplateStyle: UrlTemplateStyle.UriTemplate
87
+ });
88
+
89
+ // Express style
90
+ const fetcher3 = new Fetcher({
91
+ baseURL: 'https://api.example.com',
92
+ urlTemplateStyle: UrlTemplateStyle.Express
93
+ });
94
+
95
+ // Usage with URI Template style
96
+ const response1 = await fetcher1.get('/users/{id}', {
97
+ urlParams: {
98
+ path: { id: 123 }
99
+ }
100
+ });
101
+
102
+ // Usage with Express style
103
+ const response2 = await fetcher3.get('/users/:id', {
104
+ urlParams: {
105
+ path: { id: 123 }
106
+ }
107
+ });
108
+ ```
109
+
66
110
  ### Integration Test Example: Typicode API Integration
67
111
 
68
112
  The following example shows how to integrate with the JSONPlaceholder API, similar to the integration test in the
@@ -264,6 +308,7 @@ new Fetcher(options ? : FetcherOptions);
264
308
  - `timeout`: Request timeout in milliseconds
265
309
  - `headers`: Default request headers
266
310
  - `interceptors`: Interceptor collection for request, response, and error handling
311
+ - `urlTemplateStyle`: URL template style for path parameter resolution (default: UriTemplate)
267
312
 
268
313
  #### Properties
269
314
 
@@ -395,4 +440,4 @@ This project is licensed under the [Apache-2.0 License](https://opensource.org/l
395
440
 
396
441
  <p align="center">
397
442
  Part of the <a href="https://github.com/Ahoo-Wang/fetcher">Fetcher</a> ecosystem
398
- </p>
443
+ </p>
package/README.zh-CN.md CHANGED
@@ -12,8 +12,8 @@
12
12
 
13
13
  ## 🌟 特性
14
14
 
15
- - **⚡ 超轻量级**:仅 2.7KiB min+gzip
16
- - **🧭 路径和查询参数**:内置支持路径(`{id}`)和查询参数
15
+ - **⚡ 超轻量级**:仅 2.8KiB min+gzip
16
+ - **🧭 路径和查询参数**:内置支持路径(`{id}`/`:id`)和查询参数
17
17
  - **🔗 拦截器系统**:请求、响应和错误拦截器的中间件模式
18
18
  - **⏱️ 超时控制**:可配置的请求超时和适当的错误处理
19
19
  - **🔄 Fetch API 兼容**:与原生 Fetch API 完全兼容
@@ -63,6 +63,50 @@ const createUserResponse = await fetcher.post('/users', {
63
63
  });
64
64
  ```
65
65
 
66
+ ### URL 模板样式
67
+
68
+ Fetcher 支持不同的 URL 模板样式来处理路径参数:
69
+
70
+ 1. **URI 模板样式**(默认):使用花括号,例如 `/users/{id}/posts/{postId}`
71
+ 2. **Express 样式**:使用冒号,例如 `/users/:id/posts/:postId`
72
+
73
+ 您可以在创建 Fetcher 实例时配置 URL 模板样式:
74
+
75
+ ```typescript
76
+ import { Fetcher, UrlTemplateStyle } from '@ahoo-wang/fetcher';
77
+
78
+ // 默认 URI 模板样式
79
+ const fetcher1 = new Fetcher({
80
+ baseURL: 'https://api.example.com'
81
+ });
82
+
83
+ // 显式指定 URI 模板样式
84
+ const fetcher2 = new Fetcher({
85
+ baseURL: 'https://api.example.com',
86
+ urlTemplateStyle: UrlTemplateStyle.UriTemplate
87
+ });
88
+
89
+ // Express 样式
90
+ const fetcher3 = new Fetcher({
91
+ baseURL: 'https://api.example.com',
92
+ urlTemplateStyle: UrlTemplateStyle.Express
93
+ });
94
+
95
+ // 使用 URI 模板样式
96
+ const response1 = await fetcher1.get('/users/{id}', {
97
+ urlParams: {
98
+ path: { id: 123 }
99
+ }
100
+ });
101
+
102
+ // 使用 Express 样式
103
+ const response2 = await fetcher3.get('/users/:id', {
104
+ urlParams: {
105
+ path: { id: 123 }
106
+ }
107
+ });
108
+ ```
109
+
66
110
  ### 集成测试示例:Typicode API 集成
67
111
 
68
112
  以下示例展示了如何与 JSONPlaceholder API 集成,类似于 Fetcher
@@ -253,6 +297,7 @@ new Fetcher(options ? : FetcherOptions);
253
297
  - `timeout`:请求超时时间(毫秒)
254
298
  - `headers`:默认请求头部
255
299
  - `interceptors`:用于请求、响应和错误处理的拦截器集合
300
+ - `urlTemplateStyle`:用于路径参数解析的 URL 模板样式(默认:UriTemplate)
256
301
 
257
302
  #### 属性
258
303
 
@@ -381,4 +426,4 @@ Fetcher 拦截器集合,包括请求、响应和错误拦截器管理器。
381
426
 
382
427
  <p align="center">
383
428
  Fetcher 生态系统的一部分
384
- </p>
429
+ </p>
package/dist/fetcher.d.ts CHANGED
@@ -3,6 +3,7 @@ import { TimeoutCapable } from './timeout';
3
3
  import { FetchExchange } from './fetchExchange';
4
4
  import { BaseURLCapable, FetchRequest, FetchRequestInit, RequestHeaders, RequestHeadersCapable } from './fetchRequest';
5
5
  import { InterceptorManager } from './interceptorManager';
6
+ import { UrlTemplateStyle } from './urlTemplateResolver';
6
7
  /**
7
8
  * Configuration options for the Fetcher client.
8
9
  *
@@ -12,6 +13,7 @@ import { InterceptorManager } from './interceptorManager';
12
13
  * @example
13
14
  * ```typescript
14
15
  * const options: FetcherOptions = {
16
+ * urlTemplateStyle: UrlTemplateStyle.UriTemplate,
15
17
  * baseURL: 'https://api.example.com',
16
18
  * headers: { 'Content-Type': 'application/json' },
17
19
  * timeout: 5000,
@@ -20,6 +22,7 @@ import { InterceptorManager } from './interceptorManager';
20
22
  * ```
21
23
  */
22
24
  export interface FetcherOptions extends BaseURLCapable, RequestHeadersCapable, TimeoutCapable {
25
+ urlTemplateStyle?: UrlTemplateStyle;
23
26
  interceptors?: InterceptorManager;
24
27
  }
25
28
  export declare const DEFAULT_OPTIONS: FetcherOptions;
@@ -1 +1 @@
1
- {"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,qBAAqB,EACrB,cAAc;IAChB,YAAY,CAAC,EAAE,kBAAkB,CAAC;CACnC;AAMD,eAAO,MAAM,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,OACX,YAAW,iBAAiB,EAAE,qBAAqB,EAAE,cAAc;IACnE,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,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gB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;CAGrB"}
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export * from './types';
15
15
  export * from './urlBuilder';
16
16
  export * from './urlResolveInterceptor';
17
17
  export * from './urls';
18
+ export * from './urlTemplateResolver';
18
19
  export * from './utils';
19
20
  export * from './validateStatusInterceptor';
20
21
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,yBAAyB,CAAC;AACxC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,yBAAyB,CAAC;AACxC,cAAc,QAAQ,CAAC;AACvB,cAAc,uBAAuB,CAAC;AACtC,cAAc,SAAS,CAAC;AACxB,cAAc,6BAA6B,CAAC"}