Skip to content

Commit

Permalink
feat(javascript): add logger-console package from v4 (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3823

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
algolia-bot and shortcuts committed Sep 23, 2024
1 parent 7213be0 commit f3650d5
Show file tree
Hide file tree
Showing 53 changed files with 513 additions and 34 deletions.
27 changes: 24 additions & 3 deletions packages/algoliasearch/__tests__/algoliasearch.browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import { test, expect } from 'vitest';
/* eslint no-console: 0 */

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

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

test('sets the ua', () => {
const client = algoliasearch('APP_ID', 'API_KEY');

expect(client.transporter.algoliaAgent).toEqual({
add: expect.any(Function),
value: expect.stringContaining(
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
),
});
});

test('with logger', () => {
vi.spyOn(console, 'debug');
vi.spyOn(console, 'info');
vi.spyOn(console, 'error');

const client = algoliasearch('APP_ID', 'API_KEY', {
logger: createConsoleLogger(LogLevelEnum.Debug),
});

expect(async () => {
await client.setSettings({ indexName: 'foo', indexSettings: {} });
expect(console.debug).toHaveBeenCalledTimes(1);
expect(console.info).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledTimes(1);
}).not.toThrow();
});
5 changes: 5 additions & 0 deletions packages/algoliasearch/__tests__/algoliasearch.common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ describe('api', () => {
url: 'APP_ID-2.algolianet.com',
},
]),
logger: {
debug: expect.any(Function),
error: expect.any(Function),
info: expect.any(Function),
},
hostsCache: {
clear: expect.any(Function),
delete: expect.any(Function),
Expand Down
27 changes: 24 additions & 3 deletions packages/algoliasearch/__tests__/algoliasearch.fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
import { test, expect } from 'vitest';
/* eslint no-console: 0 */

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

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

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

test('forwards node search helpers', () => {
const client = algoliasearch('APP_ID', 'API_KEY');
expect(client.generateSecuredApiKey).not.toBeUndefined();
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
expect(() => {
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
}).not.toThrow();
});

test('with logger', () => {
vi.spyOn(console, 'debug');
vi.spyOn(console, 'info');
vi.spyOn(console, 'error');

const client = algoliasearch('APP_ID', 'API_KEY', {
logger: createConsoleLogger(LogLevelEnum.Debug),
});

expect(async () => {
await client.setSettings({ indexName: 'foo', indexSettings: {} });
expect(console.debug).toHaveBeenCalledTimes(1);
expect(console.info).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledTimes(1);
}).not.toThrow();
});
27 changes: 24 additions & 3 deletions packages/algoliasearch/__tests__/algoliasearch.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { test, expect } from 'vitest';
/* eslint no-console: 0 */

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

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

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

test('forwards node search helpers', () => {
const client = algoliasearch('APP_ID', 'API_KEY');
expect(client.generateSecuredApiKey).not.toBeUndefined();
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
expect(() => {
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
}).not.toThrow();
});

test('with logger', () => {
vi.spyOn(console, 'debug');
vi.spyOn(console, 'info');
vi.spyOn(console, 'error');

const client = algoliasearch('APP_ID', 'API_KEY', {
logger: createConsoleLogger(LogLevelEnum.Debug),
});

expect(async () => {
await client.setSettings({ indexName: 'foo', indexSettings: {} });
expect(console.debug).toHaveBeenCalledTimes(1);
expect(console.info).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledTimes(1);
}).not.toThrow();
});
2 changes: 2 additions & 0 deletions packages/algoliasearch/lite/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createFallbackableCache,
createBrowserLocalStorageCache,
Expand Down Expand Up @@ -35,6 +36,7 @@ export function liteClient(appId: string, apiKey: string, options?: ClientOption
read: DEFAULT_READ_TIMEOUT_BROWSER,
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
},
logger: createNullLogger(),
requester: createXhrRequester(),
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
Expand Down
2 changes: 2 additions & 0 deletions packages/algoliasearch/lite/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createNullCache,
DEFAULT_CONNECT_TIMEOUT_NODE,
Expand Down Expand Up @@ -34,6 +35,7 @@ export function liteClient(appId: string, apiKey: string, options?: ClientOption
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
logger: createNullLogger(),
requester: createHttpRequester(),
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
Expand Down
2 changes: 2 additions & 0 deletions packages/client-abtesting/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createFallbackableCache,
createBrowserLocalStorageCache,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function abtestingClient(
read: DEFAULT_READ_TIMEOUT_BROWSER,
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
},
logger: createNullLogger(),
requester: createXhrRequester(),
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
Expand Down
2 changes: 2 additions & 0 deletions packages/client-abtesting/builds/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createNullCache,
DEFAULT_CONNECT_TIMEOUT_NODE,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function abtestingClient(
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
logger: createNullLogger(),
algoliaAgents: [{ segment: 'Fetch' }],
requester: createFetchRequester(),
responsesCache: createNullCache(),
Expand Down
2 changes: 2 additions & 0 deletions packages/client-abtesting/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createNullCache,
DEFAULT_CONNECT_TIMEOUT_NODE,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function abtestingClient(
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
logger: createNullLogger(),
requester: createHttpRequester(),
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
Expand Down
3 changes: 1 addition & 2 deletions packages/client-abtesting/src/abtestingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,13 @@ export function createAbtestingClient({
const requestPath = '/2/abtests';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (offset !== undefined) {
queryParameters.offset = offset.toString();
}

if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (indexPrefix !== undefined) {
queryParameters.indexPrefix = indexPrefix.toString();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/client-analytics/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createFallbackableCache,
createBrowserLocalStorageCache,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function analyticsClient(
read: DEFAULT_READ_TIMEOUT_BROWSER,
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
},
logger: createNullLogger(),
requester: createXhrRequester(),
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
Expand Down
2 changes: 2 additions & 0 deletions packages/client-analytics/builds/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createNullCache,
DEFAULT_CONNECT_TIMEOUT_NODE,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function analyticsClient(
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
logger: createNullLogger(),
algoliaAgents: [{ segment: 'Fetch' }],
requester: createFetchRequester(),
responsesCache: createNullCache(),
Expand Down
2 changes: 2 additions & 0 deletions packages/client-analytics/builds/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ClientOptions } from '@algolia/client-common';
import {
createNullLogger,
createMemoryCache,
createNullCache,
DEFAULT_CONNECT_TIMEOUT_NODE,
Expand Down Expand Up @@ -46,6 +47,7 @@ export function analyticsClient(
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
logger: createNullLogger(),
requester: createHttpRequester(),
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
Expand Down
10 changes: 5 additions & 5 deletions packages/client-analytics/src/analyticsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,11 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -392,6 +394,7 @@ export function createAnalyticsClient({
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -430,6 +433,7 @@ export function createAnalyticsClient({
const requestPath = '/2/clicks/clickThroughRate';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}
Expand Down Expand Up @@ -531,7 +535,6 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -583,7 +586,6 @@ export function createAnalyticsClient({
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -622,10 +624,10 @@ export function createAnalyticsClient({
const requestPath = '/2/conversions/purchaseRate';
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -673,7 +675,6 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -725,7 +726,6 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down
9 changes: 5 additions & 4 deletions packages/client-common/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from './src/createAuth';
export * from './src/createIterablePromise';
export * from './src/cache';
export * from './src/transporter';
export * from './src/constants';
export * from './src/createAlgoliaAgent';
export * from './src/createAuth';
export * from './src/createIterablePromise';
export * from './src/getAlgoliaAgent';
export * from './src/logger';
export * from './src/transporter';
export * from './src/types';
export * from './src/constants';
24 changes: 24 additions & 0 deletions packages/client-common/src/__tests__/logger/null-logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint no-console: 0 */

import { vi, describe, test, expect } from 'vitest';

import { createNullLogger } from '../../logger';

describe('null logger', () => {
test('has a null behavior', async () => {
vi.resetAllMocks();
vi.spyOn(console, 'debug');
vi.spyOn(console, 'info');
vi.spyOn(console, 'error');

const logger = createNullLogger();

await logger.debug('foo', {});
await logger.info('foo', {});
await logger.error('foo', {});

expect(console.debug).toHaveBeenCalledTimes(0);
expect(console.info).toHaveBeenCalledTimes(0);
expect(console.error).toHaveBeenCalledTimes(0);
});
});
Loading

0 comments on commit f3650d5

Please sign in to comment.