@octokit/rest
Advanced tools
Comparing version 15.2.7 to 15.3.0
module.exports = apiPlugin | ||
const get = require('lodash/get') | ||
const pick = require('lodash/pick') | ||
@@ -15,4 +16,14 @@ | ||
Object.keys(ENDPOINT_DEFAULTS[namespaceName]).forEach(apiName => { | ||
const apiOptions = ENDPOINT_DEFAULTS[namespaceName][apiName] | ||
let apiOptions = ENDPOINT_DEFAULTS[namespaceName][apiName] | ||
let deprecated | ||
if (apiOptions.alias) { | ||
deprecated = apiOptions.deprecated | ||
apiOptions = get(ENDPOINT_DEFAULTS, apiOptions.alias) | ||
} | ||
const endpointDefaults = pick(apiOptions, ['method', 'url', 'headers', 'request']) | ||
if (deprecated) { | ||
endpointDefaults.deprecated = deprecated | ||
} | ||
@@ -19,0 +30,0 @@ octokit[namespaceName][apiName] = method.bind(null, octokit, endpointDefaults, apiOptions.params) |
@@ -16,2 +16,7 @@ module.exports = apiMethod | ||
if (endpointDefaults.deprecated) { | ||
console.warn(endpointDefaults.deprecated) | ||
delete endpointDefaults.deprecated | ||
} | ||
const endpointOptions = defaultsDeep(options, endpointDefaults) | ||
@@ -18,0 +23,0 @@ |
@@ -6,55 +6,109 @@ 'use strict' | ||
const set = require('lodash/set') | ||
const get = require('lodash/get') | ||
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) { | ||
// Alias are handled before validation, as validation rules | ||
// ar set the aliased parameter. The `mapTo` property is the other way | ||
// around, the final parameter name is the mapTo value, but validation | ||
// rules are on parameter with the mapTo property | ||
Object.keys(options).forEach(optionName => { | ||
if (!endpointParams[optionName] || !endpointParams[optionName].alias) { | ||
return | ||
} | ||
if (parameter['allow-null'] === true && paramIsNull) { | ||
return | ||
} | ||
set(options, endpointParams[optionName].alias, options[optionName]) | ||
delete options[optionName] | ||
if ((parameter.required && !paramIsPresent) || | ||
(parameter['allow-null'] === false && paramIsNull)) { | ||
throw new HttpError(`Empty value for parameter '${parameterName}': ${value}`, 400) | ||
// right now all parameters with an alias property also have a deprecated | ||
// property, but that might change in future, so we wrap it in the if block, | ||
// but ignore if for coverage | ||
/* istanbul ignore else */ | ||
if (endpointParams[optionName].deprecated) { | ||
console.warn(`DEPRECATED: ${endpointParams[optionName].deprecated}`) | ||
} | ||
}) | ||
if (parameter.enum) { | ||
if (parameter.enum.indexOf(value) === -1) { | ||
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400) | ||
Object.keys(endpointParams).forEach(parameterName => { | ||
const parameter = get(endpointParams, parameterName) | ||
const expectedType = parameter.type | ||
let parentParameterName | ||
let parentValue | ||
let parentParamIsPresent = true | ||
let parentParameterIsArray = false | ||
if (/\./.test(parameterName)) { | ||
parentParameterName = parameterName.replace(/\.[^.]+$/, '') | ||
parentParameterIsArray = parentParameterName.slice(-2) === '[]' | ||
if (parentParameterIsArray) { | ||
parentParameterName = parentParameterName.slice(0, -2) | ||
} | ||
parentValue = get(options, parentParameterName) | ||
parentParamIsPresent = parentParameterName === 'headers' || (typeof parentValue === 'object' && parentValue !== null) | ||
} | ||
if (parameter.validation) { | ||
const regex = new RegExp(parameter.validation) | ||
if (!regex.test(value)) { | ||
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400) | ||
let values = parentParameterIsArray | ||
? get(options, parentParameterName).map(value => value[parameterName.split('.').pop()]) | ||
: [get(options, parameterName)] | ||
values.forEach((value, i) => { | ||
const valueIsPresent = typeof value !== 'undefined' | ||
const valueIsNull = value === null | ||
const currentParameterName = parentParameterIsArray | ||
? parameterName.replace(/\[\]/, `[${i}]`) | ||
: parameterName | ||
if (!parameter.required && !valueIsPresent) { | ||
return | ||
} | ||
} | ||
if (expectedType === 'number') { | ||
value = parseInt(value, 10) | ||
if (isNaN(value)) { | ||
throw new HttpError(`Invalid value for parameter '${parameterName}': ${options[parameterName]} is NaN`, 400) | ||
// if the parent parameter is of type object but allows null | ||
// then the child parameters can be ignored | ||
if (!parentParamIsPresent) { | ||
return | ||
} | ||
} | ||
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) | ||
if (parameter.allowNull && valueIsNull) { | ||
return | ||
} | ||
} | ||
set(options, parameter.mapTo || parameterName, value) | ||
if (!parameter.allowNull && valueIsNull) { | ||
throw new HttpError(`'${currentParameterName}' cannot be null`, 400) | ||
} | ||
if (parameter.required && !valueIsPresent) { | ||
throw new HttpError(`Empty value for parameter '${currentParameterName}': ${JSON.stringify(value)}`, 400) | ||
} | ||
// parse to integer before checking for enum | ||
// so that string "1" will match enum with number 1 | ||
if (expectedType === 'integer') { | ||
const unparsedValue = value | ||
value = parseInt(value, 10) | ||
if (isNaN(value)) { | ||
throw new HttpError(`Invalid value for parameter '${currentParameterName}': ${JSON.stringify(unparsedValue)} is NaN`, 400) | ||
} | ||
} | ||
if (parameter.enum && parameter.enum.indexOf(value) === -1) { | ||
throw new HttpError(`Invalid value for parameter '${currentParameterName}': ${JSON.stringify(value)}`, 400) | ||
} | ||
if (parameter.validation) { | ||
const regex = new RegExp(parameter.validation) | ||
if (!regex.test(value)) { | ||
throw new HttpError(`Invalid value for parameter '${currentParameterName}': ${JSON.stringify(value)}`, 400) | ||
} | ||
} | ||
if (expectedType === 'object' && typeof value === 'string') { | ||
try { | ||
value = JSON.parse(value) | ||
} catch (exception) { | ||
throw new HttpError(`JSON parse error of value for parameter '${currentParameterName}': ${JSON.stringify(value)}`, 400) | ||
} | ||
} | ||
set(options, parameter.mapTo || currentParameterName, value) | ||
}) | ||
}) | ||
@@ -61,0 +115,0 @@ |
{ | ||
"name": "@octokit/rest", | ||
"version": "15.2.7", | ||
"version": "15.3.0", | ||
"publishConfig": { | ||
"access": "public" | ||
"access": "public", | ||
"tag": "next" | ||
}, | ||
@@ -50,2 +51,3 @@ "description": "GitHub REST API client for Node.js", | ||
"@octokit/fixtures-server": "^2.0.1", | ||
"@octokit/routes": "7.1.6", | ||
"@types/node": "^9.4.6", | ||
@@ -61,2 +63,3 @@ "apidoc": "^0.17.6", | ||
"glob": "^7.1.2", | ||
"jsondiff": "0.0.0", | ||
"mkdirp": "^0.5.1", | ||
@@ -104,2 +107,3 @@ "mocha": "^5.0.0", | ||
"generate-bundle-report": "webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html", | ||
"generate-routes": "node scripts/generate-routes", | ||
"prevalidate:ts": "npm run -s build:ts", | ||
@@ -106,0 +110,0 @@ "validate:ts": "tsc --target es6 index.d.ts", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
490127
32
13970