@algolia/client-search 5.56.0 → 5.57.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,8 @@ import {
8
8
  createMemoryCache,
9
9
  createNullCache,
10
10
  createNullLogger,
11
- serializeQueryParameters
11
+ serializeQueryParameters,
12
+ withRequestId as withRequestId2
12
13
  } from "@algolia/client-common";
13
14
  import { createHttpRequester } from "@algolia/requester-node-http";
14
15
 
@@ -20,10 +21,12 @@ import {
20
21
  createIterablePromise,
21
22
  createTransporter,
22
23
  getAlgoliaAgent,
24
+ logWarning,
23
25
  shuffle,
24
- validateRequired
26
+ validateRequired,
27
+ withRequestId
25
28
  } from "@algolia/client-common";
26
- var apiClientVersion = "5.56.0";
29
+ var apiClientVersion = "5.57.0";
27
30
  function getDefaultHosts(appId) {
28
31
  return [
29
32
  {
@@ -143,6 +146,7 @@ function createSearchClient({
143
146
  maxRetries = 100,
144
147
  timeout = (retryCount) => Math.min(retryCount * 200, 5e3)
145
148
  }, requestOptions) {
149
+ requestOptions = withRequestId(transporter, requestOptions);
146
150
  let retryCount = 0;
147
151
  return createIterablePromise({
148
152
  func: () => this.getTask({ indexName, taskID }, requestOptions),
@@ -170,6 +174,7 @@ function createSearchClient({
170
174
  maxRetries = 100,
171
175
  timeout = (retryCount) => Math.min(retryCount * 200, 5e3)
172
176
  }, requestOptions) {
177
+ requestOptions = withRequestId(transporter, requestOptions);
173
178
  let retryCount = 0;
174
179
  return createIterablePromise({
175
180
  func: () => this.getAppTask({ taskID }, requestOptions),
@@ -201,6 +206,7 @@ function createSearchClient({
201
206
  maxRetries = 100,
202
207
  timeout = (retryCount) => Math.min(retryCount * 200, 5e3)
203
208
  }, requestOptions) {
209
+ requestOptions = withRequestId(transporter, requestOptions);
204
210
  let retryCount = 0;
205
211
  const baseIteratorOptions = {
206
212
  aggregator: () => retryCount += 1,
@@ -256,6 +262,7 @@ function createSearchClient({
256
262
  * @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `browse` method and merged with the transporter requestOptions.
257
263
  */
258
264
  browseObjects({ indexName, browseParams, ...browseObjectsOptions }, requestOptions) {
265
+ requestOptions = withRequestId(transporter, requestOptions);
259
266
  return createIterablePromise({
260
267
  func: (previousResponse) => {
261
268
  return this.browse(
@@ -286,6 +293,7 @@ function createSearchClient({
286
293
  * @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `searchRules` method and merged with the transporter requestOptions.
287
294
  */
288
295
  browseRules({ indexName, searchRulesParams, ...browseRulesOptions }, requestOptions) {
296
+ requestOptions = withRequestId(transporter, requestOptions);
289
297
  const params = {
290
298
  ...searchRulesParams,
291
299
  hitsPerPage: (searchRulesParams == null ? void 0 : searchRulesParams.hitsPerPage) || 1e3
@@ -323,6 +331,7 @@ function createSearchClient({
323
331
  searchSynonymsParams,
324
332
  ...browseSynonymsOptions
325
333
  }, requestOptions) {
334
+ requestOptions = withRequestId(transporter, requestOptions);
326
335
  const params = {
327
336
  ...searchSynonymsParams,
328
337
  page: (searchSynonymsParams == null ? void 0 : searchSynonymsParams.page) || 0,
@@ -368,6 +377,7 @@ function createSearchClient({
368
377
  batchSize = 1e3,
369
378
  maxRetries = 100
370
379
  }, requestOptions) {
380
+ requestOptions = withRequestId(transporter, requestOptions);
371
381
  let requests = [];
372
382
  const responses = [];
373
383
  const objectEntries = objects.entries();
@@ -380,7 +390,7 @@ function createSearchClient({
380
390
  }
381
391
  if (waitForTasks) {
382
392
  for (const resp of responses) {
383
- await this.waitForTask({ indexName, taskID: resp.taskID, maxRetries });
393
+ await this.waitForTask({ indexName, taskID: resp.taskID, maxRetries }, requestOptions);
384
394
  }
385
395
  }
386
396
  return responses;
@@ -458,6 +468,8 @@ function createSearchClient({
458
468
  * Helper: Replaces all objects (records) in the given `index_name` with the given `objects`. A temporary index is created during this process in order to backup your data.
459
469
  * See https://api-clients-automation.netlify.app/docs/custom-helpers/#replaceallobjects for implementation details.
460
470
  *
471
+ * Warning: calling this method with an empty `objects` list replaces the index with an empty one, deleting all existing records.
472
+ *
461
473
  * @summary Helper: Replaces all objects (records) in the given `index_name` with the given `objects`. A temporary index is created during this process in order to backup your data.
462
474
  * @param replaceAllObjects - The `replaceAllObjects` object.
463
475
  * @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
@@ -474,6 +486,13 @@ function createSearchClient({
474
486
  scopes,
475
487
  maxRetries = DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES
476
488
  }, requestOptions) {
489
+ requestOptions = withRequestId(transporter, requestOptions);
490
+ if (objects.length === 0) {
491
+ logWarning(
492
+ transporter.logger,
493
+ `replaceAllObjects was called with an empty list of objects, which will delete all records currently in the "${indexName}" index.`
494
+ );
495
+ }
477
496
  const randomSuffix = Math.floor(Math.random() * 1e6) + 1e5;
478
497
  const tmpIndexName = `${indexName}_tmp_${randomSuffix}`;
479
498
  if (scopes === void 0) {
@@ -495,11 +514,14 @@ function createSearchClient({
495
514
  { indexName: tmpIndexName, objects, waitForTasks: true, batchSize, maxRetries },
496
515
  requestOptions
497
516
  );
498
- await this.waitForTask({
499
- indexName: tmpIndexName,
500
- taskID: copyOperationResponse.taskID,
501
- maxRetries
502
- });
517
+ await this.waitForTask(
518
+ {
519
+ indexName: tmpIndexName,
520
+ taskID: copyOperationResponse.taskID,
521
+ maxRetries
522
+ },
523
+ requestOptions
524
+ );
503
525
  copyOperationResponse = await this.operationIndex(
504
526
  {
505
527
  indexName,
@@ -511,11 +533,14 @@ function createSearchClient({
511
533
  },
512
534
  requestOptions
513
535
  );
514
- await this.waitForTask({
515
- indexName: tmpIndexName,
516
- taskID: copyOperationResponse.taskID,
517
- maxRetries
518
- });
536
+ await this.waitForTask(
537
+ {
538
+ indexName: tmpIndexName,
539
+ taskID: copyOperationResponse.taskID,
540
+ maxRetries
541
+ },
542
+ requestOptions
543
+ );
519
544
  const moveOperationResponse = await this.operationIndex(
520
545
  {
521
546
  indexName: tmpIndexName,
@@ -523,20 +548,23 @@ function createSearchClient({
523
548
  },
524
549
  requestOptions
525
550
  );
526
- await this.waitForTask({
527
- indexName: tmpIndexName,
528
- taskID: moveOperationResponse.taskID,
529
- maxRetries
530
- });
551
+ await this.waitForTask(
552
+ {
553
+ indexName: tmpIndexName,
554
+ taskID: moveOperationResponse.taskID,
555
+ maxRetries
556
+ },
557
+ requestOptions
558
+ );
531
559
  return { copyOperationResponse, batchResponses, moveOperationResponse };
532
560
  } catch (error) {
533
- await this.deleteIndex({ indexName: tmpIndexName });
561
+ await this.deleteIndex({ indexName: tmpIndexName }, requestOptions);
534
562
  throw error;
535
563
  }
536
564
  },
537
- async indexExists({ indexName }) {
565
+ async indexExists({ indexName }, requestOptions) {
538
566
  try {
539
- await this.getSettings({ indexName });
567
+ await this.getSettings({ indexName }, requestOptions);
540
568
  } catch (error) {
541
569
  if (error instanceof ApiError && error.status === 404) {
542
570
  return false;
@@ -4020,6 +4048,7 @@ function searchClient(appId, apiKey, options) {
4020
4048
  logger: createNullLogger(),
4021
4049
  requester: createHttpRequester(),
4022
4050
  algoliaAgents: [{ segment: "Node.js", version: process.versions.node }],
4051
+ requestIdChannel: "headers",
4023
4052
  responsesCache: createNullCache(),
4024
4053
  requestsCache: createNullCache(),
4025
4054
  hostsCache: createMemoryCache(),
@@ -4077,11 +4106,12 @@ function searchClient(appId, apiKey, options) {
4077
4106
  batchSize,
4078
4107
  maxRetries = 100
4079
4108
  }, requestOptions) {
4109
+ requestOptions = withRequestId2(this.transporter, requestOptions);
4080
4110
  const responses = [];
4081
4111
  if (this.appId === destinationAppID) {
4082
4112
  throw new IndicesInSameAppError();
4083
4113
  }
4084
- if (!await this.indexExists({ indexName: sourceIndexName })) {
4114
+ if (!await this.indexExists({ indexName: sourceIndexName }, requestOptions)) {
4085
4115
  throw new IndexNotFoundError(sourceIndexName);
4086
4116
  }
4087
4117
  const destinationClient = createSearchClient({
@@ -4100,54 +4130,66 @@ function searchClient(appId, apiKey, options) {
4100
4130
  hostsCache: createMemoryCache(),
4101
4131
  ...options
4102
4132
  });
4103
- if (await destinationClient.indexExists({ indexName: destinationIndexName })) {
4133
+ if (await destinationClient.indexExists({ indexName: destinationIndexName }, requestOptions)) {
4104
4134
  throw new IndexAlreadyExistsError(destinationIndexName);
4105
4135
  }
4106
4136
  responses.push(
4107
4137
  await destinationClient.setSettings(
4108
4138
  {
4109
4139
  indexName: destinationIndexName,
4110
- indexSettings: await this.getSettings({ indexName: sourceIndexName })
4140
+ indexSettings: await this.getSettings({ indexName: sourceIndexName }, requestOptions)
4111
4141
  },
4112
4142
  requestOptions
4113
4143
  )
4114
4144
  );
4115
- await this.browseRules({
4116
- indexName: sourceIndexName,
4117
- async aggregator(response) {
4118
- responses.push(
4119
- await destinationClient.saveRules(
4120
- { indexName: destinationIndexName, rules: response.hits },
4121
- requestOptions
4122
- )
4123
- );
4124
- }
4125
- });
4126
- await this.browseSynonyms({
4127
- indexName: sourceIndexName,
4128
- async aggregator(response) {
4129
- responses.push(
4130
- await destinationClient.saveSynonyms(
4131
- { indexName: destinationIndexName, synonymHit: response.hits },
4132
- requestOptions
4133
- )
4134
- );
4135
- }
4136
- });
4137
- await this.browseObjects({
4138
- indexName: sourceIndexName,
4139
- browseParams: batchSize ? { hitsPerPage: batchSize } : void 0,
4140
- async aggregator(response) {
4141
- responses.push(
4142
- ...await destinationClient.saveObjects(
4143
- { indexName: destinationIndexName, objects: response.hits, batchSize },
4144
- requestOptions
4145
- )
4146
- );
4147
- }
4148
- });
4145
+ await this.browseRules(
4146
+ {
4147
+ indexName: sourceIndexName,
4148
+ async aggregator(response) {
4149
+ responses.push(
4150
+ await destinationClient.saveRules(
4151
+ { indexName: destinationIndexName, rules: response.hits },
4152
+ requestOptions
4153
+ )
4154
+ );
4155
+ }
4156
+ },
4157
+ requestOptions
4158
+ );
4159
+ await this.browseSynonyms(
4160
+ {
4161
+ indexName: sourceIndexName,
4162
+ async aggregator(response) {
4163
+ responses.push(
4164
+ await destinationClient.saveSynonyms(
4165
+ { indexName: destinationIndexName, synonymHit: response.hits },
4166
+ requestOptions
4167
+ )
4168
+ );
4169
+ }
4170
+ },
4171
+ requestOptions
4172
+ );
4173
+ await this.browseObjects(
4174
+ {
4175
+ indexName: sourceIndexName,
4176
+ browseParams: batchSize ? { hitsPerPage: batchSize } : void 0,
4177
+ async aggregator(response) {
4178
+ responses.push(
4179
+ ...await destinationClient.saveObjects(
4180
+ { indexName: destinationIndexName, objects: response.hits, batchSize },
4181
+ requestOptions
4182
+ )
4183
+ );
4184
+ }
4185
+ },
4186
+ requestOptions
4187
+ );
4149
4188
  for (const response of responses) {
4150
- await destinationClient.waitForTask({ indexName: destinationIndexName, taskID: response.taskID, maxRetries });
4189
+ await destinationClient.waitForTask(
4190
+ { indexName: destinationIndexName, taskID: response.taskID, maxRetries },
4191
+ requestOptions
4192
+ );
4151
4193
  }
4152
4194
  },
4153
4195
  /**