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

+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
}