You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.5 KiB
76 lines
1.5 KiB
/**
|
|
* Creates a XHR request
|
|
*
|
|
* @param {Object} options
|
|
*/
|
|
export const createRequest = (options) => {
|
|
const xhr = new XMLHttpRequest()
|
|
xhr.responseType = 'json'
|
|
xhr.open(options.method || 'GET', options.url)
|
|
if (options.headers) {
|
|
Object.keys(options.headers).forEach(key => {
|
|
xhr.setRequestHeader(key, options.headers[key])
|
|
})
|
|
}
|
|
|
|
return xhr
|
|
}
|
|
|
|
/**
|
|
* Sends a XHR request with certain body
|
|
*
|
|
* @param {XMLHttpRequest} xhr
|
|
* @param {Object} body
|
|
*/
|
|
export const sendRequest = (xhr, body) => {
|
|
return new Promise((resolve, reject) => {
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
resolve(xhr.response)
|
|
} else {
|
|
reject(xhr.response)
|
|
}
|
|
}
|
|
xhr.onerror = () => reject(xhr.response)
|
|
xhr.send(JSON.stringify(body))
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Sends a XHR request with certain form data
|
|
*
|
|
* @param {XMLHttpRequest} xhr
|
|
* @param {Object} data
|
|
*/
|
|
export const sendFormRequest = (xhr, data) => {
|
|
const body = new FormData()
|
|
for (var name in data) {
|
|
body.append(name, data[name])
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
resolve(xhr.response)
|
|
} else {
|
|
reject(xhr.response)
|
|
}
|
|
}
|
|
xhr.onerror = () => reject(xhr.response)
|
|
xhr.send(body)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Creates and sends XHR request
|
|
*
|
|
* @param {Object} options
|
|
*
|
|
* @returns Promise
|
|
*/
|
|
export default function (options) {
|
|
const xhr = createRequest(options)
|
|
|
|
return sendRequest(xhr, options.body)
|
|
}
|