-
Notifications
You must be signed in to change notification settings - Fork 200
Add headers option support to other loaders #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add headers option support to other loaders #176
Conversation
js/jsonld.js
Outdated
/** | ||
* Build an headers object from custom headers and assert Accept header is not override. | ||
* | ||
* @param headers an o |
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.
fix
js/jsonld.js
Outdated
@@ -1646,6 +1646,30 @@ jsonld.cache = { | |||
}; | |||
|
|||
/** | |||
* Accept header. | |||
*/ | |||
jsonld.acceptHeader = 'application/ld+json, application/json'; |
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.
We should really have a jsonld.defaults
object to put things like this under, if we don't have that already. If we don't have it, let's add it now before adding yet another new top-level thing:
jsonld.defaults = {
headers: {
accept: 'application/ld+json, application/json'
}
};
I wonder if it needs further scoping than that even. Thoughts from anyone else?
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.
If this isn't a default that could be changed or if we're not into figuring this out right now, let's not expose it in the public API.
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.
This isn't a default, I just reproduced the behaviour previously implemented on the node loader.
But in my opinion, it should only be a default. The current implementation is far too restrictive on this accept
header. The RangeError
exception should only be raised when the accept
header doesn't contain application/ld+json
nor application/json
.
This way it could be overridden for more exotic things like application/ld+json, text/html, application/json
, that would still be valid.
What do you think?
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 agreed on the defaults
object, if that looks good to you.
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 recommend we just take acceptHeader
off of the public API for now (off of jsonld.
) and make a private module defaults var:
var _defaults = {
headers: {
accept: 'application/ld+json, application/json'
}
};
That way we move towards something we can expose in the future but we don't have to decide on exactly what it's shape is since that's not the point of this PR right now.
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.
Alright, will do, but keep in mind this isn't exactly a default value at the moment, since it can't be changed.
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.
Yup, it's just a private marker for a half-baked idea.
js/jsonld.js
Outdated
@@ -1646,6 +1646,30 @@ jsonld.cache = { | |||
}; | |||
|
|||
/** | |||
* Accept header. | |||
*/ | |||
jsonld.acceptHeader = 'application/ld+json, application/json'; |
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 recommend we just take acceptHeader
off of the public API for now (off of jsonld.
) and make a private module defaults var:
var _defaults = {
headers: {
accept: 'application/ld+json, application/json'
}
};
That way we move towards something we can expose in the future but we don't have to decide on exactly what it's shape is since that's not the point of this PR right now.
js/jsonld.js
Outdated
*/ | ||
jsonld.buildHeaders = function (headers) { | ||
headers = headers || {}; | ||
if ('Accept' in headers || 'accept' 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.
Nits: Remove space after if
and function
to keep in the existing code style of this module.
Also, this check here will cover only two possible capitalizations of accept
. It's annoying, but perhaps we should do this instead:
var hasAccept = Object.keys(headers).map(function(h) {
return h.toLowerCase();
}).indexOf('accept') !== -1;
if(hasAccept) {
// throw range error
}
js/jsonld.js
Outdated
@@ -1656,6 +1680,8 @@ jsonld.documentLoaders = {}; | |||
* @param $ the jquery instance to use. | |||
* @param options the options to use: | |||
* secure: require all URLs to use HTTPS. | |||
* headers: an array of headers which will be passed as request |
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.
headers
is actually not an array, but an object (map), right?
51440e3
to
7833bc2
Compare
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.
LGTM
Should I be concerned about these tests failing? It seems
|
@davidlehn, can you please review to see if we're good to accept this? Thanks! |
|
7833bc2
to
28014d9
Compare
@davidlehn |
@dlongley @davidlehn is there anything else I could do to help this to be merged? |
@lutangar, I don't think so, it LGTM. @davidlehn, can we pull this in now? |
Looks ok. I'll rebase and merge. I'm going to add a patch to use some() vs map(). Not a big deal in this case but might as well: https://jsperf.com/check-for-lowercase-key/ |
Hi there,
Pretty self-explanatory, I didn't expect to find the
headers
options to benode
loader only...The support of this option is now extended to
jquery
andxhr
loaders.I may work on a
fetch
loader in the future, with the approriate polyfill for node, that would mean same code for node and the browser and a promise first API.