@ahoo-wang/fetcher 0.9.0 → 0.9.2
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 +3 -2
- package/README.zh-CN.md +2 -2
- package/dist/fetchExchange.d.ts +7 -7
- package/dist/fetchInterceptor.d.ts +13 -13
- package/dist/fetchInterceptor.d.ts.map +1 -1
- package/dist/fetcher.d.ts +163 -56
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherError.d.ts +122 -0
- package/dist/fetcherError.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +336 -196
- package/dist/index.umd.js +1 -1
- package/dist/interceptor.d.ts +47 -49
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/requestBodyInterceptor.d.ts +14 -19
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/timeout.d.ts +6 -35
- package/dist/timeout.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts +25 -19
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urlResolveInterceptor.d.ts +6 -12
- package/dist/urlResolveInterceptor.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n,
|
|
1
|
+
(function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.Fetcher={}))})(this,(function(n){"use strict";class i extends Error{constructor(e,t){const s=e||t?.message||"An error occurred in the fetcher";super(s),this.cause=t,this.name="FetcherError",t?.stack&&(this.stack=t.stack),Object.setPrototypeOf(this,i.prototype)}}class d extends i{constructor(e){const t=e.error?.message||e.response?.statusText||`Request to ${e.request.url} failed during exchange`;super(t,e.error),this.exchange=e,this.name="ExchangeError",Object.setPrototypeOf(this,d.prototype)}}class l extends i{constructor(e,t){const s=t||e.error?.message||`Request to ${e.request.url} failed with no response`;super(s,e.error),this.exchange=e,this.name="FetchError",Object.setPrototypeOf(this,l.prototype)}}class m extends i{constructor(e){const t=e.method||"GET",s=`Request timeout of ${e.timeout}ms exceeded for ${t} ${e.url}`;super(s),this.name="FetchTimeoutError",this.request=e,Object.setPrototypeOf(this,m.prototype)}}function F(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function I(r,e){return F(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class R{constructor(e){this.baseURL=e}build(e,t){const s=t?.path,o=t?.query,a=I(this.baseURL,e);let c=this.interpolateUrl(a,s);if(o){const h=new URLSearchParams(o).toString();h&&(c+="?"+h)}return c}resolveRequestUrl(e){return this.build(e.url,e.urlParams)}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const a=t[o];if(a===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(a)}):e}}function P(r,e){return typeof r<"u"?r:e}async function w(r){const e=r.url,t=r.timeout,s=r;if(!t)return fetch(e,s);const o=new AbortController,a={...s,signal:o.signal};let c=null;const h=new Promise((G,B)=>{c=setTimeout(()=>{c&&clearTimeout(c);const C=new m(r);o.abort(C),B(C)},t)});try{return await Promise.race([fetch(e,a),h])}finally{c&&clearTimeout(c)}}function f(r,e){return e?r.filter(e).sort((t,s)=>t.order-s.order):[...r].sort((t,s)=>t.order-s.order)}var u=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(u||{});const D="Content-Type";var y=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(y||{});class A{constructor(){this.name="RequestBodyInterceptor",this.order=Number.MIN_SAFE_INTEGER+200}intercept(e){const t=e.request;if(t.body===void 0||t.body===null||typeof t.body!="object"||t.body instanceof ArrayBuffer||ArrayBuffer.isView(t.body)||t.body instanceof Blob||t.body instanceof File||t.body instanceof URLSearchParams||t.body instanceof FormData||t.body instanceof ReadableStream)return;const s={...t};s.body=JSON.stringify(t.body),s.headers||(s.headers={});const o=s.headers;o["Content-Type"]||(o["Content-Type"]=y.APPLICATION_JSON),e.request=s}}class O{constructor(){this.name="FetchInterceptor",this.order=Number.MAX_SAFE_INTEGER-100}async intercept(e){e.response=await w(e.request)}}class q{constructor(){this.name="UrlResolveInterceptor",this.order=Number.MIN_SAFE_INTEGER+100}intercept(e){const t=e.request;t.url=e.fetcher.urlBuilder.resolveRequestUrl(t)}}class p{constructor(e=[]){this.sortedInterceptors=[],this.sortedInterceptors=f(e)}get name(){return this.constructor.name}get order(){return Number.MIN_SAFE_INTEGER}use(e){return this.sortedInterceptors.some(t=>t.name===e.name)?!1:(this.sortedInterceptors=f([...this.sortedInterceptors,e]),!0)}eject(e){const t=this.sortedInterceptors;return this.sortedInterceptors=f(t,s=>s.name!==e),t.length!==this.sortedInterceptors.length}clear(){this.sortedInterceptors=[]}async intercept(e){for(const t of this.sortedInterceptors)await t.intercept(e)}}class N{constructor(){this.request=new p([new q,new A,new O]),this.response=new p,this.error=new p}}class S{constructor(e,t,s,o){this.attributes={},this.fetcher=e,this.request=t,this.response=s,this.error=o}hasError(){return!!this.error}hasResponse(){return!!this.response}}function E(r,e){if(!(r===void 0&&e===void 0))return e===void 0?r:r===void 0?e:{...r,...e}}const g={"Content-Type":y.APPLICATION_JSON},b={baseURL:"",headers:g};class U{constructor(e=b){this.headers=g,this.urlBuilder=new R(e.baseURL),this.headers=e.headers??g,this.timeout=e.timeout,this.interceptors=e.interceptors??new N}async fetch(e,t={}){const s=t;s.url=e;const o=await this.request(s);if(!o.response)throw new l(o,`Request to ${s.url} failed with no response`);return o.response}async request(e){const t=E(e.headers,this.headers),s={...e,headers:t,timeout:P(e.timeout,this.timeout)},o=new S(this,s);return this.exchange(o)}async exchange(e){try{return await this.interceptors.request.intercept(e),await this.interceptors.response.intercept(e),e}catch(t){if(e.error=t,await this.interceptors.error.intercept(e),!e.hasError())return e;throw new d(e)}}async methodFetch(e,t,s={}){return this.fetch(t,{...s,method:e})}async get(e,t={}){return this.methodFetch(u.GET,e,t)}async post(e,t={}){return this.methodFetch(u.POST,e,t)}async put(e,t={}){return this.methodFetch(u.PUT,e,t)}async delete(e,t={}){return this.methodFetch(u.DELETE,e,t)}async patch(e,t={}){return this.methodFetch(u.PATCH,e,t)}async head(e,t={}){return this.methodFetch(u.HEAD,e,t)}async options(e,t={}){return this.methodFetch(u.OPTIONS,e,t)}}const T="default";class L{constructor(){this.registrar=new Map}register(e,t){this.registrar.set(e,t)}unregister(e){return this.registrar.delete(e)}get(e){return this.registrar.get(e)}requiredGet(e){const t=this.get(e);if(!t)throw new Error(`Fetcher ${e} not found`);return t}get default(){return this.requiredGet(T)}set default(e){this.register(T,e)}get fetchers(){return new Map(this.registrar)}}const _=new L;function j(r,e){if(Object.keys(r).length===0)return e;if(Object.keys(e).length===0)return r;const t={path:E(r.urlParams?.path,e.urlParams?.path),query:E(r.urlParams?.query,e.urlParams?.query)},s={...r.headers,...e.headers},o=e.method??r.method,a=e.body??r.body,c=e.timeout??r.timeout,h=e.signal??r.signal;return{...r,...e,method:o,urlParams:t,headers:s,body:a,timeout:c,signal:h}}class v extends U{constructor(e,t=b){super(t),this.name=e,_.register(e,this)}}const M=new v(T);n.ContentTypeHeader=D,n.ContentTypeValues=y,n.DEFAULT_FETCHER_NAME=T,n.DEFAULT_OPTIONS=b,n.ExchangeError=d,n.FetchError=l,n.FetchExchange=S,n.FetchInterceptor=O,n.FetchTimeoutError=m,n.Fetcher=U,n.FetcherError=i,n.FetcherInterceptors=N,n.FetcherRegistrar=L,n.HttpMethod=u,n.InterceptorManager=p,n.NamedFetcher=v,n.RequestBodyInterceptor=A,n.UrlBuilder=R,n.UrlResolveInterceptor=q,n.combineURLs=I,n.fetcher=M,n.fetcherRegistrar=_,n.isAbsoluteURL=F,n.mergeRecords=E,n.mergeRequest=j,n.resolveTimeout=P,n.timeoutFetch=w,n.toSorted=f,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/interceptor.d.ts
CHANGED
|
@@ -2,14 +2,11 @@ import { NamedCapable } from './types';
|
|
|
2
2
|
import { OrderedCapable } from './orderedCapable';
|
|
3
3
|
import { FetchExchange } from './fetchExchange';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Interface for HTTP interceptors in the fetcher pipeline.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* Interceptors follow the Chain of Responsibility pattern, where each interceptor
|
|
12
|
-
* can modify the exchange object and pass it to the next interceptor in the chain.
|
|
7
|
+
* Interceptors are middleware components that can modify requests, responses, or handle errors
|
|
8
|
+
* at different stages of the HTTP request lifecycle. They follow the Chain of Responsibility
|
|
9
|
+
* pattern, where each interceptor can process the exchange object and pass it to the next.
|
|
13
10
|
*
|
|
14
11
|
* @example
|
|
15
12
|
* // Example of a custom request interceptor
|
|
@@ -27,21 +24,19 @@ import { FetchExchange } from './fetchExchange';
|
|
|
27
24
|
*/
|
|
28
25
|
export interface Interceptor extends NamedCapable, OrderedCapable {
|
|
29
26
|
/**
|
|
30
|
-
*
|
|
27
|
+
* Unique identifier for the interceptor.
|
|
31
28
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* including adding, removing, and preventing duplicates.
|
|
29
|
+
* Used by InterceptorManager to manage interceptors, including adding, removing,
|
|
30
|
+
* and preventing duplicates. Each interceptor must have a unique name.
|
|
35
31
|
*/
|
|
36
32
|
name: string;
|
|
37
33
|
/**
|
|
38
|
-
*
|
|
34
|
+
* Process the exchange object in the interceptor pipeline.
|
|
39
35
|
*
|
|
40
|
-
* This method is called by
|
|
41
|
-
*
|
|
42
|
-
* exchange object directly.
|
|
36
|
+
* This method is called by InterceptorManager to process the exchange object.
|
|
37
|
+
* Interceptors can modify request, response, or error properties directly.
|
|
43
38
|
*
|
|
44
|
-
* @param exchange - The
|
|
39
|
+
* @param exchange - The exchange object containing request, response, and error information
|
|
45
40
|
*
|
|
46
41
|
* @remarks
|
|
47
42
|
* Interceptors should modify the exchange object directly rather than returning it.
|
|
@@ -50,11 +45,10 @@ export interface Interceptor extends NamedCapable, OrderedCapable {
|
|
|
50
45
|
intercept(exchange: FetchExchange): void | Promise<void>;
|
|
51
46
|
}
|
|
52
47
|
/**
|
|
53
|
-
*
|
|
48
|
+
* Manager for a collection of interceptors of the same type.
|
|
54
49
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* handles one type of interceptor (request, response, or error).
|
|
50
|
+
* Handles adding, removing, and executing interceptors in the correct order.
|
|
51
|
+
* Each InterceptorManager instance manages one type of interceptor (request, response, or error).
|
|
58
52
|
*
|
|
59
53
|
* @remarks
|
|
60
54
|
* Interceptors are executed in ascending order of their `order` property.
|
|
@@ -75,23 +69,23 @@ export interface Interceptor extends NamedCapable, OrderedCapable {
|
|
|
75
69
|
*/
|
|
76
70
|
export declare class InterceptorManager implements Interceptor {
|
|
77
71
|
/**
|
|
78
|
-
* Gets the name of this interceptor manager
|
|
72
|
+
* Gets the name of this interceptor manager.
|
|
79
73
|
*
|
|
80
74
|
* @returns The constructor name of this class
|
|
81
75
|
*/
|
|
82
76
|
get name(): string;
|
|
83
77
|
/**
|
|
84
|
-
* Gets the order of this interceptor manager
|
|
78
|
+
* Gets the order of this interceptor manager.
|
|
85
79
|
*
|
|
86
80
|
* @returns Number.MIN_SAFE_INTEGER, indicating this manager should execute early
|
|
87
81
|
*/
|
|
88
82
|
get order(): number;
|
|
89
83
|
/**
|
|
90
|
-
* Array of interceptors managed by this manager, sorted by their order property
|
|
84
|
+
* Array of interceptors managed by this manager, sorted by their order property.
|
|
91
85
|
*/
|
|
92
86
|
private sortedInterceptors;
|
|
93
87
|
/**
|
|
94
|
-
*
|
|
88
|
+
* Initializes a new InterceptorManager instance.
|
|
95
89
|
*
|
|
96
90
|
* @param interceptors - Initial array of interceptors to manage
|
|
97
91
|
*
|
|
@@ -101,7 +95,7 @@ export declare class InterceptorManager implements Interceptor {
|
|
|
101
95
|
*/
|
|
102
96
|
constructor(interceptors?: Interceptor[]);
|
|
103
97
|
/**
|
|
104
|
-
* Adds an interceptor to this manager
|
|
98
|
+
* Adds an interceptor to this manager.
|
|
105
99
|
*
|
|
106
100
|
* @param interceptor - The interceptor to add
|
|
107
101
|
* @returns True if the interceptor was added, false if an interceptor with the
|
|
@@ -115,7 +109,7 @@ export declare class InterceptorManager implements Interceptor {
|
|
|
115
109
|
*/
|
|
116
110
|
use(interceptor: Interceptor): boolean;
|
|
117
111
|
/**
|
|
118
|
-
* Removes an interceptor by name
|
|
112
|
+
* Removes an interceptor by name.
|
|
119
113
|
*
|
|
120
114
|
* @param name - The name of the interceptor to remove
|
|
121
115
|
* @returns True if an interceptor was removed, false if no interceptor with the
|
|
@@ -123,14 +117,14 @@ export declare class InterceptorManager implements Interceptor {
|
|
|
123
117
|
*/
|
|
124
118
|
eject(name: string): boolean;
|
|
125
119
|
/**
|
|
126
|
-
* Removes all interceptors from this manager
|
|
120
|
+
* Removes all interceptors from this manager.
|
|
127
121
|
*/
|
|
128
122
|
clear(): void;
|
|
129
123
|
/**
|
|
130
|
-
* Executes all managed interceptors on the given exchange object
|
|
124
|
+
* Executes all managed interceptors on the given exchange object.
|
|
131
125
|
*
|
|
132
126
|
* @param exchange - The exchange object to process
|
|
133
|
-
* @returns A promise that resolves
|
|
127
|
+
* @returns A promise that resolves when all interceptors have been executed
|
|
134
128
|
*
|
|
135
129
|
* @remarks
|
|
136
130
|
* Interceptors are executed in order, with each interceptor receiving the result
|
|
@@ -143,14 +137,15 @@ export declare class InterceptorManager implements Interceptor {
|
|
|
143
137
|
intercept(exchange: FetchExchange): Promise<void>;
|
|
144
138
|
}
|
|
145
139
|
/**
|
|
146
|
-
*
|
|
140
|
+
* Collection of interceptor managers for the Fetcher client.
|
|
147
141
|
*
|
|
148
|
-
*
|
|
142
|
+
* Manages three types of interceptors:
|
|
149
143
|
* 1. Request interceptors - Process requests before sending HTTP requests
|
|
150
144
|
* 2. Response interceptors - Process responses after receiving HTTP responses
|
|
151
145
|
* 3. Error interceptors - Handle errors when they occur during the request process
|
|
152
146
|
*
|
|
153
|
-
* Each type of interceptor is managed by an InterceptorManager instance, supporting
|
|
147
|
+
* Each type of interceptor is managed by an InterceptorManager instance, supporting
|
|
148
|
+
* adding, removing, and executing interceptors.
|
|
154
149
|
*
|
|
155
150
|
* @example
|
|
156
151
|
* // Create a custom interceptor
|
|
@@ -172,44 +167,47 @@ export declare class InterceptorManager implements Interceptor {
|
|
|
172
167
|
* fetcher.interceptors.request.use(customRequestInterceptor);
|
|
173
168
|
*
|
|
174
169
|
* @remarks
|
|
175
|
-
* By default, the request interceptor manager has
|
|
176
|
-
* 1.
|
|
177
|
-
* 2.
|
|
170
|
+
* By default, the request interceptor manager has three built-in interceptors registered:
|
|
171
|
+
* 1. UrlResolveInterceptor - Resolves the final URL with parameters
|
|
172
|
+
* 2. RequestBodyInterceptor - Automatically converts object-type request bodies to JSON strings
|
|
173
|
+
* 3. FetchInterceptor - Executes actual HTTP requests and handles timeouts
|
|
178
174
|
*/
|
|
179
175
|
export declare class FetcherInterceptors {
|
|
180
176
|
/**
|
|
181
|
-
*
|
|
177
|
+
* Manager for request-phase interceptors.
|
|
182
178
|
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
179
|
+
* Executed before HTTP requests are sent. Contains three built-in interceptors by default:
|
|
180
|
+
* UrlResolveInterceptor, RequestBodyInterceptor, and FetchInterceptor.
|
|
185
181
|
*
|
|
186
182
|
* @remarks
|
|
187
|
-
* Request interceptors are executed in ascending order of their order values, with smaller
|
|
188
|
-
* The default interceptors are:
|
|
183
|
+
* Request interceptors are executed in ascending order of their order values, with smaller
|
|
184
|
+
* values having higher priority. The default interceptors are:
|
|
189
185
|
* 1. UrlResolveInterceptor (order: Number.MIN_SAFE_INTEGER) - Resolves the final URL
|
|
190
186
|
* 2. RequestBodyInterceptor (order: 0) - Converts object bodies to JSON
|
|
191
187
|
* 3. FetchInterceptor (order: Number.MAX_SAFE_INTEGER) - Executes the actual HTTP request
|
|
192
188
|
*/
|
|
193
189
|
request: InterceptorManager;
|
|
194
190
|
/**
|
|
195
|
-
*
|
|
191
|
+
* Manager for response-phase interceptors.
|
|
196
192
|
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
193
|
+
* Executed after HTTP responses are received. Empty by default, custom response processing
|
|
194
|
+
* logic can be added as needed.
|
|
199
195
|
*
|
|
200
196
|
* @remarks
|
|
201
|
-
* Response interceptors are executed in ascending order of their order values, with smaller
|
|
197
|
+
* Response interceptors are executed in ascending order of their order values, with smaller
|
|
198
|
+
* values having higher priority.
|
|
202
199
|
*/
|
|
203
200
|
response: InterceptorManager;
|
|
204
201
|
/**
|
|
205
|
-
*
|
|
202
|
+
* Manager for error-handling phase interceptors.
|
|
206
203
|
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
204
|
+
* Executed when errors occur during HTTP requests. Empty by default, custom error handling
|
|
205
|
+
* logic can be added as needed.
|
|
209
206
|
*
|
|
210
207
|
* @remarks
|
|
211
|
-
* Error interceptors are executed in ascending order of their order values, with smaller
|
|
212
|
-
* Error interceptors can transform errors into normal responses,
|
|
208
|
+
* Error interceptors are executed in ascending order of their order values, with smaller
|
|
209
|
+
* values having higher priority. Error interceptors can transform errors into normal responses,
|
|
210
|
+
* avoiding thrown exceptions.
|
|
213
211
|
*/
|
|
214
212
|
error: InterceptorManager;
|
|
215
213
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,cAAc,EAAY,MAAM,kBAAkB,CAAC;AAG5D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,cAAc,EAAY,MAAM,kBAAkB,CAAC;AAG5D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,WAAY,SAAQ,YAAY,EAAE,cAAc;IAC/D;;;;;OAKG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,kBAAmB,YAAW,WAAW;IACpD;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;OAIG;IACH,IAAI,KAAK,IAAI,MAAM,CAElB;IAED;;OAEG;IACH,OAAO,CAAC,kBAAkB,CAAqB;IAE/C;;;;;;;;OAQG;gBACS,YAAY,GAAE,WAAW,EAAO;IAI5C;;;;;;;;;;;;OAYG;IACH,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO;IAWtC;;;;;;OAMG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAS5B;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;;;;;;;;;;OAaG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAMxD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,mBAAmB;IAC9B;;;;;;;;;;;;OAYG;IACH,OAAO,EAAE,kBAAkB,CAIxB;IAEH;;;;;;;;;OASG;IACH,QAAQ,EAAE,kBAAkB,CAA4B;IAExD;;;;;;;;;;OAUG;IACH,KAAK,EAAE,kBAAkB,CAA4B;CACtD"}
|
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
import { Interceptor } from './interceptor';
|
|
2
2
|
import { FetchExchange } from './fetchExchange';
|
|
3
3
|
/**
|
|
4
|
-
* RequestBodyInterceptor Class
|
|
5
|
-
*
|
|
6
4
|
* Interceptor responsible for converting plain objects to JSON strings for HTTP request bodies.
|
|
7
|
-
* This interceptor ensures that object request bodies are properly serialized and that
|
|
8
|
-
* the appropriate Content-Type header is set.
|
|
9
5
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* before other interceptors process them.
|
|
6
|
+
* This interceptor ensures that object request bodies are properly serialized and that
|
|
7
|
+
* the appropriate Content-Type header is set. It runs early in the request processing chain
|
|
8
|
+
* to ensure request bodies are properly formatted before other interceptors process them.
|
|
14
9
|
*/
|
|
15
10
|
export declare class RequestBodyInterceptor implements Interceptor {
|
|
16
11
|
/**
|
|
17
|
-
* Interceptor name, used for identification and management
|
|
12
|
+
* Interceptor name, used for identification and management.
|
|
18
13
|
*/
|
|
19
14
|
name: string;
|
|
20
15
|
/**
|
|
21
|
-
* Interceptor execution order, set to run after UrlResolveInterceptor but before FetchInterceptor
|
|
16
|
+
* Interceptor execution order, set to run after UrlResolveInterceptor but before FetchInterceptor.
|
|
22
17
|
*
|
|
23
|
-
* @remarks
|
|
24
18
|
* This interceptor should run after URL resolution (UrlResolveInterceptor) but before
|
|
25
19
|
* the actual HTTP request is made (FetchInterceptor). The order is set to
|
|
26
20
|
* Number.MIN_SAFE_INTEGER + 200 to ensure it executes in the correct position
|
|
@@ -29,7 +23,7 @@ export declare class RequestBodyInterceptor implements Interceptor {
|
|
|
29
23
|
*/
|
|
30
24
|
order: number;
|
|
31
25
|
/**
|
|
32
|
-
* Attempts to convert request body to a valid fetch API body type
|
|
26
|
+
* Attempts to convert request body to a valid fetch API body type.
|
|
33
27
|
*
|
|
34
28
|
* According to the Fetch API specification, body can be multiple types, but for
|
|
35
29
|
* plain objects, they need to be converted to JSON strings.
|
|
@@ -51,18 +45,19 @@ export declare class RequestBodyInterceptor implements Interceptor {
|
|
|
51
45
|
* converted to JSON strings.
|
|
52
46
|
*
|
|
53
47
|
* @param exchange - The exchange object containing the request to process
|
|
54
|
-
* @returns The processed exchange object with potentially modified request body
|
|
55
48
|
*
|
|
56
49
|
* @example
|
|
57
50
|
* // Plain object body will be converted to JSON
|
|
58
|
-
* const
|
|
59
|
-
*
|
|
51
|
+
* const fetcher = new Fetcher();
|
|
52
|
+
* const exchange = new FetchExchange(
|
|
53
|
+
* fetcher,
|
|
54
|
+
* {
|
|
60
55
|
* body: { name: 'John', age: 30 }
|
|
61
56
|
* }
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* //
|
|
65
|
-
* //
|
|
57
|
+
* );
|
|
58
|
+
* interceptor.intercept(exchange);
|
|
59
|
+
* // exchange.request.body will be '{"name":"John","age":30}'
|
|
60
|
+
* // exchange.request.headers will include 'Content-Type: application/json'
|
|
66
61
|
*/
|
|
67
62
|
intercept(exchange: FetchExchange): void;
|
|
68
63
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD
|
|
1
|
+
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD;;;;;;GAMG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IACxD;;OAEG;IACH,IAAI,SAA4B;IAEhC;;;;;;;;OAQG;IACH,KAAK,SAAiC;IAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa;CA0ClC"}
|
package/dist/timeout.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FetchRequest } from './fetchRequest';
|
|
2
2
|
/**
|
|
3
|
-
* TimeoutCapable Interface
|
|
4
|
-
*
|
|
5
3
|
* Interface that defines timeout capability for HTTP requests.
|
|
4
|
+
*
|
|
5
|
+
* Objects implementing this interface can specify timeout values for HTTP requests.
|
|
6
6
|
*/
|
|
7
7
|
export interface TimeoutCapable {
|
|
8
8
|
/**
|
|
9
|
-
* Request timeout in milliseconds
|
|
9
|
+
* Request timeout in milliseconds.
|
|
10
10
|
*
|
|
11
11
|
* When the value is 0, it indicates no timeout should be set.
|
|
12
12
|
* The default value is undefined.
|
|
@@ -14,7 +14,7 @@ export interface TimeoutCapable {
|
|
|
14
14
|
timeout?: number;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* Resolves request timeout settings, prioritizing request-level timeout settings
|
|
17
|
+
* Resolves request timeout settings, prioritizing request-level timeout settings.
|
|
18
18
|
*
|
|
19
19
|
* @param requestTimeout - Request-level timeout setting
|
|
20
20
|
* @param optionsTimeout - Configuration-level timeout setting
|
|
@@ -26,38 +26,9 @@ export interface TimeoutCapable {
|
|
|
26
26
|
*/
|
|
27
27
|
export declare function resolveTimeout(requestTimeout?: number, optionsTimeout?: number): number | undefined;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* Exception class thrown when an HTTP request times out.
|
|
32
|
-
* This error is thrown by the timeoutFetch function when a request exceeds its timeout limit.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```typescript
|
|
36
|
-
* try {
|
|
37
|
-
* const response = await timeoutFetch('https://api.example.com/users', {}, 1000);
|
|
38
|
-
* } catch (error) {
|
|
39
|
-
* if (error instanceof FetchTimeoutError) {
|
|
40
|
-
* console.log(`Request timed out after ${error.timeout}ms`);
|
|
41
|
-
* }
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export declare class FetchTimeoutError extends Error {
|
|
46
|
-
/**
|
|
47
|
-
* The request options that timed out
|
|
48
|
-
*/
|
|
49
|
-
request: FetchRequest;
|
|
50
|
-
/**
|
|
51
|
-
* Creates a new FetchTimeoutError instance
|
|
52
|
-
*
|
|
53
|
-
* @param request - The request options that timed out
|
|
54
|
-
*/
|
|
55
|
-
constructor(request: FetchRequest);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* HTTP request method with timeout control
|
|
29
|
+
* HTTP request method with timeout control.
|
|
59
30
|
*
|
|
60
|
-
*
|
|
31
|
+
* Uses Promise.race to implement timeout control, initiating both
|
|
61
32
|
* fetch request and timeout Promise simultaneously. When either Promise completes,
|
|
62
33
|
* it returns the result or throws an exception.
|
|
63
34
|
*
|
package/dist/timeout.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,cAAc,CAAC,EAAE,MAAM,EACvB,cAAc,CAAC,EAAE,MAAM,GACtB,MAAM,GAAG,SAAS,CAKpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,CAyC3E"}
|
package/dist/urlBuilder.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { BaseURLCapable, FetchRequest } from './fetchRequest';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Container for URL parameters including path and query parameters.
|
|
4
|
+
*
|
|
5
|
+
* Used to define dynamic parts of a URL including path placeholders and query string parameters.
|
|
4
6
|
*/
|
|
5
7
|
export interface UrlParams {
|
|
6
8
|
/**
|
|
7
|
-
* Path parameter object used to replace placeholders in the URL (e.g., {id})
|
|
9
|
+
* Path parameter object used to replace placeholders in the URL (e.g., {id}).
|
|
10
|
+
*
|
|
11
|
+
* These parameters are used to substitute named placeholders in the URL path.
|
|
8
12
|
*
|
|
9
13
|
* @example
|
|
10
14
|
* ```typescript
|
|
@@ -14,7 +18,9 @@ export interface UrlParams {
|
|
|
14
18
|
*/
|
|
15
19
|
path?: Record<string, any>;
|
|
16
20
|
/**
|
|
17
|
-
* Query parameter object to be added to the URL query string
|
|
21
|
+
* Query parameter object to be added to the URL query string.
|
|
22
|
+
*
|
|
23
|
+
* These parameters are appended to the URL as a query string.
|
|
18
24
|
*
|
|
19
25
|
* @example
|
|
20
26
|
* ```typescript
|
|
@@ -25,33 +31,31 @@ export interface UrlParams {
|
|
|
25
31
|
query?: Record<string, any>;
|
|
26
32
|
}
|
|
27
33
|
/**
|
|
28
|
-
*
|
|
34
|
+
* Utility class for constructing complete URLs with path parameters and query parameters.
|
|
29
35
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
36
|
+
* Handles URL composition, path parameter interpolation, and query string generation.
|
|
37
|
+
* Combines a base URL with a path, replaces path placeholders with actual values, and appends
|
|
32
38
|
* query parameters to create a complete URL.
|
|
33
39
|
*
|
|
34
40
|
* @example
|
|
35
41
|
* ```typescript
|
|
36
42
|
* const urlBuilder = new UrlBuilder('https://api.example.com');
|
|
37
43
|
* const url = urlBuilder.build('/users/{id}', {
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* query: { filter: 'active' }
|
|
41
|
-
* }
|
|
44
|
+
* path: { id: 123 },
|
|
45
|
+
* query: { filter: 'active' }
|
|
42
46
|
* });
|
|
43
47
|
* // Result: https://api.example.com/users/123?filter=active
|
|
44
48
|
* ```
|
|
45
49
|
*/
|
|
46
50
|
export declare class UrlBuilder implements BaseURLCapable {
|
|
47
51
|
/**
|
|
48
|
-
* Base URL that all constructed URLs will be based on
|
|
52
|
+
* Base URL that all constructed URLs will be based on.
|
|
49
53
|
*
|
|
50
|
-
* This is typically the root of your API endpoint (e.g., 'https://api.example.com')
|
|
54
|
+
* This is typically the root of your API endpoint (e.g., 'https://api.example.com').
|
|
51
55
|
*/
|
|
52
56
|
baseURL: string;
|
|
53
57
|
/**
|
|
54
|
-
*
|
|
58
|
+
* Initializes a new UrlBuilder instance.
|
|
55
59
|
*
|
|
56
60
|
* @param baseURL - Base URL that all constructed URLs will be based on
|
|
57
61
|
*
|
|
@@ -62,7 +66,7 @@ export declare class UrlBuilder implements BaseURLCapable {
|
|
|
62
66
|
*/
|
|
63
67
|
constructor(baseURL: string);
|
|
64
68
|
/**
|
|
65
|
-
* Builds a complete URL, including path parameter replacement and query parameter addition
|
|
69
|
+
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
66
70
|
*
|
|
67
71
|
* @param url - URL path to build (e.g., '/users/{id}/posts')
|
|
68
72
|
* @param params - URL parameters including path and query parameters
|
|
@@ -81,9 +85,9 @@ export declare class UrlBuilder implements BaseURLCapable {
|
|
|
81
85
|
*/
|
|
82
86
|
build(url: string, params?: UrlParams): string;
|
|
83
87
|
/**
|
|
84
|
-
* Resolves a complete URL from a FetchRequest
|
|
88
|
+
* Resolves a complete URL from a FetchRequest.
|
|
85
89
|
*
|
|
86
|
-
*
|
|
90
|
+
* Used internally by the Fetcher to build the final URL for a request
|
|
87
91
|
* by combining the request URL with its URL parameters using this UrlBuilder.
|
|
88
92
|
*
|
|
89
93
|
* @param request - The FetchRequest containing URL and URL parameters
|
|
@@ -91,7 +95,7 @@ export declare class UrlBuilder implements BaseURLCapable {
|
|
|
91
95
|
*/
|
|
92
96
|
resolveRequestUrl(request: FetchRequest): string;
|
|
93
97
|
/**
|
|
94
|
-
* Replaces placeholders in the URL with path parameters
|
|
98
|
+
* Replaces placeholders in the URL with path parameters.
|
|
95
99
|
*
|
|
96
100
|
* @param url - Path string containing placeholders, e.g., "http://localhost/users/{id}/posts/{postId}"
|
|
97
101
|
* @param path - Path parameter object used to replace placeholders in the URL
|
|
@@ -120,11 +124,13 @@ export declare class UrlBuilder implements BaseURLCapable {
|
|
|
120
124
|
interpolateUrl(url: string, path?: Record<string, any> | null): string;
|
|
121
125
|
}
|
|
122
126
|
/**
|
|
123
|
-
* Interface for objects that have a UrlBuilder capability
|
|
127
|
+
* Interface for objects that have a UrlBuilder capability.
|
|
128
|
+
*
|
|
129
|
+
* Indicates that an object has a UrlBuilder instance for URL construction.
|
|
124
130
|
*/
|
|
125
131
|
export interface UrlBuilderCapable {
|
|
126
132
|
/**
|
|
127
|
-
* The UrlBuilder instance
|
|
133
|
+
* The UrlBuilder instance.
|
|
128
134
|
*/
|
|
129
135
|
urlBuilder: UrlBuilder;
|
|
130
136
|
}
|
package/dist/urlBuilder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
|
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;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;;;;;OAUG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE3B;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;GAgBG;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;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,UAAU,EAAE,UAAU,CAAC;CACxB"}
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import { Interceptor } from './interceptor';
|
|
2
2
|
import { FetchExchange } from './fetchExchange';
|
|
3
3
|
/**
|
|
4
|
-
* URL
|
|
4
|
+
* Interceptor responsible for resolving the final URL for a request.
|
|
5
5
|
*
|
|
6
|
-
* This interceptor
|
|
7
|
-
* the
|
|
6
|
+
* This interceptor combines the base URL, path parameters, and query parameters
|
|
7
|
+
* to create the final URL for a request. It should be executed early in
|
|
8
8
|
* the interceptor chain to ensure the URL is properly resolved before other interceptors
|
|
9
9
|
* process the request.
|
|
10
10
|
*
|
|
11
|
-
* @remarks
|
|
12
|
-
* This interceptor has the lowest possible order (Number.MIN_SAFE_INTEGER) to ensure
|
|
13
|
-
* it runs first in the request interceptor chain, before any other request processing.
|
|
14
|
-
*
|
|
15
11
|
* @example
|
|
16
12
|
* // With baseURL: 'https://api.example.com'
|
|
17
13
|
* // Request URL: '/users/{id}'
|
|
@@ -21,13 +17,12 @@ import { FetchExchange } from './fetchExchange';
|
|
|
21
17
|
*/
|
|
22
18
|
export declare class UrlResolveInterceptor implements Interceptor {
|
|
23
19
|
/**
|
|
24
|
-
* The name of this interceptor
|
|
20
|
+
* The name of this interceptor.
|
|
25
21
|
*/
|
|
26
22
|
name: string;
|
|
27
23
|
/**
|
|
28
|
-
* The order of this interceptor (executed first)
|
|
24
|
+
* The order of this interceptor (executed first).
|
|
29
25
|
*
|
|
30
|
-
* @remarks
|
|
31
26
|
* This interceptor should run first in the request interceptor chain to ensure
|
|
32
27
|
* URL resolution happens before any other request processing. The order is set to
|
|
33
28
|
* Number.MIN_SAFE_INTEGER + 100 to allow for other interceptors that need to run
|
|
@@ -35,10 +30,9 @@ export declare class UrlResolveInterceptor implements Interceptor {
|
|
|
35
30
|
*/
|
|
36
31
|
order: number;
|
|
37
32
|
/**
|
|
38
|
-
* Resolves the final URL by combining the base URL, path parameters, and query parameters
|
|
33
|
+
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
39
34
|
*
|
|
40
35
|
* @param exchange - The fetch exchange containing the request information
|
|
41
|
-
* @returns The modified exchange with the resolved URL
|
|
42
36
|
*/
|
|
43
37
|
intercept(exchange: FetchExchange): void;
|
|
44
38
|
}
|
|
@@ -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
|
|
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;;;;;;;;;;;;;;GAcG;AACH,qBAAa,qBAAsB,YAAW,WAAW;IACvD;;OAEG;IACH,IAAI,SAA2B;IAE/B;;;;;;;OAOG;IACH,KAAK,SAAiC;IAEtC;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa;CAIlC"}
|