Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): refactored, documented and added factories
- Loading branch information
1 parent
4bb8329
commit 0fb578f
Showing
12 changed files
with
719 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {urlEncode} from './utils' | ||
|
||
export default { | ||
method: 'get', | ||
baseUrl: '', | ||
headers: { | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
'Accept': 'application/json' | ||
}, | ||
onRequest (xhr, options) { | ||
if (options.headers['Content-Type'] === 'application/x-www-form-urlencoded') { | ||
options.body = urlEncode(options.body) | ||
} else if (options.headers['Content-Type'].indexOf('application/json') >= 0) { | ||
options.body = JSON.stringify(options.body) | ||
} | ||
|
||
xhr.withCredentials = Boolean(options.withCredentials) | ||
|
||
Object.keys(options.headers).forEach((key) => { | ||
xhr.setRequestHeader(key, options.headers[key]) | ||
}) | ||
|
||
xhr.send(options.body) | ||
}, | ||
onResponse (xhr, resolve, reject) { | ||
let result = xhr.responseText | ||
|
||
if (result && xhr.getResponseHeader('Content-Type').indexOf('application/json') >= 0) { | ||
result = JSON.parse(xhr.responseText) | ||
} | ||
|
||
if (xhr.status >= 200 && xhr.status < 300) { | ||
resolve({ | ||
status: xhr.status, | ||
result: result | ||
}) | ||
} else { | ||
reject({ | ||
status: xhr.status, | ||
result: result | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.