|
31 | 31 | })
|
32 | 32 | }
|
33 | 33 |
|
34 |
| - function __request(debug, method, url, headers, data, asJson, onRequestError, callback) { |
35 |
| - if (!url) { |
36 |
| - return; |
37 |
| - } |
38 |
| - if (asJson) { |
39 |
| - var body = JSON.stringify({query: data.query, variables: data.variables}); |
40 |
| - } else { |
41 |
| - var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables)) |
42 |
| - } |
43 |
| - if (debug) { |
44 |
| - console.groupCollapsed('[graphql]: ' |
45 |
| - + method.toUpperCase() + ' ' + url + ': ' |
46 |
| - + data.query.split(/\n/)[0].substr(0, 50) + '... with ' |
47 |
| - + JSON.stringify(data.variables).substr(0, 50) + '...') |
48 |
| - console.log('QUERY: %c%s', 'font-weight: bold', data.query) |
49 |
| - console.log('VARIABLES: %c%s\n\nsending as ' + (asJson ? 'json' : 'form url-data'), 'font-weight: bold', JSON.stringify(data.variables, null, 2), data.variables) |
50 |
| - console.groupEnd() |
51 |
| - } |
52 |
| - if (typeof XMLHttpRequest != 'undefined') { |
| 34 | + if (typeof XMLHttpRequest !== 'undefined') { |
| 35 | + function __doRequest( |
| 36 | + method, url, contentType, accept, headers, body, _onRequestError, callback |
| 37 | + ) { |
53 | 38 | var xhr = new XMLHttpRequest
|
54 | 39 | xhr.open(method, url, true)
|
55 |
| - xhr.setRequestHeader('Content-Type', (asJson ? 'application/json' : 'application/x-www-form-urlencoded')) |
56 |
| - xhr.setRequestHeader('Accept', 'application/json') |
| 40 | + xhr.setRequestHeader('Content-Type', contentType) |
| 41 | + xhr.setRequestHeader('Accept', accept) |
57 | 42 | for (var key in headers) { xhr.setRequestHeader(key, headers[key]) }
|
58 | 43 | xhr.onerror = function () { callback(xhr, xhr.status) }
|
59 | 44 | xhr.onload = function () {
|
|
65 | 50 | }
|
66 | 51 | }
|
67 | 52 | xhr.send(body)
|
68 |
| - } else if (typeof require == 'function') { |
69 |
| - var http = require('http'), https = require('https'), URL = require('url'), uri = URL.parse(url); |
| 53 | + } |
| 54 | + } else if (typeof require !== 'function') { |
| 55 | + function __doRequest( |
| 56 | + method, url, contentType, accept, headers, body, onRequestError, callback |
| 57 | + ) { |
| 58 | + var http = require('http'), https = require('https'), URL = require('url'), uri = URL.parse(url) |
70 | 59 | var req = (uri.protocol === 'https:' ? https : http).request({
|
71 | 60 | protocol: uri.protocol,
|
72 | 61 | hostname: uri.hostname,
|
73 | 62 | port: uri.port,
|
74 | 63 | path: uri.path,
|
75 |
| - method: "POST", |
76 |
| - headers: __extend({ |
77 |
| - 'Content-type': (asJson ? 'application/json' : 'application/x-www-form-urlencoded'), |
78 |
| - 'Accept': 'application/json' |
79 |
| - }, headers) |
| 64 | + method: method.toUpperCase(), |
| 65 | + headers: __extend({ 'Content-type': contentType, 'Accept': accept }, headers) |
80 | 66 | }, function (response) {
|
81 | 67 | var str = ''
|
82 | 68 | response.setEncoding('utf8')
|
|
95 | 81 | }
|
96 | 82 | }
|
97 | 83 |
|
| 84 | + function __request(debug, method, url, headers, data, asJson, onRequestError, callback) { |
| 85 | + if (!url) { |
| 86 | + return; |
| 87 | + } |
| 88 | + if (asJson) { |
| 89 | + var body = JSON.stringify({query: data.query, variables: data.variables}); |
| 90 | + } else { |
| 91 | + var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables)) |
| 92 | + } |
| 93 | + if (debug) { |
| 94 | + console.groupCollapsed('[graphql]: ' |
| 95 | + + method.toUpperCase() + ' ' + url + ': ' |
| 96 | + + data.query.split(/\n/)[0].substr(0, 50) + '... with ' |
| 97 | + + JSON.stringify(data.variables).substr(0, 50) + '...') |
| 98 | + console.log('QUERY: %c%s', 'font-weight: bold', data.query) |
| 99 | + console.log('VARIABLES: %c%s\n\nsending as ' + (asJson ? 'json' : 'form url-data'), 'font-weight: bold', JSON.stringify(data.variables, null, 2), data.variables) |
| 100 | + console.groupEnd() |
| 101 | + } |
| 102 | + |
| 103 | + for (var key in headers) { |
| 104 | + if (typeof headers[key] === 'function') { |
| 105 | + headers[key] = headers[key]() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + __doRequest( |
| 110 | + method, |
| 111 | + url, |
| 112 | + asJson ? 'application/json' : 'application/x-www-form-urlencoded', |
| 113 | + 'application/json', |
| 114 | + headers, |
| 115 | + body, |
| 116 | + onRequestError, |
| 117 | + callback |
| 118 | + ) |
| 119 | + } |
| 120 | + |
98 | 121 | function __isTagCall(strings) {
|
99 | 122 | return Object.prototype.toString.call(strings) == '[object Array]' && strings.raw
|
100 | 123 | }
|
|
0 commit comments