Skip to content

Commit 1e5f985

Browse files
feat(javascript): add bridge to transformation on algoliasearch (#4852) (generated) [skip ci]
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 8cb1e50 commit 1e5f985

File tree

5 files changed

+443
-11
lines changed

5 files changed

+443
-11
lines changed

clients/algoliasearch-client-javascript/packages/algoliasearch/builds/browser.ts

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
22

3-
import type { ClientOptions } from '@algolia/client-common';
3+
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
44

55
import type { AbtestingClient } from '@algolia/client-abtesting';
66
import { abtestingClient } from '@algolia/client-abtesting';
@@ -21,9 +21,13 @@ import { monitoringClient } from '@algolia/monitoring';
2121
import type { RecommendClient } from '@algolia/recommend';
2222
import { recommendClient } from '@algolia/recommend';
2323

24+
import type { PartialUpdateObjectsOptions, SaveObjectsOptions } from '@algolia/client-search';
25+
import type { PushTaskRecords, WatchResponse } from '@algolia/ingestion';
26+
2427
import type {
2528
AbtestingRegionOptions,
2629
AnalyticsRegionOptions,
30+
IngestionRegion,
2731
IngestionRegionOptions,
2832
InitClientOptions,
2933
InsightsRegionOptions,
@@ -42,9 +46,56 @@ export type Algoliasearch = SearchClient & {
4246
initPersonalization: (initOptions: InitClientOptions & PersonalizationRegionOptions) => PersonalizationClient;
4347
initQuerySuggestions: (initOptions: InitClientOptions & QuerySuggestionsRegionOptions) => QuerySuggestionsClient;
4448
initRecommend: (initOptions?: InitClientOptions) => RecommendClient;
49+
50+
// Bridge helpers to expose along with the search endpoints at the root of the API client
51+
52+
/**
53+
* Helper: Similar to the `saveObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method.
54+
*
55+
* @summary Save objects to an Algolia index 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/).
56+
* @param saveObjects - The `saveObjects` object.
57+
* @param saveObjects.indexName - The `indexName` to save `objects` in.
58+
* @param saveObjects.objects - The array of `objects` to store in the given Algolia `indexName`.
59+
* @param saveObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
60+
* @param saveObjects.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.
61+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `batch` method and merged with the transporter requestOptions.
62+
*/
63+
saveObjectsWithTransformation: (
64+
options: SaveObjectsOptions,
65+
requestOptions?: RequestOptions,
66+
) => Promise<WatchResponse>;
67+
68+
/**
69+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method.
70+
*
71+
* @summary Save objects to an Algolia index 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/).
72+
* @param partialUpdateObjects - The `partialUpdateObjects` object.
73+
* @param partialUpdateObjects.indexName - The `indexName` to update `objects` in.
74+
* @param partialUpdateObjects.objects - The array of `objects` to update in the given Algolia `indexName`.
75+
* @param partialUpdateObjects.createIfNotExists - To be provided if non-existing objects are passed, otherwise, the call will fail..
76+
* @param partialUpdateObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
77+
* @param partialUpdateObjects.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.
78+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
79+
*/
80+
partialUpdateObjectsWithTransformation: (
81+
options: PartialUpdateObjectsOptions,
82+
requestOptions?: RequestOptions,
83+
) => Promise<WatchResponse>;
84+
};
85+
86+
export type TransformationOptions = {
87+
// When provided, a second transporter will be created in order to leverage the `*WithTransformation` methods exposed by the Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/).
88+
transformation?: {
89+
// The region of your Algolia application ID, used to target the correct hosts of the transformation service.
90+
region: IngestionRegion;
91+
};
4592
};
4693

47-
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
94+
export function algoliasearch(
95+
appId: string,
96+
apiKey: string,
97+
options?: ClientOptions & TransformationOptions,
98+
): Algoliasearch {
4899
if (!appId || typeof appId !== 'string') {
49100
throw new Error('`appId` is missing.');
50101
}
@@ -55,9 +106,66 @@ export function algoliasearch(appId: string, apiKey: string, options?: ClientOpt
55106

56107
const client = searchClient(appId, apiKey, options);
57108

109+
let ingestionTransporter: IngestionClient | undefined;
110+
111+
if (options?.transformation) {
112+
if (!options.transformation.region) {
113+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
114+
}
115+
116+
ingestionTransporter = ingestionClient(appId, apiKey, options.transformation.region, options);
117+
}
118+
58119
return {
59120
...client,
60121

122+
async saveObjectsWithTransformation({ indexName, objects, waitForTasks }, requestOptions): Promise<WatchResponse> {
123+
if (!ingestionTransporter) {
124+
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
125+
}
126+
127+
if (!options?.transformation?.region) {
128+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
129+
}
130+
131+
return ingestionTransporter?.push(
132+
{
133+
indexName,
134+
watch: waitForTasks,
135+
pushTaskPayload: {
136+
action: 'addObject',
137+
records: objects as PushTaskRecords[],
138+
},
139+
},
140+
requestOptions,
141+
);
142+
},
143+
144+
async partialUpdateObjectsWithTransformation(
145+
{ indexName, objects, createIfNotExists, waitForTasks },
146+
requestOptions,
147+
): Promise<WatchResponse> {
148+
if (!ingestionTransporter) {
149+
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
150+
}
151+
152+
if (!options?.transformation?.region) {
153+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
154+
}
155+
156+
return ingestionTransporter?.push(
157+
{
158+
indexName,
159+
watch: waitForTasks,
160+
pushTaskPayload: {
161+
action: createIfNotExists ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate',
162+
records: objects as PushTaskRecords[],
163+
},
164+
},
165+
requestOptions,
166+
);
167+
},
168+
61169
/**
62170
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
63171
*/

clients/algoliasearch-client-javascript/packages/algoliasearch/builds/fetch.ts

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
22

3-
import type { ClientOptions } from '@algolia/client-common';
3+
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
44

55
import type { AbtestingClient } from '@algolia/client-abtesting';
66
import { abtestingClient } from '@algolia/client-abtesting';
@@ -21,9 +21,13 @@ import { monitoringClient } from '@algolia/monitoring';
2121
import type { RecommendClient } from '@algolia/recommend';
2222
import { recommendClient } from '@algolia/recommend';
2323

24+
import type { PartialUpdateObjectsOptions, SaveObjectsOptions } from '@algolia/client-search';
25+
import type { PushTaskRecords, WatchResponse } from '@algolia/ingestion';
26+
2427
import type {
2528
AbtestingRegionOptions,
2629
AnalyticsRegionOptions,
30+
IngestionRegion,
2731
IngestionRegionOptions,
2832
InitClientOptions,
2933
InsightsRegionOptions,
@@ -42,9 +46,56 @@ export type Algoliasearch = SearchClient & {
4246
initPersonalization: (initOptions: InitClientOptions & PersonalizationRegionOptions) => PersonalizationClient;
4347
initQuerySuggestions: (initOptions: InitClientOptions & QuerySuggestionsRegionOptions) => QuerySuggestionsClient;
4448
initRecommend: (initOptions?: InitClientOptions) => RecommendClient;
49+
50+
// Bridge helpers to expose along with the search endpoints at the root of the API client
51+
52+
/**
53+
* Helper: Similar to the `saveObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method.
54+
*
55+
* @summary Save objects to an Algolia index 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/).
56+
* @param saveObjects - The `saveObjects` object.
57+
* @param saveObjects.indexName - The `indexName` to save `objects` in.
58+
* @param saveObjects.objects - The array of `objects` to store in the given Algolia `indexName`.
59+
* @param saveObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
60+
* @param saveObjects.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.
61+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `batch` method and merged with the transporter requestOptions.
62+
*/
63+
saveObjectsWithTransformation: (
64+
options: SaveObjectsOptions,
65+
requestOptions?: RequestOptions,
66+
) => Promise<WatchResponse>;
67+
68+
/**
69+
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method.
70+
*
71+
* @summary Save objects to an Algolia index 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/).
72+
* @param partialUpdateObjects - The `partialUpdateObjects` object.
73+
* @param partialUpdateObjects.indexName - The `indexName` to update `objects` in.
74+
* @param partialUpdateObjects.objects - The array of `objects` to update in the given Algolia `indexName`.
75+
* @param partialUpdateObjects.createIfNotExists - To be provided if non-existing objects are passed, otherwise, the call will fail..
76+
* @param partialUpdateObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
77+
* @param partialUpdateObjects.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.
78+
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
79+
*/
80+
partialUpdateObjectsWithTransformation: (
81+
options: PartialUpdateObjectsOptions,
82+
requestOptions?: RequestOptions,
83+
) => Promise<WatchResponse>;
84+
};
85+
86+
export type TransformationOptions = {
87+
// When provided, a second transporter will be created in order to leverage the `*WithTransformation` methods exposed by the Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/).
88+
transformation?: {
89+
// The region of your Algolia application ID, used to target the correct hosts of the transformation service.
90+
region: IngestionRegion;
91+
};
4592
};
4693

47-
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
94+
export function algoliasearch(
95+
appId: string,
96+
apiKey: string,
97+
options?: ClientOptions & TransformationOptions,
98+
): Algoliasearch {
4899
if (!appId || typeof appId !== 'string') {
49100
throw new Error('`appId` is missing.');
50101
}
@@ -55,9 +106,66 @@ export function algoliasearch(appId: string, apiKey: string, options?: ClientOpt
55106

56107
const client = searchClient(appId, apiKey, options);
57108

109+
let ingestionTransporter: IngestionClient | undefined;
110+
111+
if (options?.transformation) {
112+
if (!options.transformation.region) {
113+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
114+
}
115+
116+
ingestionTransporter = ingestionClient(appId, apiKey, options.transformation.region, options);
117+
}
118+
58119
return {
59120
...client,
60121

122+
async saveObjectsWithTransformation({ indexName, objects, waitForTasks }, requestOptions): Promise<WatchResponse> {
123+
if (!ingestionTransporter) {
124+
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
125+
}
126+
127+
if (!options?.transformation?.region) {
128+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
129+
}
130+
131+
return ingestionTransporter?.push(
132+
{
133+
indexName,
134+
watch: waitForTasks,
135+
pushTaskPayload: {
136+
action: 'addObject',
137+
records: objects as PushTaskRecords[],
138+
},
139+
},
140+
requestOptions,
141+
);
142+
},
143+
144+
async partialUpdateObjectsWithTransformation(
145+
{ indexName, objects, createIfNotExists, waitForTasks },
146+
requestOptions,
147+
): Promise<WatchResponse> {
148+
if (!ingestionTransporter) {
149+
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
150+
}
151+
152+
if (!options?.transformation?.region) {
153+
throw new Error('`region` must be provided when leveraging the transformation pipeline');
154+
}
155+
156+
return ingestionTransporter?.push(
157+
{
158+
indexName,
159+
watch: waitForTasks,
160+
pushTaskPayload: {
161+
action: createIfNotExists ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate',
162+
records: objects as PushTaskRecords[],
163+
},
164+
},
165+
requestOptions,
166+
);
167+
},
168+
61169
/**
62170
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
63171
*/

0 commit comments

Comments
 (0)