@@ -115,13 +115,18 @@ function textParser(response: Response) {
115115 * existing one using the parent's `clone` function. When cloning, pass a new data-fetching function (if required) so
116116 * the clone uses this one instead of the one of the parent fetcher.
117117 */
118- export class DrFetch < TStatusCode extends number = StatusCode , T = unknown , Abortable extends boolean = false > {
119- #fetchFn: FetchFn ;
118+ export class DrFetch <
119+ TStatusCode extends number = StatusCode ,
120+ TFetchInit extends FetchFnInit = FetchFnInit ,
121+ T = unknown ,
122+ Abortable extends boolean = false
123+ > {
124+ #fetchFn: FetchFn < TFetchInit > ;
120125 #customProcessors: [ ProcessorPattern , ( response : Response , stockParsers : { json : BodyParserFn < any > ; text : BodyParserFn < string > ; } ) => Promise < any > ] [ ] = [ ] ;
121- #fetchImpl: ( url : FetchFnUrl , init ?: FetchFnInit ) => Promise < any > ;
126+ #fetchImpl: ( url : FetchFnUrl , init ?: TFetchInit ) => Promise < any > ;
122127 #autoAbortMap: Map < AutoAbortKey , AbortController > | undefined ;
123128
124- async #abortableFetch( url : FetchFnUrl , init ?: FetchFnInit ) {
129+ async #abortableFetch( url : FetchFnUrl , init ?: TFetchInit ) {
125130 try {
126131 return await this . #simpleFetch( url , init ) ;
127132 }
@@ -136,7 +141,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
136141 }
137142 }
138143
139- async #simpleFetch( url : FetchFnUrl , init ?: FetchFnInit ) {
144+ async #simpleFetch( url : FetchFnUrl , init ?: TFetchInit ) {
140145 const response = await this . #fetchFn( url , init ) ;
141146 const body = await this . #readBody( response ) ;
142147 return {
@@ -182,7 +187,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
182187 * function to register a custom body processor.
183188 * @param fetchFn Optional data-fetching function to use instead of the stock `fetch` function.
184189 */
185- constructor ( fetchFn ?: FetchFn ) {
190+ constructor ( fetchFn ?: FetchFn < TFetchInit > ) {
186191 this . #fetchFn = fetchFn ?? fetch . bind ( globalThis . window || global ) ;
187192 this . #fetchImpl = this . #simpleFetch. bind ( this ) ;
188193 }
@@ -219,7 +224,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
219224 if ( opts . preserveAbortable && this . isAbortable ) {
220225 newClone . abortable ( ) ;
221226 }
222- return newClone as DrFetch < TStatusCode , TInherit extends true ? T : unknown , CloneAbortable > ;
227+ return newClone as DrFetch < TStatusCode , TFetchInit , TInherit extends true ? T : unknown , CloneAbortable > ;
223228 }
224229
225230 /**
@@ -249,7 +254,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
249254 * @returns This fetcher object with its response type modified to include the body specification provided.
250255 */
251256 for < TStatus extends TStatusCode , TBody = { } > ( ) {
252- return this as DrFetch < TStatusCode , FetchResult < T , TStatus , TBody > , Abortable > ;
257+ return this as DrFetch < TStatusCode , TFetchInit , FetchResult < T , TStatus , TBody > , Abortable > ;
253258 }
254259
255260 #contentMatchesType( contentType : string , response : Response , ...types : ProcessorPattern [ ] ) {
@@ -308,7 +313,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
308313 abortable ( ) {
309314 this . #fetchImpl = this . #abortableFetch. bind ( this ) ;
310315 this . #autoAbortMap ??= new Map < AutoAbortKey , AbortController > ( ) ;
311- return this as DrFetch < TStatusCode , T , true > ;
316+ return this as DrFetch < TStatusCode , TFetchInit , T , true > ;
312317 }
313318
314319 /**
@@ -318,7 +323,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
318323 * @param init Options for the data-fetching function.
319324 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
320325 */
321- async fetch ( url : FetchFnUrl , init ?: FetchFnInit ) : Promise < ( Abortable extends true ? {
326+ async fetch ( url : FetchFnUrl , init ?: TFetchInit ) : Promise < ( Abortable extends true ? {
322327 aborted : true ;
323328 error : DOMException ;
324329 } | T : T ) > {
@@ -333,7 +338,7 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
333338 this . #autoAbortMap?. get ( autoAbort . key ) ?. abort ( ) ;
334339 const ac = new AbortController ( ) ;
335340 this . #autoAbortMap! . set ( autoAbort . key , ac ) ;
336- init ??= { } ;
341+ init ??= { } as TFetchInit ;
337342 init . signal = ac . signal ;
338343 if ( autoAbort . delay !== undefined ) {
339344 const aborted = await new Promise < boolean > ( ( rs ) => {
@@ -371,17 +376,17 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
371376 * @param url URL for the fetch function call.
372377 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
373378 */
374- get ( url : URL | string , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
375- return this . fetch ( url , { ...init , method : 'GET' } ) ;
379+ get ( url : URL | string , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
380+ return this . fetch ( url , { ...init , method : 'GET' } as TFetchInit ) ;
376381 }
377382
378383 /**
379384 * Shortcut method to emit a HEAD HTTP request.
380385 * @param url URL for the fetch function call.
381386 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
382387 */
383- head ( url : URL | string , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
384- return this . fetch ( url , { ...init , method : 'HEAD' } ) ;
388+ head ( url : URL | string , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
389+ return this . fetch ( url , { ...init , method : 'HEAD' } as TFetchInit ) ;
385390 }
386391
387392 /**
@@ -398,10 +403,10 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
398403 * does in those cases.
399404 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
400405 */
401- post ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
406+ post ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
402407 const fullInit = this . #createInit( body , init ) ;
403408 fullInit . method = 'POST' ;
404- return this . fetch ( url , fullInit ) ;
409+ return this . fetch ( url , fullInit as TFetchInit ) ;
405410 }
406411
407412 /**
@@ -418,10 +423,10 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
418423 * does in those cases.
419424 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
420425 */
421- patch ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
426+ patch ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
422427 const fullInit = this . #createInit( body , init ) ;
423428 fullInit . method = 'PATCH' ;
424- return this . fetch ( url , fullInit ) ;
429+ return this . fetch ( url , fullInit as TFetchInit ) ;
425430 }
426431
427432 /**
@@ -438,10 +443,10 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
438443 * does in those cases.
439444 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
440445 */
441- delete ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
446+ delete ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
442447 const fullInit = this . #createInit( body , init ) ;
443448 fullInit . method = 'DELETE' ;
444- return this . fetch ( url , fullInit ) ;
449+ return this . fetch ( url , fullInit as TFetchInit ) ;
445450 }
446451
447452 /**
@@ -458,9 +463,9 @@ export class DrFetch<TStatusCode extends number = StatusCode, T = unknown, Abort
458463 * does in those cases.
459464 * @returns A response object with the HTTP response's `ok`, `status`, `statusText` and `body` properties.
460465 */
461- put ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < FetchFnInit , 'method' | 'body' > ) {
466+ put ( url : URL | string , body ?: BodyInit | null | Record < string , any > , init ?: Omit < TFetchInit , 'method' | 'body' > ) {
462467 const fullInit = this . #createInit( body , init ) ;
463468 fullInit . method = 'PUT' ;
464- return this . fetch ( url , fullInit ) ;
469+ return this . fetch ( url , fullInit as TFetchInit ) ;
465470 }
466471}
0 commit comments