-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
request.js
165 lines (152 loc) · 5.35 KB
/
request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { hasXMLHttpRequest } from './utils.js'
import * as fetchNode from './getFetch.cjs'
let fetchApi
if (typeof fetch === 'function') {
if (typeof global !== 'undefined' && global.fetch) {
fetchApi = global.fetch
} else if (typeof window !== 'undefined' && window.fetch) {
fetchApi = window.fetch
} else {
fetchApi = fetch
}
}
let XmlHttpRequestApi
if (hasXMLHttpRequest()) {
if (typeof global !== 'undefined' && global.XMLHttpRequest) {
XmlHttpRequestApi = global.XMLHttpRequest
} else if (typeof window !== 'undefined' && window.XMLHttpRequest) {
XmlHttpRequestApi = window.XMLHttpRequest
}
}
let ActiveXObjectApi
if (typeof ActiveXObject === 'function') {
if (typeof global !== 'undefined' && global.ActiveXObject) {
ActiveXObjectApi = global.ActiveXObject
} else if (typeof window !== 'undefined' && window.ActiveXObject) {
ActiveXObjectApi = window.ActiveXObject
}
}
if (!fetchApi && fetchNode && !XmlHttpRequestApi && !ActiveXObjectApi) fetchApi = fetchNode.default || fetchNode // because of strange export
if (typeof fetchApi !== 'function') fetchApi = undefined
const addQueryString = (url, params) => {
if (params && typeof params === 'object') {
let queryString = ''
// Must encode data
for (const paramName in params) {
queryString += '&' + encodeURIComponent(paramName) + '=' + encodeURIComponent(params[paramName])
}
if (!queryString) return url
url = url + (url.indexOf('?') !== -1 ? '&' : '?') + queryString.slice(1)
}
return url
}
const fetchIt = (url, fetchOptions, callback) => {
const resolver = (response) => {
if (!response.ok) return callback(response.statusText || 'Error', { status: response.status })
response.text().then((data) => {
callback(null, { status: response.status, data })
}).catch(callback)
}
if (typeof fetch === 'function') { // react-native debug mode needs the fetch function to be called directly (no alias)
fetch(url, fetchOptions).then(resolver).catch(callback)
} else {
fetchApi(url, fetchOptions).then(resolver).catch(callback)
}
}
let omitFetchOptions = false
// fetch api stuff
const requestWithFetch = (options, url, payload, callback) => {
if (options.queryStringParams) {
url = addQueryString(url, options.queryStringParams)
}
const headers = {
...(typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders)
}
if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) {
headers['User-Agent'] = `i18next-http-backend (node/${global.process.version}; ${global.process.platform} ${global.process.arch})`
}
if (payload) headers['Content-Type'] = 'application/json'
const reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions
const fetchOptions = {
method: payload ? 'POST' : 'GET',
body: payload ? options.stringify(payload) : undefined,
headers,
...(omitFetchOptions ? {} : reqOptions)
}
try {
fetchIt(url, fetchOptions, callback)
} catch (e) {
if (!reqOptions || Object.keys(reqOptions).length === 0 || !e.message || e.message.indexOf('not implemented') < 0) {
return callback(e)
}
try {
Object.keys(reqOptions).forEach((opt) => {
delete fetchOptions[opt]
})
fetchIt(url, fetchOptions, callback)
omitFetchOptions = true
} catch (err) {
callback(err)
}
}
}
// xml http request stuff
const requestWithXmlHttpRequest = (options, url, payload, callback) => {
if (payload && typeof payload === 'object') {
// if (!cache) payload._t = Date.now()
// URL encoded form payload must be in querystring format
payload = addQueryString('', payload).slice(1)
}
if (options.queryStringParams) {
url = addQueryString(url, options.queryStringParams)
}
try {
let x
if (XmlHttpRequestApi) {
x = new XmlHttpRequestApi()
} else {
x = new ActiveXObjectApi('MSXML2.XMLHTTP.3.0')
}
x.open(payload ? 'POST' : 'GET', url, 1)
if (!options.crossDomain) {
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
}
x.withCredentials = !!options.withCredentials
if (payload) {
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
}
if (x.overrideMimeType) {
x.overrideMimeType('application/json')
}
let h = options.customHeaders
h = typeof h === 'function' ? h() : h
if (h) {
for (const i in h) {
x.setRequestHeader(i, h[i])
}
}
x.onreadystatechange = () => {
x.readyState > 3 && callback(x.status >= 400 ? x.statusText : null, { status: x.status, data: x.responseText })
}
x.send(payload)
} catch (e) {
console && console.log(e)
}
}
const request = (options, url, payload, callback) => {
if (typeof payload === 'function') {
callback = payload
payload = undefined
}
callback = callback || (() => {})
if (fetchApi && url.indexOf('file:') !== 0) {
// use fetch api
return requestWithFetch(options, url, payload, callback)
}
if (hasXMLHttpRequest() || typeof ActiveXObject === 'function') {
// use xml http request
return requestWithXmlHttpRequest(options, url, payload, callback)
}
callback(new Error('No fetch and no xhr implementation found!'))
}
export default request