Skip to content

Commit

Permalink
feat: resolve with url property (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jan 20, 2019
1 parent cac3656 commit 9a08b13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ All other options will passed depending on the `method` and `url` options.

**Result**

`octokitRequest` returns a promise and resolves with 3 keys
`octokitRequest` returns a promise and resolves with 4 keys

<table>
<thead>
Expand All @@ -234,16 +234,21 @@ All other options will passed depending on the `method` and `url` options.
</th>
</tr>
</thead>
<tr>
<th align=left><code>status</code></th>
<td>Integer</td>
<td>Response status status</td>
</tr>
<tr>
<th align=left><code>url</code></th>
<td>String</td>
<td>URL of response. If a request results in redirects, this is the final URL. You can send a <code>HEAD</code> request to retrieve it without loading the full response body.</td>
</tr>
<tr>
<th align=left><code>headers</code></th>
<td>Object</td>
<td>All response headers</td>
</tr>
<tr>
<th align=left><code>code</code></th>
<td>Integer</td>
<td>Response status code</td>
</tr>
<tr>
<th align=left><code>data</code></th>
<td>Any</td>
Expand Down
9 changes: 5 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

module.exports = request

const isPlainObject = require('is-plain-object')
Expand All @@ -15,6 +13,7 @@ function request (requestOptions) {

let headers = {}
let status
let url

return mockable.fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
Expand All @@ -24,6 +23,7 @@ function request (requestOptions) {
}, requestOptions.request))

.then(response => {
url = response.url
status = response.status

for (const keyAndValue of response.headers.entries()) {
Expand Down Expand Up @@ -78,9 +78,10 @@ function request (requestOptions) {

.then(data => {
return {
data,
status,
headers
url,
headers,
data
}
})

Expand Down
12 changes: 12 additions & 0 deletions test/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,16 @@ describe('octokitRequest()', () => {
expect(status).to.equal(200)
})
})

it('Resolves with url', () => {
// this test cannot be mocked with `fetch-mock`. I don’t like to rely on
// external websites to run tests, but in this case I’ll make an exception.
// The alternative would be to start a local server we then send a request to,
// this would only work in Node, so we would need to adapt the test setup, too.
return octokitRequest('/')

.then(({ url }) => {
expect(url).to.equal('https://api.github.com/')
})
})
})

0 comments on commit 9a08b13

Please sign in to comment.