2020-01-24 12:20:19 -05:00
|
|
|
|
module.exports = registerEndpoints;
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const { Deprecation } = require("deprecation");
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
function registerEndpoints(octokit, routes) {
|
2019-08-06 18:26:04 -04:00
|
|
|
|
Object.keys(routes).forEach(namespaceName => {
|
|
|
|
|
if (!octokit[namespaceName]) {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
octokit[namespaceName] = {};
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object.keys(routes[namespaceName]).forEach(apiName => {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const apiOptions = routes[namespaceName][apiName];
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const endpointDefaults = ["method", "url", "headers"].reduce(
|
|
|
|
|
(map, key) => {
|
|
|
|
|
if (typeof apiOptions[key] !== "undefined") {
|
|
|
|
|
map[key] = apiOptions[key];
|
|
|
|
|
}
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
return map;
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
|
|
|
|
endpointDefaults.request = {
|
|
|
|
|
validate: apiOptions.params
|
2020-01-24 12:20:19 -05:00
|
|
|
|
};
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
let request = octokit.request.defaults(endpointDefaults);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
|
|
|
|
// patch request & endpoint methods to support deprecated parameters.
|
|
|
|
|
// Not the most elegant solution, but we don’t want to move deprecation
|
|
|
|
|
// logic into octokit/endpoint.js as it’s out of scope
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const hasDeprecatedParam = Object.keys(apiOptions.params || {}).find(
|
|
|
|
|
key => apiOptions.params[key].deprecated
|
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
if (hasDeprecatedParam) {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const patch = patchForDeprecation.bind(null, octokit, apiOptions);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
request = patch(
|
|
|
|
|
octokit.request.defaults(endpointDefaults),
|
|
|
|
|
`.${namespaceName}.${apiName}()`
|
2020-01-24 12:20:19 -05:00
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
request.endpoint = patch(
|
|
|
|
|
request.endpoint,
|
|
|
|
|
`.${namespaceName}.${apiName}.endpoint()`
|
2020-01-24 12:20:19 -05:00
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
request.endpoint.merge = patch(
|
|
|
|
|
request.endpoint.merge,
|
|
|
|
|
`.${namespaceName}.${apiName}.endpoint.merge()`
|
2020-01-24 12:20:19 -05:00
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (apiOptions.deprecated) {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
octokit[namespaceName][apiName] = function deprecatedEndpointMethod() {
|
|
|
|
|
octokit.log.warn(
|
|
|
|
|
new Deprecation(`[@octokit/rest] ${apiOptions.deprecated}`)
|
|
|
|
|
);
|
|
|
|
|
octokit[namespaceName][apiName] = request;
|
|
|
|
|
return request.apply(null, arguments);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return;
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
octokit[namespaceName][apiName] = request;
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
function patchForDeprecation(octokit, apiOptions, method, methodName) {
|
|
|
|
|
const patchedMethod = options => {
|
|
|
|
|
options = Object.assign({}, options);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
|
|
|
|
Object.keys(options).forEach(key => {
|
|
|
|
|
if (apiOptions.params[key] && apiOptions.params[key].deprecated) {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
const aliasKey = apiOptions.params[key].alias;
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
octokit.log.warn(
|
|
|
|
|
new Deprecation(
|
|
|
|
|
`[@octokit/rest] "${key}" parameter is deprecated for "${methodName}". Use "${aliasKey}" instead`
|
|
|
|
|
)
|
|
|
|
|
);
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
|
|
|
|
if (!(aliasKey in options)) {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
options[aliasKey] = options[key];
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
2020-01-24 12:20:19 -05:00
|
|
|
|
delete options[key];
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|
2020-01-24 12:20:19 -05:00
|
|
|
|
});
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
return method(options);
|
|
|
|
|
};
|
2019-08-06 18:26:04 -04:00
|
|
|
|
Object.keys(method).forEach(key => {
|
2020-01-24 12:20:19 -05:00
|
|
|
|
patchedMethod[key] = method[key];
|
|
|
|
|
});
|
2019-08-06 18:26:04 -04:00
|
|
|
|
|
2020-01-24 12:20:19 -05:00
|
|
|
|
return patchedMethod;
|
2019-08-06 18:26:04 -04:00
|
|
|
|
}
|