Skip to content

Commit db4cf48

Browse files
authored
feat(javascript): add logger-console package from v4 (#3823)
1 parent ab1b02f commit db4cf48

File tree

28 files changed

+364
-19
lines changed

28 files changed

+364
-19
lines changed
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/browser';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/browser';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
11+
812
expect(client.transporter.algoliaAgent).toEqual({
913
add: expect.any(Function),
1014
value: expect.stringContaining(
1115
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
1216
),
1317
});
1418
});
19+
20+
test('with logger', () => {
21+
vi.spyOn(console, 'debug');
22+
vi.spyOn(console, 'info');
23+
vi.spyOn(console, 'error');
24+
25+
const client = algoliasearch('APP_ID', 'API_KEY', {
26+
logger: createConsoleLogger(LogLevelEnum.Debug),
27+
});
28+
29+
expect(async () => {
30+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
31+
expect(console.debug).toHaveBeenCalledTimes(1);
32+
expect(console.info).toHaveBeenCalledTimes(1);
33+
expect(console.error).toHaveBeenCalledTimes(1);
34+
}).not.toThrow();
35+
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ describe('api', () => {
103103
url: 'APP_ID-2.algolianet.com',
104104
},
105105
]),
106+
logger: {
107+
debug: expect.any(Function),
108+
error: expect.any(Function),
109+
info: expect.any(Function),
110+
},
106111
hostsCache: {
107112
clear: expect.any(Function),
108113
delete: expect.any(Function),
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/fetch';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/fetch';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
811
expect(client.transporter.algoliaAgent).toEqual({
912
add: expect.any(Function),
1013
value: expect.stringContaining(`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Fetch`),
1114
});
1215
});
1316

1417
test('forwards node search helpers', () => {
18+
const client = algoliasearch('APP_ID', 'API_KEY');
1519
expect(client.generateSecuredApiKey).not.toBeUndefined();
1620
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
1721
expect(() => {
1822
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
1923
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
2024
}).not.toThrow();
2125
});
26+
27+
test('with logger', () => {
28+
vi.spyOn(console, 'debug');
29+
vi.spyOn(console, 'info');
30+
vi.spyOn(console, 'error');
31+
32+
const client = algoliasearch('APP_ID', 'API_KEY', {
33+
logger: createConsoleLogger(LogLevelEnum.Debug),
34+
});
35+
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();
42+
});
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/node';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/node';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
811
expect(client.transporter.algoliaAgent).toEqual({
912
add: expect.any(Function),
1013
value: expect.stringContaining(
@@ -14,10 +17,28 @@ test('sets the ua', () => {
1417
});
1518

1619
test('forwards node search helpers', () => {
20+
const client = algoliasearch('APP_ID', 'API_KEY');
1721
expect(client.generateSecuredApiKey).not.toBeUndefined();
1822
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
1923
expect(() => {
2024
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
2125
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
2226
}).not.toThrow();
2327
});
28+
29+
test('with logger', () => {
30+
vi.spyOn(console, 'debug');
31+
vi.spyOn(console, 'info');
32+
vi.spyOn(console, 'error');
33+
34+
const client = algoliasearch('APP_ID', 'API_KEY', {
35+
logger: createConsoleLogger(LogLevelEnum.Debug),
36+
});
37+
38+
expect(async () => {
39+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
40+
expect(console.debug).toHaveBeenCalledTimes(1);
41+
expect(console.info).toHaveBeenCalledTimes(1);
42+
expect(console.error).toHaveBeenCalledTimes(1);
43+
}).not.toThrow();
44+
});

clients/algoliasearch-client-javascript/packages/client-common/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
export * from './src/createAuth';
2-
export * from './src/createIterablePromise';
31
export * from './src/cache';
4-
export * from './src/transporter';
2+
export * from './src/constants';
53
export * from './src/createAlgoliaAgent';
4+
export * from './src/createAuth';
5+
export * from './src/createIterablePromise';
66
export * from './src/getAlgoliaAgent';
7+
export * from './src/logger';
8+
export * from './src/transporter';
79
export * from './src/types';
8-
export * from './src/constants';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint no-console: 0 */
2+
3+
import { vi, describe, test, expect } from 'vitest';
4+
5+
import { createNullLogger } from '../../logger';
6+
7+
describe('null logger', () => {
8+
test('has a null behavior', async () => {
9+
vi.resetAllMocks();
10+
vi.spyOn(console, 'debug');
11+
vi.spyOn(console, 'info');
12+
vi.spyOn(console, 'error');
13+
14+
const logger = createNullLogger();
15+
16+
await logger.debug('foo', {});
17+
await logger.info('foo', {});
18+
await logger.error('foo', {});
19+
20+
expect(console.debug).toHaveBeenCalledTimes(0);
21+
expect(console.info).toHaveBeenCalledTimes(0);
22+
expect(console.error).toHaveBeenCalledTimes(0);
23+
});
24+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Logger } from '../types/logger';
2+
3+
export function createNullLogger(): Logger {
4+
return {
5+
debug(_message: string, _args?: any): Promise<void> {
6+
return Promise.resolve();
7+
},
8+
info(_message: string, _args?: any): Promise<void> {
9+
return Promise.resolve();
10+
},
11+
error(_message: string, _args?: any): Promise<void> {
12+
return Promise.resolve();
13+
},
14+
};
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './createNullLogger';

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function createTransporter({
2525
hosts,
2626
hostsCache,
2727
baseHeaders,
28+
logger,
2829
baseQueryParameters,
2930
algoliaAgent,
3031
timeouts,
@@ -174,8 +175,7 @@ export function createTransporter({
174175
* the end user to debug / store stack frames even
175176
* when a retry error does not happen.
176177
*/
177-
// eslint-disable-next-line no-console -- this will be fixed by exposing a `logger` to the transporter
178-
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
178+
logger.info('Retryable failure', stackFrameWithoutCredentials(stackFrame));
179179

180180
/**
181181
* We also store the state of the host in failure cases. If the host, is
@@ -304,6 +304,7 @@ export function createTransporter({
304304
hostsCache,
305305
requester,
306306
timeouts,
307+
logger,
307308
algoliaAgent,
308309
baseHeaders,
309310
baseQueryParameters,

clients/algoliasearch-client-javascript/packages/client-common/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './cache';
22
export * from './createClient';
33
export * from './createIterablePromise';
44
export * from './host';
5+
export * from './logger';
56
export * from './requester';
67
export * from './transporter';

0 commit comments

Comments
 (0)