Source: managers/search.ts

  1. /**
  2. * @fileoverview Manager for the Box Search Resource
  3. */
  4. // -----------------------------------------------------------------------------
  5. // Requirements
  6. // -----------------------------------------------------------------------------
  7. import { Promise } from 'bluebird';
  8. import BoxClient from '../box-client';
  9. import urlPath from '../util/url-path';
  10. // -----------------------------------------------------------------------------
  11. // Typedefs
  12. // -----------------------------------------------------------------------------
  13. /**
  14. * Search metadata filter
  15. * @typedef {Object} SearchMetadataFilter
  16. * @property {string} templateKey The template to filter against
  17. * @property {string} scope The scope of the template, e.g. 'global' or 'enterprise'
  18. * @property {Object} filters Key/value filters against individual metadata template properties
  19. */
  20. type SearchMetadataFilter = {
  21. templateKey: string;
  22. scope: string;
  23. filters: Record<string, any>;
  24. };
  25. /**
  26. * Valid search scopes
  27. * @readonly
  28. * @enum {SearchScope}
  29. */
  30. enum SearchScope {
  31. USER = 'user_content',
  32. ENTERPRISE = 'enterprise_content',
  33. }
  34. /** @typedef {string} SearchTrashContent */
  35. type SearchTrashContent = string;
  36. // -----------------------------------------------------------------------------
  37. // Private
  38. // -----------------------------------------------------------------------------
  39. var API_PATHS_SEARCH = '/search';
  40. // -----------------------------------------------------------------------------
  41. // Public
  42. // -----------------------------------------------------------------------------
  43. /**
  44. * Simple manager for interacting with the search endpoints and actions.
  45. *
  46. * @constructor
  47. * @param {BoxClient} client - The Box API Client that is responsible for making calls to the API
  48. * @returns {void}
  49. */
  50. class Search {
  51. client: BoxClient;
  52. scopes!: typeof SearchScope;
  53. constructor(client: BoxClient) {
  54. this.client = client;
  55. }
  56. /**
  57. * Searches Box for the given query.
  58. *
  59. * @param {string} searchString - The query string to use for search
  60. * @param {Object} [options] - Additional search filters. Can be left null in most cases.
  61. * @param {SearchScope} [options.scope] - The scope on which you want search. Can be user_content for a search limited to the current user or enterprise_content to search an entire enterprise
  62. * @param {string} [options.file_extensions] - Single or comma-delimited list of file extensions to filter against
  63. * @param {string} [options.created_at_range] - Date range for filtering on item creation time, e.g. '2014-05-15T13:35:01-07:00,2014-05-17T13:35:01-07:00'
  64. * @param {string} [options.updated_at_range] - Date range for filtering on item update time, e.g. '2014-05-15T13:35:01-07:00,2014-05-17T13:35:01-07:00'
  65. * @param {string} [options.size_range] - Range of item sizes (in bytes) to filter on, as lower_bound,upper_bound. Either bound can be ommitted, e.g. ',100000' for <= 100KB
  66. * @param {string} [options.owner_user_ids] - Comma-delimited list of user IDs to filter item owner against
  67. * @param {string} [options.ancestor_folder_ids] - Comma-delimited list of folder IDs, search results will contain only items in these folders (and folders within them)
  68. * @param {string} [options.content_types] - Query within specified comma-delimited fields. The types can be name, description, file_content, comments, or tags
  69. * @param {string} [options.type] - The type of objects you want to include in the search results. The type can be file, folder, or web_link
  70. * @param {string} [options.trash_content=non_trashed_only] - Controls whether to search in the trash. The value can be trashed_only or non_trashed_only
  71. * @param {SearchMetadataFilter[]} [options.mdfilters] - Searches for objects with a specific metadata object association. Searches with this parameter do not require a query string
  72. * @param {boolean} [options.include_recent_shared_links] - Determines whether to include items accessible only via shared link in the response.
  73. * @param {string} [options.fields] - Comma-delimited list of fields to be included in the response
  74. * @param {int} [options.limit=30] - The number of search results to return, max 200
  75. * @param {int} [options.offset=0] - The search result at which to start the response, must be a multiple of limit
  76. * @param {string} [options.sort] - The field on which the results should be sorted, e.g. "modified_at"
  77. * @param {string} [options.direction] - The sort direction: "ASC" for ascending and "DESC" for descending
  78. * @param {APIRequest~Callback} [callback] - passed the new comment data if it was posted successfully
  79. * @returns {Promise<Object>} A promise resolving to the collection of search results
  80. */
  81. query(
  82. searchString: string,
  83. options?: {
  84. scope?: SearchScope;
  85. file_extensions?: string;
  86. created_at_range?: string;
  87. updated_at_range?: string;
  88. size_range?: string;
  89. owner_user_ids?: string;
  90. ancestor_folder_ids?: string;
  91. content_types?: string;
  92. type?: string;
  93. trash_content?: string;
  94. mdfilters?: SearchMetadataFilter[];
  95. include_recent_shared_links?: boolean;
  96. fields?: string;
  97. limit?: number;
  98. offset?: number;
  99. sort?: string;
  100. direction?: string;
  101. },
  102. callback?: Function
  103. ) {
  104. var apiPath = urlPath(API_PATHS_SEARCH),
  105. qs = options || ({} as Record<string, any>);
  106. qs.query = searchString;
  107. if (qs.mdfilters) {
  108. if (Array.isArray(qs.mdfilters)) {
  109. qs.mdfilters = JSON.stringify(qs.mdfilters);
  110. } else {
  111. return Promise.reject(
  112. new Error('Invalid mdfilters parameter: must be a valid array')
  113. ).asCallback(callback);
  114. }
  115. }
  116. var params = {
  117. qs,
  118. };
  119. return this.client.wrapWithDefaultHandler(this.client.get)(
  120. apiPath,
  121. params,
  122. callback
  123. );
  124. }
  125. }
  126. /**
  127. * Valid search scopes
  128. * @readonly
  129. * @enum {SearchScope}
  130. */
  131. Search.prototype.scopes = SearchScope;
  132. export = Search;