install @octokit/rest

This commit is contained in:
Luis Fernando Molina committed 2018-02-25 19:00:40 +01:00
1 parent 39d9c0de99
commit e44c6cb357
1145 files changed
+67878 -1

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
# `@octokit/plugins/*`
---
# ⚠️ Plugins are currently experimental. APIs can change at any time.
---
## Usage
```js
const octokit = require('@octokit/rest')()
function myPlugin (octokit) {
octokit.myMethod = function () {
// ...
}
octokit.hook.before('request', (options) => {
// change options or cancel request.
// return promise for async methods
})
octokit.hook.after('request', (result, options) => {
// ...
})
}
octokit.plugin(myPlugin)
octokit.myMethod()
octokit.request({url: '/'}) // runs before/after hooks defined above
```
+34
View File
@@ -0,0 +1,34 @@
module.exports = authenticate
function authenticate (state, options) {
if (!options) {
state.auth = false
return
}
switch (options.type) {
case 'basic':
if (!options.username || !options.password) {
throw new Error('Basic authentication requires both a username and password to be set')
}
break
case 'oauth':
if (!options.token && !(options.key && options.secret)) {
throw new Error('OAuth2 authentication requires a token or key & secret to be set')
}
break
case 'token':
case 'integration':
if (!options.token) {
throw new Error('Token authentication requires a token to be set')
}
break
default:
throw new Error("Invalid authentication type, must be 'basic', 'integration', or 'oauth'")
}
state.auth = options
}
@@ -0,0 +1,37 @@
module.exports = authenticationBeforeRequest
function authenticationBeforeRequest (state, options) {
if (!state.auth.type) {
return
}
if (state.auth.type === 'basic') {
const hash = Buffer.from(
`${state.auth.username}:${state.auth.password}`, 'ascii'
).toString('base64')
options.headers['authorization'] = `Basic ${hash}`
return
}
if (state.auth.type === 'token') {
options.headers['authorization'] = `token ${state.auth.token}`
return
}
if (state.auth.type === 'integration') {
options.headers['authorization'] = `Bearer ${state.auth.token}`
options.headers['accept'] = 'application/vnd.github.machine-man-preview+json'
return
}
options.url += options.url.indexOf('?') === -1 ? '?' : '&'
if (state.auth.token) {
options.url += `access_token=${encodeURIComponent(state.auth.token)}`
return
}
const key = encodeURIComponent(state.auth.key)
const secret = encodeURIComponent(state.auth.secret)
options.url += `client_id=${key}&client_secret=${secret}`
}
+12
View File
@@ -0,0 +1,12 @@
module.exports = authenticationPlugin
const authenticate = require('./authenticate')
const beforeRequest = require('./before-request')
function authenticationPlugin (octokit) {
const state = {
auth: false
}
octokit.authenticate = authenticate.bind(null, state)
octokit.hook.before('request', beforeRequest.bind(null, state))
}
+11
View File
@@ -0,0 +1,11 @@
module.exports = deprecate
function deprecate (func, message) {
return function () {
const caller = (new Error()).stack.split('\n')[2]
console.warn('DEPRECATED: ' + message)
console.warn(caller)
return func.apply(null, arguments)
}
}
+29
View File
@@ -0,0 +1,29 @@
module.exports = apiPlugin
const _ = require('lodash')
const method = require('./method')
const deprecate = require('./deprecate')
const ENDPOINT_DEFAULTS = require('../../routes.json')
function apiPlugin (octokit) {
Object.keys(ENDPOINT_DEFAULTS).forEach(namespaceName => {
octokit[namespaceName] = {}
Object.keys(ENDPOINT_DEFAULTS[namespaceName]).forEach(apiName => {
const apiOptions = ENDPOINT_DEFAULTS[namespaceName][apiName]
const endpointDefaults = _.pick(apiOptions, ['method', 'url', 'headers', 'request'])
octokit[namespaceName][apiName] = method.bind(null, octokit, endpointDefaults, apiOptions.params)
// log deprecation warning for APIs flagged as deprecated
if (apiOptions.deprecated) {
octokit[namespaceName][apiName] = deprecate(
octokit[namespaceName][apiName],
apiOptions.deprecated
)
}
})
})
}
+20
View File
@@ -0,0 +1,20 @@
module.exports = apiMethod
const defaultsDeep = require('lodash/defaultsDeep')
const validate = require('./validate')
function apiMethod (octokit, endpointDefaults, endpointParams, options, callback) {
const endpointOptions = defaultsDeep(options, endpointDefaults)
const promise = Promise.resolve(endpointOptions)
.then(validate.bind(null, endpointParams))
.then(octokit.request)
if (callback) {
promise.then(callback.bind(null, null), callback)
return
}
return promise
}
+62
View File
@@ -0,0 +1,62 @@
'use strict'
module.exports = validate
const set = require('lodash/set')
const HttpError = require('../../request/http-error')
function validate (endpointParams, options) {
Object.keys(endpointParams).forEach(parameterName => {
const parameter = endpointParams[parameterName]
const expectedType = parameter.type
let value = options[parameterName]
const paramIsPresent = parameterName in options
const paramIsNull = value === null
if (!parameter.required && !paramIsPresent) {
return
}
if (parameter['allow-null'] === true && paramIsNull) {
return
}
if ((parameter.required && !paramIsPresent) ||
(parameter['allow-null'] === false && paramIsNull)) {
throw new HttpError(`Empty value for parameter '${parameterName}': ${value}`, 400)
}
if (parameter.enum) {
if (parameter.enum.indexOf(value) === -1) {
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400)
}
}
if (parameter.validation) {
const regex = new RegExp(parameter.validation)
if (!regex.test(value)) {
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400)
}
}
if (expectedType === 'number') {
value = parseInt(value, 10)
if (isNaN(value)) {
throw new HttpError(`Invalid value for parameter '${parameterName}': ${options[parameterName]} is NaN`, 400)
}
}
if (expectedType === 'json' && typeof value === 'string') {
try {
value = JSON.parse(value)
} catch (exception) {
throw new HttpError(`JSON parse error of value for parameter '${parameterName}': ${value}`, 400)
}
}
set(options, parameter.mapTo || parameterName, value)
})
return options
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = getFirstPage
const getPage = require('./get-page')
function getFirstPage (octokit, link, headers, callback) {
return getPage(octokit, link, 'first', headers, callback)
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = getLastPage
const getPage = require('./get-page')
function getLastPage (octokit, link, headers, callback) {
return getPage(octokit, link, 'last', headers, callback)
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = getNextPage
const getPage = require('./get-page')
function getNextPage (octokit, link, headers, callback) {
return getPage(octokit, link, 'next', headers, callback)
}
+15
View File
@@ -0,0 +1,15 @@
module.exports = getPageLinks
function getPageLinks (link) {
link = link.link || link.meta.link || ''
const links = {}
// link format:
// '<https://api.github.com/users/aseemk/followers?page=2>; rel="next", <https://api.github.com/users/aseemk/followers?page=2>; rel="last"'
link.replace(/<([^>]*)>;\s*rel="([\w]*)"/g, (m, uri, type) => {
links[type] = uri
})
return links
}
+46
View File
@@ -0,0 +1,46 @@
module.exports = getPage
const HttpError = require('../../request/http-error')
const getPageLinks = require('./get-page-links')
function getPage (octokit, link, which, headers, callback) {
if (typeof headers === 'function') {
callback = headers
headers = null
}
const url = getPageLinks(link)[which]
if (!url) {
const urlError = new HttpError(`No ${which} page found`, 404)
if (callback) {
return callback(urlError)
}
return Promise.reject(urlError)
}
const requestOptions = {
url,
headers: applyAcceptHeader(link, headers)
}
const promise = octokit.request(requestOptions)
if (callback) {
promise.then(callback.bind(null, null), callback)
return
}
return promise
}
function applyAcceptHeader (res, headers) {
const previous = res.meta && res.meta['x-github-media-type']
if (!previous || (headers && headers.accept)) {
return headers
}
headers = headers || {}
headers.accept = `application/vnd.${previous.replace('; format=', '+')}`
return headers
}
@@ -0,0 +1,7 @@
module.exports = getPreviousPage
const getPage = require('./get-page')
function getPreviousPage (octokit, link, headers, callback) {
return getPage(octokit, link, 'prev', headers, callback)
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = hasFirstPage
const getPageLinks = require('./get-page-links')
function hasFirstPage (link) {
return getPageLinks(link).first
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = hasLastPage
const getPageLinks = require('./get-page-links')
function hasLastPage (link) {
return getPageLinks(link).last
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = hasNextPage
const getPageLinks = require('./get-page-links')
function hasNextPage (link) {
return getPageLinks(link).next
}
@@ -0,0 +1,7 @@
module.exports = hasPreviousPage
const getPageLinks = require('./get-page-links')
function hasPreviousPage (link) {
return getPageLinks(link).prev
}
+12
View File
@@ -0,0 +1,12 @@
module.exports = paginationPlugin
function paginationPlugin (octokit) {
octokit.getFirstPage = require('./get-first-page').bind(null, octokit)
octokit.getLastPage = require('./get-last-page').bind(null, octokit)
octokit.getNextPage = require('./get-next-page').bind(null, octokit)
octokit.getPreviousPage = require('./get-previous-page').bind(null, octokit)
octokit.hasFirstPage = require('./has-first-page')
octokit.hasLastPage = require('./has-last-page')
octokit.hasNextPage = require('./has-next-page')
octokit.hasPreviousPage = require('./has-previous-page')
}