@ahoo-wang/fetcher 0.8.3 → 0.8.6

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.
@@ -1 +1 @@
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;AAED,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,oBAAY,iBAAiB;IAC3B,gBAAgB,qBAAqB;IACrC,iBAAiB,sBAAsB;CACxC;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,QAAQ;QAChB;;;;WAIG;QACH,IAAI,CAAC,CAAC,GAAG,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;KAC7B;CACF"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAYA;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,QAAQ;QAChB;;;;WAIG;QACH,IAAI,CAAC,CAAC,GAAG,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;KAC7B;CACF"}
@@ -1,48 +1,95 @@
1
- import { BaseURLCapable } from './types';
1
+ import { BaseURLCapable, FetchRequest } from './fetchRequest';
2
2
  /**
3
- * UrlBuilder Class
3
+ * Interface for URL parameters including path and query parameters
4
+ */
5
+ export interface UrlParams {
6
+ /**
7
+ * Path parameter object used to replace placeholders in the URL (e.g., {id})
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // For URL template '/users/{id}/posts/{postId}'
12
+ * const path = { id: 123, postId: 456 };
13
+ * ```
14
+ */
15
+ path?: Record<string, any>;
16
+ /**
17
+ * Query parameter object to be added to the URL query string
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const query = { filter: 'active', page: 1, limit: 10 };
22
+ * // Results in query string: ?filter=active&page=1&limit=10
23
+ * ```
24
+ */
25
+ query?: Record<string, any>;
26
+ }
27
+ /**
28
+ * URL Builder class for constructing complete URLs with path parameters and query parameters
4
29
  *
5
- * URL builder class for constructing complete URLs with path parameters and query parameters.
6
30
  * This class handles URL composition, path parameter interpolation, and query string generation.
31
+ * It combines a base URL with a path, replaces path placeholders with actual values, and appends
32
+ * query parameters to create a complete URL.
7
33
  *
8
34
  * @example
9
35
  * ```typescript
10
36
  * const urlBuilder = new UrlBuilder('https://api.example.com');
11
- * const url = urlBuilder.build('/users/{id}', { id: 123 }, { filter: 'active' });
37
+ * const url = urlBuilder.build('/users/{id}', {
38
+ * urlParams: {
39
+ * path: { id: 123 },
40
+ * query: { filter: 'active' }
41
+ * }
42
+ * });
12
43
  * // Result: https://api.example.com/users/123?filter=active
13
44
  * ```
14
45
  */
15
46
  export declare class UrlBuilder implements BaseURLCapable {
16
47
  /**
17
48
  * Base URL that all constructed URLs will be based on
49
+ *
50
+ * This is typically the root of your API endpoint (e.g., 'https://api.example.com')
18
51
  */
19
52
  baseURL: string;
20
53
  /**
21
54
  * Creates a UrlBuilder instance
22
55
  *
23
56
  * @param baseURL - Base URL that all constructed URLs will be based on
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const urlBuilder = new UrlBuilder('https://api.example.com');
61
+ * ```
24
62
  */
25
63
  constructor(baseURL: string);
26
64
  /**
27
65
  * Builds a complete URL, including path parameter replacement and query parameter addition
28
66
  *
29
- * @param url - URL path to build
30
- * @param path - Path parameter object used to replace placeholders in the URL (e.g., {id})
31
- * @param query - Query parameter object to be added to the URL query string
32
- * @returns Complete URL string
67
+ * @param url - URL path to build (e.g., '/users/{id}/posts')
68
+ * @param params - URL parameters including path and query parameters
69
+ * @returns Complete URL string with base URL, path parameters interpolated, and query string appended
33
70
  * @throws Error when required path parameters are missing
34
71
  *
35
72
  * @example
36
73
  * ```typescript
37
74
  * const urlBuilder = new UrlBuilder('https://api.example.com');
38
- * const url = urlBuilder.build('/users/{id}/posts/{postId}',
39
- * { id: 123, postId: 456 },
40
- * { filter: 'active', limit: 10 }
41
- * );
75
+ * const url = urlBuilder.build('/users/{id}/posts/{postId}', {
76
+ * path: { id: 123, postId: 456 },
77
+ * query: { filter: 'active', limit: 10 }
78
+ * });
42
79
  * // Result: https://api.example.com/users/123/posts/456?filter=active&limit=10
43
80
  * ```
44
81
  */
45
- build(url: string, path?: Record<string, any>, query?: Record<string, any>): string;
82
+ build(url: string, params?: UrlParams): string;
83
+ /**
84
+ * Resolves a complete URL from a FetchRequest
85
+ *
86
+ * This method is used internally by the Fetcher to build the final URL for a request
87
+ * by combining the request URL with its URL parameters using this UrlBuilder.
88
+ *
89
+ * @param request - The FetchRequest containing URL and URL parameters
90
+ * @returns Complete resolved URL string
91
+ */
92
+ resolveRequestUrl(request: FetchRequest): string;
46
93
  /**
47
94
  * Replaces placeholders in the URL with path parameters
48
95
  *
@@ -54,13 +101,31 @@ export declare class UrlBuilder implements BaseURLCapable {
54
101
  * @example
55
102
  * ```typescript
56
103
  * const urlBuilder = new UrlBuilder('https://api.example.com');
57
- * const result = urlBuilder.interpolateUrl('/users/{id}/posts/{postId}', { id: 123, postId: 456 });
58
- * // Result: /users/123/posts/456
104
+ * const result = urlBuilder.interpolateUrl('/users/{id}/posts/{postId}', {
105
+ * path: { id: 123, postId: 456 }
106
+ * });
107
+ * // Result: https://api.example.com/users/123/posts/456
108
+ * ```
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * // Missing required parameter throws an error
113
+ * try {
114
+ * urlBuilder.interpolateUrl('/users/{id}', { name: 'John' });
115
+ * } catch (error) {
116
+ * console.error(error.message); // "Missing required path parameter: id"
117
+ * }
59
118
  * ```
60
119
  */
61
- interpolateUrl(url: string, path?: Record<string, any>): string;
120
+ interpolateUrl(url: string, path?: Record<string, any> | null): string;
62
121
  }
122
+ /**
123
+ * Interface for objects that have a UrlBuilder capability
124
+ */
63
125
  export interface UrlBuilderCapable {
126
+ /**
127
+ * The UrlBuilder instance
128
+ */
64
129
  urlBuilder: UrlBuilder;
65
130
  }
66
131
  //# sourceMappingURL=urlBuilder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;;;;;;GAYG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC1B,MAAM;IAYT;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;CAWhE;AAED,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,UAAU,CAAC;CACxB"}
1
+ {"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9D;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;;;OAQG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE3B;;;;;;;;OAQG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;;;;OASG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,GAAG,MAAM;IAc9C;;;;;;;;OAQG;IACH,iBAAiB,CAAC,OAAO,EAAE,YAAY,GAAG,MAAM;IAIhD;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,GAAG,MAAM;CAWvE;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,UAAU,EAAE,UAAU,CAAC;CACxB"}
@@ -1 +1 @@
1
- {"version":3,"file":"urlResolveInterceptor.d.ts","sourceRoot":"","sources":["../src/urlResolveInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,qBAAsB,YAAW,WAAW;IACvD;;OAEG;IACH,IAAI,SAA2B;IAE/B;;;;;;;;OAQG;IACH,KAAK,SAAiC;IAEtC;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CAQlD"}
1
+ {"version":3,"file":"urlResolveInterceptor.d.ts","sourceRoot":"","sources":["../src/urlResolveInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,qBAAsB,YAAW,WAAW;IACvD;;OAEG;IACH,IAAI,SAA2B;IAE/B;;;;;;;;OAQG;IACH,KAAK,SAAiC;IAEtC;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CAKlD"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Merges two record objects, giving precedence to the second record for overlapping keys.
3
+ *
4
+ * This utility function is used to merge configuration objects where the second object
5
+ * takes precedence over the first when there are conflicting keys.
6
+ *
7
+ * @template V - The type of values in the records
8
+ * @param first - The first record to merge (lower precedence)
9
+ * @param second - The second record to merge (higher precedence)
10
+ * @returns A new merged record, or undefined if both inputs are undefined
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Merge two objects
15
+ * const defaults = { timeout: 5000, retries: 3 };
16
+ * const overrides = { timeout: 10000 };
17
+ * const result = mergeRecords(defaults, overrides);
18
+ * // Result: { timeout: 10000, retries: 3 }
19
+ *
20
+ * // Handle undefined values
21
+ * const result2 = mergeRecords(undefined, { timeout: 5000 });
22
+ * // Result: { timeout: 5000 }
23
+ *
24
+ * // Return undefined when both are undefined
25
+ * const result3 = mergeRecords(undefined, undefined);
26
+ * // Result: undefined
27
+ * ```
28
+ */
29
+ export declare function mergeRecords<V>(first?: Record<string, V>, second?: Record<string, V>): Record<string, V> | undefined;
30
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,EACzB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,CAkB/B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.8.3",
3
+ "version": "0.8.6",
4
4
  "description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
5
5
  "keywords": [
6
6
  "fetch",