Skip to content

Commit 2200706

Browse files
authored
fix: remove private .fetch API (#8)
1 parent 428ee8b commit 2200706

File tree

6 files changed

+38
-36
lines changed

6 files changed

+38
-36
lines changed

index.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
const endpoint = require('@octokit/endpoint')
2-
const fetch = require('node-fetch').default
32
const getUserAgent = require('universal-user-agent')
43

5-
const request = require('./lib/request')
64
const version = require('./package.json').version
75
const userAgent = `octokit-request.js/${version} ${getUserAgent()}`
8-
9-
function octokitRequest (endpoint, route, options) {
10-
return request(module.exports.fetch, endpoint(route, options))
11-
}
12-
13-
function withDefaults (oldEndpoint, newDefaults) {
14-
const endpoint = oldEndpoint.defaults(newDefaults)
15-
const request = octokitRequest.bind(null, endpoint)
16-
request.endpoint = endpoint
17-
request.defaults = withDefaults.bind(null, endpoint)
18-
return request
19-
}
6+
const withDefaults = require('./lib/with-defaults')
207

218
module.exports = withDefaults(endpoint, {
229
headers: {
2310
'user-agent': userAgent
2411
}
2512
})
26-
27-
// expose internally used `fetch` method for testing/mocking only
28-
module.exports.fetch = fetch

lib/fetch.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// expose internally used `fetch` method for testing/mocking only
2+
module.exports.fetch = require('node-fetch').default

lib/request.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ module.exports = request
44

55
const isPlainObject = require('is-plain-object')
66

7+
const mockable = require('./fetch')
78
const getBuffer = require('./get-buffer-response')
89
const HttpError = require('./http-error')
910

10-
function request (fetch, requestOptions) {
11+
function request (endpoint, route, options) {
12+
const requestOptions = endpoint(route, options)
13+
1114
if (isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body)) {
1215
requestOptions.body = JSON.stringify(requestOptions.body)
1316
}
1417

1518
let headers = {}
1619
let status
1720

18-
return fetch(requestOptions.url, Object.assign({
21+
return mockable.fetch(requestOptions.url, Object.assign({
1922
method: requestOptions.method,
2023
body: requestOptions.body,
2124
headers: requestOptions.headers,

lib/with-defaults.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = withDefaults
2+
3+
const request = require('./request')
4+
5+
function withDefaults (oldEndpoint, newDefaults) {
6+
const endpoint = oldEndpoint.defaults(newDefaults)
7+
const newApi = request.bind(null, endpoint)
8+
newApi.endpoint = endpoint
9+
newApi.defaults = withDefaults.bind(null, endpoint)
10+
return newApi
11+
}

test/defaults-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fetchMock = require('fetch-mock/es5/server')
33
const sinonChai = require('sinon-chai')
44

55
const octokitRequest = require('..')
6+
const mockable = require('../lib/fetch')
67

78
const expect = chai.expect
89
chai.use(sinonChai)
@@ -13,7 +14,7 @@ describe('endpoint.defaults()', () => {
1314
})
1415

1516
it('README example', () => {
16-
octokitRequest.fetch = fetchMock.sandbox()
17+
mockable.fetch = fetchMock.sandbox()
1718
.mock('https://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos?per_page=100', [], {
1819
headers: {
1920
accept: 'application/vnd.github.v3+json',
@@ -40,7 +41,7 @@ describe('endpoint.defaults()', () => {
4041
})
4142

4243
it('repeated defaults', () => {
43-
octokitRequest.fetch = fetchMock.sandbox()
44+
mockable.fetch = fetchMock.sandbox()
4445
.get('https://github-enterprise.acme-inc.com/api/v3/orgs/my-project/repos', [], {
4546
headers: {
4647
accept: 'application/vnd.github.v3+json',

test/request-test.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ const fetchMock = require('fetch-mock/es5/server')
44
const sinonChai = require('sinon-chai')
55

66
const octokitRequest = require('..')
7+
const mockable = require('../lib/fetch')
78

89
chai.use(sinonChai)
910
const expect = chai.expect
10-
const originalFetch = octokitRequest.fetch
11+
const originalFetch = mockable.fetch
1112

1213
const pkg = require('../package.json')
1314
const userAgent = `octokit-request.js/${pkg.version} ${getUserAgent()}`
1415

1516
describe('octokitRequest()', () => {
1617
afterEach(() => {
17-
octokitRequest.fetch = originalFetch
18+
mockable.fetch = originalFetch
1819
})
1920
it('is a function', () => {
2021
expect(octokitRequest).to.be.a('function')
2122
})
2223

2324
it('README example', () => {
24-
octokitRequest.fetch = fetchMock.sandbox()
25+
mockable.fetch = fetchMock.sandbox()
2526
.mock('https://api.github.com/orgs/octokit/repos?type=private', [], {
2627
headers: {
2728
accept: 'application/vnd.github.v3+json',
@@ -44,7 +45,7 @@ describe('octokitRequest()', () => {
4445
})
4546

4647
it('README example alternative', () => {
47-
octokitRequest.fetch = fetchMock.sandbox()
48+
mockable.fetch = fetchMock.sandbox()
4849
.mock('https://api.github.com/orgs/octokit/repos?type=private', [], {
4950
headers: {
5051
accept: 'application/vnd.github.v3+json',
@@ -53,7 +54,7 @@ describe('octokitRequest()', () => {
5354
}
5455
})
5556

56-
octokitRequest.fetch = fetchMock.sandbox()
57+
mockable.fetch = fetchMock.sandbox()
5758
.mock('https://api.github.com/orgs/octokit/repos?type=private', [])
5859

5960
return octokitRequest({
@@ -72,7 +73,7 @@ describe('octokitRequest()', () => {
7273
})
7374

7475
it('Request with body', () => {
75-
octokitRequest.fetch = fetchMock.sandbox()
76+
mockable.fetch = fetchMock.sandbox()
7677
.mock('https://api.github.com/repos/octocat/hello-world/issues', 201, {
7778
headers: {
7879
'content-type': 'application/json; charset=utf-8'
@@ -102,7 +103,7 @@ describe('octokitRequest()', () => {
102103
})
103104

104105
it('Put without request body', () => {
105-
octokitRequest.fetch = fetchMock.sandbox()
106+
mockable.fetch = fetchMock.sandbox()
106107
.mock('https://api.github.com/user/starred/octocat/hello-world', 204, {
107108
headers: {
108109
'content-length': 0
@@ -123,7 +124,7 @@ describe('octokitRequest()', () => {
123124
})
124125

125126
it('HEAD requests (octokit/rest.js#841)', () => {
126-
octokitRequest.fetch = fetchMock.sandbox()
127+
mockable.fetch = fetchMock.sandbox()
127128
.head('https://api.github.com/repos/whatwg/html/pulls/1', {
128129
status: 200,
129130
headers: {
@@ -163,7 +164,7 @@ describe('octokitRequest()', () => {
163164
})
164165

165166
it.skip('Binary response with redirect (🤔 unclear how to mock fetch redirect properly)', () => {
166-
octokitRequest.fetch = fetchMock.sandbox()
167+
mockable.fetch = fetchMock.sandbox()
167168
.get('https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master', {
168169
status: 200,
169170
body: Buffer.from('1f8b0800000000000003cb4f2ec9cfce2cd14dcbac28292d4ad5cd2f4ad74d4f2dd14d2c4acec82c4bd53580007d060a0050bfb9b9a90203c428741ac2313436343307222320dbc010a8dc5c81c194124b8905a5c525894540a714e5e797e05347481edd734304e41319ff41ae8e2ebeae7ab92964d801d46f66668227fe0d4d51e3dfc8d0c8d808284f75df6201233cfe951590627ba01d330a46c1281805a3806e000024cb59d6000a0000', 'hex'),
@@ -188,7 +189,7 @@ describe('octokitRequest()', () => {
188189
// TODO: fails with "response.buffer is not a function" in browser
189190
if (!process.browser) {
190191
it('Binary response', () => {
191-
octokitRequest.fetch = fetchMock.sandbox()
192+
mockable.fetch = fetchMock.sandbox()
192193
.get('https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master', {
193194
status: 200,
194195

@@ -206,7 +207,7 @@ describe('octokitRequest()', () => {
206207
}
207208

208209
it('304 etag', () => {
209-
octokitRequest.fetch = fetchMock.sandbox()
210+
mockable.fetch = fetchMock.sandbox()
210211
.get((url, { headers }) => {
211212
return url === 'https://api.github.com/orgs/myorg' &&
212213
headers['if-none-match'] === 'etag'
@@ -227,7 +228,7 @@ describe('octokitRequest()', () => {
227228
})
228229

229230
it('Not found', () => {
230-
octokitRequest.fetch = fetchMock.sandbox()
231+
mockable.fetch = fetchMock.sandbox()
231232
.get('path:/orgs/nope', 404)
232233

233234
return octokitRequest('GET /orgs/:org', {
@@ -244,7 +245,7 @@ describe('octokitRequest()', () => {
244245
})
245246

246247
it('non-JSON response', () => {
247-
octokitRequest.fetch = fetchMock.sandbox()
248+
mockable.fetch = fetchMock.sandbox()
248249
.get('path:/repos/octokit-fixture-org/hello-world/contents/README.md', {
249250
status: 200,
250251
body: '# hello-world',
@@ -284,7 +285,7 @@ describe('octokitRequest()', () => {
284285
}
285286

286287
it('custom user-agent', () => {
287-
octokitRequest.fetch = fetchMock.sandbox()
288+
mockable.fetch = fetchMock.sandbox()
288289
.get((url, { headers }) => headers['user-agent'] === 'funky boom boom pow', 200)
289290

290291
return octokitRequest('GET /', {
@@ -295,7 +296,7 @@ describe('octokitRequest()', () => {
295296
})
296297

297298
it('passes node-fetch options to fetch only', () => {
298-
octokitRequest.fetch = (url, options) => {
299+
mockable.fetch = (url, options) => {
299300
expect(url).to.equal('https://api.github.com/')
300301
expect(options.timeout).to.equal(100)
301302
return Promise.reject(new Error('ok'))

0 commit comments

Comments
 (0)