LegacyUnifiedSearchService.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import { generateOcsUrl } from '@nextcloud/router'
  6. import { loadState } from '@nextcloud/initial-state'
  7. import axios from '@nextcloud/axios'
  8. export const defaultLimit = loadState('unified-search', 'limit-default')
  9. export const minSearchLength = loadState('unified-search', 'min-search-length', 1)
  10. export const enableLiveSearch = loadState('unified-search', 'live-search', true)
  11. export const regexFilterIn = /(^|\s)in:([a-z_-]+)/ig
  12. export const regexFilterNot = /(^|\s)-in:([a-z_-]+)/ig
  13. /**
  14. * Create a cancel token
  15. *
  16. * @return {import('axios').CancelTokenSource}
  17. */
  18. const createCancelToken = () => axios.CancelToken.source()
  19. /**
  20. * Get the list of available search providers
  21. *
  22. * @return {Promise<Array>}
  23. */
  24. export async function getTypes() {
  25. try {
  26. const { data } = await axios.get(generateOcsUrl('search/providers'), {
  27. params: {
  28. // Sending which location we're currently at
  29. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  30. },
  31. })
  32. if ('ocs' in data && 'data' in data.ocs && Array.isArray(data.ocs.data) && data.ocs.data.length > 0) {
  33. // Providers are sorted by the api based on their order key
  34. return data.ocs.data
  35. }
  36. } catch (error) {
  37. console.error(error)
  38. }
  39. return []
  40. }
  41. /**
  42. * Get the list of available search providers
  43. *
  44. * @param {object} options destructuring object
  45. * @param {string} options.type the type to search
  46. * @param {string} options.query the search
  47. * @param {number|string|undefined} options.cursor the offset for paginated searches
  48. * @return {object} {request: Promise, cancel: Promise}
  49. */
  50. export function search({ type, query, cursor }) {
  51. /**
  52. * Generate an axios cancel token
  53. */
  54. const cancelToken = createCancelToken()
  55. const request = async () => axios.get(generateOcsUrl('search/providers/{type}/search', { type }), {
  56. cancelToken: cancelToken.token,
  57. params: {
  58. term: query,
  59. cursor,
  60. // Sending which location we're currently at
  61. from: window.location.pathname.replace('/index.php', '') + window.location.search,
  62. },
  63. })
  64. return {
  65. request,
  66. cancel: cancelToken.cancel,
  67. }
  68. }