Skip to content

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

Conversation

lutangar
Copy link
Contributor

Hi there,

Pretty self-explanatory, I didn't expect to find the headers options to be node loader only...
The support of this option is now extended to jquery and xhr 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.

js/jsonld.js Outdated
/**
* Build an headers object from custom headers and assert Accept header is not override.
*
* @param headers an o
Copy link
Member

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

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?

Copy link
Member

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.

Copy link
Contributor Author

@lutangar lutangar Apr 27, 2017

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

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

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

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?

@lutangar lutangar force-pushed the feature/loader-headers-options branch from 51440e3 to 7833bc2 Compare April 28, 2017 15:26
Copy link
Member

@dlongley dlongley left a comment

Choose a reason for hiding this comment

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

LGTM

@lutangar
Copy link
Contributor Author

lutangar commented May 1, 2017

Should I be concerned about these tests failing? It seems master also have these errors...
https://travis-ci.org/digitalbazaar/jsonld.js/jobs/226858807

1) JSON-LD JSON-LD Test Suite Error handling #tp002 If not specified using processingMode, processing mode is taken as json-ld-1.0:
    AssertionError: 'invalid term definition' == 'processing mode conflict'
2) JSON-LD JSON-LD Test Suite Error handling #tp003 If @version is specified, it must be 1.1:
    AssertionError: 'invalid term definition' == 'invalid @version value'
3) JSON-LD JSON-LD Test Suite Error handling #tp005 If not specified using processingMode, processing mode is taken as json-ld-1.0:
    AssertionError: 'invalid term definition' == 'processing mode conflict'
4) JSON-LD JSON-LD Test Suite Error handling #tp006 If @version is specified, it must be 1.1:
    AssertionError: 'invalid term definition' == 'invalid @version value'

@dlongley
Copy link
Member

dlongley commented May 1, 2017

@lutangar,

No those tests are an ongoing issue right now (#172), you can safely ignore them. It looks like the other tests pass, so you're good.

@dlongley
Copy link
Member

dlongley commented May 1, 2017

@davidlehn, can you please review to see if we're good to accept this? Thanks!

@davidlehn
Copy link
Member

jsonld.buildHeaders should probably just be a local function for now instead of being exposed on jsonld. Does it need to be a public API?

jsonld.buildHeaders is mutating the input options and adding the 'Accept' field. This would be a problem if you call the APIs with the same options object twice and the code checking for 'Accept' would throw an error the second time. Can fix this by copying all the options headers over into a new headers object (like some of the code did before).

@lutangar lutangar force-pushed the feature/loader-headers-options branch from 7833bc2 to 28014d9 Compare May 2, 2017 08:38
@lutangar
Copy link
Contributor Author

lutangar commented May 2, 2017

@davidlehn buildHeaders doesn't need to be public at the moment, this has been changed.
I also changed back headers construction to something closer to previous code, meaning without the mutation.

@lutangar
Copy link
Contributor Author

lutangar commented May 9, 2017

@dlongley @davidlehn is there anything else I could do to help this to be merged?

@dlongley
Copy link
Member

dlongley commented May 9, 2017

@lutangar, I don't think so, it LGTM. @davidlehn, can we pull this in now?

@davidlehn
Copy link
Member

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/

@davidlehn davidlehn merged commit 22dac47 into digitalbazaar:master May 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants