client-account.cjs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var clientCommon = require('@algolia/client-common');
  4. var clientSearch = require('@algolia/client-search');
  5. function createDestinationIndiceExistsError() {
  6. return {
  7. name: 'DestinationIndiceAlreadyExistsError',
  8. message: 'Destination indice already exists.',
  9. };
  10. }
  11. function createIndicesInSameAppError(appId) {
  12. return {
  13. name: 'IndicesInTheSameAppError',
  14. message: 'Indices are in the same application. Use SearchClient.copyIndex instead.',
  15. appId,
  16. };
  17. }
  18. const accountCopyIndex = (source, destination, requestOptions) => {
  19. // eslint-disable-next-line functional/prefer-readonly-type
  20. const responses = [];
  21. const promise = clientSearch.exists(destination)()
  22. .then(res => {
  23. if (source.appId === destination.appId) {
  24. throw createIndicesInSameAppError(source.appId);
  25. }
  26. if (res) {
  27. throw createDestinationIndiceExistsError();
  28. }
  29. })
  30. .then(() => clientSearch.getSettings(source)())
  31. .then(settings =>
  32. // eslint-disable-next-line functional/immutable-data
  33. responses.push(clientSearch.setSettings(destination)(settings, requestOptions)))
  34. .then(() => clientSearch.browseRules(source)({
  35. // eslint-disable-next-line functional/immutable-data
  36. batch: rules => responses.push(clientSearch.saveRules(destination)(rules, requestOptions)),
  37. }))
  38. .then(() => clientSearch.browseSynonyms(source)({
  39. // eslint-disable-next-line functional/immutable-data
  40. batch: synonyms => responses.push(clientSearch.saveSynonyms(destination)(synonyms, requestOptions)),
  41. }))
  42. .then(() => clientSearch.browseObjects(source)({
  43. // eslint-disable-next-line functional/immutable-data
  44. batch: objects => responses.push(clientSearch.saveObjects(destination)(objects, requestOptions)),
  45. }));
  46. return clientCommon.createWaitablePromise(
  47. /**
  48. * The original promise will return an array of async responses, now
  49. * we need to resolve that array of async responses using a
  50. * `Promise.all`, and then resolve `void` for the end-user.
  51. */
  52. promise.then(() => Promise.all(responses)).then(() => undefined),
  53. /**
  54. * Next, if the end-user calls the `wait` method, we need to also call
  55. * the `wait` method on each element of of async responses.
  56. */
  57. (_response, waitRequestOptions) => {
  58. return Promise.all(responses.map(response => response.wait(waitRequestOptions)));
  59. });
  60. };
  61. exports.accountCopyIndex = accountCopyIndex;
  62. exports.createDestinationIndiceExistsError = createDestinationIndiceExistsError;
  63. exports.createIndicesInSameAppError = createIndicesInSameAppError;