Source: managers/shared-items.ts

  1. /**
  2. * @fileoverview Manager for the Shared Items
  3. */
  4. // ------------------------------------------------------------------------------
  5. // Requirements
  6. // ------------------------------------------------------------------------------
  7. import httpStatusCodes from 'http-status';
  8. import BoxClient from '../box-client';
  9. import errors from '../util/errors';
  10. // ------------------------------------------------------------------------------
  11. // Private
  12. // ------------------------------------------------------------------------------
  13. const BASE_PATH = '/shared_items';
  14. // ------------------------------------------------------------------------------
  15. // Public
  16. // ------------------------------------------------------------------------------
  17. /**
  18. * Simple manager for interacting with all 'Shared Item' endpoints and actions.
  19. *
  20. * @constructor
  21. * @param {BoxClient} client - The Box API Client that is responsible for making calls to the API
  22. * @returns {void}
  23. */
  24. class SharedItems {
  25. client: BoxClient;
  26. constructor(client: BoxClient) {
  27. this.client = client;
  28. }
  29. /**
  30. * Requests a Box item associated with a shared link.
  31. *
  32. * API Endpoint: '/shared_items'
  33. * Method: GET
  34. *
  35. * @param {string} url - Shared Link URL
  36. * @param {string} [password] - Shared Link Password (null if no password)
  37. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  38. * @param {Function} [callback] - passed the shared item if it was successfully acquired
  39. * @returns {Promise<Object>} A promise resolving to the shared item object
  40. */
  41. get(
  42. url: string,
  43. password: string,
  44. options?: Record<string, any>,
  45. callback?: Function
  46. ) {
  47. var params = {
  48. qs: options,
  49. headers: {
  50. BoxApi: this.client.buildSharedItemAuthHeader(url, password),
  51. },
  52. };
  53. // Handle the Special API Response
  54. return this.client
  55. .get(BASE_PATH, params)
  56. .then((response: any /* FIXME */) => {
  57. switch (response.statusCode) {
  58. // 200 - Shared Item Recieved
  59. case httpStatusCodes.OK:
  60. return response.body;
  61. // 403 - Incorrect or missing password
  62. // Propagate an error explaining that the password is either missing or incorrect
  63. case httpStatusCodes.FORBIDDEN:
  64. var errMessage = password
  65. ? 'Incorrect shared link password'
  66. : 'Shared link password missing';
  67. throw errors.buildResponseError(response, errMessage);
  68. // Unexpected Response
  69. default:
  70. throw errors.buildUnexpectedResponseError(response);
  71. }
  72. })
  73. .asCallback(callback);
  74. }
  75. }
  76. /**
  77. * @module box-node-sdk/lib/managers/shared-items
  78. * @see {@Link SharedItems}
  79. */
  80. export = SharedItems;