-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
99 lines (85 loc) · 2.5 KB
/
Copy pathrequest.js
File metadata and controls
99 lines (85 loc) · 2.5 KB
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
import check from 'check-arg-types'
const toType = check.prototype.toType
let storage = null
let apiUrl = 'http://10.0.2.2:8000'
export const configure = ({ storage: uStorage, apiUrl: uApiUrl }) => {
if (uStorage) storage = uStorage
if (uApiUrl) apiUrl = uApiUrl
}
const safelyParse = (json, key) => {
try {
const parsed = JSON.parse(json)
return key != null ? parsed[key] : parsed
} catch (_) {
return json
}
}
export const getAuthHeader = (headers = {}, noAuth) => new Promise((resolve, reject) => {
if (storage == null || noAuth) {
return resolve(headers)
}
const token = storage.getItem('token')
if (token && typeof token.then === 'function') {
token.then(t => {
headers.Authorization = `Token ${token}`
resolve(headers)
})
} else if (token) {
headers.Authorization = `Token ${token}`
}
resolve(headers)
})
const makeErr = (code, msg) => {
const e = new Error(msg)
e.code = code
if (code === 401 && storage != null) {
storage.removeItem('token')
}
// console.error('makeErr', { code, msg })
return e
}
export function request ({
endpoint,
url,
method = 'get',
data,
headers,
noAuth = false,
contentType = 'application/json'
}) {
if (endpoint != null && endpoint.indexOf('http') === -1) {
url = `${apiUrl}/${endpoint}`
}
if (url == null) {
url = endpoint
}
const xhr = new window.XMLHttpRequest()
const promise = new Promise((resolve, reject) => {
xhr.open(method.toUpperCase(), url)
xhr.onreadystatechange = () => {
if (xhr.readyState !== xhr.DONE) return
const badResponse = xhr.status !== 204 && xhr.response === ''
badResponse || xhr.status >= 400
? reject(makeErr(xhr.status, safelyParse(xhr.response, 'detail')))
: resolve(safelyParse(xhr.response))
}
xhr.onerror = () => reject(xhr)
contentType && xhr.setRequestHeader('Content-Type', contentType)
getAuthHeader(headers, noAuth).then(headers => {
// Our XHR may be aborted (by useRequest or user) and this reference
// may be past the opened state, in which case it's too late to set headers.
if (xhr.readyState !== xhr.OPENED) return
if (headers && toType(headers) === 'object') {
for (const k in headers) {
xhr.setRequestHeader(k, headers[k])
}
}
const dataType = toType(data)
xhr.send(dataType === 'object' || dataType === 'array'
? JSON.stringify(data)
: data)
})
})
return { xhr, promise }
}
export default request