Gist.js

  1. /**
  2. * @file
  3. * @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.
  4. * @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.
  5. * Github.js is freely distributable.
  6. */
  7. import Requestable from './Requestable';
  8. /**
  9. * A Gist can retrieve and modify gists.
  10. */
  11. class Gist extends Requestable {
  12. /**
  13. * Create a Gist.
  14. * @param {string} id - the id of the gist (not required when creating a gist)
  15. * @param {Requestable.auth} [auth] - information required to authenticate to Github
  16. * @param {string} [apiBase=https://api.github.com] - the base Github API URL
  17. */
  18. constructor(id, auth, apiBase) {
  19. super(auth, apiBase);
  20. this.__id = id;
  21. }
  22. /**
  23. * Fetch a gist.
  24. * @see https://developer.github.com/v3/gists/#get-a-single-gist
  25. * @param {Requestable.callback} [cb] - will receive the gist
  26. * @return {Promise} - the Promise for the http request
  27. */
  28. read(cb) {
  29. return this._request('GET', `/gists/${this.__id}`, null, cb);
  30. }
  31. /**
  32. * Create a new gist.
  33. * @see https://developer.github.com/v3/gists/#create-a-gist
  34. * @param {Object} gist - the data for the new gist
  35. * @param {Requestable.callback} [cb] - will receive the new gist upon creation
  36. * @return {Promise} - the Promise for the http request
  37. */
  38. create(gist, cb) {
  39. return this._request('POST', '/gists', gist, cb)
  40. .then((response) => {
  41. this.__id = response.data.id;
  42. return response;
  43. });
  44. }
  45. /**
  46. * Delete a gist.
  47. * @see https://developer.github.com/v3/gists/#delete-a-gist
  48. * @param {Requestable.callback} [cb] - will receive true if the request succeeds
  49. * @return {Promise} - the Promise for the http request
  50. */
  51. delete(cb) {
  52. return this._request('DELETE', `/gists/${this.__id}`, null, cb);
  53. }
  54. /**
  55. * Fork a gist.
  56. * @see https://developer.github.com/v3/gists/#fork-a-gist
  57. * @param {Requestable.callback} [cb] - the function that will receive the gist
  58. * @return {Promise} - the Promise for the http request
  59. */
  60. fork(cb) {
  61. return this._request('POST', `/gists/${this.__id}/forks`, null, cb);
  62. }
  63. /**
  64. * Update a gist.
  65. * @see https://developer.github.com/v3/gists/#edit-a-gist
  66. * @param {Object} gist - the new data for the gist
  67. * @param {Requestable.callback} [cb] - the function that receives the API result
  68. * @return {Promise} - the Promise for the http request
  69. */
  70. update(gist, cb) {
  71. return this._request('PATCH', `/gists/${this.__id}`, gist, cb);
  72. }
  73. /**
  74. * Star a gist.
  75. * @see https://developer.github.com/v3/gists/#star-a-gist
  76. * @param {Requestable.callback} [cb] - will receive true if the request is successful
  77. * @return {Promise} - the Promise for the http request
  78. */
  79. star(cb) {
  80. return this._request('PUT', `/gists/${this.__id}/star`, null, cb);
  81. }
  82. /**
  83. * Unstar a gist.
  84. * @see https://developer.github.com/v3/gists/#unstar-a-gist
  85. * @param {Requestable.callback} [cb] - will receive true if the request is successful
  86. * @return {Promise} - the Promise for the http request
  87. */
  88. unstar(cb) {
  89. return this._request('DELETE', `/gists/${this.__id}/star`, null, cb);
  90. }
  91. /**
  92. * Check if a gist is starred by the user.
  93. * @see https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
  94. * @param {Requestable.callback} [cb] - will receive true if the gist is starred and false if the gist is not starred
  95. * @return {Promise} - the Promise for the http request
  96. */
  97. isStarred(cb) {
  98. return this._request204or404(`/gists/${this.__id}/star`, null, cb);
  99. }
  100. }
  101. module.exports = Gist;