@@ -1645,6 +1645,41 @@ jsonld.cache = {
1645
1645
activeCtx : new jsonld . ActiveContextCache ( )
1646
1646
} ;
1647
1647
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
+
1648
1683
/**
1649
1684
* Document loaders.
1650
1685
*/
@@ -1656,6 +1691,8 @@ jsonld.documentLoaders = {};
1656
1691
* @param $ the jquery instance to use.
1657
1692
* @param options the options to use:
1658
1693
* 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.
1659
1696
* usePromise: true to use a promises API, false for a
1660
1697
* callback-continuation-style API; defaults to true if Promise
1661
1698
* is globally defined, false if not.
@@ -1665,6 +1702,7 @@ jsonld.documentLoaders = {};
1665
1702
jsonld . documentLoaders . jquery = function ( $ , options ) {
1666
1703
options = options || { } ;
1667
1704
var queue = new jsonld . RequestQueue ( ) ;
1705
+ var headers = buildHeaders ( options . headers ) ;
1668
1706
1669
1707
// use option or, by default, use Promise when its defined
1670
1708
var usePromise = ( 'usePromise' in options ?
@@ -1694,12 +1732,9 @@ jsonld.documentLoaders.jquery = function($, options) {
1694
1732
$ . ajax ( {
1695
1733
url : url ,
1696
1734
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
1702
1736
} ,
1737
+ headers : headers ,
1703
1738
dataType : 'json' ,
1704
1739
crossDomain : true ,
1705
1740
success : function ( data , textStatus , jqXHR ) {
@@ -1747,7 +1782,7 @@ jsonld.documentLoaders.jquery = function($, options) {
1747
1782
* default.
1748
1783
* request: the object which will make the request, default is
1749
1784
* 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
1751
1786
* headers for the requested document. Accept is not allowed.
1752
1787
* usePromise: true to use a promises API, false for a
1753
1788
* callback-continuation-style API; false by default.
@@ -1756,10 +1791,10 @@ jsonld.documentLoaders.jquery = function($, options) {
1756
1791
*/
1757
1792
jsonld . documentLoaders . node = function ( options ) {
1758
1793
options = options || { } ;
1794
+ var headers = buildHeaders ( options . headers ) ;
1759
1795
var strictSSL = ( 'strictSSL' in options ) ? options . strictSSL : true ;
1760
1796
var maxRedirects = ( 'maxRedirects' in options ) ? options . maxRedirects : - 1 ;
1761
1797
var request = ( 'request' in options ) ? options . request : require ( 'request' ) ;
1762
- var acceptHeader = 'application/ld+json, application/json' ;
1763
1798
var http = require ( 'http' ) ;
1764
1799
// TODO: disable cache until HTTP caching implemented
1765
1800
//var cache = new jsonld.DocumentCache();
@@ -1770,12 +1805,7 @@ jsonld.documentLoaders.node = function(options) {
1770
1805
return jsonld . promisify ( loadDocument , url , [ ] ) ;
1771
1806
} ) ;
1772
1807
}
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
+
1779
1809
return queue . wrapLoader ( function ( url , callback ) {
1780
1810
loadDocument ( url , [ ] , callback ) ;
1781
1811
} ) ;
@@ -1800,8 +1830,7 @@ jsonld.documentLoaders.node = function(options) {
1800
1830
if ( doc !== null ) {
1801
1831
return callback ( null , doc ) ;
1802
1832
}
1803
- var headers = { 'Accept' : acceptHeader } ;
1804
- for ( var k in options . headers ) { headers [ k ] = options . headers [ k ] ; }
1833
+
1805
1834
request ( {
1806
1835
url : url ,
1807
1836
headers : headers ,
@@ -1892,6 +1921,8 @@ jsonld.documentLoaders.node = function(options) {
1892
1921
*
1893
1922
* @param options the options to use:
1894
1923
* 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.
1895
1926
* usePromise: true to use a promises API, false for a
1896
1927
* callback-continuation-style API; defaults to true if Promise
1897
1928
* is globally defined, false if not.
@@ -1903,6 +1934,7 @@ jsonld.documentLoaders.xhr = function(options) {
1903
1934
options = options || { } ;
1904
1935
var rlink = / ( ^ | ( \r \n ) ) l i n k : / i;
1905
1936
var queue = new jsonld . RequestQueue ( ) ;
1937
+ var headers = buildHeaders ( options . headers ) ;
1906
1938
1907
1939
// use option or, by default, use Promise when its defined
1908
1940
var usePromise = ( 'usePromise' in options ?
@@ -1975,7 +2007,11 @@ jsonld.documentLoaders.xhr = function(options) {
1975
2007
{ contextUrl : null , documentUrl : url , document : null } ) ;
1976
2008
} ;
1977
2009
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
+
1979
2015
req . send ( ) ;
1980
2016
}
1981
2017
} ;
0 commit comments