Skip to content

Commit 7833bc2

Browse files
committed
add headers option support to other loaders
1 parent dc07d60 commit 7833bc2

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

js/jsonld.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,39 @@ 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} headers 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+
jsonld.buildHeaders = function(headers) {
1665+
headers = headers || {};
1666+
var hasAccept = Object.keys(headers).map(function(h) {
1667+
return h.toLowerCase();
1668+
}).indexOf('accept') !== -1;
1669+
1670+
if(hasAccept) {
1671+
throw new RangeError(
1672+
'Accept header may not be specified as an option; only "' +
1673+
_defaults.headers.accept + '" is supported.');
1674+
}
1675+
1676+
headers['Accept'] = _defaults.headers.accept;
1677+
1678+
return headers;
1679+
};
1680+
16481681
/**
16491682
* Document loaders.
16501683
*/
@@ -1656,6 +1689,8 @@ jsonld.documentLoaders = {};
16561689
* @param $ the jquery instance to use.
16571690
* @param options the options to use:
16581691
* secure: require all URLs to use HTTPS.
1692+
* headers: an object (map) of headers which will be passed as request
1693+
* headers for the requested document. Accept is not allowed.
16591694
* usePromise: true to use a promises API, false for a
16601695
* callback-continuation-style API; defaults to true if Promise
16611696
* is globally defined, false if not.
@@ -1665,6 +1700,7 @@ jsonld.documentLoaders = {};
16651700
jsonld.documentLoaders.jquery = function($, options) {
16661701
options = options || {};
16671702
var queue = new jsonld.RequestQueue();
1703+
var headers = jsonld.buildHeaders(options.headers);
16681704

16691705
// use option or, by default, use Promise when its defined
16701706
var usePromise = ('usePromise' in options ?
@@ -1694,12 +1730,9 @@ jsonld.documentLoaders.jquery = function($, options) {
16941730
$.ajax({
16951731
url: url,
16961732
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'
1733+
json: _defaults.headers.accept
17021734
},
1735+
headers: headers,
17031736
dataType: 'json',
17041737
crossDomain: true,
17051738
success: function(data, textStatus, jqXHR) {
@@ -1747,7 +1780,7 @@ jsonld.documentLoaders.jquery = function($, options) {
17471780
* default.
17481781
* request: the object which will make the request, default is
17491782
* provided by `https://www.npmjs.com/package/request`.
1750-
* headers: an array of headers which will be passed as request
1783+
* headers: an object (map) of headers which will be passed as request
17511784
* headers for the requested document. Accept is not allowed.
17521785
* usePromise: true to use a promises API, false for a
17531786
* callback-continuation-style API; false by default.
@@ -1756,10 +1789,10 @@ jsonld.documentLoaders.jquery = function($, options) {
17561789
*/
17571790
jsonld.documentLoaders.node = function(options) {
17581791
options = options || {};
1792+
var headers = jsonld.buildHeaders(options.headers);
17591793
var strictSSL = ('strictSSL' in options) ? options.strictSSL : true;
17601794
var maxRedirects = ('maxRedirects' in options) ? options.maxRedirects : -1;
17611795
var request = ('request' in options) ? options.request : require('request');
1762-
var acceptHeader = 'application/ld+json, application/json';
17631796
var http = require('http');
17641797
// TODO: disable cache until HTTP caching implemented
17651798
//var cache = new jsonld.DocumentCache();
@@ -1770,12 +1803,7 @@ jsonld.documentLoaders.node = function(options) {
17701803
return jsonld.promisify(loadDocument, url, []);
17711804
});
17721805
}
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-
}
1806+
17791807
return queue.wrapLoader(function(url, callback) {
17801808
loadDocument(url, [], callback);
17811809
});
@@ -1800,8 +1828,7 @@ jsonld.documentLoaders.node = function(options) {
18001828
if(doc !== null) {
18011829
return callback(null, doc);
18021830
}
1803-
var headers = {'Accept': acceptHeader};
1804-
for(var k in options.headers) { headers[k] = options.headers[k]; }
1831+
18051832
request({
18061833
url: url,
18071834
headers: headers,
@@ -1892,6 +1919,8 @@ jsonld.documentLoaders.node = function(options) {
18921919
*
18931920
* @param options the options to use:
18941921
* secure: require all URLs to use HTTPS.
1922+
* headers: an object (map) of headers which will be passed as request
1923+
* headers for the requested document. Accept is not allowed.
18951924
* usePromise: true to use a promises API, false for a
18961925
* callback-continuation-style API; defaults to true if Promise
18971926
* is globally defined, false if not.
@@ -1903,6 +1932,7 @@ jsonld.documentLoaders.xhr = function(options) {
19031932
options = options || {};
19041933
var rlink = /(^|(\r\n))link:/i;
19051934
var queue = new jsonld.RequestQueue();
1935+
var headers = jsonld.buildHeaders(options.headers);
19061936

19071937
// use option or, by default, use Promise when its defined
19081938
var usePromise = ('usePromise' in options ?
@@ -1975,7 +2005,11 @@ jsonld.documentLoaders.xhr = function(options) {
19752005
{contextUrl: null, documentUrl: url, document: null});
19762006
};
19772007
req.open('GET', url, true);
1978-
req.setRequestHeader('Accept', 'application/ld+json, application/json');
2008+
2009+
for(var k in headers) {
2010+
req.setRequestHeader(k, headers[k]);
2011+
}
2012+
19792013
req.send();
19802014
}
19812015
};

0 commit comments

Comments
 (0)