1
1
// (C) Treasure Data 2020
2
2
/* global XMLHttpRequest fetch */
3
3
var win = require ( 'global/window' )
4
- var assign = require ( './lodash' ) . assign
5
4
6
5
var OK_STATUS = 200
7
6
var NOT_MODIFIED = 304
8
7
9
- var defaultHeaders = {
10
- 'Content-Type' : 'application/json' ,
11
- 'X-TD-Fetch-Api' : 'true'
12
- }
13
-
14
8
var FETCH_CREDENTIALS = {
15
9
'same-origin' : 'same-origin' ,
16
10
include : 'include' ,
@@ -33,26 +27,10 @@ function toJSON (text) {
33
27
return result
34
28
}
35
29
36
- function getHeaders ( headers , ignoreDefaultHeaders ) {
37
- headers = headers || { }
38
-
39
- if ( ignoreDefaultHeaders ) {
40
- return assign ( { } , headers )
41
- }
42
-
43
- return assign ( { } , defaultHeaders , headers )
44
- }
45
-
46
30
function isFetchSupported ( ) {
47
31
return 'fetch' in win
48
32
}
49
33
50
- function isDefaultHeadersIgnored ( options ) {
51
- options = options || { }
52
-
53
- return options . ignoreDefaultHeaders || false
54
- }
55
-
56
34
function getCredentials ( options ) {
57
35
options = options || { }
58
36
@@ -66,7 +44,7 @@ function postWithFetch (url, body, options) {
66
44
67
45
return fetch ( url , {
68
46
method : 'POST' ,
69
- headers : getHeaders ( headers , isDefaultHeadersIgnored ( options ) ) ,
47
+ headers : headers ,
70
48
credentials : getCredentials ( options ) ,
71
49
body : JSON . stringify ( body )
72
50
} ) . then ( function ( response ) {
@@ -76,9 +54,7 @@ function postWithFetch (url, body, options) {
76
54
return response . text ( )
77
55
} )
78
56
. then ( function ( text ) {
79
- if ( ! text ) return { }
80
-
81
- return JSON . parse ( text )
57
+ return toJSON ( text )
82
58
} )
83
59
}
84
60
@@ -89,7 +65,7 @@ function getWithFetch (url, options) {
89
65
90
66
return fetch ( url , {
91
67
method : method ,
92
- headers : getHeaders ( headers , isDefaultHeadersIgnored ( options ) ) ,
68
+ headers : headers ,
93
69
credentials : getCredentials ( options )
94
70
} )
95
71
. then ( function ( response ) {
@@ -100,9 +76,7 @@ function getWithFetch (url, options) {
100
76
return response . text ( )
101
77
} )
102
78
. then ( function ( text ) {
103
- if ( ! text ) return { }
104
-
105
- return JSON . parse ( text )
79
+ return toJSON ( text )
106
80
} )
107
81
}
108
82
@@ -127,7 +101,6 @@ function createXHR (method, url, options) {
127
101
128
102
xhr . withCredentials = Boolean ( getCredentials ( options ) )
129
103
130
- headers = getHeaders ( options . headers , isDefaultHeadersIgnored ( options ) )
131
104
var headerKey
132
105
for ( headerKey in headers ) {
133
106
if ( headers . hasOwnProperty ( headerKey ) ) {
@@ -150,10 +123,6 @@ function postWithTimeout (url, body, milliseconds, options) {
150
123
if ( window . AbortController ) {
151
124
var controller = new window . AbortController ( )
152
125
153
- var headers = getHeaders ( options . headers , isDefaultHeadersIgnored ( options ) )
154
-
155
- options . headers = headers
156
-
157
126
var promise = window . fetch ( url , Object . assign ( { } , options , { signal : controller . signal } ) )
158
127
var timeoutId = setTimeout ( function ( ) {
159
128
controller . abort ( )
0 commit comments