22
33module . 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' )
108const ENDPOINT_DEFAULTS = require ( '../../routes.json' )
119
1210function 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}
0 commit comments