Source: posts.js

  1. var request = require('request');
  2. /** @namespace */
  3. var posts = {};
  4. /**
  5. * Callback for all APIs.
  6. * @callback done
  7. * @param {Error} err - Error. If the call was successful, this will be null
  8. * @param {String} res - Raw response from Product Hunt
  9. */
  10. module.exports = function(client) {
  11. /**
  12. * @summary Get the tech posts of today
  13. * @memberof posts
  14. * @param {Object} [options] - options
  15. * @param {String} [options.category_name] - Name of the category
  16. * @param {Number} [options.params.days_ago] - Parameter for pagination
  17. * @param {String} [options.params.day] - Alternate parameter for requesting specific days (Format: day=YYYY-MM-DD)
  18. * @param {done} done - Callback
  19. *
  20. * @see https://api.producthunt.com/v1/docs/posts/posts_index_get_the_tech_posts_of_today
  21. */
  22. posts.index = function (options, done) {
  23. if (typeof options === 'function') {
  24. done = options;
  25. options = {
  26. params: {}
  27. };
  28. }
  29. var path;
  30. if (options.category_name) {
  31. path = `/categories/${options.category_name}/posts`;
  32. } else {
  33. path = '/posts';
  34. }
  35. client.httpGet(path, options.params, done);
  36. };
  37. /**
  38. * @summary Get all the newest posts
  39. * @memberof posts
  40. * @param {Object} [options] - options
  41. * @param {String} [options.params.search.url] - Filter parameter: can filter posts by url
  42. * @param {Number} [options.params.search.category] - Filter parameter: can filter posts by category. Default = unspecified (All categories)
  43. * @param {String} [options.params.older] - Filter parameter: get only records older than the provided id
  44. * @param {String} [options.params.newer] - Filter parameter: get only records newer than the provided id
  45. * @param {String} [options.params.per_page] - Filter parameter: define the amount of records sent per call (max 50)
  46. * @param {done} done - Callback
  47. *
  48. * @see https://api.producthunt.com/v1/docs/posts/posts_all_get_all_the_newest_posts
  49. */
  50. posts.all = function (options, done) {
  51. if (typeof options === 'function') {
  52. done = options;
  53. options = {};
  54. }
  55. var path;
  56. if (options.user) {
  57. path = `/users/${options.user}/${options.type}`;
  58. } else {
  59. path = '/posts/all';
  60. }
  61. client.httpGet(path, options.params, done);
  62. };
  63. /**
  64. * @summary Get details of a post
  65. * @memberof posts
  66. * @param {Object} options - options
  67. * @param {Number} options.id - The numeric ID of the Post you want to fetch
  68. * @param {done} done - Callback
  69. *
  70. * @see https://api.producthunt.com/v1/docs/posts/posts_show_get_details_of_a_post
  71. */
  72. posts.show = function (options, done) {
  73. if (typeof options === 'function') {
  74. done = options;
  75. options = {};
  76. }
  77. client.httpGet(`/posts/${options.id}`, {}, done);
  78. };
  79. /**
  80. * @summary Create a post
  81. * @memberof posts
  82. * @param {Object} options - options
  83. * @param {String} options.url - The url of the product
  84. * @param {String} options.name - The name of the product
  85. * @param {String} options.tagline - Your short description of the product
  86. * @param {done} done - Callback
  87. *
  88. * @see https://api.producthunt.com/v1/docs/posts/posts_create_create_a_post
  89. */
  90. posts.create = function (options, done) {
  91. var opts = {
  92. body: {
  93. post: {
  94. url: options.url,
  95. name: options.name,
  96. tagline: options.tagline
  97. }
  98. }
  99. };
  100. client.httpPost('/posts', opts, done);
  101. };
  102. /**
  103. * @summary Create a post
  104. * @memberof posts
  105. * @param {Object} options - options
  106. * @param {String} options.collection_id - The numeric ID of the collection
  107. * @param {String} options.type - Type of action. Valid options are 'add' or 'remove'.
  108. * @param {done} done - Callback
  109. *
  110. * @see https://api.producthunt.com/v1/docs/posts/posts_collect_add_a_post_to_a_collection
  111. */
  112. posts.collect = function (options, done) {
  113. var opts = {
  114. body: {
  115. collection_id: options.collection_id
  116. }
  117. };
  118. var path = `/posts/${options.post_id}/collect`;
  119. if (options.type === 'add') {
  120. client.httpPost(path, opts, done);
  121. } else if (options.type === 'remove') {
  122. client.httpDelete(path, opts, done);
  123. }
  124. };
  125. return posts;
  126. };