Skip to content

Commit 22dac47

Browse files
lutangardavidlehn
authored andcommitted
add headers option support to other loaders
1 parent acf553d commit 22dac47

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

js/jsonld.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,41 @@ jsonld.cache = {
16451645
activeCtx: new jsonld.ActiveContextCache()
16461646
};
16471647

1648+
/**
1649+
* Accept header.
1650+
*/
1651+
var _defaults = {
1652+
headers: {
1653+
accept: 'application/ld+json, application/json'
1654+
}
1655+
};
1656+
1657+
/**
1658+
* Build an headers object from custom headers and assert `accept` header isn't overridden.
1659+
*
1660+
* @param {Object} optionsHeaders an object (map) of headers
1661+
* with key as header name and value as header value.
1662+
* @return {Object} an object (map) of headers with a valid `accept` header.
1663+
*/
1664+
function buildHeaders(optionsHeaders) {
1665+
optionsHeaders = optionsHeaders || {};
1666+
1667+
var hasAccept = Object.keys(optionsHeaders).map(function(h) {
1668+
return h.toLowerCase();
1669+
}).indexOf('accept') !== -1;
1670+
1671+
if(hasAccept) {
1672+
throw new RangeError(
1673+
'Accept header may not be specified as an option; only "' +
1674+
_defaults.headers.accept + '" is supported.');
1675+
}
1676+
1677+
var headers = {'Accept': _defaults.headers.accept};
1678+
for(var k in optionsHeaders) { headers[k] = optionsHeaders[k]; }
1679+
1680+
return headers;
1681+
}
1682+
16481683
/**
16491684
* Document loaders.
16501685
*/
@@ -1656,6 +1691,8 @@ jsonld.documentLoaders = {};
16561691
* @param $ the jquery instance to use.
16571692
* @param options the options to use:
16581693
* secure: require all URLs to use HTTPS.
1694+
* headers: an object (map) of headers which will be passed as request
1695+
* headers for the requested document. Accept is not allowed.
16591696
* usePromise: true to use a promises API, false for a
16601697
* callback-continuation-style API; defaults to true if Promise
16611698
* is globally defined, false if not.
@@ -1665,6 +1702,7 @@ jsonld.documentLoaders = {};
16651702
jsonld.documentLoaders.jquery = function($, options) {
16661703
options = options || {};
16671704
var queue = new jsonld.RequestQueue();
1705+
var headers = buildHeaders(options.headers);
16681706

16691707
// use option or, by default, use Promise when its defined
16701708
var usePromise = ('usePromise' in options ?
@@ -1694,12 +1732,9 @@ jsonld.documentLoaders.jquery = function($, options) {
16941732
$.ajax({
16951733
url: url,
16961734
accepts: {
1697-
json: 'application/ld+json, application/json'
1698-
},
1699-
// ensure Accept header is very specific for JSON-LD/JSON
1700-
headers: {
1701-
'Accept': 'application/ld+json, application/json'
1735+
json: _defaults.headers.accept
17021736
},
1737+
headers: headers,
17031738
dataType: 'json',
17041739
crossDomain: true,
17051740
success: function(data, textStatus, jqXHR) {
@@ -1747,7 +1782,7 @@ jsonld.documentLoaders.jquery = function($, options) {
17471782
* default.
17481783
* request: the object which will make the request, default is
17491784
* provided by `https://www.npmjs.com/package/request`.
1750-
* headers: an array of headers which will be passed as request
1785+
* headers: an object (map) of headers which will be passed as request
17511786
* headers for the requested document. Accept is not allowed.
17521787
* usePromise: true to use a promises API, false for a
17531788
* callback-continuation-style API; false by default.
@@ -1756,10 +1791,10 @@ jsonld.documentLoaders.jquery = function($, options) {
17561791
*/
17571792
jsonld.documentLoaders.node = function(options) {
17581793
options = options || {};
1794+
var headers = buildHeaders(options.headers);
17591795
var strictSSL = ('strictSSL' in options) ? options.strictSSL : true;
17601796
var maxRedirects = ('maxRedirects' in options) ? options.maxRedirects : -1;
17611797
var request = ('request' in options) ? options.request : require('request');
1762-
var acceptHeader = 'application/ld+json, application/json';
17631798
var http = require('http');
17641799
// TODO: disable cache until HTTP caching implemented
17651800
//var cache = new jsonld.DocumentCache();
@@ -1770,12 +1805,7 @@ jsonld.documentLoaders.node = function(options) {
17701805
return jsonld.promisify(loadDocument, url, []);
17711806
});
17721807
}
1773-
var headers = options.headers || {};
1774-
if('Accept' in headers || 'accept' in headers) {
1775-
throw new RangeError(
1776-
'Accept header may not be specified as an option; only "' +
1777-
acceptHeader + '" is supported.');
1778-
}
1808+
17791809
return queue.wrapLoader(function(url, callback) {
17801810
loadDocument(url, [], callback);
17811811
});
@@ -1800,8 +1830,7 @@ jsonld.documentLoaders.node = function(options) {
18001830
if(doc !== null) {
18011831
return callback(null, doc);
18021832
}
1803-
var headers = {'Accept': acceptHeader};
1804-
for(var k in options.headers) { headers[k] = options.headers[k]; }
1833+
18051834
request({
18061835
url: url,
18071836
headers: headers,
@@ -1892,6 +1921,8 @@ jsonld.documentLoaders.node = function(options) {
18921921
*
18931922
* @param options the options to use:
18941923
* secure: require all URLs to use HTTPS.
1924+
* headers: an object (map) of headers which will be passed as request
1925+
* headers for the requested document. Accept is not allowed.
18951926
* usePromise: true to use a promises API, false for a
18961927
* callback-continuation-style API; defaults to true if Promise
18971928
* is globally defined, false if not.
@@ -1903,6 +1934,7 @@ jsonld.documentLoaders.xhr = function(options) {
19031934
options = options || {};
19041935
var rlink = /(^|(\r\n))link:/i;
19051936
var queue = new jsonld.RequestQueue();
1937+
var headers = buildHeaders(options.headers);
19061938

19071939
// use option or, by default, use Promise when its defined
19081940
var usePromise = ('usePromise' in options ?
@@ -1975,7 +2007,11 @@ jsonld.documentLoaders.xhr = function(options) {
19752007
{contextUrl: null, documentUrl: url, document: null});
19762008
};
19772009
req.open('GET', url, true);
1978-
req.setRequestHeader('Accept', 'application/ld+json, application/json');
2010+
2011+
for(var k in headers) {
2012+
req.setRequestHeader(k, headers[k]);
2013+
}
2014+
19792015
req.send();
19802016
}
19812017
};

0 commit comments

Comments
 (0)