@mohasinac/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Logger Class — @mohasinac/core
3
+ *
4
+ * Singleton class for application logging.
5
+ * Pure utility — no framework or app-specific imports.
6
+ *
7
+ * The `logFileUrl` constructor option replaces the previous app-specific
8
+ * `import("@/constants")` call, making this usable in any context.
9
+ */
10
+ type LogLevel = "debug" | "info" | "warn" | "error";
11
+ interface LogEntry {
12
+ level: LogLevel;
13
+ message: string;
14
+ timestamp: Date;
15
+ data?: unknown;
16
+ }
17
+ interface LoggerOptions {
18
+ minLevel?: LogLevel;
19
+ enableConsole?: boolean;
20
+ enableStorage?: boolean;
21
+ /**
22
+ * URL to POST error log entries. Set to your `/api/logs/write` route.
23
+ */
24
+ logFileUrl?: string;
25
+ /**
26
+ * @deprecated Prefer `logFileUrl`. When `true` and `logFileUrl` is not set,
27
+ * defaults to `/api/logs/write` for backward compatibility with the
28
+ * previous app-coupled Logger implementation.
29
+ */
30
+ enableFileLogging?: boolean;
31
+ maxEntries?: number;
32
+ /** Optional function to sanitize data before logging (e.g., PII redaction). */
33
+ sanitizer?: (data: unknown) => unknown;
34
+ }
35
+ declare class Logger {
36
+ private static instance;
37
+ private logs;
38
+ private options;
39
+ private levelPriority;
40
+ private constructor();
41
+ /** Get singleton instance */
42
+ static getInstance(options?: LoggerOptions): Logger;
43
+ /** Set or update the data sanitizer (e.g., for PII redaction). */
44
+ static setSanitizer(fn: (data: unknown) => unknown): void;
45
+ private shouldLog;
46
+ private addLog;
47
+ private logToConsole;
48
+ private saveToStorage;
49
+ private writeToFile;
50
+ debug(message: string, data?: unknown): void;
51
+ info(message: string, data?: unknown): void;
52
+ warn(message: string, data?: unknown): void;
53
+ error(message: string, data?: unknown): void;
54
+ getLogs(level?: LogLevel): LogEntry[];
55
+ clear(): void;
56
+ export(): string;
57
+ getStats(): Record<LogLevel, number>;
58
+ }
59
+ /** Shared singleton instance */
60
+ declare const logger: Logger;
61
+
62
+ /**
63
+ * Queue Class — @mohasinac/core
64
+ *
65
+ * Generic priority queue for task management.
66
+ * Pure utility — no framework or app-specific imports.
67
+ */
68
+ interface QueueOptions {
69
+ concurrency?: number;
70
+ autoStart?: boolean;
71
+ }
72
+ interface Task<T = unknown> {
73
+ id: string;
74
+ fn: () => Promise<T>;
75
+ priority?: number;
76
+ }
77
+ declare class Queue<T = unknown> {
78
+ private tasks;
79
+ private running;
80
+ private readonly concurrency;
81
+ private autoStart;
82
+ private results;
83
+ private errors;
84
+ constructor(options?: QueueOptions);
85
+ /** Add a task to the queue */
86
+ add(id: string, fn: () => Promise<T>, priority?: number): void;
87
+ private process;
88
+ start(): void;
89
+ pause(): void;
90
+ resume(): void;
91
+ clear(): void;
92
+ getResult(id: string): T | undefined;
93
+ getError(id: string): Error | undefined;
94
+ size(): number;
95
+ getRunning(): number;
96
+ isEmpty(): boolean;
97
+ waitForCompletion(): Promise<void>;
98
+ getStats(): {
99
+ pending: number;
100
+ running: number;
101
+ completed: number;
102
+ failed: number;
103
+ };
104
+ }
105
+
106
+ /**
107
+ * StorageManager Class — @mohasinac/core
108
+ *
109
+ * Singleton class for managing localStorage and sessionStorage with
110
+ * namespace prefixing and SSR-safe fallbacks.
111
+ * Pure utility — no framework or app-specific imports.
112
+ */
113
+ type StorageType = "local" | "session";
114
+ interface StorageOptions {
115
+ type?: StorageType;
116
+ prefix?: string;
117
+ }
118
+ declare class StorageManager {
119
+ /** Per-prefix instance map — prevents namespace collisions */
120
+ private static instances;
121
+ private readonly prefix;
122
+ private constructor();
123
+ /**
124
+ * Get-or-create a StorageManager for the given prefix.
125
+ * Each prefix returns an independent instance.
126
+ */
127
+ static getInstance(prefix?: string): StorageManager;
128
+ private getStorage;
129
+ private generateKey;
130
+ set<T>(key: string, value: T, options?: StorageOptions): boolean;
131
+ get<T>(key: string, options?: StorageOptions): T | null;
132
+ remove(key: string, options?: StorageOptions): boolean;
133
+ clear(options?: StorageOptions): boolean;
134
+ has(key: string, options?: StorageOptions): boolean;
135
+ keys(options?: StorageOptions): string[];
136
+ size(options?: StorageOptions): number;
137
+ isAvailable(type?: StorageType): boolean;
138
+ getAll<T = unknown>(options?: StorageOptions): Record<string, T>;
139
+ }
140
+ /** Default StorageManager instance (empty prefix) */
141
+ declare const storageManager: StorageManager;
142
+
143
+ /**
144
+ * EventBus Class — @mohasinac/core
145
+ *
146
+ * Singleton pub/sub event bus for decoupled communication.
147
+ * Pure utility — no framework or app-specific imports.
148
+ */
149
+ type EventCallback = (...args: any[]) => void;
150
+ interface EventSubscription {
151
+ unsubscribe: () => void;
152
+ }
153
+ declare class EventBus {
154
+ private static instance;
155
+ private readonly events;
156
+ private constructor();
157
+ static getInstance(): EventBus;
158
+ on(event: string, callback: EventCallback): EventSubscription;
159
+ once(event: string, callback: EventCallback): EventSubscription;
160
+ off(event: string, callback: EventCallback): void;
161
+ emit(event: string, ...args: any[]): void;
162
+ removeAllListeners(event?: string): void;
163
+ listenerCount(event: string): number;
164
+ eventNames(): string[];
165
+ hasListeners(event: string): boolean;
166
+ }
167
+ /** Shared singleton instance */
168
+ declare const eventBus: EventBus;
169
+
170
+ /**
171
+ * CacheManager Class — @mohasinac/core
172
+ *
173
+ * Simple TTL-based in-memory cache with FIFO eviction.
174
+ * Pure utility — no framework or app-specific imports.
175
+ */
176
+ interface CacheOptions {
177
+ ttl?: number;
178
+ }
179
+ interface CacheEntry<T> {
180
+ value: T;
181
+ timestamp: number;
182
+ ttl?: number;
183
+ }
184
+ declare class CacheManager {
185
+ private static instance;
186
+ private readonly cache;
187
+ private readonly maxSize;
188
+ private constructor();
189
+ static getInstance(maxSize?: number): CacheManager;
190
+ set<T>(key: string, value: T, options?: CacheOptions): void;
191
+ get<T>(key: string): T | null;
192
+ has(key: string): boolean;
193
+ delete(key: string): boolean;
194
+ clear(): void;
195
+ size(): number;
196
+ keys(): string[];
197
+ cleanExpired(): number;
198
+ }
199
+ /** Default shared CacheManager singleton */
200
+ declare const cacheManager: CacheManager;
201
+
202
+ export { type CacheEntry, CacheManager, type CacheOptions, EventBus, type EventSubscription, type LogEntry, type LogLevel, Logger, type LoggerOptions, Queue, type QueueOptions, StorageManager, type StorageOptions, type StorageType, type Task, cacheManager, eventBus, logger, storageManager };
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Logger Class — @mohasinac/core
3
+ *
4
+ * Singleton class for application logging.
5
+ * Pure utility — no framework or app-specific imports.
6
+ *
7
+ * The `logFileUrl` constructor option replaces the previous app-specific
8
+ * `import("@/constants")` call, making this usable in any context.
9
+ */
10
+ type LogLevel = "debug" | "info" | "warn" | "error";
11
+ interface LogEntry {
12
+ level: LogLevel;
13
+ message: string;
14
+ timestamp: Date;
15
+ data?: unknown;
16
+ }
17
+ interface LoggerOptions {
18
+ minLevel?: LogLevel;
19
+ enableConsole?: boolean;
20
+ enableStorage?: boolean;
21
+ /**
22
+ * URL to POST error log entries. Set to your `/api/logs/write` route.
23
+ */
24
+ logFileUrl?: string;
25
+ /**
26
+ * @deprecated Prefer `logFileUrl`. When `true` and `logFileUrl` is not set,
27
+ * defaults to `/api/logs/write` for backward compatibility with the
28
+ * previous app-coupled Logger implementation.
29
+ */
30
+ enableFileLogging?: boolean;
31
+ maxEntries?: number;
32
+ /** Optional function to sanitize data before logging (e.g., PII redaction). */
33
+ sanitizer?: (data: unknown) => unknown;
34
+ }
35
+ declare class Logger {
36
+ private static instance;
37
+ private logs;
38
+ private options;
39
+ private levelPriority;
40
+ private constructor();
41
+ /** Get singleton instance */
42
+ static getInstance(options?: LoggerOptions): Logger;
43
+ /** Set or update the data sanitizer (e.g., for PII redaction). */
44
+ static setSanitizer(fn: (data: unknown) => unknown): void;
45
+ private shouldLog;
46
+ private addLog;
47
+ private logToConsole;
48
+ private saveToStorage;
49
+ private writeToFile;
50
+ debug(message: string, data?: unknown): void;
51
+ info(message: string, data?: unknown): void;
52
+ warn(message: string, data?: unknown): void;
53
+ error(message: string, data?: unknown): void;
54
+ getLogs(level?: LogLevel): LogEntry[];
55
+ clear(): void;
56
+ export(): string;
57
+ getStats(): Record<LogLevel, number>;
58
+ }
59
+ /** Shared singleton instance */
60
+ declare const logger: Logger;
61
+
62
+ /**
63
+ * Queue Class — @mohasinac/core
64
+ *
65
+ * Generic priority queue for task management.
66
+ * Pure utility — no framework or app-specific imports.
67
+ */
68
+ interface QueueOptions {
69
+ concurrency?: number;
70
+ autoStart?: boolean;
71
+ }
72
+ interface Task<T = unknown> {
73
+ id: string;
74
+ fn: () => Promise<T>;
75
+ priority?: number;
76
+ }
77
+ declare class Queue<T = unknown> {
78
+ private tasks;
79
+ private running;
80
+ private readonly concurrency;
81
+ private autoStart;
82
+ private results;
83
+ private errors;
84
+ constructor(options?: QueueOptions);
85
+ /** Add a task to the queue */
86
+ add(id: string, fn: () => Promise<T>, priority?: number): void;
87
+ private process;
88
+ start(): void;
89
+ pause(): void;
90
+ resume(): void;
91
+ clear(): void;
92
+ getResult(id: string): T | undefined;
93
+ getError(id: string): Error | undefined;
94
+ size(): number;
95
+ getRunning(): number;
96
+ isEmpty(): boolean;
97
+ waitForCompletion(): Promise<void>;
98
+ getStats(): {
99
+ pending: number;
100
+ running: number;
101
+ completed: number;
102
+ failed: number;
103
+ };
104
+ }
105
+
106
+ /**
107
+ * StorageManager Class — @mohasinac/core
108
+ *
109
+ * Singleton class for managing localStorage and sessionStorage with
110
+ * namespace prefixing and SSR-safe fallbacks.
111
+ * Pure utility — no framework or app-specific imports.
112
+ */
113
+ type StorageType = "local" | "session";
114
+ interface StorageOptions {
115
+ type?: StorageType;
116
+ prefix?: string;
117
+ }
118
+ declare class StorageManager {
119
+ /** Per-prefix instance map — prevents namespace collisions */
120
+ private static instances;
121
+ private readonly prefix;
122
+ private constructor();
123
+ /**
124
+ * Get-or-create a StorageManager for the given prefix.
125
+ * Each prefix returns an independent instance.
126
+ */
127
+ static getInstance(prefix?: string): StorageManager;
128
+ private getStorage;
129
+ private generateKey;
130
+ set<T>(key: string, value: T, options?: StorageOptions): boolean;
131
+ get<T>(key: string, options?: StorageOptions): T | null;
132
+ remove(key: string, options?: StorageOptions): boolean;
133
+ clear(options?: StorageOptions): boolean;
134
+ has(key: string, options?: StorageOptions): boolean;
135
+ keys(options?: StorageOptions): string[];
136
+ size(options?: StorageOptions): number;
137
+ isAvailable(type?: StorageType): boolean;
138
+ getAll<T = unknown>(options?: StorageOptions): Record<string, T>;
139
+ }
140
+ /** Default StorageManager instance (empty prefix) */
141
+ declare const storageManager: StorageManager;
142
+
143
+ /**
144
+ * EventBus Class — @mohasinac/core
145
+ *
146
+ * Singleton pub/sub event bus for decoupled communication.
147
+ * Pure utility — no framework or app-specific imports.
148
+ */
149
+ type EventCallback = (...args: any[]) => void;
150
+ interface EventSubscription {
151
+ unsubscribe: () => void;
152
+ }
153
+ declare class EventBus {
154
+ private static instance;
155
+ private readonly events;
156
+ private constructor();
157
+ static getInstance(): EventBus;
158
+ on(event: string, callback: EventCallback): EventSubscription;
159
+ once(event: string, callback: EventCallback): EventSubscription;
160
+ off(event: string, callback: EventCallback): void;
161
+ emit(event: string, ...args: any[]): void;
162
+ removeAllListeners(event?: string): void;
163
+ listenerCount(event: string): number;
164
+ eventNames(): string[];
165
+ hasListeners(event: string): boolean;
166
+ }
167
+ /** Shared singleton instance */
168
+ declare const eventBus: EventBus;
169
+
170
+ /**
171
+ * CacheManager Class — @mohasinac/core
172
+ *
173
+ * Simple TTL-based in-memory cache with FIFO eviction.
174
+ * Pure utility — no framework or app-specific imports.
175
+ */
176
+ interface CacheOptions {
177
+ ttl?: number;
178
+ }
179
+ interface CacheEntry<T> {
180
+ value: T;
181
+ timestamp: number;
182
+ ttl?: number;
183
+ }
184
+ declare class CacheManager {
185
+ private static instance;
186
+ private readonly cache;
187
+ private readonly maxSize;
188
+ private constructor();
189
+ static getInstance(maxSize?: number): CacheManager;
190
+ set<T>(key: string, value: T, options?: CacheOptions): void;
191
+ get<T>(key: string): T | null;
192
+ has(key: string): boolean;
193
+ delete(key: string): boolean;
194
+ clear(): void;
195
+ size(): number;
196
+ keys(): string[];
197
+ cleanExpired(): number;
198
+ }
199
+ /** Default shared CacheManager singleton */
200
+ declare const cacheManager: CacheManager;
201
+
202
+ export { type CacheEntry, CacheManager, type CacheOptions, EventBus, type EventSubscription, type LogEntry, type LogLevel, Logger, type LoggerOptions, Queue, type QueueOptions, StorageManager, type StorageOptions, type StorageType, type Task, cacheManager, eventBus, logger, storageManager };