From 21106d68483d0fd83edf6f4dbadeb718caab3a3a Mon Sep 17 00:00:00 2001 From: Seth Silesky Date: Wed, 24 Aug 2022 16:07:16 -0500 Subject: [PATCH] loosen coupling by using constructor injection, add internal --- packages/core/src/context/index.ts | 66 ++++++++----------- .../core/src/logger/__tests__/index.test.ts | 8 +-- packages/core/src/logger/index.ts | 14 ++-- packages/node/package.json | 2 +- yarn.lock | 4 +- 5 files changed, 41 insertions(+), 53 deletions(-) diff --git a/packages/core/src/context/index.ts b/packages/core/src/context/index.ts index cf0c7650b..92679f4d8 100644 --- a/packages/core/src/context/index.ts +++ b/packages/core/src/context/index.ts @@ -2,7 +2,7 @@ import { CoreSegmentEvent } from '../events/interfaces' import { v4 as uuid } from '@lukeed/uuid' import { dset } from 'dset' -import Logger, { LogLevel, LogMessage } from '../logger' +import { CoreLogger, LogLevel, LogMessage } from '../logger' import Stats, { Metric } from '../stats' export function isContext(v?: object): v is CoreContext { @@ -39,17 +39,23 @@ export class ContextCancelation { } export class CoreContext { - private _event: Event - private _attempts: number - public logger = new Logger() - private _id: string + event: Event + logger: CoreLogger + stats?: Stats + attempts = 0 + private _failedDelivery?: ContextFailedDelivery - public stats?: Stats + private _id: string - constructor(event: Event, id?: string, stats?: Stats) { - this._attempts = 0 - this._event = event - this._id = id ?? uuid() + constructor( + event: Event, + id = uuid(), + stats?: Stats, + logger: CoreLogger = new CoreLogger() + ) { + this.event = event + this._id = id + this.logger = logger this.stats = stats } @@ -58,7 +64,7 @@ export class CoreContext { } isSame(other: CoreContext): boolean { - return other._id === this._id + return other.id === this.id } cancel = (error?: Error | ContextCancelation): never => { @@ -73,53 +79,37 @@ export class CoreContext { this.logger.log(level, message, extras) } - public get id(): string { + get id(): string { return this._id } - public get event(): Event { - return this._event - } - - public set event(evt: Event) { - this._event = evt - } - - public get attempts(): number { - return this._attempts - } - - public set attempts(attempts: number) { - this._attempts = attempts - } - - public updateEvent(path: string, val: unknown): Event { + updateEvent(path: string, val: unknown): Event { // Don't allow integrations that are set to false to be overwritten with integration settings. if (path.split('.')[0] === 'integrations') { const integrationName = path.split('.')[1] - if (this._event.integrations?.[integrationName] === false) { - return this._event + if (this.event.integrations?.[integrationName] === false) { + return this.event } } - dset(this._event, path, val) - return this._event + dset(this.event, path, val) + return this.event } - public failedDelivery(): ContextFailedDelivery | undefined { + failedDelivery(): ContextFailedDelivery | undefined { return this._failedDelivery } - public setFailedDelivery(options: ContextFailedDelivery) { + setFailedDelivery(options: ContextFailedDelivery) { this._failedDelivery = options } - public logs(): LogMessage[] { + logs(): LogMessage[] { return this.logger.logs } - public flush(): void { + flush(): void { this.logger.flush() this.stats?.flush() } @@ -127,7 +117,7 @@ export class CoreContext { toJSON(): SerializedContext { return { id: this._id, - event: this._event, + event: this.event, logs: this.logger.logs, metrics: this.stats?.metrics, } diff --git a/packages/core/src/logger/__tests__/index.test.ts b/packages/core/src/logger/__tests__/index.test.ts index 4ce69d359..079c84578 100644 --- a/packages/core/src/logger/__tests__/index.test.ts +++ b/packages/core/src/logger/__tests__/index.test.ts @@ -1,10 +1,10 @@ -import Logger from '..' +import { CoreLogger } from '..' -describe(Logger, () => { - let logger: Logger +describe(CoreLogger, () => { + let logger: CoreLogger beforeEach(() => { - logger = new Logger() + logger = new CoreLogger() }) it('logs events at different levels', () => { diff --git a/packages/core/src/logger/index.ts b/packages/core/src/logger/index.ts index 6230e8427..fb1f6a33a 100644 --- a/packages/core/src/logger/index.ts +++ b/packages/core/src/logger/index.ts @@ -3,19 +3,17 @@ export type LogMessage = { level: LogLevel message: string time?: Date - extras?: object & { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any - } + extras?: Record } -export interface BaseLogger { - log(...args: any[]): void - flush(...args: any[]): void +// interface is just for clarity +export interface Logger { + log(level: LogLevel, message: string, extras?: object): void + flush(): void logs: LogMessage[] } -export default class Logger { +export class CoreLogger implements Logger { private _logs: LogMessage[] = [] log = (level: LogLevel, message: string, extras?: object): void => { diff --git a/packages/node/package.json b/packages/node/package.json index 0c6bd59fb..fd5e4ccc7 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -38,7 +38,7 @@ "tslib": "^2.4.0" }, "devDependencies": { - "@internal/config": "workspace:^", + "@internal/config": "0.0.0", "@types/node": "^12.12.14" }, "packageManager": "yarn@3.2.1" diff --git a/yarn.lock b/yarn.lock index 196793631..53518f0e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -939,7 +939,7 @@ __metadata: languageName: node linkType: hard -"@internal/config@workspace:^, @internal/config@workspace:internal/config": +"@internal/config@0.0.0, @internal/config@workspace:^, @internal/config@workspace:internal/config": version: 0.0.0-use.local resolution: "@internal/config@workspace:internal/config" languageName: unknown @@ -1580,7 +1580,7 @@ __metadata: version: 0.0.0-use.local resolution: "@segment/analytics-node@workspace:packages/node" dependencies: - "@internal/config": "workspace:^" + "@internal/config": 0.0.0 "@segment/analytics-core": 1.0.1 "@segment/analytics-plugin-validation": 0.0.0 "@types/node": ^12.12.14