4
4
const fetch = require ( 'node-fetch' )
5
5
const merge = require ( 'merge-options' ) . bind ( { ignoreUndefined : true } )
6
6
const { URL , URLSearchParams } = require ( 'iso-url' )
7
- const TextDecoder = require ( './text-encoder ' )
7
+ const TextDecoder = require ( './text-decoder ' )
8
8
const AbortController = require ( 'abort-controller' )
9
9
const anySignal = require ( 'any-signal' )
10
10
@@ -47,8 +47,8 @@ const timeout = (promise, ms, abortController) => {
47
47
}
48
48
} , ms )
49
49
50
- const after = ( next ) => {
51
- return ( res ) => {
50
+ const after = next => {
51
+ return res => {
52
52
clearTimeout ( timeoutID )
53
53
54
54
if ( timedOut ( ) ) {
@@ -60,8 +60,7 @@ const timeout = (promise, ms, abortController) => {
60
60
}
61
61
}
62
62
63
- promise
64
- . then ( after ( resolve ) , after ( reject ) )
63
+ promise . then ( after ( resolve ) , after ( reject ) )
65
64
} )
66
65
}
67
66
@@ -112,14 +111,23 @@ class HTTP {
112
111
opts . headers = new Headers ( opts . headers )
113
112
114
113
// validate resource type
115
- if ( typeof resource !== 'string' && ! ( resource instanceof URL || resource instanceof Request ) ) {
114
+ if (
115
+ typeof resource !== 'string' &&
116
+ ! ( resource instanceof URL || resource instanceof Request )
117
+ ) {
116
118
throw new TypeError ( '`resource` must be a string, URL, or Request' )
117
119
}
118
120
119
121
// validate resource format and normalize with prefixUrl
120
- if ( opts . base && typeof opts . base === 'string' && typeof resource === 'string' ) {
122
+ if (
123
+ opts . base &&
124
+ typeof opts . base === 'string' &&
125
+ typeof resource === 'string'
126
+ ) {
121
127
if ( resource . startsWith ( '/' ) ) {
122
- throw new Error ( '`resource` must not begin with a slash when using `base`' )
128
+ throw new Error (
129
+ '`resource` must not begin with a slash when using `base`'
130
+ )
123
131
}
124
132
125
133
if ( ! opts . base . endsWith ( '/' ) ) {
@@ -133,7 +141,9 @@ class HTTP {
133
141
const url = new URL ( resource , opts . base )
134
142
135
143
if ( opts . searchParams ) {
136
- url . search = opts . transformSearchParams ( new URLSearchParams ( opts . searchParams ) )
144
+ url . search = opts . transformSearchParams (
145
+ new URLSearchParams ( opts . searchParams )
146
+ )
137
147
}
138
148
139
149
if ( opts . json !== undefined ) {
@@ -144,11 +154,15 @@ class HTTP {
144
154
const abortController = new AbortController ( )
145
155
const signal = anySignal ( [ abortController . signal , opts . signal ] )
146
156
147
- const response = await timeout ( fetch ( url , {
148
- ...opts ,
149
- signal,
150
- timeout : undefined
151
- } ) , opts . timeout , abortController )
157
+ const response = await timeout (
158
+ fetch ( url , {
159
+ ...opts ,
160
+ signal,
161
+ timeout : undefined
162
+ } ) ,
163
+ opts . timeout ,
164
+ abortController
165
+ )
152
166
153
167
if ( ! response . ok && opts . throwHttpErrors ) {
154
168
if ( opts . handleError ) {
@@ -161,7 +175,7 @@ class HTTP {
161
175
const it = streamToAsyncIterator ( response . body )
162
176
163
177
if ( ! isAsyncIterator ( it ) ) {
164
- throw new Error ( ' Can\ 't convert fetch body into a Async Iterator:' )
178
+ throw new Error ( " Can't convert fetch body into a Async Iterator:" )
165
179
}
166
180
167
181
return it
@@ -273,7 +287,10 @@ const ndjson = async function * (source) {
273
287
const streamToAsyncIterator = function ( source ) {
274
288
if ( isAsyncIterator ( source ) ) {
275
289
// Workaround for https://github.com/node-fetch/node-fetch/issues/766
276
- if ( Object . prototype . hasOwnProperty . call ( source , 'readable' ) && Object . prototype . hasOwnProperty . call ( source , 'writable' ) ) {
290
+ if (
291
+ Object . prototype . hasOwnProperty . call ( source , 'readable' ) &&
292
+ Object . prototype . hasOwnProperty . call ( source , 'writable' )
293
+ ) {
277
294
const iter = source [ Symbol . asyncIterator ] ( )
278
295
279
296
const wrapper = {
@@ -310,11 +327,13 @@ const streamToAsyncIterator = function (source) {
310
327
}
311
328
}
312
329
313
- const isAsyncIterator = ( obj ) => {
314
- return typeof obj === 'object' &&
315
- obj !== null &&
316
- // typeof obj.next === 'function' &&
317
- typeof obj [ Symbol . asyncIterator ] === 'function'
330
+ const isAsyncIterator = obj => {
331
+ return (
332
+ typeof obj === 'object' &&
333
+ obj !== null &&
334
+ // typeof obj.next === 'function' &&
335
+ typeof obj [ Symbol . asyncIterator ] === 'function'
336
+ )
318
337
}
319
338
320
339
HTTP . HTTPError = HTTPError
@@ -354,6 +373,7 @@ HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options)
354
373
* @param {APIOptions } options
355
374
* @returns {Promise<Response> }
356
375
*/
357
- HTTP . options = ( resource , options ) => new HTTP ( options ) . options ( resource , options )
376
+ HTTP . options = ( resource , options ) =>
377
+ new HTTP ( options ) . options ( resource , options )
358
378
359
379
module . exports = HTTP
0 commit comments