generate.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright 2015-2016 PayPal, Inc. */
  2. "use strict";
  3. var api = require('./api');
  4. /**
  5. * Attach REST operations from restFunctions as required by a PayPal API
  6. * resource e.g. create, get and list are attahed for Payment resource
  7. * @param {Object} destObject A PayPal resource e.g. Invoice
  8. * @param {Array} operations Rest operations that the destObject will allow e.g. get
  9. * @return {Object}
  10. */
  11. function mixin(destObject, operations) {
  12. operations.forEach(function (property) {
  13. destObject[property] = restFunctions[property];
  14. });
  15. return destObject;
  16. }
  17. /**
  18. * restFunctions Object containing the REST CRUD methods and paypal specific REST methods that
  19. * are shared between at least two of the REST endpoints, otherwise the function
  20. * will be defined within the resource definition itself
  21. * @type {Object}
  22. */
  23. var restFunctions = {
  24. create: function create(data, config, cb) {
  25. api.executeHttp('POST', this.baseURL, data, config, cb);
  26. },
  27. get: function get(id, config, cb) {
  28. api.executeHttp('GET', this.baseURL + id, {}, config, cb);
  29. },
  30. list: function list(data, config, cb) {
  31. if (typeof data === 'function') {
  32. config = data;
  33. data = {};
  34. }
  35. api.executeHttp('GET', this.baseURL, data, config, cb);
  36. },
  37. del: function del(id, config, cb) {
  38. api.executeHttp('DELETE', this.baseURL + id, {}, config, cb);
  39. },
  40. //provided for compatibility with 0.* versions
  41. delete: function del(id, config, cb) {
  42. api.executeHttp('DELETE', this.baseURL + id, {}, config, cb);
  43. },
  44. capture: function capture(id, data, config, cb) {
  45. api.executeHttp('POST', this.baseURL + id + '/capture', data, config, cb);
  46. },
  47. refund: function refund(id, data, config, cb) {
  48. api.executeHttp('POST', this.baseURL + id + '/refund', data, config, cb);
  49. },
  50. update: function update(id, data, config, cb) {
  51. api.executeHttp('PATCH', this.baseURL + id, data, config, cb);
  52. },
  53. cancel: function cancel(id, data, config, cb) {
  54. api.executeHttp('POST', this.baseURL + id + '/cancel', data, config, cb);
  55. }
  56. };
  57. module.exports.mixin = mixin;