@algolia/ingestion 1.27.0 → 1.29.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.27.0";
2
+ import { createAuth, createIterablePromise, createTransporter, getAlgoliaAgent } from "@algolia/client-common";
3
+ var apiClientVersion = "1.29.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
  *
@@ -265,12 +328,6 @@ function createIngestionClient({
265
328
  if (!transformationCreate.name) {
266
329
  throw new Error("Parameter `transformationCreate.name` is required when calling `createTransformation`.");
267
330
  }
268
- if (!transformationCreate.type) {
269
- throw new Error("Parameter `transformationCreate.type` is required when calling `createTransformation`.");
270
- }
271
- if (!transformationCreate.input) {
272
- throw new Error("Parameter `transformationCreate.input` is required when calling `createTransformation`.");
273
- }
274
331
  const requestPath = "/1/transformations";
275
332
  const headers = {};
276
333
  const queryParameters = {};
@@ -286,7 +343,7 @@ function createIngestionClient({
286
343
  /**
287
344
  * This method lets you send requests to the Algolia REST API.
288
345
  * @param customDelete - The customDelete object.
289
- * @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`.
290
347
  * @param customDelete.parameters - Query parameters to apply to the current query.
291
348
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
292
349
  */
@@ -308,7 +365,7 @@ function createIngestionClient({
308
365
  /**
309
366
  * This method lets you send requests to the Algolia REST API.
310
367
  * @param customGet - The customGet object.
311
- * @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`.
312
369
  * @param customGet.parameters - Query parameters to apply to the current query.
313
370
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
314
371
  */
@@ -330,7 +387,7 @@ function createIngestionClient({
330
387
  /**
331
388
  * This method lets you send requests to the Algolia REST API.
332
389
  * @param customPost - The customPost object.
333
- * @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`.
334
391
  * @param customPost.parameters - Query parameters to apply to the current query.
335
392
  * @param customPost.body - Parameters to send with the custom request.
336
393
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
@@ -354,7 +411,7 @@ function createIngestionClient({
354
411
  /**
355
412
  * This method lets you send requests to the Algolia REST API.
356
413
  * @param customPut - The customPut object.
357
- * @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`.
358
415
  * @param customPut.parameters - Query parameters to apply to the current query.
359
416
  * @param customPut.body - Parameters to send with the custom request.
360
417
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
@@ -1303,9 +1360,10 @@ function createIngestionClient({
1303
1360
  * @param push.indexName - Name of the index on which to perform the operation.
1304
1361
  * @param push.pushTaskPayload - The pushTaskPayload object.
1305
1362
  * @param push.watch - When provided, the push operation will be synchronous and the API will wait for the ingestion to be finished before responding.
1363
+ * @param push.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).
1306
1364
  * @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
1307
1365
  */
1308
- push({ indexName, pushTaskPayload, watch }, requestOptions) {
1366
+ push({ indexName, pushTaskPayload, watch, referenceIndexName }, requestOptions) {
1309
1367
  if (!indexName) {
1310
1368
  throw new Error("Parameter `indexName` is required when calling `push`.");
1311
1369
  }
@@ -1324,6 +1382,9 @@ function createIngestionClient({
1324
1382
  if (watch !== void 0) {
1325
1383
  queryParameters["watch"] = watch.toString();
1326
1384
  }
1385
+ if (referenceIndexName !== void 0) {
1386
+ queryParameters["referenceIndexName"] = referenceIndexName.toString();
1387
+ }
1327
1388
  const request = {
1328
1389
  method: "POST",
1329
1390
  path: requestPath,
@@ -1336,7 +1397,7 @@ function createIngestionClient({
1336
1397
  connect: 18e4,
1337
1398
  read: 18e4,
1338
1399
  write: 18e4,
1339
- ...requestOptions?.timeouts
1400
+ ...requestOptions == null ? void 0 : requestOptions.timeouts
1340
1401
  }
1341
1402
  };
1342
1403
  return transporter.request(request, requestOptions);
@@ -1385,7 +1446,7 @@ function createIngestionClient({
1385
1446
  connect: 18e4,
1386
1447
  read: 18e4,
1387
1448
  write: 18e4,
1388
- ...requestOptions?.timeouts
1449
+ ...requestOptions == null ? void 0 : requestOptions.timeouts
1389
1450
  }
1390
1451
  };
1391
1452
  return transporter.request(request, requestOptions);
@@ -1681,7 +1742,7 @@ function createIngestionClient({
1681
1742
  connect: 18e4,
1682
1743
  read: 18e4,
1683
1744
  write: 18e4,
1684
- ...requestOptions?.timeouts
1745
+ ...requestOptions == null ? void 0 : requestOptions.timeouts
1685
1746
  }
1686
1747
  };
1687
1748
  return transporter.request(request, requestOptions);
@@ -1700,9 +1761,6 @@ function createIngestionClient({
1700
1761
  if (!transformationTry) {
1701
1762
  throw new Error("Parameter `transformationTry` is required when calling `tryTransformation`.");
1702
1763
  }
1703
- if (!transformationTry.code) {
1704
- throw new Error("Parameter `transformationTry.code` is required when calling `tryTransformation`.");
1705
- }
1706
1764
  if (!transformationTry.sampleRecord) {
1707
1765
  throw new Error("Parameter `transformationTry.sampleRecord` is required when calling `tryTransformation`.");
1708
1766
  }
@@ -1737,9 +1795,6 @@ function createIngestionClient({
1737
1795
  if (!transformationTry) {
1738
1796
  throw new Error("Parameter `transformationTry` is required when calling `tryTransformationBeforeUpdate`.");
1739
1797
  }
1740
- if (!transformationTry.code) {
1741
- throw new Error("Parameter `transformationTry.code` is required when calling `tryTransformationBeforeUpdate`.");
1742
- }
1743
1798
  if (!transformationTry.sampleRecord) {
1744
1799
  throw new Error(
1745
1800
  "Parameter `transformationTry.sampleRecord` is required when calling `tryTransformationBeforeUpdate`."
@@ -1930,12 +1985,6 @@ function createIngestionClient({
1930
1985
  if (!transformationCreate.name) {
1931
1986
  throw new Error("Parameter `transformationCreate.name` is required when calling `updateTransformation`.");
1932
1987
  }
1933
- if (!transformationCreate.type) {
1934
- throw new Error("Parameter `transformationCreate.type` is required when calling `updateTransformation`.");
1935
- }
1936
- if (!transformationCreate.input) {
1937
- throw new Error("Parameter `transformationCreate.input` is required when calling `updateTransformation`.");
1938
- }
1939
1988
  const requestPath = "/1/transformations/{transformationID}".replace(
1940
1989
  "{transformationID}",
1941
1990
  encodeURIComponent(transformationID)
@@ -1977,7 +2026,7 @@ function createIngestionClient({
1977
2026
  connect: 18e4,
1978
2027
  read: 18e4,
1979
2028
  write: 18e4,
1980
- ...requestOptions?.timeouts
2029
+ ...requestOptions == null ? void 0 : requestOptions.timeouts
1981
2030
  }
1982
2031
  };
1983
2032
  return transporter.request(request, requestOptions);
@@ -2016,7 +2065,7 @@ function createIngestionClient({
2016
2065
  connect: 18e4,
2017
2066
  read: 18e4,
2018
2067
  write: 18e4,
2019
- ...requestOptions?.timeouts
2068
+ ...requestOptions == null ? void 0 : requestOptions.timeouts
2020
2069
  }
2021
2070
  };
2022
2071
  return transporter.request(request, requestOptions);