diff --git a/lib/deprecate.js b/lib/deprecate.js deleted file mode 100644 index c56f7ab6..00000000 --- a/lib/deprecate.js +++ /dev/null @@ -1,12 +0,0 @@ -module.exports = deprecate - -const loggedMessages = {} - -function deprecate (message) { - if (loggedMessages[message]) { - return - } - - console.warn(`DEPRECATED (@octokit/rest): ${message}`) - loggedMessages[message] = 1 -} diff --git a/lib/endpoint/index.js b/lib/endpoint/index.js deleted file mode 100644 index 0674a9a9..00000000 --- a/lib/endpoint/index.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict' - -module.exports = restEndpoint - -const defaultsDeep = require('lodash/defaultsDeep') -const intersection = require('lodash/intersection') -const mapKeys = require('lodash/mapKeys') -const omit = require('lodash/omit') -const urlTemplate = require('url-template') -const getUserAgent = require('universal-user-agent') - -const addQueryParameters = require('./add-query-parameters') -const deprecate = require('../deprecate') -const extractUrlVariableNames = require('./extract-url-variable-names') -const pkg = require('../../package.json') - -const DEFAULTS = module.exports.DEFAULTS = require('./defaults') -const NON_PARAMETERS = [ - 'request', - 'baseUrl' -] - -function restEndpoint (options) { - // lowercase header names (#760) - options.headers = mapKeys(options.headers, (value, key) => key.toLowerCase()) - - let userAgent = `octokit.js/${pkg.version} ${getUserAgent()}` - if (options.headers['user-agent']) { - userAgent = `${options.headers['user-agent']} ${userAgent}` - } - options.headers['user-agent'] = userAgent - - options = defaultsDeep({}, options, DEFAULTS) - - let method = options.method.toLowerCase() - let baseUrl = options.baseUrl - let url = options.url - let body = options.body - let headers = options.headers - let remainingOptions = omit(options, ['method', 'baseUrl', 'url', 'headers']) - - // replace :varname with {varname} to make it RFC 6570 compatible - url = url.replace(/:([a-z]\w+)/g, '{+$1}') - - // extract variable names from URL to calculate remaining variables later - const urlVariableNames = extractUrlVariableNames(url) - - url = urlTemplate.parse(url).expand(remainingOptions) - - if (!/^http/.test(url)) { - url = (baseUrl) + url - } - - const requestOptions = remainingOptions.request - remainingOptions = omit(remainingOptions, intersection(Object.keys(options), urlVariableNames).concat(NON_PARAMETERS)) - - if (method === 'get' || method === 'head') { - url = addQueryParameters(url, remainingOptions) - } else { - if ('input' in remainingOptions) { - deprecate('"input" option has been renamed to "data"') - remainingOptions.data = remainingOptions.input - delete remainingOptions.input - } - - if ('data' in remainingOptions) { - body = remainingOptions.data - } else { - body = Object.keys(remainingOptions).length ? remainingOptions : undefined - } - } - - return Object.assign(requestOptions, { - method, - url, - headers, - body - }) -} diff --git a/lib/get-request-agent.js b/lib/get-request-agent.js deleted file mode 100644 index 1467b6b3..00000000 --- a/lib/get-request-agent.js +++ /dev/null @@ -1,53 +0,0 @@ -module.exports = getRequestAgent - -const urlParse = require('url').parse - -const HttpAgent = require('http').Agent -const HttpsAgent = require('https').Agent -const HttpProxyAgent = require('http-proxy-agent') -const HttpsProxyAgent = require('https-proxy-agent') -const merge = require('lodash/merge') -const omit = require('lodash/omit') -const pick = require('lodash/pick') - -const deprecate = require('./deprecate') - -function getRequestAgent (baseUrl, options) { - if (options.agent) { - return options.agent - } - - const agentOptionNames = ['ca', 'proxy', 'rejectUnauthorized', 'family'].filter(key => key in options) - - if (agentOptionNames.length === 0) { - return - } - agentOptionNames.forEach(option => { - deprecate(`options.${option} (use "options.agent" instead)`) - }) - - const agentOptions = pick(options, agentOptionNames) - - const protocol = urlParse(baseUrl).protocol.replace(':', '') - - /* istanbul ignore if */ - if ('proxy' in options) { - const proxyAgentOptions = merge( - urlParse(agentOptions.proxy), - omit(agentOptions, 'proxy') - ) - - if (protocol === 'http') { - return new HttpProxyAgent(proxyAgentOptions) - } - - return new HttpsProxyAgent(proxyAgentOptions) - } - - /* istanbul ignore if */ - if (protocol === 'http') { - return new HttpAgent(agentOptions) - } - - return new HttpsAgent(agentOptions) -} diff --git a/lib/parse-client-options.js b/lib/parse-client-options.js index 2303f5fd..0c18e45f 100644 --- a/lib/parse-client-options.js +++ b/lib/parse-client-options.js @@ -2,10 +2,9 @@ module.exports = parseOptions const deepmerge = require('deepmerge') const getUserAgent = require('universal-user-agent') +const isPlainObject = require('is-plain-object') const pick = require('lodash/pick') -const deprecate = require('./deprecate') -const getRequestAgent = require('./get-request-agent') const pkg = require('../package.json') const DEFAULTS = require('./defaults') @@ -21,30 +20,6 @@ function parseOptions (userOptions) { userOptions = {} } - if ('followRedirects' in userOptions) { - deprecate('followRedirects option is no longer supported. All redirects are followed correctly') - } - - if ('protocol' in userOptions) { - deprecate('protocol option is no longer supported') - } - - if ('host' in userOptions) { - deprecate('host option is no longer supported') - } - - if ('port' in userOptions) { - deprecate('port option is no longer supported') - } - - if ('pathPrefix' in userOptions) { - deprecate('pathPrefix option is no longer supported') - } - - if ('Promise' in userOptions) { - deprecate('Promise option is no longer supported. The native Promise API is used') - } - if (userOptions.headers) { userOptions.headers = Object.keys(userOptions.headers).reduce((newObj, key) => { newObj[key.toLowerCase()] = userOptions.headers[key] @@ -52,33 +27,15 @@ function parseOptions (userOptions) { }, {}) } - const options = deepmerge(DEFAULTS, pick(userOptions, OPTION_NAMES)) + const options = deepmerge(DEFAULTS, pick(userOptions, OPTION_NAMES), { isMergeableObject: isPlainObject }) const clientDefaults = { baseUrl: options.baseUrl, headers: options.headers, request: { - timeout: options.timeout - } - } - if (userOptions.protocol) { - clientDefaults.baseUrl = `${userOptions.protocol}://${userOptions.host}` - - /* istanbul ignore else */ - if (userOptions.port) { - clientDefaults.baseUrl += `:${userOptions.port}` + timeout: options.timeout, + agent: options.agent } - - // Check if a prefix is passed in the options and strip any leading or trailing slashes from it. - /* istanbul ignore else */ - if (userOptions.pathPrefix) { - clientDefaults.baseUrl += '/' + userOptions.pathPrefix.replace(/(^[/]+|[/]+$)/g, '') - } - } - /* istanbul ignore else */ - - if (!process.browser) { - clientDefaults.request.agent = getRequestAgent(clientDefaults.baseUrl, userOptions) } const userAgentOption = clientDefaults.headers['user-agent'] diff --git a/lib/plugins/endpoint-methods/method.js b/lib/plugins/endpoint-methods/method.js deleted file mode 100644 index d18a8c5b..00000000 --- a/lib/plugins/endpoint-methods/method.js +++ /dev/null @@ -1,35 +0,0 @@ -module.exports = apiMethod - -const clone = require('lodash/clone') -const defaultsDeep = require('lodash/defaultsDeep') -const mapKeys = require('lodash/mapKeys') - -const deprecate = require('../../deprecate') -const validate = require('./validate') - -function apiMethod (octokit, endpointDefaults, endpointParams, options, callback) { - // Do not alter passed options (#786) - options = clone(options) || {} - - // lowercase header names (#760) - options.headers = mapKeys(options.headers, (value, key) => key.toLowerCase()) - - if (endpointDefaults.deprecated) { - deprecate(endpointDefaults.deprecated) - delete endpointDefaults.deprecated - } - - const endpointOptions = defaultsDeep(options, endpointDefaults) - - const promise = Promise.resolve(endpointOptions) - .then(validate.bind(null, endpointParams)) - .then(octokit.request) - - if (callback) { - deprecate('callbacks will be removed in v16. Use async/await or Promises instead.') - promise.then(callback.bind(null, null), callback) - return - } - - return promise -} diff --git a/lib/request/http-error.js b/lib/request/http-error.js deleted file mode 100644 index 243d84eb..00000000 --- a/lib/request/http-error.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict' - -const deprecate = require('../deprecate') - -const STATUS_CODES = { - 304: 'Not Modified', // See PR #673 (https://github.com/octokit/rest.js/pull/673) - 400: 'Bad Request', - 404: 'Not Found', - 500: 'Internal Server Error', - 504: 'Gateway Timeout' -} - -module.exports = class HttpError extends Error { - constructor (message, code, headers) { - super(message) - // Maintains proper stack trace for where our error was thrown (only available on V8) - /* istanbul ignore else */ - if (Error.captureStackTrace) { - Error.captureStackTrace(this, this.constructor) - } - this.name = 'HttpError' - this.code = code - this.status = STATUS_CODES[code] - this.headers = headers - } - - toString () { - return this.message - } - - toJSON () { - deprecate('error.toJSON() – use `error.code`, `error.status`, `error.message` instead') - return { - code: this.code, - status: this.status, - message: this.message - } - } -} diff --git a/lib/request/request.js b/lib/request/request.js deleted file mode 100644 index f36effba..00000000 --- a/lib/request/request.js +++ /dev/null @@ -1,113 +0,0 @@ -'use strict' - -module.exports = request - -const fetch = require('node-fetch').default -const debug = require('debug')('octokit:rest') -const defaults = require('lodash/defaults') -const isPlainObject = require('lodash/isPlainObject') -const pick = require('lodash/pick') - -const deprecate = require('../deprecate') -const getBuffer = require('./get-buffer-response') -const HttpError = require('./http-error') - -function request (requestOptions) { - debug('REQUEST:', requestOptions) - - // calculate content length unless body is a stream, in which case the - // content length is already set per option - if (requestOptions.body) { - defaults(requestOptions.headers, { - 'content-type': 'application/json; charset=utf-8' - }) - } - - // https://fetch.spec.whatwg.org/#methods - requestOptions.method = requestOptions.method.toUpperCase() - - // GitHub expects "content-length: 0" header for PUT/PATCH requests without body - // fetch does not allow to set `content-length` header, but we can set body to an empty string - if (['PATCH', 'PUT'].indexOf(requestOptions.method) >= 0 && !requestOptions.body) { - requestOptions.body = '' - } - - if (isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body)) { - requestOptions.body = JSON.stringify(requestOptions.body) - } - - let headers = {} - let status - - return fetch(requestOptions.url, pick(requestOptions, 'method', 'body', 'headers', 'timeout', 'agent')) - - .then(response => { - status = response.status - for (const keyAndValue of response.headers.entries()) { - headers[keyAndValue[0]] = keyAndValue[1] - } - - if (status === 204 || status === 205) { - return - } - - // GitHub API returns 200 for HEAD requsets - if (requestOptions.method === 'HEAD') { - if (status < 400) { - return - } - - throw new HttpError(response.statusText, status, headers) - } - - if (status === 304) { - requestOptions.url = response.headers.location - throw new HttpError('Not modified', status, headers) - } - - if (status >= 400) { - return response.text() - - .then(message => { - throw new HttpError(message, status, headers) - }) - } - - const contentType = response.headers.get('content-type') - if (/application\/json/.test(contentType)) { - return response.json() - } - - if (!contentType || /^text\/|charset=utf-8$/.test(contentType)) { - return response.text() - } - - return getBuffer(response) - }) - - .then(data => { - return { - data, - status, - headers, - get meta () { - deprecate('response.meta – use response.headers instead (#896)') - return headers - } - } - }) - - .catch(error => { - if (error instanceof HttpError) { - try { - Object.assign(error, JSON.parse(error.message)) - } catch (_error) { - // ignore, see #684 - } - - throw error - } - - throw new HttpError(error.message, 500, headers) - }) -} diff --git a/plugins/authentication/before-request.js b/plugins/authentication/before-request.js index d62b5e61..4915f813 100644 --- a/plugins/authentication/before-request.js +++ b/plugins/authentication/before-request.js @@ -3,8 +3,6 @@ module.exports = authenticationBeforeRequest const btoa = require('btoa-lite') const uniq = require('lodash/uniq') -const deprecate = require('../../lib/deprecate') - function authenticationBeforeRequest (state, options) { if (!state.auth.type) { return @@ -21,12 +19,6 @@ function authenticationBeforeRequest (state, options) { return } - // deprecate state.auth.type === 'integration', rename to 'app' - if (state.auth.type === 'integration') { - deprecate('authentication type "integration" is deprecated. Use "app" instead.') - state.auth.type = 'app' - } - if (state.auth.type === 'app') { options.headers['authorization'] = `Bearer ${state.auth.token}` const acceptHeaders = options.headers['accept'].split(',') diff --git a/plugins/validate/validate.js b/plugins/validate/validate.js index 73106a0a..9ecda943 100644 --- a/plugins/validate/validate.js +++ b/plugins/validate/validate.js @@ -6,39 +6,12 @@ const set = require('lodash/set') const get = require('lodash/get') const HttpError = require('@octokit/request/lib/http-error') -const deprecate = require('../../lib/deprecate') - function validate (options) { if (!options.request.validate) { return } - const { validate: params, deprecated } = options.request - - if (deprecated) { - deprecate(deprecated) - } - - // Alias are handled before validation, as validation rules - // are set to 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 (!params[optionName] || !params[optionName].alias) { - return - } - - set(options, params[optionName].alias, options[optionName]) - delete options[optionName] - - // 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 (params[optionName].deprecated) { - deprecate(`"${optionName}" parameter has been renamed to "${params[optionName].alias}"`) - } - }) + const { validate: params } = options.request Object.keys(params).forEach(parameterName => { const parameter = get(params, parameterName)