Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Adding fetch implementation of request api, issue #139 #243

Merged
merged 8 commits into from
Dec 13, 2016

Conversation

rorticus
Copy link
Contributor

Added request/fetch for performing requests based on the fetch API.

timeout && timeout.destroy();

let { responseType = '' } = options;
let body: any = null;
Copy link
Member

@agubler agubler Nov 21, 2016

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...

Copy link
Contributor Author

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 :/

Copy link
Member

@agubler agubler left a 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 = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not const?

Copy link
Member

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;
}

Copy link
Contributor Author

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Contributor Author

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Contributor Author

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(':');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Contributor Author

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) {
Copy link
Member

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...

Copy link
Contributor Author

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) => {
Copy link
Member

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');
Copy link
Member

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`);

Copy link
Contributor Author

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 {
Copy link
Member

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));
			})();

Copy link
Contributor Author

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) => {
Copy link
Member

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]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops typo.

Copy link
Contributor Author

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? ....

Copy link
Member

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!

@dylans dylans added this to the 2016.11 milestone Nov 21, 2016
@codecov-io
Copy link

codecov-io commented Nov 21, 2016

Current coverage is 94.52% (diff: 100%)

Merging #243 into master will decrease coverage by 3.32%

@@             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   

Powered by Codecov. Last update d2bbb57...1d35e74

break;

case 'xml':
body = fetchResponse.text().then((asText: string) => {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member

@agubler agubler left a 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.

@agubler
Copy link
Member

agubler commented Dec 5, 2016

Not sure if anyone else will get the opportunity to review this 🎱

@dylans
Copy link
Member

dylans commented Dec 5, 2016

A few stray thoughts that are probably worth addressing mostly later as we decide how useful the fetch API might be:

  • the fetch API doesn't seem to have a convenience method for json, so the user must explicitly parse json? Is that how our request implementation works also?
  • the fetch API seems to implement ReadableStream... I assume we're planning to ignore that for now given the size of the streams shim?
  • are we ignoring the type Document (I see we have arraybuffer and blob), or does that get parsed the same way as plain text?
  • the fetch API has a number of scenarios around cors, requestType, requestDestination, etc. Do we plan to/need to support these features now or in the future?

@dylans dylans modified the milestones: 2016.12, 2016.11 Dec 5, 2016
@rorticus
Copy link
Contributor Author

rorticus commented Dec 5, 2016

the fetch API doesn't seem to have a convenience method for json, so the user must explicitly parse json? Is that how our request implementation works also?

We have a response filter that runs at a "higher level" that will turn the json type into an actual JSON object.

the fetch API seems to implement ReadableStream... I assume we're planning to ignore that for now given the size of the streams shim?

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 streams without the dependency in the node request.

are we ignoring the type Document (I see we have arraybuffer and blob), or does that get parsed the same way as plain text?

It'll get parsed as plain text.

the fetch API has a number of scenarios around cors, requestType, requestDestination, etc. Do we plan to/need to support these features now or in the future?

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.

@rorticus
Copy link
Contributor Author

Dylan's comments have been split into #255 and #254 to get this landed.

@rorticus rorticus merged commit e5b8f09 into dojo:master Dec 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants