-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add logger-console package from v4 (#3823)
- Loading branch information
Showing
28 changed files
with
364 additions
and
19 deletions.
There are no files selected for viewing
27 changes: 24 additions & 3 deletions
27
...iasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
...oliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.fetch.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
clients/algoliasearch-client-javascript/packages/client-common/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
...asearch-client-javascript/packages/client-common/src/__tests__/logger/null-logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
...nts/algoliasearch-client-javascript/packages/client-common/src/logger/createNullLogger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Logger } from '../types/logger'; | ||
|
||
export function createNullLogger(): Logger { | ||
return { | ||
debug(_message: string, _args?: any): Promise<void> { | ||
return Promise.resolve(); | ||
}, | ||
info(_message: string, _args?: any): Promise<void> { | ||
return Promise.resolve(); | ||
}, | ||
error(_message: string, _args?: any): Promise<void> { | ||
return Promise.resolve(); | ||
}, | ||
}; | ||
} |
1 change: 1 addition & 0 deletions
1
clients/algoliasearch-client-javascript/packages/client-common/src/logger/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './createNullLogger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
clients/algoliasearch-client-javascript/packages/client-common/src/types/logger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const LogLevelEnum: Readonly<Record<string, LogLevelType>> = { | ||
Debug: 1, | ||
Info: 2, | ||
Error: 3, | ||
}; | ||
|
||
export type LogLevelType = 1 | 2 | 3; | ||
|
||
export type Logger = { | ||
/** | ||
* Logs debug messages. | ||
*/ | ||
debug: (message: string, args?: any) => Promise<void>; | ||
|
||
/** | ||
* Logs info messages. | ||
*/ | ||
info: (message: string, args?: any) => Promise<void>; | ||
|
||
/** | ||
* Logs error messages. | ||
*/ | ||
error: (message: string, args?: any) => Promise<void>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
clients/algoliasearch-client-javascript/packages/logger-console/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/logger'; |
52 changes: 52 additions & 0 deletions
52
clients/algoliasearch-client-javascript/packages/logger-console/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "@algolia/logger-console", | ||
"version": "5.5.3", | ||
"description": "Promise-based log library using console log.", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/algolia/algoliasearch-client-javascript.git" | ||
}, | ||
"license": "MIT", | ||
"author": "Algolia", | ||
"type": "module", | ||
"files": [ | ||
"dist", | ||
"src", | ||
"index.ts" | ||
], | ||
"exports": { | ||
".": { | ||
"types": { | ||
"import": "./dist/logger.d.ts", | ||
"module": "./dist/logger.d.ts", | ||
"require": "./dist/logger.d.cts" | ||
}, | ||
"import": "./dist/logger.js", | ||
"module": "./dist/logger.js", | ||
"require": "./dist/logger.cjs" | ||
}, | ||
"./src/*": "./src/*.ts" | ||
}, | ||
"scripts": { | ||
"build": "yarn clean && yarn tsup", | ||
"clean": "rm -rf ./dist || true", | ||
"test": "vitest --run", | ||
"test:bundle": "publint . && attw --pack ." | ||
}, | ||
"devDependencies": { | ||
"@arethetypeswrong/cli": "0.16.4", | ||
"@types/node": "22.5.5", | ||
"jsdom": "25.0.0", | ||
"publint": "0.2.11", | ||
"ts-node": "10.9.2", | ||
"tsup": "8.3.0", | ||
"typescript": "5.6.2", | ||
"vitest": "2.1.1" | ||
}, | ||
"dependencies": { | ||
"@algolia/client-common": "5.5.3" | ||
}, | ||
"engines": { | ||
"node": ">= 14.0.0" | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...oliasearch-client-javascript/packages/logger-console/src/__tests__/logger-console.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint no-console: 0 */ | ||
|
||
import { LogLevelEnum } from '@algolia/client-common'; | ||
import { vi, beforeEach, describe, test, expect } from 'vitest'; | ||
|
||
import { createConsoleLogger } from '../logger'; | ||
|
||
describe('console logger', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
vi.spyOn(console, 'debug'); | ||
vi.spyOn(console, 'info'); | ||
vi.spyOn(console, 'error'); | ||
}); | ||
|
||
test('respects log level type debug', async () => { | ||
const logger = createConsoleLogger(LogLevelEnum.Debug); | ||
|
||
await logger.debug('foo', {}); | ||
await logger.info('foo', {}); | ||
await logger.error('foo', {}); | ||
|
||
expect(console.debug).toHaveBeenCalledTimes(1); | ||
expect(console.info).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('respects log level type info', async () => { | ||
const logger = createConsoleLogger(LogLevelEnum.Info); | ||
|
||
await logger.debug('foo', {}); | ||
await logger.info('foo', {}); | ||
await logger.error('foo', {}); | ||
|
||
expect(console.debug).toHaveBeenCalledTimes(0); | ||
expect(console.info).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('respects log level type error', async () => { | ||
const logger = createConsoleLogger(LogLevelEnum.Error); | ||
|
||
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(1); | ||
}); | ||
}); |
Oops, something went wrong.