Skip to content

Commit cb3174b

Browse files
algolia-api-clients-automation-bot[bot]eric-zahariaclaude
committed
fix(javascript): add warning in replaceAllObjects for empty objects (generated)
algolia/api-clients-automation#6608 Co-authored-by: algolia-api-clients-automation-bot[bot] <288895823+algolia-api-clients-automation-bot[bot]@users.noreply.github.com> Co-authored-by: Eric Zaharia <94015633+eric-zaharia@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent d6430d4 commit cb3174b

17 files changed

Lines changed: 162 additions & 45 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
{
6565
"path": "packages/client-search/dist/builds/browser.umd.js",
66-
"maxSize": "10KB"
66+
"maxSize": "11KB"
6767
},
6868
{
6969
"path": "packages/ingestion/dist/builds/browser.umd.js",

packages/algoliasearch/__tests__/algoliasearch.browser.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ test('sets the ua', () => {
1515
});
1616
});
1717

18-
test('with logger', () => {
19-
vi.spyOn(console, 'debug');
20-
vi.spyOn(console, 'info');
21-
vi.spyOn(console, 'error');
18+
test('with logger', async () => {
19+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
2220

2321
const client = algoliasearch('APP_ID', 'API_KEY', {
2422
logger: createConsoleLogger(LogLevelEnum.Debug),
23+
// every host fails, exercising the retry logging path without a real request that could outlive the test
24+
requester: {
25+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
26+
},
2527
});
2628

27-
expect(async () => {
28-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
29-
expect(console.debug).toHaveBeenCalledTimes(1);
30-
expect(console.info).toHaveBeenCalledTimes(1);
31-
expect(console.error).toHaveBeenCalledTimes(1);
32-
}).not.toThrow();
29+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
30+
31+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
3332
});

packages/algoliasearch/__tests__/algoliasearch.fetch.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ test('forwards node search helpers', () => {
2222
}).not.toThrow();
2323
});
2424

25-
test('with logger', () => {
26-
vi.spyOn(console, 'debug');
27-
vi.spyOn(console, 'info');
28-
vi.spyOn(console, 'error');
25+
test('with logger', async () => {
26+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
2927

3028
const client = algoliasearch('APP_ID', 'API_KEY', {
3129
logger: createConsoleLogger(LogLevelEnum.Debug),
30+
// every host fails, exercising the retry logging path without a real request that could outlive the test
31+
requester: {
32+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
33+
},
3234
});
3335

34-
expect(async () => {
35-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
36-
expect(console.debug).toHaveBeenCalledTimes(1);
37-
expect(console.info).toHaveBeenCalledTimes(1);
38-
expect(console.error).toHaveBeenCalledTimes(1);
39-
}).not.toThrow();
36+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
37+
38+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
4039
});

packages/algoliasearch/__tests__/algoliasearch.node.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ test('forwards node search helpers', () => {
2424
}).not.toThrow();
2525
});
2626

27-
test('with logger', () => {
28-
vi.spyOn(console, 'debug');
29-
vi.spyOn(console, 'info');
30-
vi.spyOn(console, 'error');
27+
test('with logger', async () => {
28+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
3129

3230
const client = algoliasearch('APP_ID', 'API_KEY', {
3331
logger: createConsoleLogger(LogLevelEnum.Debug),
32+
// every host fails, exercising the retry logging path without a real request that could outlive the test
33+
requester: {
34+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
35+
},
3436
});
3537

36-
expect(async () => {
37-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
38-
expect(console.debug).toHaveBeenCalledTimes(1);
39-
expect(console.info).toHaveBeenCalledTimes(1);
40-
expect(console.error).toHaveBeenCalledTimes(1);
41-
}).not.toThrow();
38+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
39+
40+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
4241
});

packages/algoliasearch/__tests__/algoliasearch.worker.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ test('web crypto implementation gives the same result as node crypto', async ()
3737
expect(resp).toEqual(nodeResp);
3838
});
3939

40-
test('with logger', () => {
41-
vi.spyOn(console, 'debug');
42-
vi.spyOn(console, 'info');
43-
vi.spyOn(console, 'error');
40+
test('with logger', async () => {
41+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
4442

4543
const client = algoliasearch('APP_ID', 'API_KEY', {
4644
logger: createConsoleLogger(LogLevelEnum.Debug),
45+
// every host fails, exercising the retry logging path without a real request that could outlive the test
46+
requester: {
47+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
48+
},
4749
});
4850

49-
expect(async () => {
50-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
51-
expect(console.debug).toHaveBeenCalledTimes(1);
52-
expect(console.info).toHaveBeenCalledTimes(1);
53-
expect(console.error).toHaveBeenCalledTimes(1);
54-
}).not.toThrow();
51+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
52+
53+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
5554
});

packages/algoliasearch/builds/browser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

33
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
4-
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES } from '@algolia/client-common';
4+
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES, logWarning } from '@algolia/client-common';
55

66
import type { AbtestingV3Client } from '@algolia/abtesting';
77
import { abtestingV3Client } from '@algolia/abtesting';
@@ -97,6 +97,8 @@ export type Algoliasearch = SearchClient & {
9797
/**
9898
* Helper: Similar to the `replaceAllObjects` 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 have been passed to the client instantiation method.
9999
*
100+
* Warning: calling this method with an empty `objects` list replaces the index with an empty one, deleting all existing records.
101+
*
100102
* @summary Helper: Replaces all objects (records) in the given `index_name` 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/) with the given `objects`. A temporary index is created during this process in order to backup your data.
101103
* @param replaceAllObjects - The `replaceAllObjects` object.
102104
* @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
@@ -220,6 +222,13 @@ export function algoliasearch(
220222
);
221223
}
222224

225+
if (objects.length === 0) {
226+
logWarning(
227+
client.transporter.logger,
228+
`replaceAllObjectsWithTransformation was called with an empty list of objects, which will delete all records currently in the "${indexName}" index.`,
229+
);
230+
}
231+
223232
const randomSuffix = Math.floor(Math.random() * 1000000) + 100000;
224233
const tmpIndexName = `${indexName}_tmp_${randomSuffix}`;
225234

packages/algoliasearch/builds/fetch.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

33
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
4-
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES } from '@algolia/client-common';
4+
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES, logWarning } from '@algolia/client-common';
55

66
import type { AbtestingV3Client } from '@algolia/abtesting';
77
import { abtestingV3Client } from '@algolia/abtesting';
@@ -97,6 +97,8 @@ export type Algoliasearch = SearchClient & {
9797
/**
9898
* Helper: Similar to the `replaceAllObjects` 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 have been passed to the client instantiation method.
9999
*
100+
* Warning: calling this method with an empty `objects` list replaces the index with an empty one, deleting all existing records.
101+
*
100102
* @summary Helper: Replaces all objects (records) in the given `index_name` 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/) with the given `objects`. A temporary index is created during this process in order to backup your data.
101103
* @param replaceAllObjects - The `replaceAllObjects` object.
102104
* @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
@@ -220,6 +222,13 @@ export function algoliasearch(
220222
);
221223
}
222224

225+
if (objects.length === 0) {
226+
logWarning(
227+
client.transporter.logger,
228+
`replaceAllObjectsWithTransformation was called with an empty list of objects, which will delete all records currently in the "${indexName}" index.`,
229+
);
230+
}
231+
223232
const randomSuffix = Math.floor(Math.random() * 1000000) + 100000;
224233
const tmpIndexName = `${indexName}_tmp_${randomSuffix}`;
225234

packages/algoliasearch/builds/node.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

33
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
4-
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES } from '@algolia/client-common';
4+
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES, logWarning } from '@algolia/client-common';
55

66
import type { AbtestingV3Client } from '@algolia/abtesting';
77
import { abtestingV3Client } from '@algolia/abtesting';
@@ -97,6 +97,8 @@ export type Algoliasearch = SearchClient & {
9797
/**
9898
* Helper: Similar to the `replaceAllObjects` 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 have been passed to the client instantiation method.
9999
*
100+
* Warning: calling this method with an empty `objects` list replaces the index with an empty one, deleting all existing records.
101+
*
100102
* @summary Helper: Replaces all objects (records) in the given `index_name` 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/) with the given `objects`. A temporary index is created during this process in order to backup your data.
101103
* @param replaceAllObjects - The `replaceAllObjects` object.
102104
* @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
@@ -220,6 +222,13 @@ export function algoliasearch(
220222
);
221223
}
222224

225+
if (objects.length === 0) {
226+
logWarning(
227+
client.transporter.logger,
228+
`replaceAllObjectsWithTransformation was called with an empty list of objects, which will delete all records currently in the "${indexName}" index.`,
229+
);
230+
}
231+
223232
const randomSuffix = Math.floor(Math.random() * 1000000) + 100000;
224233
const tmpIndexName = `${indexName}_tmp_${randomSuffix}`;
225234

packages/algoliasearch/builds/worker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

33
import type { ClientOptions, RequestOptions } from '@algolia/client-common';
4-
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES } from '@algolia/client-common';
4+
import { DEFAULT_REPLACE_ALL_OBJECTS_MAX_RETRIES, logWarning } from '@algolia/client-common';
55

66
import type { AbtestingV3Client } from '@algolia/abtesting';
77
import { abtestingV3Client } from '@algolia/abtesting';
@@ -97,6 +97,8 @@ export type Algoliasearch = SearchClient & {
9797
/**
9898
* Helper: Similar to the `replaceAllObjects` 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 have been passed to the client instantiation method.
9999
*
100+
* Warning: calling this method with an empty `objects` list replaces the index with an empty one, deleting all existing records.
101+
*
100102
* @summary Helper: Replaces all objects (records) in the given `index_name` 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/) with the given `objects`. A temporary index is created during this process in order to backup your data.
101103
* @param replaceAllObjects - The `replaceAllObjects` object.
102104
* @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
@@ -220,6 +222,13 @@ export function algoliasearch(
220222
);
221223
}
222224

225+
if (objects.length === 0) {
226+
logWarning(
227+
client.transporter.logger,
228+
`replaceAllObjectsWithTransformation was called with an empty list of objects, which will delete all records currently in the "${indexName}" index.`,
229+
);
230+
}
231+
223232
const randomSuffix = Math.floor(Math.random() * 1000000) + 100000;
224233
const tmpIndexName = `${indexName}_tmp_${randomSuffix}`;
225234

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test, vi } from 'vitest';
2+
3+
import { createNullLogger, logWarning } from '../../logger';
4+
5+
describe('logWarning', () => {
6+
test('prefers the logger warn method when implemented', () => {
7+
vi.resetAllMocks();
8+
vi.spyOn(console, 'warn');
9+
10+
const warn = vi.fn().mockResolvedValue(undefined);
11+
12+
logWarning({ ...createNullLogger(), warn }, 'foo');
13+
14+
expect(warn).toHaveBeenCalledWith('foo');
15+
expect(console.warn).toHaveBeenCalledTimes(0);
16+
});
17+
18+
test('falls back to console.warn when the logger does not implement warn', () => {
19+
vi.resetAllMocks();
20+
vi.spyOn(console, 'warn');
21+
22+
logWarning(createNullLogger(), 'foo');
23+
24+
expect(console.warn).toHaveBeenCalledWith('foo');
25+
});
26+
});

0 commit comments

Comments
 (0)