Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ const result = await octokitRequest({
Set request body directly instead of setting it to JSON based on additional parameters. See <a href="#data-parameter">"The `data` parameter"</a> below.
</td>
</tr>
<tr>
<th align=left>
<code>request</code>
</th>
<td>
Object
</td>
<td>
Pass [node-fetch extensions options](https://github.com/bitinn/node-fetch#options), such as `agent` or `timeout`
</td>
</tr>
</table>

All other options will passed depending on the `method` and `url` options.
Expand Down
22 changes: 3 additions & 19 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,19 @@ const getBuffer = require('./get-buffer-response')
const HttpError = require('./http-error')

function request (fetch, requestOptions) {
// https://fetch.spec.whatwg.org/#methods
requestOptions.method = requestOptions.method.toUpperCase()

// default content-type for JSON
if (requestOptions.body && !requestOptions.headers['content-type']) {
requestOptions.headers['content-type'] = 'application/json; charset=utf-8'
}

// GitHub expects "content-length: 0" header for PUT/PATCH requests without body
// fetch does not allow to set `content-length` header, but we can set body to an empty string
if (['PATCH', 'PUT'].indexOf(requestOptions.method) >= 0 && !requestOptions.body) {
requestOptions.body = ''
}

if (isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body)
}

let headers = {}
let status

return fetch(requestOptions.url, {
return fetch(requestOptions.url, Object.assign({
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers,
redirect: requestOptions.redirect,
timeout: requestOptions.timeout,
agent: requestOptions.agent
})
redirect: requestOptions.redirect
}, requestOptions.request))

.then(response => {
status = response.status
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"url": "https://github.com/octokit/request.js/issues"
},
"homepage": "https://github.com/octokit/request.js#readme",
"dependencies": {
"@octokit/endpoint": "^2.0.1",
"is-plain-object": "^2.0.4",
"universal-user-agent": "^2.0.1"
},
"devDependencies": {
"chai": "^4.2.0",
"compression-webpack-plugin": "^2.0.0",
Expand Down Expand Up @@ -86,10 +91,5 @@
"it",
"expect"
]
},
"dependencies": {
"@octokit/endpoint": "^1.3.0",
"is-plain-object": "^2.0.4",
"universal-user-agent": "^2.0.1"
}
}
25 changes: 25 additions & 0 deletions test/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,29 @@ describe('octokitRequest()', () => {
}
})
})

it('passes node-fetch options to fetch only', () => {
octokitRequest.fetch = (url, options) => {
expect(url).to.equal('https://api.github.com/')
expect(options.timeout).to.equal(100)
return Promise.reject(new Error('ok'))
}

return octokitRequest('GET /', {
headers: {
'user-agent': 'funky boom boom pow'
},
'request': {
timeout: 100
}
})

.catch(error => {
if (error.message === 'ok') {
return
}

throw error
})
})
})