diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 4d5fd16ab9d8..0b2fbfe0b62f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -110,6 +110,7 @@ libs/common/src/state-migrations @bitwarden/team-platform-dev libs/platform @bitwarden/team-platform-dev libs/storage-core @bitwarden/team-platform-dev libs/logging @bitwarden/team-platform-dev +libs/logging-angular @bitwarden/team-platform-dev libs/storage-test-utils @bitwarden/team-platform-dev libs/messaging @bitwarden/team-platform-dev libs/serialization @bitwarden/team-platform-dev diff --git a/jest.config.js b/jest.config.js index b3bae138b4cb..96f163128fbc 100644 --- a/jest.config.js +++ b/jest.config.js @@ -41,6 +41,7 @@ module.exports = { "/libs/key-management/jest.config.js", "/libs/key-management-ui/jest.config.js", "/libs/logging/jest.config.js", + "/libs/logging-angular/jest.config.js", "/libs/messaging/jest.config.js", "/libs/node/jest.config.js", "/libs/platform/jest.config.js", diff --git a/libs/logging-angular/README.md b/libs/logging-angular/README.md new file mode 100644 index 000000000000..0af65c046d56 --- /dev/null +++ b/libs/logging-angular/README.md @@ -0,0 +1,26 @@ +# logging-angular + +Owned by: platform + +Angular wrapper for [`@bitwarden/logging`](../logging/README.md). Provides `FlightRecorderService`, an +`@Injectable` subclass of `FlightRecorder` that wires SDK readiness automatically via +`SdkLoadService.Ready`. + +## Usage + +```typescript +import { FlightRecorderService } from "@bitwarden/logging-angular"; + +@Component({ + /* ... */ +}) +export class MyComponent { + private recorder = inject(FlightRecorderService); + + async viewEvents() { + const events = await this.recorder.read(); + } +} +``` + +See [`@bitwarden/logging`](../logging/README.md) for the full API surface inherited by this service. diff --git a/libs/logging-angular/eslint.config.mjs b/libs/logging-angular/eslint.config.mjs new file mode 100644 index 000000000000..9c37d10e3ff9 --- /dev/null +++ b/libs/logging-angular/eslint.config.mjs @@ -0,0 +1,3 @@ +import baseConfig from "../../eslint.config.mjs"; + +export default [...baseConfig]; diff --git a/libs/logging-angular/jest.config.js b/libs/logging-angular/jest.config.js new file mode 100644 index 000000000000..b1c9409dd050 --- /dev/null +++ b/libs/logging-angular/jest.config.js @@ -0,0 +1,16 @@ +const { pathsToModuleNameMapper } = require("ts-jest"); + +const { compilerOptions } = require("../../tsconfig.base"); + +const sharedConfig = require("../../libs/shared/jest.config.angular"); + +/** @type {import('jest').Config} */ +module.exports = { + ...sharedConfig, + displayName: "logging-angular", + setupFilesAfterEnv: ["/test.setup.ts"], + moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths ?? {}, { + prefix: "/../../", + }), + coverageDirectory: "../../coverage/libs/logging-angular", +}; diff --git a/libs/logging-angular/package.json b/libs/logging-angular/package.json new file mode 100644 index 000000000000..d08f953a4511 --- /dev/null +++ b/libs/logging-angular/package.json @@ -0,0 +1,15 @@ +{ + "name": "@bitwarden/logging-angular", + "version": "0.0.1", + "description": "Angular wrapper for @bitwarden/logging", + "private": true, + "type": "commonjs", + "main": "index.js", + "types": "index.d.ts", + "license": "GPL-3.0", + "author": "platform", + "scripts": { + "clean": "rimraf dist", + "build": "tsc --noEmit -p tsconfig.lib.json" + } +} diff --git a/libs/logging-angular/project.json b/libs/logging-angular/project.json new file mode 100644 index 000000000000..c987c2e8a8f4 --- /dev/null +++ b/libs/logging-angular/project.json @@ -0,0 +1,30 @@ +{ + "name": "logging-angular", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/logging-angular/src", + "projectType": "library", + "tags": [], + "targets": { + "build": { + "executor": "nx:run-script", + "dependsOn": [], + "options": { + "script": "build" + } + }, + "lint": { + "executor": "@nx/eslint:lint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["libs/logging-angular/**/*.ts"] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "libs/logging-angular/jest.config.js" + } + } + } +} diff --git a/libs/logging-angular/src/flight-recorder.service.ts b/libs/logging-angular/src/flight-recorder.service.ts new file mode 100644 index 000000000000..3106156825fa --- /dev/null +++ b/libs/logging-angular/src/flight-recorder.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from "@angular/core"; + +import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service"; +import { FlightRecorder } from "@bitwarden/logging"; + +/** + * Angular wrapper for {@link FlightRecorder}. + * + * Provides the flight recorder as an injectable service, wiring it + * to {@link SdkLoadService.Ready} automatically. + */ +@Injectable({ providedIn: "root" }) +export class FlightRecorderService extends FlightRecorder { + constructor() { + super(SdkLoadService.Ready); + } +} diff --git a/libs/logging-angular/src/index.ts b/libs/logging-angular/src/index.ts new file mode 100644 index 000000000000..4da61ec5bcb8 --- /dev/null +++ b/libs/logging-angular/src/index.ts @@ -0,0 +1,3 @@ +export { FlightRecorderService } from "./flight-recorder.service"; +// Re-export types from logging for convenience +export { FlightRecorderEvent } from "@bitwarden/logging"; diff --git a/libs/logging-angular/src/logging-angular.spec.ts b/libs/logging-angular/src/logging-angular.spec.ts new file mode 100644 index 000000000000..d2757a50f9e4 --- /dev/null +++ b/libs/logging-angular/src/logging-angular.spec.ts @@ -0,0 +1,23 @@ +import { TestBed } from "@angular/core/testing"; + +import { FlightRecorder } from "@bitwarden/logging"; + +import { FlightRecorderService } from "./index"; + +describe("FlightRecorderService", () => { + beforeEach(() => { + TestBed.configureTestingModule({}); + }); + + it("is injectable via Angular DI", () => { + expect(TestBed.inject(FlightRecorderService)).toBeInstanceOf(FlightRecorderService); + }); + + it("is a singleton at the root injector", () => { + expect(TestBed.inject(FlightRecorderService)).toBe(TestBed.inject(FlightRecorderService)); + }); + + it("inherits from FlightRecorder", () => { + expect(TestBed.inject(FlightRecorderService)).toBeInstanceOf(FlightRecorder); + }); +}); diff --git a/libs/logging-angular/test.setup.ts b/libs/logging-angular/test.setup.ts new file mode 100644 index 000000000000..3f9ef28ad007 --- /dev/null +++ b/libs/logging-angular/test.setup.ts @@ -0,0 +1 @@ +import "@bitwarden/ui-common/setup-jest"; diff --git a/libs/logging-angular/tsconfig.eslint.json b/libs/logging-angular/tsconfig.eslint.json new file mode 100644 index 000000000000..3daf120441ad --- /dev/null +++ b/libs/logging-angular/tsconfig.eslint.json @@ -0,0 +1,6 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": ["src/**/*.ts", "src/**/*.js"], + "exclude": ["**/build", "**/dist"] +} diff --git a/libs/logging-angular/tsconfig.json b/libs/logging-angular/tsconfig.json new file mode 100644 index 000000000000..62ebbd946474 --- /dev/null +++ b/libs/logging-angular/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/logging-angular/tsconfig.lib.json b/libs/logging-angular/tsconfig.lib.json new file mode 100644 index 000000000000..9cbf67360077 --- /dev/null +++ b/libs/logging-angular/tsconfig.lib.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["jest.config.js", "src/**/*.spec.ts"] +} diff --git a/libs/logging-angular/tsconfig.spec.json b/libs/logging-angular/tsconfig.spec.json new file mode 100644 index 000000000000..1275f148a18c --- /dev/null +++ b/libs/logging-angular/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "moduleResolution": "node10", + "types": ["jest", "node"] + }, + "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"] +} diff --git a/package-lock.json b/package-lock.json index e4956bdeca16..740ab1a1f1d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -536,6 +536,11 @@ "version": "0.0.1", "license": "GPL-3.0" }, + "libs/logging-angular": { + "name": "@bitwarden/logging-angular", + "version": "0.0.1", + "license": "GPL-3.0" + }, "libs/messaging": { "name": "@bitwarden/messaging", "version": "0.0.1", @@ -5055,6 +5060,10 @@ "resolved": "libs/logging", "link": true }, + "node_modules/@bitwarden/logging-angular": { + "resolved": "libs/logging-angular", + "link": true + }, "node_modules/@bitwarden/messaging": { "resolved": "libs/messaging", "link": true diff --git a/tsconfig.base.json b/tsconfig.base.json index 3aba1a13c7a5..c4d93333548f 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -47,6 +47,7 @@ "@bitwarden/key-management": ["./libs/key-management/src"], "@bitwarden/key-management-ui": ["./libs/key-management-ui/src"], "@bitwarden/logging": ["./libs/logging/src"], + "@bitwarden/logging-angular": ["./libs/logging-angular/src/index.ts"], "@bitwarden/messaging": ["./libs/messaging/src/index.ts"], "@bitwarden/node/*": ["./libs/node/src/*"], "@bitwarden/nx-plugin": ["./libs/nx-plugin/src/index.ts"],