sbx-client-request
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,2 +0,2 @@ | ||
import * as Cookie from 'sbx-client-cookie'; | ||
import Cookie from 'sbx-client-cookie'; | ||
@@ -7,24 +7,26 @@ import qs from './query-string'; | ||
constructor(props) { | ||
this.config = { | ||
url: props.url || '', | ||
needAuthorization: props.hasOwnProperty('needAuthorization') | ||
? props.needAuthorization | ||
: true, | ||
needAuthorization: props.hasOwnProperty('needAuthorization') ? | ||
props.needAuthorization : | ||
true, | ||
authorization: this.getAuthorization(), | ||
commonData: props.hasOwnProperty('commonData') ? props.commonData : {}, | ||
headers: props.hasOwnProperty('headers') | ||
? props.headers | ||
: { 'Content-Type': 'application/json' } | ||
headers: props.hasOwnProperty('headers') ? | ||
props.headers : | ||
{ | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
} | ||
request(method, request) { | ||
let { | ||
commonData, | ||
headers, | ||
authorization, | ||
needAuthorization, | ||
url | ||
} = this.config, | ||
commonData, | ||
headers, | ||
authorization, | ||
needAuthorization, | ||
url | ||
} = this.config, | ||
self = this; | ||
@@ -39,4 +41,7 @@ return new Promise((resolve, reject) => { | ||
let responseHeaders; | ||
requestData = { ...commonData, ...requestData }; | ||
requestData = { | ||
...commonData, | ||
...requestData | ||
}; | ||
if (method === 'GET') { | ||
@@ -48,6 +53,6 @@ requestUrl += qs(requestData); | ||
if ( | ||
request.hasOwnProperty('needAuthorization') | ||
? request.needAuthorization | ||
: needAuthorization | ||
){ | ||
request.hasOwnProperty('needAuthorization') ? | ||
request.needAuthorization : | ||
needAuthorization | ||
) { | ||
headers['Authorization'] = self.getAuthorization(); | ||
@@ -59,3 +64,3 @@ } | ||
xhr.send(JSON.stringify(requestData)); | ||
xhr.onreadystatechange = function() { | ||
xhr.onreadystatechange = function () { | ||
if (this.readyState == this.HEADERS_RECEIVED) { | ||
@@ -67,3 +72,3 @@ responseHeaders = xhr | ||
let headersMap = {}; | ||
responseHeaders.forEach(function(line) { | ||
responseHeaders.forEach(function (line) { | ||
let parts = line.split(': '), | ||
@@ -74,5 +79,5 @@ header = parts.shift(), | ||
if ( | ||
(request.hasOwnProperty('needAuthorization') | ||
? request.needAuthorization | ||
: needAuthorization) && | ||
(request.hasOwnProperty('needAuthorization') ? | ||
request.needAuthorization : | ||
needAuthorization) && | ||
header.toLowerCase() === 'authorization' | ||
@@ -93,3 +98,3 @@ ) | ||
if (xhr.status < 200 || xhr.status > 300) reject(response); | ||
else resolve(response); | ||
else resolve(response); | ||
}; | ||
@@ -124,20 +129,35 @@ }); | ||
post(data) { | ||
return this.request('POST', data); | ||
post(url, data, cfg = {}) { | ||
return this.request('POST', Object.assign({ | ||
url, | ||
data | ||
}, cfg)); | ||
} | ||
get(data) { | ||
return this.request('GET', data); | ||
get(url, data, cfg) { | ||
return this.request('GET', Object.assign({ | ||
url, | ||
data | ||
}, cfg)); | ||
} | ||
put(data) { | ||
return this.request('PUT', data); | ||
put(url, data, cfg) { | ||
return this.request('PUT', Object.assign({ | ||
url, | ||
data | ||
}, cfg)); | ||
} | ||
delete(data) { | ||
return this.request('DELETE', data); | ||
delete(url, data, cfg) { | ||
return this.request('DELETE', Object.assign({ | ||
url, | ||
data | ||
}, cfg)); | ||
} | ||
patch(data) { | ||
return this.request('PATCH', data); | ||
patch(url, data, cfg) { | ||
return this.request('PATCH', Object.assign({ | ||
url, | ||
data | ||
}, cfg)); | ||
} | ||
@@ -144,0 +164,0 @@ } |
{ | ||
"name": "sbx-client-request", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "sbx-client-request", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
6292
166