Skip to content

Commit

Permalink
feat: response.status
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed May 27, 2018
1 parent 7ec8a03 commit 5bf104c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/request/request.js
Expand Up @@ -43,37 +43,39 @@ function request (requestOptions) {
}

let headers = {}
let status

return fetch(requestOptions.url, pick(requestOptions, 'method', 'body', 'headers', 'timeout', 'agent'))

.then(response => {
status = response.status
for (const keyAndValue of response.headers.entries()) {
headers[keyAndValue[0]] = keyAndValue[1]
}

if (response.status === 204 || response.status === 205) {
if (status === 204 || status === 205) {
return
}

// GitHub API returns 200 for HEAD requsets
if (requestOptions.method === 'HEAD') {
if (response.status < 400) {
if (status < 400) {
return
}

throw new HttpError(response.statusText, response.status, headers)
throw new HttpError(response.statusText, status, headers)
}

if (response.status === 304) {
if (status === 304) {
requestOptions.url = response.headers.location
throw new HttpError('Not modified', response.status, headers)
throw new HttpError('Not modified', status, headers)
}

if (response.status >= 400) {
if (status >= 400) {
return response.text()

.then(message => {
throw new HttpError(message, response.status, headers)
throw new HttpError(message, status, headers)
})
}

Expand All @@ -92,6 +94,7 @@ function request (requestOptions) {
.then(data => {
return {
data,
status,
headers,
get meta () {
deprecate('response.meta – use response.headers instead (#896)')
Expand Down

0 comments on commit 5bf104c

Please sign in to comment.