Skip to content

Commit

Permalink
feat: plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 17, 2018
1 parent 9625a58 commit 70e114a
Show file tree
Hide file tree
Showing 24 changed files with 49 additions and 63 deletions.
31 changes: 6 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
module.exports = GitHubApi
const factory = require('./lib/factory')

const endpoint = require('@octokit/request').endpoint
const Hook = require('before-after-hook')

const parseClientOptions = require('./lib/parse-client-options')
const requestWithDefaults = require('./lib/request-with-defaults')

const PLUGINS = [
require('./lib/plugins/authentication'),
require('./lib/plugins/endpoint-methods'),
require('./lib/plugins/pagination')
const CORE_PLUGINS = [
require('./plugins/authentication'),
require('./plugins/endpoint-methods'),
require('./plugins/pagination')
]

function GitHubApi (options) {
const hook = new Hook()
const api = {
// NOTE: github.hook and github.plugin are experimental APIs
// at this point and can change at any time
hook,
plugin: (pluginFunction) => pluginFunction(api),
request: requestWithDefaults(hook, endpoint, parseClientOptions(options))
}

PLUGINS.forEach(api.plugin)

return api
}
module.exports = factory(CORE_PLUGINS)
19 changes: 19 additions & 0 deletions lib/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = Octokit

const endpoint = require('@octokit/request').endpoint
const Hook = require('before-after-hook')

const parseClientOptions = require('./parse-client-options')
const requestWithDefaults = require('./request-with-defaults')

function Octokit (plugins, options) {
const hook = new Hook()
const api = {
hook,
request: requestWithDefaults(hook, endpoint, parseClientOptions(options))
}

plugins.forEach(pluginFunction => pluginFunction(api, options))

return api
}
10 changes: 10 additions & 0 deletions lib/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = factory

const Octokit = require('./constructor')
const registerPlugin = require('./register-plugin')

function factory (plugins) {
const Api = Octokit.bind(null, plugins)
Api.plugin = registerPlugin.bind(null, plugins)
return Api
}
31 changes: 0 additions & 31 deletions lib/plugins/README.md

This file was deleted.

7 changes: 7 additions & 0 deletions lib/register-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = registerPlugin

const factory = require('./factory')

function registerPlugin (plugins, pluginFunction) {
return factory(plugins.concat(pluginFunction))
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = authenticationBeforeRequest
const btoa = require('btoa-lite')
const uniq = require('lodash/uniq')

const deprecate = require('../../deprecate')
const deprecate = require('../../lib/deprecate')

function authenticationBeforeRequest (state, options) {
if (!state.auth.type) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = apiPlugin
const set = require('lodash/set')

const validate = require('./validate')
const ENDPOINT_DEFAULTS = require('../../routes.json')
const ENDPOINT_DEFAULTS = require('./routes.json')

function apiPlugin (octokit) {
octokit.hook.before('request', validate)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const set = require('lodash/set')
const get = require('lodash/get')
const HttpError = require('@octokit/request/lib/http-error')

const deprecate = require('../../deprecate')
const deprecate = require('../../lib/deprecate')

function validate (options) {
if (!options.request.endpoint) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/generate-api-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function toSectionComment (namespaceName) {
function toApiComment (namespaceName, apiName, api) {
if (!api.method) {
throw new Error(
`No HTTP method specified for ${namespaceName}.${apiName} in routes.json`
`No HTTP method specified for ${namespaceName}.${apiName} in plugins/endpoint-methods/routes.json`
)
}

Expand Down
6 changes: 3 additions & 3 deletions scripts/generate-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function sortRoutesByKeys (routes) {
Object.keys(routes[scope]).forEach(method => {
routes[scope][method] = sortByKeys(routes[scope][method])

// routes-for-api-docs keeps parameters as array, while lib/routes.json uses an object
// routes-for-api-docs keeps parameters as array, while plugins/endpoint-methods/routes.json uses an object
if (Array.isArray(routes[scope][method].params)) {
return
}
Expand Down Expand Up @@ -101,7 +101,7 @@ NEW_ROUTES['users'].push(...NEW_ROUTES['orgs'].filter(endpoint => ORG_USER_PATHS
NEW_ROUTES['users'].push(...NEW_ROUTES['repos'].filter(endpoint => REPOS_USER_PATHS.includes(endpoint.path)))
NEW_ROUTES['users'].push(...NEW_ROUTES['apps'].filter(endpoint => APPS_USER_PATHS.includes(endpoint.path)))

// map scopes from @octokit/routes to what we currently have in lib/routes.json
// map scopes from @octokit/routes to what we currently have in plugins/endpoint-methods/routes.json
const mapScopes = {
activity: 'activity',
apps: 'apps',
Expand Down Expand Up @@ -314,5 +314,5 @@ newRoutes.users.unsuspend = CURRENT_ROUTES.users.unsuspend
// don’t break the deprecated "integrations" scope
newRoutes.integrations = CURRENT_ROUTES.integrations

writeFileSync('lib/routes.json', JSON.stringify(sortRoutesByKeys(newRoutes), null, 2) + '\n')
writeFileSync('plugins/endpoint-methods/routes.json', JSON.stringify(sortRoutesByKeys(newRoutes), null, 2) + '\n')
writeFileSync('scripts/routes-for-api-docs.json', JSON.stringify(sortRoutesByKeys(newDocRoutes), null, 2))

0 comments on commit 70e114a

Please sign in to comment.