Skip to content

Commit 1fdfbf0

Browse files
committed
feat: octokit.request & octokit.request.endpoint
1 parent 5fd0e42 commit 1fdfbf0

File tree

10 files changed

+79
-51
lines changed

10 files changed

+79
-51
lines changed

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
module.exports = GitHubApi
22

3-
const defaultsDeep = require('lodash/defaultsDeep')
3+
const endpoint = require('@octokit/request').endpoint
44
const Hook = require('before-after-hook')
55

66
const parseClientOptions = require('./lib/parse-client-options')
7-
const request = require('./lib/request')
8-
const ENDPOINT_DEFAULTS = require('./lib/endpoint').DEFAULTS
7+
const requestWithDefaults = require('./lib/request-with-defaults')
98

109
const PLUGINS = [
1110
require('./lib/plugins/authentication'),
@@ -14,15 +13,13 @@ const PLUGINS = [
1413
]
1514

1615
function GitHubApi (options) {
17-
const defaults = defaultsDeep(parseClientOptions(options), ENDPOINT_DEFAULTS)
18-
1916
const hook = new Hook()
2017
const api = {
21-
// NOTE: github.hook, github.plugin and github.request are experimental APIs
18+
// NOTE: github.hook and github.plugin are experimental APIs
2219
// at this point and can change at any time
2320
hook,
2421
plugin: (pluginFunction) => pluginFunction(api),
25-
request: (options) => api.hook('request', defaultsDeep(options, defaults), request)
22+
request: requestWithDefaults(hook, endpoint, parseClientOptions(options))
2623
}
2724

2825
PLUGINS.forEach(api.plugin)

lib/parse-client-options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
module.exports = parseOptions
22

33
const defaults = require('lodash/defaults')
4+
const getUserAgent = require('universal-user-agent')
45
const pick = require('lodash/pick')
56

67
const deprecate = require('./deprecate')
78
const getRequestAgent = require('./get-request-agent')
9+
const pkg = require('../package.json')
10+
811
const DEFAULTS = require('./defaults')
912
const OPTION_NAMES = [
1013
'timeout',
@@ -42,6 +45,13 @@ function parseOptions (userOptions) {
4245
deprecate('Promise option is no longer supported. The native Promise API is used')
4346
}
4447

48+
if (userOptions.headers) {
49+
userOptions.headers = Object.keys(userOptions.headers).reduce((newObj, key) => {
50+
newObj[key.toLowerCase()] = userOptions.headers[key]
51+
return newObj
52+
}, {})
53+
}
54+
4555
const options = defaults(pick(userOptions, OPTION_NAMES), DEFAULTS)
4656

4757
const clientDefaults = {
@@ -71,5 +81,9 @@ function parseOptions (userOptions) {
7181
clientDefaults.request.agent = getRequestAgent(clientDefaults.baseUrl, userOptions)
7282
}
7383

84+
const userAgentOption = clientDefaults.headers['user-agent']
85+
const defaultUserAgent = `octokit.js/${pkg.version} ${getUserAgent()}`
86+
clientDefaults.headers['user-agent'] = [userAgentOption, defaultUserAgent].filter(Boolean).join(' ')
87+
7488
return clientDefaults
7589
}

lib/plugins/endpoint-methods/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
module.exports = apiPlugin
44

5-
const get = require('lodash/get')
6-
const pick = require('lodash/pick')
7-
8-
const method = require('./method')
5+
const set = require('lodash/set')
96

7+
const validate = require('./validate')
108
const ENDPOINT_DEFAULTS = require('../../routes.json')
119

1210
function apiPlugin (octokit) {
11+
octokit.hook.before('request', validate)
12+
1313
Object.keys(ENDPOINT_DEFAULTS).forEach(namespaceName => {
1414
octokit[namespaceName] = {}
1515

@@ -19,15 +19,23 @@ function apiPlugin (octokit) {
1919

2020
if (apiOptions.alias) {
2121
deprecated = apiOptions.deprecated
22-
apiOptions = get(ENDPOINT_DEFAULTS, apiOptions.alias)
22+
const [aliasNamespaceName, aliasApiName] = apiOptions.alias.split('.')
23+
apiOptions = ENDPOINT_DEFAULTS[aliasNamespaceName][aliasApiName]
2324
}
2425

25-
const endpointDefaults = pick(apiOptions, ['method', 'url', 'headers', 'request'])
26-
if (deprecated) {
27-
endpointDefaults.deprecated = deprecated
28-
}
26+
const endpointDefaults = ['method', 'url', 'headers'].reduce((map, key) => {
27+
if (typeof apiOptions[key] !== 'undefined') {
28+
map[key] = apiOptions[key]
29+
}
30+
31+
return map
32+
}, {})
33+
34+
set(endpointDefaults, 'request.endpoint', Object.assign({
35+
deprecated
36+
}, apiOptions))
2937

30-
octokit[namespaceName][apiName] = method.bind(null, octokit, endpointDefaults, apiOptions.params)
38+
octokit[namespaceName][apiName] = octokit.request.defaults(endpointDefaults)
3139
})
3240
})
3341
}

lib/plugins/endpoint-methods/validate.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@ module.exports = validate
44

55
const set = require('lodash/set')
66
const get = require('lodash/get')
7-
const HttpError = require('../../request/http-error')
7+
const HttpError = require('@octokit/request/lib/http-error')
8+
89
const deprecate = require('../../deprecate')
910

10-
function validate (endpointParams, options) {
11+
function validate (options) {
12+
if (!options.request.endpoint) {
13+
return
14+
}
15+
16+
const endpointParams = options.request.endpoint.params
17+
const deprecated = options.request.endpoint.deprecated
18+
19+
if (deprecated) {
20+
deprecate(deprecated)
21+
}
22+
1123
// Alias are handled before validation, as validation rules
12-
// ar set the aliased parameter. The `mapTo` property is the other way
24+
// are set to the aliased parameter. The `mapTo` property is the other way
1325
// around, the final parameter name is the mapTo value, but validation
1426
// rules are on parameter with the mapTo property
1527
Object.keys(options).forEach(optionName => {

lib/plugins/pagination/get-first-page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module.exports = getFirstPage
22

33
const getPage = require('./get-page')
44

5-
function getFirstPage (octokit, link, headers, callback) {
6-
return getPage(octokit, link, 'first', headers, callback)
5+
function getFirstPage (octokit, link, headers) {
6+
return getPage(octokit, link, 'first', headers)
77
}

lib/plugins/pagination/get-last-page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module.exports = getLastPage
22

33
const getPage = require('./get-page')
44

5-
function getLastPage (octokit, link, headers, callback) {
6-
return getPage(octokit, link, 'last', headers, callback)
5+
function getLastPage (octokit, link, headers) {
6+
return getPage(octokit, link, 'last', headers)
77
}

lib/plugins/pagination/get-next-page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module.exports = getNextPage
22

33
const getPage = require('./get-page')
44

5-
function getNextPage (octokit, link, headers, callback) {
6-
return getPage(octokit, link, 'next', headers, callback)
5+
function getNextPage (octokit, link, headers) {
6+
return getPage(octokit, link, 'next', headers)
77
}

lib/plugins/pagination/get-page.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
module.exports = getPage
22

3-
const HttpError = require('../../request/http-error')
3+
const HttpError = require('@octokit/request/lib/http-error')
44
const getPageLinks = require('./get-page-links')
5-
const deprecate = require('../../deprecate')
6-
7-
function getPage (octokit, link, which, headers, callback) {
8-
if (typeof headers === 'function') {
9-
callback = headers
10-
headers = null
11-
}
12-
13-
if (callback) {
14-
deprecate('callbacks will be removed in v16. Use async/await or Promises instead.')
15-
}
165

6+
function getPage (octokit, link, which, headers) {
177
const url = getPageLinks(link)[which]
188

199
if (!url) {
2010
const urlError = new HttpError(`No ${which} page found`, 404)
21-
if (callback) {
22-
return callback(urlError)
23-
}
2411
return Promise.reject(urlError)
2512
}
2613

@@ -29,14 +16,7 @@ function getPage (octokit, link, which, headers, callback) {
2916
headers: applyAcceptHeader(link, headers)
3017
}
3118

32-
const promise = octokit.request(requestOptions)
33-
34-
if (callback) {
35-
promise.then(callback.bind(null, null), callback)
36-
return
37-
}
38-
39-
return promise
19+
return octokit.request(requestOptions)
4020
}
4121

4222
function applyAcceptHeader (res, headers) {

lib/plugins/pagination/get-previous-page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module.exports = getPreviousPage
22

33
const getPage = require('./get-page')
44

5-
function getPreviousPage (octokit, link, headers, callback) {
6-
return getPage(octokit, link, 'prev', headers, callback)
5+
function getPreviousPage (octokit, link, headers) {
6+
return getPage(octokit, link, 'prev', headers)
77
}

lib/request-with-defaults.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = withDefaults
2+
3+
const request = require('@octokit/request/lib/request')
4+
5+
function withDefaults (hook, oldEndpoint, newDefaults) {
6+
const endpoint = oldEndpoint.defaults(newDefaults)
7+
8+
const newApi = function (route, options) {
9+
const endpointOptions = endpoint.merge(route, options)
10+
return hook('request', endpointOptions, function (options) {
11+
return request(endpoint.parse(options))
12+
})
13+
}
14+
newApi.endpoint = endpoint
15+
newApi.defaults = withDefaults.bind(null, hook, endpoint)
16+
return newApi
17+
}

0 commit comments

Comments
 (0)