@@ -1645,6 +1645,30 @@ jsonld.cache = {
1645
1645
activeCtx : new jsonld . ActiveContextCache ( )
1646
1646
} ;
1647
1647
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
+
1648
1672
/**
1649
1673
* Document loaders.
1650
1674
*/
@@ -1656,6 +1680,8 @@ jsonld.documentLoaders = {};
1656
1680
* @param $ the jquery instance to use.
1657
1681
* @param options the options to use:
1658
1682
* 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.
1659
1685
* usePromise: true to use a promises API, false for a
1660
1686
* callback-continuation-style API; defaults to true if Promise
1661
1687
* is globally defined, false if not.
@@ -1665,6 +1691,7 @@ jsonld.documentLoaders = {};
1665
1691
jsonld . documentLoaders . jquery = function ( $ , options ) {
1666
1692
options = options || { } ;
1667
1693
var queue = new jsonld . RequestQueue ( ) ;
1694
+ var headers = jsonld . buildHeaders ( options . headers ) ;
1668
1695
1669
1696
// use option or, by default, use Promise when its defined
1670
1697
var usePromise = ( 'usePromise' in options ?
@@ -1694,12 +1721,10 @@ jsonld.documentLoaders.jquery = function($, options) {
1694
1721
$ . ajax ( {
1695
1722
url : url ,
1696
1723
accepts : {
1697
- json : 'application/ld+json, application/json'
1724
+ json : jsonld . acceptHeader
1698
1725
} ,
1699
1726
// ensure Accept header is very specific for JSON-LD/JSON
1700
- headers : {
1701
- 'Accept' : 'application/ld+json, application/json'
1702
- } ,
1727
+ headers : headers ,
1703
1728
dataType : 'json' ,
1704
1729
crossDomain : true ,
1705
1730
success : function ( data , textStatus , jqXHR ) {
@@ -1756,10 +1781,10 @@ jsonld.documentLoaders.jquery = function($, options) {
1756
1781
*/
1757
1782
jsonld . documentLoaders . node = function ( options ) {
1758
1783
options = options || { } ;
1784
+ var headers = jsonld . buildHeaders ( options . headers ) ;
1759
1785
var strictSSL = ( 'strictSSL' in options ) ? options . strictSSL : true ;
1760
1786
var maxRedirects = ( 'maxRedirects' in options ) ? options . maxRedirects : - 1 ;
1761
1787
var request = ( 'request' in options ) ? options . request : require ( 'request' ) ;
1762
- var acceptHeader = 'application/ld+json, application/json' ;
1763
1788
var http = require ( 'http' ) ;
1764
1789
// TODO: disable cache until HTTP caching implemented
1765
1790
//var cache = new jsonld.DocumentCache();
@@ -1770,12 +1795,7 @@ jsonld.documentLoaders.node = function(options) {
1770
1795
return jsonld . promisify ( loadDocument , url , [ ] ) ;
1771
1796
} ) ;
1772
1797
}
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
+
1779
1799
return queue . wrapLoader ( function ( url , callback ) {
1780
1800
loadDocument ( url , [ ] , callback ) ;
1781
1801
} ) ;
@@ -1800,8 +1820,7 @@ jsonld.documentLoaders.node = function(options) {
1800
1820
if ( doc !== null ) {
1801
1821
return callback ( null , doc ) ;
1802
1822
}
1803
- var headers = { 'Accept' : acceptHeader } ;
1804
- for ( var k in options . headers ) { headers [ k ] = options . headers [ k ] ; }
1823
+
1805
1824
request ( {
1806
1825
url : url ,
1807
1826
headers : headers ,
@@ -1892,6 +1911,8 @@ jsonld.documentLoaders.node = function(options) {
1892
1911
*
1893
1912
* @param options the options to use:
1894
1913
* 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.
1895
1916
* usePromise: true to use a promises API, false for a
1896
1917
* callback-continuation-style API; defaults to true if Promise
1897
1918
* is globally defined, false if not.
@@ -1903,6 +1924,7 @@ jsonld.documentLoaders.xhr = function(options) {
1903
1924
options = options || { } ;
1904
1925
var rlink = / ( ^ | ( \r \n ) ) l i n k : / i;
1905
1926
var queue = new jsonld . RequestQueue ( ) ;
1927
+ var headers = jsonld . buildHeaders ( options . headers ) ;
1906
1928
1907
1929
// use option or, by default, use Promise when its defined
1908
1930
var usePromise = ( 'usePromise' in options ?
@@ -1975,7 +1997,11 @@ jsonld.documentLoaders.xhr = function(options) {
1975
1997
{ contextUrl : null , documentUrl : url , document : null } ) ;
1976
1998
} ;
1977
1999
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
+
1979
2005
req . send ( ) ;
1980
2006
}
1981
2007
} ;
0 commit comments