From f3e1ffd69280b629334d02fbafbaa886570f1e2c Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Mon, 11 Mar 2024 05:44:46 -0500 Subject: [PATCH] Switch to dual-type strategy for parsing Logger constructor args --- src/Logger.ts | 13 +++++++++---- src/types/logger.ts | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/Logger.ts b/src/Logger.ts index 36e59fb..be44295 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,4 +1,9 @@ -import { LogLabel, LogLevel, LogOutputs } from "./types/logger"; +import { + LogLabel, + LogLevel, + StrictLogConfig, + StrictLogOutputs, +} from "./types/logger"; import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities"; import { DEFAULT_STYLES } from "./lib/defaults"; @@ -24,13 +29,13 @@ export class Logger { private buffer: string[]; private bufferLength: number; private bucketIndex: BucketInfo[]; - private outputs: LogOutputs; + private outputs: StrictLogOutputs; private bufferCapacity: number; - constructor(config: Partial = {}) { + constructor(config: LogConfig = {}) { this.buffer = []; this.bufferLength = 0; - const parsedConfig = LogConfig.parse(config); + const parsedConfig = StrictLogConfig.parse(config); this.bufferCapacity = parsedConfig.bufferCapacity; this.outputs = parsedConfig.outputs; diff --git a/src/types/logger.ts b/src/types/logger.ts index a0b0a95..b6ef956 100644 --- a/src/types/logger.ts +++ b/src/types/logger.ts @@ -27,6 +27,33 @@ export interface ConsoleOutputOpts { } export const LogOutputs = z.object({ + console: z + .object({ + enabled: z.boolean().default(true).optional(), + }) + .default({}) + .optional(), + tampermonkey: z + .object({ + enabled: z.boolean().default(false).optional(), + maxBuckets: z.number().default(10).optional(), + bucketIndexKey: z.string().default("bucket_index").optional(), + }) + .default({}) + .optional(), + callback: z.function().args(z.string()).optional(), +}); + +export type LogOutputs = z.infer; + +export const LogConfig = z.object({ + outputs: LogOutputs.default({}).optional(), + bufferCapacity: z.number().default(100000).optional(), +}); + +export type LogConfig = z.infer; + +export const StrictLogOutputs = z.object({ console: z .object({ enabled: z.boolean().default(true), @@ -39,17 +66,17 @@ export const LogOutputs = z.object({ bucketIndexKey: z.string().default("bucket_index"), }) .default({}), - callback: z.function().args(z.string()).nullish(), + callback: z.function().args(z.string()).optional(), }); -export type LogOutputs = z.infer; +export type StrictLogOutputs = z.infer; -export const LogConfig = z.object({ - outputs: LogOutputs.default({}), +export const StrictLogConfig = z.object({ + outputs: StrictLogOutputs.default({}), bufferCapacity: z.number().default(100000), }); -export type LogConfig = z.infer; +export type StrictLogConfig = z.infer; export interface LogContext { level?: number;