client-account.esm.js 2.5 KB

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