-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement provideLumberjackCustomDrivers to allow the angular c…
…lient to use agnostic drivers
- Loading branch information
1 parent
771815b
commit 2c7f384
Showing
7 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
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
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
35 changes: 35 additions & 0 deletions
35
packages/lumberjackjs/angular/src/lib/configuration/provide-custom-drivers.spec.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,35 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { createNoopDriver } from '@internal/core/test-util'; | ||
import { LumberjackLevel } from '@lumberjackjs/core'; | ||
import { LumberjackConsoleDriver } from '@lumberjackjs/core/console-driver'; | ||
|
||
import { lumberjackLogDriverToken } from '../log-drivers/lumberjack-log-driver.token'; | ||
|
||
import { provideLumberjackCustomDrivers } from './provide-custom-drivers'; | ||
|
||
describe(provideLumberjackCustomDrivers.name, () => { | ||
const consoleDriver = new LumberjackConsoleDriver({ levels: [LumberjackLevel.Verbose] }); | ||
const noopDriver = createNoopDriver({ levels: [LumberjackLevel.Verbose] }); | ||
|
||
it('provides the custom driver', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideLumberjackCustomDrivers(consoleDriver)], | ||
}); | ||
const customDriver = TestBed.inject(lumberjackLogDriverToken); | ||
|
||
expect(customDriver).toBeDefined(); | ||
expect(customDriver).toEqual([consoleDriver]); | ||
}); | ||
|
||
it('provides the multiple custom drivers', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideLumberjackCustomDrivers([consoleDriver, noopDriver])], | ||
}); | ||
const customDrivers = TestBed.inject(lumberjackLogDriverToken); | ||
|
||
expect(customDrivers).toBeDefined(); | ||
expect(customDrivers.length).toBe(2); | ||
expect(customDrivers).toEqual([consoleDriver, noopDriver]); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/lumberjackjs/angular/src/lib/configuration/provide-custom-drivers.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,48 @@ | ||
import { Provider } from '@angular/core'; | ||
|
||
import { LumberjackLogDriver } from '@lumberjackjs/core'; | ||
|
||
import { lumberjackLogDriverToken } from '../log-drivers/lumberjack-log-driver.token'; | ||
|
||
/** | ||
* Returns the [dependency-injection providers](https://angular.io/guide/glossary#provider) | ||
* | ||
* for one or more custom/agnostic `LumberjackDriver` and its `LumberjackDriverConfig`. | ||
* @usageNotes | ||
* | ||
* The function is useful when you want to bootstrap an application using | ||
* the `bootstrapApplication` function and want to make available the `LumberjackDriver` providers. | ||
* | ||
* ```typescript | ||
* bootstrapApplication(RootComponent, { | ||
* providers: [ | ||
* provideLumberjack({...}), | ||
* provideLumberjackCustomDriver({...}) | ||
* ] | ||
* }); | ||
* ``` | ||
* | ||
* @publicApi | ||
*/ | ||
export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>(driver: Driver): Provider[]; | ||
export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>(drivers: Driver[]): Provider[]; | ||
|
||
export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>( | ||
drivers: Driver | Driver[] | ||
): Provider[] { | ||
if (Array.isArray(drivers)) { | ||
return drivers.map((driver) => ({ | ||
provide: lumberjackLogDriverToken, | ||
useValue: driver, | ||
multi: true, | ||
})); | ||
} | ||
|
||
return [ | ||
{ | ||
provide: lumberjackLogDriverToken, | ||
useValue: drivers, | ||
multi: true, | ||
}, | ||
]; | ||
} |
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