Skip to content

Commit 0513b8a

Browse files
authored
Merge 1974388 into 9db03b5
2 parents 9db03b5 + 1974388 commit 0513b8a

File tree

4 files changed

+214
-114
lines changed

4 files changed

+214
-114
lines changed

clients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.common.test.ts

+87-97
Original file line numberDiff line numberDiff line change
@@ -143,131 +143,121 @@ describe('api', () => {
143143
});
144144
});
145145

146-
describe('init clients', () => {
147-
test('provides an init method for the analytics client', () => {
148-
expect(client.initAnalytics).not.toBeUndefined();
146+
describe('bridge methods', () => {
147+
test('throws when missing transformation.region', () => {
148+
//@ts-expect-error
149+
expect(() => algoliasearch('APP_ID', 'API_KEY', { transformation: { taskID: 'foo' } })).toThrow(
150+
'`region` must be provided when leveraging the transformation pipeline',
151+
);
149152
});
150153

151-
test('provides an init method for the abtesting client', () => {
152-
expect(client.initAbtesting).not.toBeUndefined();
154+
test('throws when missing transformation.taskID', () => {
155+
//@ts-expect-error
156+
expect(() => algoliasearch('APP_ID', 'API_KEY', { transformation: { region: 'us' } })).toThrow(
157+
'`taskID` must be provided when leveraging the transformation pipeline',
158+
);
153159
});
154160

155-
test('provides an init method for the personalization client', () => {
156-
expect(client.initPersonalization).not.toBeUndefined();
157-
});
161+
test('throws when calling the transformation methods without init parameters', async () => {
162+
await expect(
163+
client.saveObjectsWithTransformation({
164+
indexName: 'foo',
165+
objects: [{ objectID: 'bar', baz: 42 }],
166+
waitForTasks: true,
167+
}),
168+
).rejects.toThrow(
169+
'`transformation.taskID` and `transformation.region` must be provided at client instantiation before calling this method.',
170+
);
158171

159-
test('provides an init method for the recommend client', () => {
160-
expect(client.initRecommend).not.toBeUndefined();
172+
await expect(
173+
client.partialUpdateObjectsWithTransformation({
174+
indexName: 'foo',
175+
objects: [{ objectID: 'bar', baz: 42 }],
176+
waitForTasks: true,
177+
}),
178+
).rejects.toThrow(
179+
'`transformation.taskID` and `transformation.region` must be provided at client instantiation before calling this method.',
180+
);
161181
});
162182

163-
test('default `init` clients to the root `algoliasearch` credentials', async () => {
164-
const abtestingClient = client.initAbtesting({ options: { requester: browserEchoRequester() } });
165-
const analyticsClient = client.initAnalytics({ options: { requester: browserEchoRequester() } });
166-
const recommendClient = client.initRecommend({ options: { requester: browserEchoRequester() } });
167-
const personalizationClient = client.initPersonalization({
168-
region: 'eu',
169-
options: { requester: browserEchoRequester() },
183+
test('exposes the transformation methods at the root of the client', async () => {
184+
const ingestionClient = algoliasearch('APP_ID', 'API_KEY', {
185+
requester: browserEchoRequester(),
186+
transformation: { taskID: 'foo', region: 'us' },
170187
});
171188

172-
const res1 = (await abtestingClient.customGet({
173-
path: 'abtestingClient',
174-
})) as unknown as EchoResponse;
175-
const res2 = (await analyticsClient.customGet({
176-
path: 'analyticsClient',
177-
})) as unknown as EchoResponse;
178-
const res3 = (await personalizationClient.customGet({
179-
path: 'personalizationClient',
180-
})) as unknown as EchoResponse;
181-
const res4 = (await recommendClient.customGet({
182-
path: 'recommendClient',
189+
expect(ingestionClient.saveObjectsWithTransformation).not.toBeUndefined();
190+
191+
let res = (await ingestionClient.saveObjectsWithTransformation({
192+
indexName: 'foo',
193+
objects: [{ objectID: 'bar', baz: 42 }],
194+
waitForTasks: true,
183195
})) as unknown as EchoResponse;
184196

185-
expect(res1.headers).toEqual(
197+
expect(res.headers).toEqual(
186198
expect.objectContaining({
187199
'x-algolia-application-id': 'APP_ID',
188200
'x-algolia-api-key': 'API_KEY',
189201
}),
190202
);
191-
expect(res2.headers).toEqual(
192-
expect.objectContaining({
193-
'x-algolia-application-id': 'APP_ID',
194-
'x-algolia-api-key': 'API_KEY',
195-
}),
196-
);
197-
expect(res3.headers).toEqual(
198-
expect.objectContaining({
199-
'x-algolia-application-id': 'APP_ID',
200-
'x-algolia-api-key': 'API_KEY',
201-
}),
202-
);
203-
expect(res4.headers).toEqual(
203+
expect(res.url.startsWith('https://data.us.algolia.com/2/tasks/foo/push?watch=true')).toBeTruthy();
204+
expect(res.data).toEqual({
205+
action: 'addObject',
206+
records: [
207+
{
208+
baz: 42,
209+
objectID: 'bar',
210+
},
211+
],
212+
});
213+
expect(ingestionClient.partialUpdateObjectsWithTransformation).not.toBeUndefined();
214+
215+
res = (await ingestionClient.partialUpdateObjectsWithTransformation({
216+
indexName: 'foo',
217+
objects: [{ objectID: 'bar', baz: 42 }],
218+
waitForTasks: true,
219+
createIfNotExists: true,
220+
})) as unknown as EchoResponse;
221+
222+
expect(res.headers).toEqual(
204223
expect.objectContaining({
205224
'x-algolia-application-id': 'APP_ID',
206225
'x-algolia-api-key': 'API_KEY',
207226
}),
208227
);
209-
});
210-
211-
test('`init` clients accept different credentials', async () => {
212-
const abtestingClient = client.initAbtesting({
213-
appId: 'appId1',
214-
apiKey: 'apiKey1',
215-
options: { requester: browserEchoRequester() },
216-
});
217-
const analyticsClient = client.initAnalytics({
218-
appId: 'appId2',
219-
apiKey: 'apiKey2',
220-
options: { requester: browserEchoRequester() },
221-
});
222-
const personalizationClient = client.initPersonalization({
223-
appId: 'appId3',
224-
apiKey: 'apiKey3',
225-
region: 'eu',
226-
options: { requester: browserEchoRequester() },
227-
});
228-
const recommendClient = client.initRecommend({
229-
appId: 'appId4',
230-
apiKey: 'apiKey4',
231-
options: { requester: browserEchoRequester() },
228+
expect(res.url.startsWith('https://data.us.algolia.com/2/tasks/foo/push?watch=true')).toBeTruthy();
229+
expect(res.data).toEqual({
230+
action: 'partialUpdateObject',
231+
records: [
232+
{
233+
baz: 42,
234+
objectID: 'bar',
235+
},
236+
],
232237
});
233238

234-
const res1 = (await abtestingClient.customGet({
235-
path: 'abtestingClient',
236-
})) as unknown as EchoResponse;
237-
const res2 = (await analyticsClient.customGet({
238-
path: 'analyticsClient',
239-
})) as unknown as EchoResponse;
240-
const res3 = (await personalizationClient.customGet({
241-
path: 'personalizationClient',
242-
})) as unknown as EchoResponse;
243-
const res4 = (await recommendClient.customGet({
244-
path: 'recommendClient',
239+
res = (await ingestionClient.partialUpdateObjectsWithTransformation({
240+
indexName: 'foo',
241+
objects: [{ objectID: 'bar', baz: 42 }],
242+
waitForTasks: true,
245243
})) as unknown as EchoResponse;
246244

247-
expect(res1.headers).toEqual(
248-
expect.objectContaining({
249-
'x-algolia-application-id': 'appId1',
250-
'x-algolia-api-key': 'apiKey1',
251-
}),
252-
);
253-
expect(res2.headers).toEqual(
254-
expect.objectContaining({
255-
'x-algolia-application-id': 'appId2',
256-
'x-algolia-api-key': 'apiKey2',
257-
}),
258-
);
259-
expect(res3.headers).toEqual(
245+
expect(res.headers).toEqual(
260246
expect.objectContaining({
261-
'x-algolia-application-id': 'appId3',
262-
'x-algolia-api-key': 'apiKey3',
263-
}),
264-
);
265-
expect(res4.headers).toEqual(
266-
expect.objectContaining({
267-
'x-algolia-application-id': 'appId4',
268-
'x-algolia-api-key': 'apiKey4',
247+
'x-algolia-application-id': 'APP_ID',
248+
'x-algolia-api-key': 'API_KEY',
269249
}),
270250
);
251+
expect(res.url.startsWith('https://data.us.algolia.com/2/tasks/foo/push?watch=true')).toBeTruthy();
252+
expect(res.data).toEqual({
253+
action: 'partialUpdateObjectNoCreate',
254+
records: [
255+
{
256+
baz: 42,
257+
objectID: 'bar',
258+
},
259+
],
260+
});
271261
});
272262
});
273263
});

playground/javascript/node/algoliasearch.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import type { SearchResponses } from 'algoliasearch';
66

77
const appId = process.env.ALGOLIA_APPLICATION_ID || '**** APP_ID *****';
88
const apiKey = process.env.ALGOLIA_SEARCH_KEY || '**** SEARCH_API_KEY *****';
9+
const adminApiKey = process.env.ALGOLIA_ADMIN_KEY || '**** ADMIN_API_KEY *****';
910

1011
const searchIndex = process.env.SEARCH_INDEX || 'test_index';
1112
const searchQuery = process.env.SEARCH_QUERY || 'test_query';
1213
const analyticsIndex = process.env.ANALYTICS_INDEX || 'test_index';
1314

14-
// Init client with appId and apiKey
15-
const client = algoliasearch(appId, apiKey);
16-
const clientLite = liteClient(appId, apiKey);
17-
18-
client.addAlgoliaAgent('algoliasearch node playground', '0.0.1');
19-
2015
async function testAlgoliasearch() {
16+
// Init client with appId and apiKey
17+
const client = algoliasearch(appId, apiKey);
18+
const clientLite = liteClient(appId, apiKey);
19+
20+
client.addAlgoliaAgent('algoliasearch node playground', '0.0.1');
21+
2122
try {
2223
const res: SearchResponses = await client.search({
2324
requests: [
@@ -131,4 +132,14 @@ async function testAlgoliasearch() {
131132
}
132133
}
133134

134-
testAlgoliasearch();
135+
async function testAlgoliasearchBridgeIngestion() {
136+
// Init client with appId and apiKey
137+
const client = algoliasearch(appId, adminApiKey, { transformation: {taskID: '942f7183-8596-4e51-a887-977c5b362f3d', region: 'eu'}});
138+
139+
await client.saveObjectsWithTransformation({indexName: "foo", objects: [{objectID: "foo", data: {baz: "baz", win: 42}}], waitForTasks: true })
140+
141+
await client.partialUpdateObjectsWithTransformation({indexName: "foo", objects: [{objectID: "foo", data: {baz: "baz", win: 42}}], waitForTasks: true, createIfNotExists: false })
142+
}
143+
144+
// testAlgoliasearch();
145+
testAlgoliasearchBridgeIngestion()

0 commit comments

Comments
 (0)