-
Notifications
You must be signed in to change notification settings - Fork 58
Adding fetch implementation of request api, issue #139 #243
Conversation
timeout && timeout.destroy(); | ||
|
||
let { responseType = '' } = options; | ||
let body: any = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you type this as a Promise<any>
(or not any
if you know the body type) and then you wouldn't need the Promise.resolve('body').then...
just simply body.then...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Good point. I guess I had typed that before I realized it was going to have to be a Promise, and then forgot to change it :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple minor comments/questions.
break; | ||
} | ||
|
||
let responseHeaders: any = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason not const
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also do you know the type of headers
, presumably something like maybe
interface Headers {
[index string]: string;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
|
||
case 'xml': | ||
body = fetchResponse.text().then((asText: string) => { | ||
let parser = new DOMParser(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
fetch(request).then((fetchResponse: any) => { | ||
timeout && timeout.destroy(); | ||
|
||
let { responseType = '' } = options; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
const requestUrl = generateRequestUrl(url, options); | ||
|
||
if ((!options.user || !options.password) && options.auth) { | ||
let auth = options.auth.split(':'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
let hasContentTypeHeader = false; | ||
let hasRequestedWithHeader = false; | ||
|
||
for (let header in headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry about all the const
suggestions...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
responseHeaders[ key.toLowerCase() ] = fetchResponse.headers.get(key); | ||
}); | ||
|
||
Promise.resolve(body).then((body: any) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call this data
and you can just use data
in the resolved object and drop the : body
if (options.timeout > 0 && options.timeout !== Infinity) { | ||
timeout = (function (): Handle { | ||
const timer = setTimeout(function (): void { | ||
const error = new RequestTimeoutError('Request timed out after ' + options.timeout + 'ms'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about using template literals
new RequestTimeoutError(`Request timed out after ${options.timeout}ms`);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
reject(error); | ||
}, options.timeout); | ||
|
||
return createHandle(function (): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can do if you so desire,
return createHandle((): void => {
clearTimeout(timer);
});
or perhaps even
timeout = ((): Handle => {
const timer = setTimeout((): void => {
const error = new RequestTimeoutError('Request timed out after ' + options.timeout + 'ms');
reject(error);
}, options.timeout);
return createHandle(clearTimeout.bind(null, timer));
})();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
|
||
const responseHeaders: ResponseHeaders = {}; | ||
|
||
forOf(fetchResponse.headers.keys(), (key: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another mainly styling suggestion (I think that works 😉 )
forOf(fetchResponse.headers.entries(), ([key, value]) => {
responseHeaders[ key.toLowerCase() ] = value;
});
|
||
const responseHeaders: ResponseHeaders = {}; | ||
|
||
forOf(fetchResponse.headers.enrties(), ([key, value]) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops typo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. how did that build? ....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really do not know!
Current coverage is 94.52% (diff: 100%)@@ master #243 diff @@
==========================================
Files 47 48 +1
Lines 2457 2519 +62
Methods 28 29 +1
Messages 0 0
Branches 464 477 +13
==========================================
- Hits 2404 2381 -23
Misses 51 51
- Partials 2 87 +85
|
break; | ||
|
||
case 'xml': | ||
body = fetchResponse.text().then((asText: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe .then
should be on a new line as per the style guide
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved from me! Someone else might want eyes on before the big green light.
Not sure if anyone else will get the opportunity to review this 🎱 |
A few stray thoughts that are probably worth addressing mostly later as we decide how useful the fetch API might be:
|
We have a response filter that runs at a "higher level" that will turn the json type into an actual JSON object.
I'm OK with ignoring that :) If we do decide to implement it, we should at least figure out how we're going to pull in
It'll get parsed as plain text.
That is a great question :) I've ignored them for now because we didn't have anything similar in the xhr/node requests, but adding those options might be a compelling reason why someone would want to use the fetch API over the alternatives. |
Added
request/fetch
for performing requests based on thefetch
API.