endpoints-to-methods.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. export function endpointsToMethods(octokit, endpointsMap) {
  2. const newMethods = {};
  3. for (const [scope, endpoints] of Object.entries(endpointsMap)) {
  4. for (const [methodName, endpoint] of Object.entries(endpoints)) {
  5. const [route, defaults, decorations] = endpoint;
  6. const [method, url] = route.split(/ /);
  7. const endpointDefaults = Object.assign({ method, url }, defaults);
  8. if (!newMethods[scope]) {
  9. newMethods[scope] = {};
  10. }
  11. const scopeMethods = newMethods[scope];
  12. if (decorations) {
  13. scopeMethods[methodName] = decorate(octokit, scope, methodName, endpointDefaults, decorations);
  14. continue;
  15. }
  16. scopeMethods[methodName] = octokit.request.defaults(endpointDefaults);
  17. }
  18. }
  19. return newMethods;
  20. }
  21. function decorate(octokit, scope, methodName, defaults, decorations) {
  22. const requestWithDefaults = octokit.request.defaults(defaults);
  23. /* istanbul ignore next */
  24. function withDecorations(...args) {
  25. // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
  26. let options = requestWithDefaults.endpoint.merge(...args);
  27. // There are currently no other decorations than `.mapToData`
  28. if (decorations.mapToData) {
  29. options = Object.assign({}, options, {
  30. data: options[decorations.mapToData],
  31. [decorations.mapToData]: undefined,
  32. });
  33. return requestWithDefaults(options);
  34. }
  35. if (decorations.renamed) {
  36. const [newScope, newMethodName] = decorations.renamed;
  37. octokit.log.warn(`octokit.${scope}.${methodName}() has been renamed to octokit.${newScope}.${newMethodName}()`);
  38. }
  39. if (decorations.deprecated) {
  40. octokit.log.warn(decorations.deprecated);
  41. }
  42. if (decorations.renamedParameters) {
  43. // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
  44. const options = requestWithDefaults.endpoint.merge(...args);
  45. for (const [name, alias] of Object.entries(decorations.renamedParameters)) {
  46. if (name in options) {
  47. octokit.log.warn(`"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead`);
  48. if (!(alias in options)) {
  49. options[alias] = options[name];
  50. }
  51. delete options[name];
  52. }
  53. }
  54. return requestWithDefaults(options);
  55. }
  56. // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
  57. return requestWithDefaults(...args);
  58. }
  59. return Object.assign(withDecorations, requestWithDefaults);
  60. }