@algolia/ingestion 1.28.0 → 1.30.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.
@@ -1,6 +1,6 @@
1
1
  // src/ingestionClient.ts
2
- import { createAuth, createTransporter, getAlgoliaAgent } from "@algolia/client-common";
3
- var apiClientVersion = "1.28.0";
2
+ import { createAuth, createIterablePromise, createTransporter, getAlgoliaAgent } from "@algolia/client-common";
3
+ var apiClientVersion = "1.30.0";
4
4
  var REGIONS = ["eu", "us"];
5
5
  function getDefaultHosts(region) {
6
6
  const url = "data.{region}.algolia.com".replace("{region}", region);
@@ -86,6 +86,69 @@ function createIngestionClient({
86
86
  transporter.baseQueryParameters["x-algolia-api-key"] = apiKey;
87
87
  }
88
88
  },
89
+ /**
90
+ * Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `push` requests by leveraging the Transformation pipeline setup in the Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/).
91
+ *
92
+ * @summary Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
93
+ * @param chunkedPush - The `chunkedPush` object.
94
+ * @param chunkedPush.indexName - The `indexName` to replace `objects` in.
95
+ * @param chunkedPush.objects - The array of `objects` to store in the given Algolia `indexName`.
96
+ * @param chunkedPush.action - The `batch` `action` to perform on the given array of `objects`, defaults to `addObject`.
97
+ * @param chunkedPush.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
98
+ * @param chunkedPush.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
99
+ * @param chunkedPush.referenceIndexName - This is required when targeting an index that does not have a push connector setup (e.g. a tmp index), but you wish to attach another index's transformation to it (e.g. the source index name).
100
+ * @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getEvent` method and merged with the transporter requestOptions.
101
+ */
102
+ async chunkedPush({
103
+ indexName,
104
+ objects,
105
+ action = "addObject",
106
+ waitForTasks,
107
+ batchSize = 1e3,
108
+ referenceIndexName
109
+ }, requestOptions) {
110
+ let records = [];
111
+ const responses = [];
112
+ const objectEntries = objects.entries();
113
+ for (const [i, obj] of objectEntries) {
114
+ records.push(obj);
115
+ if (records.length === batchSize || i === objects.length - 1) {
116
+ responses.push(
117
+ await this.push({ indexName, pushTaskPayload: { action, records }, referenceIndexName }, requestOptions)
118
+ );
119
+ records = [];
120
+ }
121
+ }
122
+ let retryCount = 0;
123
+ if (waitForTasks) {
124
+ for (const resp of responses) {
125
+ if (!resp.eventID) {
126
+ throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
127
+ }
128
+ await createIterablePromise({
129
+ func: async () => {
130
+ if (resp.eventID === void 0 || !resp.eventID) {
131
+ throw new Error("received unexpected response from the push endpoint, eventID must not be undefined");
132
+ }
133
+ return this.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error) => {
134
+ if (error.status === 404) {
135
+ return void 0;
136
+ }
137
+ throw error;
138
+ });
139
+ },
140
+ validate: (response) => response !== void 0,
141
+ aggregator: () => retryCount += 1,
142
+ error: {
143
+ validate: () => retryCount >= 50,
144
+ message: () => `The maximum number of retries exceeded. (${retryCount}/${50})`
145
+ },
146
+ timeout: () => Math.min(retryCount * 500, 5e3)
147
+ });
148
+ }
149
+ }
150
+ return responses;
151
+ },
89
152
  /**
90
153
  * Creates a new authentication resource.
91
154
  *
@@ -280,7 +343,7 @@ function createIngestionClient({
280
343
  /**
281
344
  * This method lets you send requests to the Algolia REST API.
282
345
  * @param customDelete - The customDelete object.
283
- * @param customDelete.path - Path of the endpoint, anything after \"/1\" must be specified.
346
+ * @param customDelete.path - Path of the endpoint, for example `1/newFeature`.
284
347
  * @param customDelete.parameters - Query parameters to apply to the current query.
285
348
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
286
349
  */
@@ -302,7 +365,7 @@ function createIngestionClient({
302
365
  /**
303
366
  * This method lets you send requests to the Algolia REST API.
304
367
  * @param customGet - The customGet object.
305
- * @param customGet.path - Path of the endpoint, anything after \"/1\" must be specified.
368
+ * @param customGet.path - Path of the endpoint, for example `1/newFeature`.
306
369
  * @param customGet.parameters - Query parameters to apply to the current query.
307
370
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
308
371
  */
@@ -324,7 +387,7 @@ function createIngestionClient({
324
387
  /**
325
388
  * This method lets you send requests to the Algolia REST API.
326
389
  * @param customPost - The customPost object.
327
- * @param customPost.path - Path of the endpoint, anything after \"/1\" must be specified.
390
+ * @param customPost.path - Path of the endpoint, for example `1/newFeature`.
328
391
  * @param customPost.parameters - Query parameters to apply to the current query.
329
392
  * @param customPost.body - Parameters to send with the custom request.
330
393
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
@@ -348,7 +411,7 @@ function createIngestionClient({
348
411
  /**
349
412
  * This method lets you send requests to the Algolia REST API.
350
413
  * @param customPut - The customPut object.
351
- * @param customPut.path - Path of the endpoint, anything after \"/1\" must be specified.
414
+ * @param customPut.path - Path of the endpoint, for example `1/newFeature`.
352
415
  * @param customPut.parameters - Query parameters to apply to the current query.
353
416
  * @param customPut.body - Parameters to send with the custom request.
354
417
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
@@ -1334,7 +1397,7 @@ function createIngestionClient({
1334
1397
  connect: 18e4,
1335
1398
  read: 18e4,
1336
1399
  write: 18e4,
1337
- ...requestOptions == null ? void 0 : requestOptions.timeouts
1400
+ ...requestOptions?.timeouts
1338
1401
  }
1339
1402
  };
1340
1403
  return transporter.request(request, requestOptions);
@@ -1383,7 +1446,7 @@ function createIngestionClient({
1383
1446
  connect: 18e4,
1384
1447
  read: 18e4,
1385
1448
  write: 18e4,
1386
- ...requestOptions == null ? void 0 : requestOptions.timeouts
1449
+ ...requestOptions?.timeouts
1387
1450
  }
1388
1451
  };
1389
1452
  return transporter.request(request, requestOptions);
@@ -1679,7 +1742,7 @@ function createIngestionClient({
1679
1742
  connect: 18e4,
1680
1743
  read: 18e4,
1681
1744
  write: 18e4,
1682
- ...requestOptions == null ? void 0 : requestOptions.timeouts
1745
+ ...requestOptions?.timeouts
1683
1746
  }
1684
1747
  };
1685
1748
  return transporter.request(request, requestOptions);
@@ -1963,7 +2026,7 @@ function createIngestionClient({
1963
2026
  connect: 18e4,
1964
2027
  read: 18e4,
1965
2028
  write: 18e4,
1966
- ...requestOptions == null ? void 0 : requestOptions.timeouts
2029
+ ...requestOptions?.timeouts
1967
2030
  }
1968
2031
  };
1969
2032
  return transporter.request(request, requestOptions);
@@ -2002,7 +2065,7 @@ function createIngestionClient({
2002
2065
  connect: 18e4,
2003
2066
  read: 18e4,
2004
2067
  write: 18e4,
2005
- ...requestOptions == null ? void 0 : requestOptions.timeouts
2068
+ ...requestOptions?.timeouts
2006
2069
  }
2007
2070
  };
2008
2071
  return transporter.request(request, requestOptions);