@@ -5,7 +5,7 @@ import is from '@sindresorhus/is';
5
5
import { ParseError , ReadError , HTTPError } from './errors' ;
6
6
import { normalizeArguments , mergeOptions } from './normalize-arguments' ;
7
7
import requestAsEventEmitter , { proxyEvents } from './request-as-event-emitter' ;
8
- import { CancelableRequest , GeneralError , NormalizedOptions , Response } from './utils/ types' ;
8
+ import { CancelableRequest , GeneralError , NormalizedOptions , Response } from './types' ;
9
9
10
10
const parseBody = ( body : Buffer , responseType : NormalizedOptions [ 'responseType' ] , encoding : NormalizedOptions [ 'encoding' ] ) : unknown => {
11
11
if ( responseType === 'json' ) {
@@ -27,7 +27,6 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ
27
27
const proxy = new EventEmitter ( ) ;
28
28
let body : Buffer ;
29
29
30
- // @ts -ignore `.json()`, `.buffer()` and `.text()` are added later
31
30
const promise = new PCancelable < Response | Response [ 'body' ] > ( ( resolve , reject , onCancel ) => {
32
31
const emitter = requestAsEventEmitter ( options ) ;
33
32
onCancel ( emitter . abort ) ;
@@ -84,10 +83,10 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ
84
83
85
84
try {
86
85
for ( const [ index , hook ] of options . hooks . afterResponse . entries ( ) ) {
87
- // @ts -ignore Promise is not assignable to CancelableRequest
86
+ // @ts -ignore TS doesn't notice that CancelableRequest is a Promise
88
87
// eslint-disable-next-line no-await-in-loop
89
- response = await hook ( response , async ( updatedOptions : NormalizedOptions ) => {
90
- updatedOptions = normalizeArguments ( mergeOptions ( options , {
88
+ response = await hook ( response , async ( updatedOptions ) : CancelableRequest < Response > => {
89
+ const typedOptions = normalizeArguments ( mergeOptions ( options , {
91
90
...updatedOptions ,
92
91
retry : {
93
92
calculateDelay : ( ) => 0
@@ -98,21 +97,21 @@ export default function asPromise<T>(options: NormalizedOptions): CancelableRequ
98
97
99
98
// Remove any further hooks for that request, because we'll call them anyway.
100
99
// The loop continues. We don't want duplicates (asPromise recursion).
101
- updatedOptions . hooks . afterResponse = options . hooks . afterResponse . slice ( 0 , index ) ;
100
+ typedOptions . hooks . afterResponse = options . hooks . afterResponse . slice ( 0 , index ) ;
102
101
103
102
for ( const hook of options . hooks . beforeRetry ) {
104
103
// eslint-disable-next-line no-await-in-loop
105
- await hook ( updatedOptions ) ;
104
+ await hook ( typedOptions ) ;
106
105
}
107
106
108
- const promise = asPromise ( updatedOptions ) ;
107
+ const promise = asPromise ( typedOptions ) ;
109
108
110
109
onCancel ( ( ) => {
111
110
promise . catch ( ( ) => { } ) ;
112
111
promise . cancel ( ) ;
113
112
} ) ;
114
113
115
- return promise ;
114
+ return promise as unknown as CancelableRequest < Response > ;
116
115
} ) ;
117
116
}
118
117
} catch ( error ) {
0 commit comments