Skip to content

Commit 25579c4

Browse files
committed
add headers option support to other loaders
1 parent dc07d60 commit 25579c4

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

js/jsonld.js

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

1648+
/**
1649+
* Accept header.
1650+
*/
1651+
jsonld.acceptHeader = 'application/ld+json, application/json';
1652+
1653+
/**
1654+
* Build an headers object from custom headers and assert Accept header is not override.
1655+
*
1656+
* @param headers an o
1657+
* @return {*|{}}
1658+
*/
1659+
jsonld.buildHeaders = function (headers) {
1660+
headers = headers || {};
1661+
if ('Accept' in headers || 'accept' in headers) {
1662+
throw new RangeError(
1663+
'Accept header may not be specified as an option; only "' +
1664+
jsonld.acceptHeader + '" is supported.');
1665+
}
1666+
1667+
headers['Accept'] = jsonld.acceptHeader;
1668+
1669+
return headers;
1670+
};
1671+
16481672
/**
16491673
* Document loaders.
16501674
*/
@@ -1656,6 +1680,8 @@ jsonld.documentLoaders = {};
16561680
* @param $ the jquery instance to use.
16571681
* @param options the options to use:
16581682
* secure: require all URLs to use HTTPS.
1683+
* headers: an array of headers which will be passed as request
1684+
* headers for the requested document. Accept is not allowed.
16591685
* usePromise: true to use a promises API, false for a
16601686
* callback-continuation-style API; defaults to true if Promise
16611687
* is globally defined, false if not.
@@ -1665,6 +1691,7 @@ jsonld.documentLoaders = {};
16651691
jsonld.documentLoaders.jquery = function($, options) {
16661692
options = options || {};
16671693
var queue = new jsonld.RequestQueue();
1694+
var headers = jsonld.buildHeaders(options.headers);
16681695

16691696
// use option or, by default, use Promise when its defined
16701697
var usePromise = ('usePromise' in options ?
@@ -1694,12 +1721,10 @@ jsonld.documentLoaders.jquery = function($, options) {
16941721
$.ajax({
16951722
url: url,
16961723
accepts: {
1697-
json: 'application/ld+json, application/json'
1724+
json: jsonld.acceptHeader
16981725
},
16991726
// ensure Accept header is very specific for JSON-LD/JSON
1700-
headers: {
1701-
'Accept': 'application/ld+json, application/json'
1702-
},
1727+
headers: headers,
17031728
dataType: 'json',
17041729
crossDomain: true,
17051730
success: function(data, textStatus, jqXHR) {
@@ -1756,10 +1781,10 @@ jsonld.documentLoaders.jquery = function($, options) {
17561781
*/
17571782
jsonld.documentLoaders.node = function(options) {
17581783
options = options || {};
1784+
var headers = jsonld.buildHeaders(options.headers);
17591785
var strictSSL = ('strictSSL' in options) ? options.strictSSL : true;
17601786
var maxRedirects = ('maxRedirects' in options) ? options.maxRedirects : -1;
17611787
var request = ('request' in options) ? options.request : require('request');
1762-
var acceptHeader = 'application/ld+json, application/json';
17631788
var http = require('http');
17641789
// TODO: disable cache until HTTP caching implemented
17651790
//var cache = new jsonld.DocumentCache();
@@ -1770,12 +1795,7 @@ jsonld.documentLoaders.node = function(options) {
17701795
return jsonld.promisify(loadDocument, url, []);
17711796
});
17721797
}
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-
}
1798+
17791799
return queue.wrapLoader(function(url, callback) {
17801800
loadDocument(url, [], callback);
17811801
});
@@ -1800,8 +1820,7 @@ jsonld.documentLoaders.node = function(options) {
18001820
if(doc !== null) {
18011821
return callback(null, doc);
18021822
}
1803-
var headers = {'Accept': acceptHeader};
1804-
for(var k in options.headers) { headers[k] = options.headers[k]; }
1823+
18051824
request({
18061825
url: url,
18071826
headers: headers,
@@ -1892,6 +1911,8 @@ jsonld.documentLoaders.node = function(options) {
18921911
*
18931912
* @param options the options to use:
18941913
* secure: require all URLs to use HTTPS.
1914+
* headers: an array of headers which will be passed as request
1915+
* headers for the requested document. Accept is not allowed.
18951916
* usePromise: true to use a promises API, false for a
18961917
* callback-continuation-style API; defaults to true if Promise
18971918
* is globally defined, false if not.
@@ -1903,6 +1924,7 @@ jsonld.documentLoaders.xhr = function(options) {
19031924
options = options || {};
19041925
var rlink = /(^|(\r\n))link:/i;
19051926
var queue = new jsonld.RequestQueue();
1927+
var headers = jsonld.buildHeaders(options.headers);
19061928

19071929
// use option or, by default, use Promise when its defined
19081930
var usePromise = ('usePromise' in options ?
@@ -1975,7 +1997,11 @@ jsonld.documentLoaders.xhr = function(options) {
19751997
{contextUrl: null, documentUrl: url, document: null});
19761998
};
19771999
req.open('GET', url, true);
1978-
req.setRequestHeader('Accept', 'application/ld+json, application/json');
2000+
2001+
for(var k in headers) {
2002+
req.setRequestHeader(k, headers[k]);
2003+
}
2004+
19792005
req.send();
19802006
}
19812007
};

0 commit comments

Comments
 (0)