@oxog/types 1.0.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.
- package/LICENSE +21 -0
- package/README.md +408 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +945 -0
- package/dist/index.d.ts +945 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/metafile-cjs.json +1 -0
- package/dist/metafile-esm.json +1 -0
- package/package.json +66 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,945 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oxog/types - Common TypeScript utility types
|
|
3
|
+
* @version 1.0.0
|
|
4
|
+
* @author Ersin Koç
|
|
5
|
+
*/
|
|
6
|
+
/** Brand symbol for branded types */
|
|
7
|
+
declare const __brand: unique symbol;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a branded type for type-safe identifiers.
|
|
10
|
+
*
|
|
11
|
+
* Branded types allow you to create distinct types that share the same
|
|
12
|
+
* underlying representation, preventing accidental type mixing.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* type UserId = Branded<string, 'UserId'>;
|
|
17
|
+
* type OrderId = Branded<string, 'OrderId'>;
|
|
18
|
+
*
|
|
19
|
+
* const userId: UserId = 'user_123' as UserId;
|
|
20
|
+
* const orderId: OrderId = 'order_456' as OrderId;
|
|
21
|
+
*
|
|
22
|
+
* // Type error: Type 'UserId' is not assignable to type 'OrderId'
|
|
23
|
+
* const wrong: OrderId = userId;
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
type Branded<T, B extends string> = T & {
|
|
27
|
+
readonly [__brand]: B;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Shorthand for Branded.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* type ProductId = Brand<number, 'ProductId'>;
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
type Brand<T, B extends string> = Branded<T, B>;
|
|
38
|
+
/**
|
|
39
|
+
* Deep partial - all nested properties optional.
|
|
40
|
+
*
|
|
41
|
+
* Recursively makes all properties and nested properties optional.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* interface User {
|
|
46
|
+
* name: string;
|
|
47
|
+
* address: {
|
|
48
|
+
* street: string;
|
|
49
|
+
* city: string;
|
|
50
|
+
* };
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* const partial: DeepPartial<User> = {
|
|
54
|
+
* name: 'John',
|
|
55
|
+
* address: {
|
|
56
|
+
* street: 'Main St'
|
|
57
|
+
* // city is optional
|
|
58
|
+
* }
|
|
59
|
+
* };
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
type DeepPartial<T> = T extends object ? {
|
|
63
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
64
|
+
} : T;
|
|
65
|
+
/**
|
|
66
|
+
* Deep readonly - all nested properties readonly.
|
|
67
|
+
*
|
|
68
|
+
* Recursively makes all properties and nested properties readonly.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* interface Config {
|
|
73
|
+
* api: {
|
|
74
|
+
* url: string;
|
|
75
|
+
* timeout: number;
|
|
76
|
+
* };
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* const readonlyConfig: DeepReadonly<Config> = {
|
|
80
|
+
* api: {
|
|
81
|
+
* url: 'https://api.example.com',
|
|
82
|
+
* timeout: 5000
|
|
83
|
+
* }
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* // Type error: Cannot assign to 'url' because it is a read-only property
|
|
87
|
+
* readonlyConfig.api.url = 'https://other.com';
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
type DeepReadonly<T> = T extends object ? {
|
|
91
|
+
readonly [P in keyof T]: DeepReadonly<T[P]>;
|
|
92
|
+
} : T;
|
|
93
|
+
/**
|
|
94
|
+
* Deep required - all nested properties required.
|
|
95
|
+
*
|
|
96
|
+
* Recursively removes optional markers from all properties and nested properties.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* interface OptionalUser {
|
|
101
|
+
* name?: string;
|
|
102
|
+
* address?: {
|
|
103
|
+
* street?: string;
|
|
104
|
+
* city?: string;
|
|
105
|
+
* };
|
|
106
|
+
* }
|
|
107
|
+
*
|
|
108
|
+
* const required: DeepRequired<OptionalUser> = {
|
|
109
|
+
* name: 'John',
|
|
110
|
+
* address: {
|
|
111
|
+
* street: 'Main St',
|
|
112
|
+
* city: 'NYC'
|
|
113
|
+
* }
|
|
114
|
+
* };
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
type DeepRequired<T> = T extends object ? {
|
|
118
|
+
[P in keyof T]-?: DeepRequired<T[P]>;
|
|
119
|
+
} : T;
|
|
120
|
+
/**
|
|
121
|
+
* Value that may be a promise.
|
|
122
|
+
*
|
|
123
|
+
* Represents a value that can be either synchronous or asynchronous.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* function process(data: string): MaybePromise<string> {
|
|
128
|
+
* if (useCache) {
|
|
129
|
+
* return cachedData; // Synchronous
|
|
130
|
+
* }
|
|
131
|
+
* return fetchData(); // Promise
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
136
|
+
/**
|
|
137
|
+
* Any function type.
|
|
138
|
+
*
|
|
139
|
+
* Represents any callable function with any arguments and return type.
|
|
140
|
+
*/
|
|
141
|
+
type AnyFunction = (...args: any[]) => any;
|
|
142
|
+
/**
|
|
143
|
+
* Async function type.
|
|
144
|
+
*
|
|
145
|
+
* Represents an asynchronous function.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const fetchData: AsyncFunction<string> = async () => {
|
|
150
|
+
* return await fetch('/api/data').then(r => r.text());
|
|
151
|
+
* };
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
type AsyncFunction<T = unknown> = (...args: any[]) => Promise<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Sync function type.
|
|
157
|
+
*
|
|
158
|
+
* Represents a synchronous function.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const add: SyncFunction<number> = (a: number, b: number) => {
|
|
163
|
+
* return a + b;
|
|
164
|
+
* };
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
type SyncFunction<T = unknown> = (...args: any[]) => T;
|
|
168
|
+
/**
|
|
169
|
+
* JSON primitive types.
|
|
170
|
+
*
|
|
171
|
+
* Represents valid JSON primitive values.
|
|
172
|
+
*/
|
|
173
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
174
|
+
/**
|
|
175
|
+
* JSON array type.
|
|
176
|
+
*
|
|
177
|
+
* Represents a JSON array of JsonValue elements.
|
|
178
|
+
*/
|
|
179
|
+
type JsonArray = JsonValue[];
|
|
180
|
+
/**
|
|
181
|
+
* JSON object type.
|
|
182
|
+
*
|
|
183
|
+
* Represents a JSON object with string keys and JsonValue values.
|
|
184
|
+
*/
|
|
185
|
+
type JsonObject = {
|
|
186
|
+
[key: string]: JsonValue;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Any valid JSON value.
|
|
190
|
+
*
|
|
191
|
+
* Represents any value that can be serialized to JSON.
|
|
192
|
+
*/
|
|
193
|
+
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
|
|
194
|
+
/**
|
|
195
|
+
* Make type prettier in IDE.
|
|
196
|
+
*
|
|
197
|
+
* Utility type that flattens type structure for better IDE display.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* type ComplexType = { a: string } & { b: number };
|
|
202
|
+
* type PrettyType = Prettify<ComplexType>;
|
|
203
|
+
* // IDE shows properties directly instead of intersection
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
type Prettify<T> = {
|
|
207
|
+
[K in keyof T]: T[K];
|
|
208
|
+
} & {};
|
|
209
|
+
/**
|
|
210
|
+
* Strict omit that requires valid keys.
|
|
211
|
+
*
|
|
212
|
+
* Like TypeScript's Omit, but ensures the keys exist in the type.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* interface User {
|
|
217
|
+
* id: number;
|
|
218
|
+
* name: string;
|
|
219
|
+
* email: string;
|
|
220
|
+
* }
|
|
221
|
+
*
|
|
222
|
+
* // Only 'id' and 'name' allowed - TypeScript will error on invalid keys
|
|
223
|
+
* type UserWithoutEmail = StrictOmit<User, 'email'>;
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
type StrictOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
227
|
+
/**
|
|
228
|
+
* Strict pick that requires valid keys.
|
|
229
|
+
*
|
|
230
|
+
* Like TypeScript's Pick, but ensures the keys exist in the type.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* interface User {
|
|
235
|
+
* id: number;
|
|
236
|
+
* name: string;
|
|
237
|
+
* email: string;
|
|
238
|
+
* }
|
|
239
|
+
*
|
|
240
|
+
* // Only 'id' and 'name' allowed - TypeScript will error on invalid keys
|
|
241
|
+
* type UserBasicInfo = StrictPick<User, 'id' | 'name'>;
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
type StrictPick<T, K extends keyof T> = Pick<T, K>;
|
|
245
|
+
/**
|
|
246
|
+
* Array with at least one element.
|
|
247
|
+
*
|
|
248
|
+
* Represents arrays that are guaranteed to have at least one element.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* const nonEmpty: NonEmptyArray<number> = [1];
|
|
253
|
+
* const nonEmpty2: NonEmptyArray<string> = ['hello', 'world'];
|
|
254
|
+
*
|
|
255
|
+
* // Type error: Argument of type 'number[]' is not assignable
|
|
256
|
+
* const empty: NonEmptyArray<number> = [];
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
type NonEmptyArray<T> = [T, ...T[]];
|
|
260
|
+
/**
|
|
261
|
+
* Value that can be null.
|
|
262
|
+
*
|
|
263
|
+
* Alias for nullable types.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* const value: Nullable<string> = null;
|
|
268
|
+
* const value2: Nullable<number> = 42;
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
type Nullable<T> = T | null;
|
|
272
|
+
/**
|
|
273
|
+
* Value that can be undefined.
|
|
274
|
+
*
|
|
275
|
+
* Alias for optional types.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```typescript
|
|
279
|
+
* const value: Optional<string> = undefined;
|
|
280
|
+
* const value2: Optional<number> = 123;
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
type Optional<T> = T | undefined;
|
|
284
|
+
/**
|
|
285
|
+
* Unsubscribe function returned by event subscriptions.
|
|
286
|
+
*
|
|
287
|
+
* Call this function to unsubscribe from an event.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* const unsubscribe = eventEmitter.on('event', handler);
|
|
292
|
+
* // Later, when done listening:
|
|
293
|
+
* unsubscribe();
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
type Unsubscribe = () => void;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* @oxog/types - Plugin and Kernel interfaces
|
|
300
|
+
* @version 1.0.0
|
|
301
|
+
* @author Ersin Koç
|
|
302
|
+
*/
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Standard plugin interface for @oxog ecosystem.
|
|
306
|
+
*
|
|
307
|
+
* A plugin is a self-contained module that can be registered with a Kernel.
|
|
308
|
+
* Plugins can depend on other plugins and communicate through the Kernel's
|
|
309
|
+
* event system.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const myPlugin: Plugin = {
|
|
314
|
+
* name: 'my-plugin',
|
|
315
|
+
* version: '1.0.0',
|
|
316
|
+
* dependencies: ['logger'],
|
|
317
|
+
* install(kernel) {
|
|
318
|
+
* kernel.on('init', () => console.log('Plugin installed'));
|
|
319
|
+
* },
|
|
320
|
+
* onInit(context) {
|
|
321
|
+
* // Called after all plugins installed
|
|
322
|
+
* },
|
|
323
|
+
* onDestroy() {
|
|
324
|
+
* // Cleanup resources
|
|
325
|
+
* },
|
|
326
|
+
* onError(error) {
|
|
327
|
+
* console.error('Plugin error:', error);
|
|
328
|
+
* }
|
|
329
|
+
* };
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
interface Plugin<TContext = unknown> {
|
|
333
|
+
/** Unique plugin identifier (kebab-case) */
|
|
334
|
+
readonly name: string;
|
|
335
|
+
/** Semantic version (e.g., "1.0.0") */
|
|
336
|
+
readonly version: string;
|
|
337
|
+
/** Plugin dependencies by name */
|
|
338
|
+
readonly dependencies?: readonly string[];
|
|
339
|
+
/** Called when plugin is registered with the kernel */
|
|
340
|
+
install: (kernel: Kernel<TContext>) => void;
|
|
341
|
+
/** Called after ALL plugins are installed */
|
|
342
|
+
onInit?: (context: TContext) => MaybePromise<void>;
|
|
343
|
+
/** Called when plugin is unregistered */
|
|
344
|
+
onDestroy?: () => MaybePromise<void>;
|
|
345
|
+
/** Called when an error occurs in this plugin */
|
|
346
|
+
onError?: (error: Error) => void;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Micro-kernel interface for @oxog packages.
|
|
350
|
+
*
|
|
351
|
+
* The Kernel manages plugins and provides event communication between them.
|
|
352
|
+
* It maintains a plugin registry and exposes methods for plugin lifecycle
|
|
353
|
+
* management.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* // Register a plugin
|
|
358
|
+
* kernel.use(myPlugin);
|
|
359
|
+
*
|
|
360
|
+
* // Get a plugin
|
|
361
|
+
* const plugin = kernel.getPlugin('logger');
|
|
362
|
+
*
|
|
363
|
+
* // Check if plugin exists
|
|
364
|
+
* if (kernel.hasPlugin('logger')) {
|
|
365
|
+
* // Plugin is registered
|
|
366
|
+
* }
|
|
367
|
+
*
|
|
368
|
+
* // Emit event
|
|
369
|
+
* kernel.emit('user:login', { userId: '123' });
|
|
370
|
+
*
|
|
371
|
+
* // Subscribe to events
|
|
372
|
+
* const unsubscribe = kernel.on('user:login', (payload) => {
|
|
373
|
+
* console.log('User logged in:', payload);
|
|
374
|
+
* });
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
interface Kernel<TContext = unknown> {
|
|
378
|
+
/** Register a plugin */
|
|
379
|
+
use(plugin: Plugin<TContext>): this;
|
|
380
|
+
/** Unregister a plugin by name */
|
|
381
|
+
unregister(name: string): boolean;
|
|
382
|
+
/** Get registered plugin by name */
|
|
383
|
+
getPlugin<T extends Plugin<TContext> = Plugin<TContext>>(name: string): T | undefined;
|
|
384
|
+
/** List all registered plugins */
|
|
385
|
+
listPlugins(): ReadonlyArray<Plugin<TContext>>;
|
|
386
|
+
/** Check if plugin is registered */
|
|
387
|
+
hasPlugin(name: string): boolean;
|
|
388
|
+
/** Emit event to all plugins */
|
|
389
|
+
emit<K extends string>(event: K, payload?: unknown): void;
|
|
390
|
+
/** Subscribe to kernel events */
|
|
391
|
+
on<K extends string>(event: K, handler: (payload: unknown) => void): Unsubscribe;
|
|
392
|
+
/** Get shared context */
|
|
393
|
+
getContext(): TContext;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Plugin options for configuration.
|
|
397
|
+
*
|
|
398
|
+
* Configuration options that can be passed when creating or configuring
|
|
399
|
+
* a plugin-enabled application.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```typescript
|
|
403
|
+
* const options: PluginOptions = {
|
|
404
|
+
* debug: true,
|
|
405
|
+
* logger: console
|
|
406
|
+
* };
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
interface PluginOptions {
|
|
410
|
+
/** Whether to enable debug mode */
|
|
411
|
+
debug?: boolean;
|
|
412
|
+
/** Custom logger */
|
|
413
|
+
logger?: PluginLogger;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Plugin logger interface.
|
|
417
|
+
*
|
|
418
|
+
* Standard logging interface that plugins can use to log messages.
|
|
419
|
+
* This abstraction allows custom logging implementations.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```typescript
|
|
423
|
+
* const logger: PluginLogger = {
|
|
424
|
+
* debug(message, ...args) { console.log('[DEBUG]', message, ...args); },
|
|
425
|
+
* info(message, ...args) { console.log('[INFO]', message, ...args); },
|
|
426
|
+
* warn(message, ...args) { console.warn('[WARN]', message, ...args); },
|
|
427
|
+
* error(message, ...args) { console.error('[ERROR]', message, ...args); }
|
|
428
|
+
* };
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
interface PluginLogger {
|
|
432
|
+
debug(message: string, ...args: unknown[]): void;
|
|
433
|
+
info(message: string, ...args: unknown[]): void;
|
|
434
|
+
warn(message: string, ...args: unknown[]): void;
|
|
435
|
+
error(message: string, ...args: unknown[]): void;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Result type - either Ok<T> or Err<E>.
|
|
440
|
+
*
|
|
441
|
+
* A type that represents either success (Ok) or failure (Err).
|
|
442
|
+
* This is inspired by Rust's Result type and provides functional
|
|
443
|
+
* error handling without exceptions.
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* function divide(a: number, b: number): Result<number, string> {
|
|
448
|
+
* if (b === 0) {
|
|
449
|
+
* return Err('Division by zero');
|
|
450
|
+
* }
|
|
451
|
+
* return Ok(a / b);
|
|
452
|
+
* }
|
|
453
|
+
*
|
|
454
|
+
* const result = divide(10, 2);
|
|
455
|
+
*
|
|
456
|
+
* // Pattern matching
|
|
457
|
+
* const message = result.match({
|
|
458
|
+
* ok: (value) => `Result: ${value}`,
|
|
459
|
+
* err: (error) => `Error: ${error}`
|
|
460
|
+
* });
|
|
461
|
+
*
|
|
462
|
+
* // Chaining
|
|
463
|
+
* const doubled = result
|
|
464
|
+
* .map(x => x * 2)
|
|
465
|
+
* .mapErr(e => `Calculation failed: ${e}`);
|
|
466
|
+
* ```
|
|
467
|
+
*/
|
|
468
|
+
type Result<T, E> = Ok<T> | Err<E>;
|
|
469
|
+
/**
|
|
470
|
+
* @oxog/types - Result type for functional error handling
|
|
471
|
+
* @version 1.0.0
|
|
472
|
+
* @author Ersin Koç
|
|
473
|
+
*/
|
|
474
|
+
/**
|
|
475
|
+
* Represents a successful result.
|
|
476
|
+
*
|
|
477
|
+
* Contains a value and provides methods for transforming the value
|
|
478
|
+
* or handling errors. Only available on successful results.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```typescript
|
|
482
|
+
* const ok = Ok(42);
|
|
483
|
+
* if (isOk(ok)) {
|
|
484
|
+
* console.log(ok.value); // 42
|
|
485
|
+
* }
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
interface Ok<T> {
|
|
489
|
+
readonly ok: true;
|
|
490
|
+
readonly value: T;
|
|
491
|
+
readonly error?: never;
|
|
492
|
+
/** Transform the value if successful */
|
|
493
|
+
map<U>(fn: (value: T) => U): Result<U, never>;
|
|
494
|
+
/** Transform the error (no-op for Ok) */
|
|
495
|
+
mapErr<F>(fn: (error: never) => F): Ok<T>;
|
|
496
|
+
/** Pattern matching for both cases */
|
|
497
|
+
match<U>(handlers: {
|
|
498
|
+
ok: (value: T) => U;
|
|
499
|
+
err: (error: never) => U;
|
|
500
|
+
}): U;
|
|
501
|
+
/** Unwrap the value (asserts success) */
|
|
502
|
+
unwrap(): T;
|
|
503
|
+
/** Unwrap with default value */
|
|
504
|
+
unwrapOr(defaultValue: T): T;
|
|
505
|
+
/** Unwrap using a fallback function */
|
|
506
|
+
unwrapOrElse(fn: () => T): T;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Creates a successful Result containing a value.
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* ```typescript
|
|
513
|
+
* const ok = Ok(42);
|
|
514
|
+
* const okStr = Ok('hello');
|
|
515
|
+
* const okObj = Ok({ id: 1 });
|
|
516
|
+
* ```
|
|
517
|
+
*
|
|
518
|
+
* @param value - The value to wrap
|
|
519
|
+
* @returns An Ok result containing the value
|
|
520
|
+
*/
|
|
521
|
+
declare function Ok<T>(value: T): Ok<T>;
|
|
522
|
+
/**
|
|
523
|
+
* Represents a failed result.
|
|
524
|
+
*
|
|
525
|
+
* Contains an error and provides methods for transforming the error
|
|
526
|
+
* or handling success cases. Only available on failed results.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```typescript
|
|
530
|
+
* const err = Err('Something went wrong');
|
|
531
|
+
* if (isErr(err)) {
|
|
532
|
+
* console.log(err.error); // 'Something went wrong'
|
|
533
|
+
* }
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
interface Err<E> {
|
|
537
|
+
readonly ok: false;
|
|
538
|
+
readonly value?: never;
|
|
539
|
+
readonly error: E;
|
|
540
|
+
/** Transform the value (no-op for Err) */
|
|
541
|
+
map<U>(fn: (value: never) => U): Err<E>;
|
|
542
|
+
/** Transform the error if failed */
|
|
543
|
+
mapErr<F>(fn: (error: E) => F): Result<never, F>;
|
|
544
|
+
/** Pattern matching for both cases */
|
|
545
|
+
match<U>(handlers: {
|
|
546
|
+
ok: (value: never) => U;
|
|
547
|
+
err: (error: E) => U;
|
|
548
|
+
}): U;
|
|
549
|
+
/** Unwrap the value (asserts success, throws for Err) */
|
|
550
|
+
unwrap(): never;
|
|
551
|
+
/** Unwrap with default value */
|
|
552
|
+
unwrapOr<T>(defaultValue: T): T;
|
|
553
|
+
/** Unwrap using a fallback function */
|
|
554
|
+
unwrapOrElse<T>(fn: () => T): T;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Creates a failed Result containing an error.
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* ```typescript
|
|
561
|
+
* const err = Err('Something went wrong');
|
|
562
|
+
* const errNum = Err(404);
|
|
563
|
+
* const errObj = Err({ code: 'NOT_FOUND' });
|
|
564
|
+
* ```
|
|
565
|
+
*
|
|
566
|
+
* @param error - The error to wrap
|
|
567
|
+
* @returns An Err result containing the error
|
|
568
|
+
*/
|
|
569
|
+
declare function Err<E>(error: E): Err<E>;
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* @oxog/types - Standardized error classes
|
|
573
|
+
* @version 1.0.0
|
|
574
|
+
* @author Ersin Koç
|
|
575
|
+
*/
|
|
576
|
+
/**
|
|
577
|
+
* Standard error codes for @oxog ecosystem.
|
|
578
|
+
*
|
|
579
|
+
* These codes provide programmatic ways to identify and handle errors.
|
|
580
|
+
*/
|
|
581
|
+
declare enum ErrorCodes {
|
|
582
|
+
/** Unknown error */
|
|
583
|
+
UNKNOWN = "UNKNOWN",
|
|
584
|
+
/** Validation error */
|
|
585
|
+
VALIDATION_ERROR = "VALIDATION_ERROR",
|
|
586
|
+
/** Plugin-related error */
|
|
587
|
+
PLUGIN_ERROR = "PLUGIN_ERROR",
|
|
588
|
+
/** Resource not found */
|
|
589
|
+
NOT_FOUND = "NOT_FOUND",
|
|
590
|
+
/** Operation timed out */
|
|
591
|
+
TIMEOUT = "TIMEOUT",
|
|
592
|
+
/** Dependency error */
|
|
593
|
+
DEPENDENCY_ERROR = "DEPENDENCY_ERROR"
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Base error class for all @oxog errors.
|
|
597
|
+
*
|
|
598
|
+
* Provides structured error information with code, message, and context.
|
|
599
|
+
* All other @oxog errors inherit from this class.
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* ```typescript
|
|
603
|
+
* throw new OxogError(
|
|
604
|
+
* 'Database connection failed',
|
|
605
|
+
* ErrorCodes.DEPENDENCY_ERROR,
|
|
606
|
+
* { host: 'localhost', port: 5432 }
|
|
607
|
+
* );
|
|
608
|
+
* ```
|
|
609
|
+
*/
|
|
610
|
+
declare class OxogError extends Error {
|
|
611
|
+
readonly code: string;
|
|
612
|
+
readonly context?: Record<string, unknown> | undefined;
|
|
613
|
+
/** Error name */
|
|
614
|
+
readonly name: string;
|
|
615
|
+
/**
|
|
616
|
+
* Creates a new OxogError.
|
|
617
|
+
*
|
|
618
|
+
* @param message - Human-readable error message
|
|
619
|
+
* @param code - Error code for programmatic handling
|
|
620
|
+
* @param context - Additional context about the error
|
|
621
|
+
*/
|
|
622
|
+
constructor(message: string, code: string, context?: Record<string, unknown> | undefined);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Error thrown when validation fails.
|
|
626
|
+
*
|
|
627
|
+
* Used for input validation, schema validation, and type checking errors.
|
|
628
|
+
*
|
|
629
|
+
* @example
|
|
630
|
+
* ```typescript
|
|
631
|
+
* throw new ValidationError(
|
|
632
|
+
* 'Invalid email format',
|
|
633
|
+
* { field: 'email', value: 'not-an-email' }
|
|
634
|
+
* );
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
declare class ValidationError extends OxogError {
|
|
638
|
+
readonly context?: Record<string, unknown> | undefined;
|
|
639
|
+
/** Error name */
|
|
640
|
+
readonly name: string;
|
|
641
|
+
/**
|
|
642
|
+
* Creates a new ValidationError.
|
|
643
|
+
*
|
|
644
|
+
* @param message - Human-readable error message
|
|
645
|
+
* @param context - Additional context about the validation failure
|
|
646
|
+
*/
|
|
647
|
+
constructor(message: string, context?: Record<string, unknown> | undefined);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Error thrown when a plugin operation fails.
|
|
651
|
+
*
|
|
652
|
+
* Used for plugin initialization, dependency resolution, and runtime errors
|
|
653
|
+
* specific to plugins.
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* ```typescript
|
|
657
|
+
* throw new PluginError(
|
|
658
|
+
* 'Failed to initialize cache plugin',
|
|
659
|
+
* 'cache-plugin',
|
|
660
|
+
* { reason: 'Redis connection failed' }
|
|
661
|
+
* );
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
declare class PluginError extends OxogError {
|
|
665
|
+
readonly pluginName: string;
|
|
666
|
+
/** Error name */
|
|
667
|
+
readonly name: string;
|
|
668
|
+
/**
|
|
669
|
+
* Creates a new PluginError.
|
|
670
|
+
*
|
|
671
|
+
* @param message - Human-readable error message
|
|
672
|
+
* @param pluginName - Name of the plugin that caused the error
|
|
673
|
+
* @param context - Additional context about the plugin error
|
|
674
|
+
*/
|
|
675
|
+
constructor(message: string, pluginName: string, context?: Record<string, unknown>);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* @oxog/types - Type guard functions
|
|
680
|
+
* @version 1.0.0
|
|
681
|
+
* @author Ersin Koç
|
|
682
|
+
*/
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Type guard for Plugin.
|
|
686
|
+
*
|
|
687
|
+
* Checks if a value is a valid Plugin instance.
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```typescript
|
|
691
|
+
* const plugin = { name: 'test', version: '1.0.0', install: () => {} };
|
|
692
|
+
* if (isPlugin(plugin)) {
|
|
693
|
+
* console.log(plugin.name); // TypeScript knows it's a Plugin
|
|
694
|
+
* }
|
|
695
|
+
* ```
|
|
696
|
+
*/
|
|
697
|
+
declare function isPlugin<T>(value: unknown): value is Plugin<T>;
|
|
698
|
+
/**
|
|
699
|
+
* Type guard for Kernel.
|
|
700
|
+
*
|
|
701
|
+
* Checks if a value is a valid Kernel instance.
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```typescript
|
|
705
|
+
* const kernel = {
|
|
706
|
+
* use: (plugin) => kernel,
|
|
707
|
+
* unregister: () => true,
|
|
708
|
+
* getPlugin: () => undefined,
|
|
709
|
+
* listPlugins: () => [],
|
|
710
|
+
* hasPlugin: () => false,
|
|
711
|
+
* emit: () => {},
|
|
712
|
+
* on: () => () => {},
|
|
713
|
+
* getContext: () => null
|
|
714
|
+
* };
|
|
715
|
+
* if (isKernel(kernel)) {
|
|
716
|
+
* console.log(kernel.listPlugins()); // TypeScript knows it's a Kernel
|
|
717
|
+
* }
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
declare function isKernel<T>(value: unknown): value is Kernel<T>;
|
|
721
|
+
/**
|
|
722
|
+
* Type guard for OxogError.
|
|
723
|
+
*
|
|
724
|
+
* Checks if a value is an OxogError instance.
|
|
725
|
+
*
|
|
726
|
+
* @example
|
|
727
|
+
* ```typescript
|
|
728
|
+
* const error = new OxogError('Test', 'TEST');
|
|
729
|
+
* if (isOxogError(error)) {
|
|
730
|
+
* console.log(error.code); // TypeScript knows it's an OxogError
|
|
731
|
+
* }
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
734
|
+
declare function isOxogError(value: unknown): value is OxogError;
|
|
735
|
+
/**
|
|
736
|
+
* Type guard for ValidationError.
|
|
737
|
+
*
|
|
738
|
+
* Checks if a value is a ValidationError instance.
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* ```typescript
|
|
742
|
+
* const error = new ValidationError('Invalid', { field: 'test' });
|
|
743
|
+
* if (isValidationError(error)) {
|
|
744
|
+
* console.log(error.context); // TypeScript knows it's a ValidationError
|
|
745
|
+
* }
|
|
746
|
+
* ```
|
|
747
|
+
*/
|
|
748
|
+
declare function isValidationError(value: unknown): value is ValidationError;
|
|
749
|
+
/**
|
|
750
|
+
* Type guard for PluginError.
|
|
751
|
+
*
|
|
752
|
+
* Checks if a value is a PluginError instance.
|
|
753
|
+
*
|
|
754
|
+
* @example
|
|
755
|
+
* ```typescript
|
|
756
|
+
* const error = new PluginError('Plugin failed', 'my-plugin');
|
|
757
|
+
* if (isPluginError(error)) {
|
|
758
|
+
* console.log(error.pluginName); // TypeScript knows it's a PluginError
|
|
759
|
+
* }
|
|
760
|
+
* ```
|
|
761
|
+
*/
|
|
762
|
+
declare function isPluginError(value: unknown): value is PluginError;
|
|
763
|
+
/**
|
|
764
|
+
* Type guard for Result.
|
|
765
|
+
*
|
|
766
|
+
* Checks if a value is a Result instance (either Ok or Err).
|
|
767
|
+
*
|
|
768
|
+
* @example
|
|
769
|
+
* ```typescript
|
|
770
|
+
* const result = Ok(42);
|
|
771
|
+
* if (isResult(result)) {
|
|
772
|
+
* // result is Ok<number, unknown>
|
|
773
|
+
* }
|
|
774
|
+
*
|
|
775
|
+
* const err = Err('error');
|
|
776
|
+
* if (isResult(err)) {
|
|
777
|
+
* // err is Err<unknown, string>
|
|
778
|
+
* }
|
|
779
|
+
* ```
|
|
780
|
+
*/
|
|
781
|
+
declare function isResult<T, E>(value: unknown): value is Result<T, E>;
|
|
782
|
+
/**
|
|
783
|
+
* Type guard for Ok result.
|
|
784
|
+
*
|
|
785
|
+
* Checks if a Result is an Ok (success) instance.
|
|
786
|
+
*
|
|
787
|
+
* @example
|
|
788
|
+
* ```typescript
|
|
789
|
+
* const ok = Ok(42);
|
|
790
|
+
* if (isOk(ok)) {
|
|
791
|
+
* console.log(ok.value); // TypeScript knows it's Ok
|
|
792
|
+
* }
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
declare function isOk<T>(value: Result<T, unknown>): value is Ok<T>;
|
|
796
|
+
/**
|
|
797
|
+
* Type guard for Err result.
|
|
798
|
+
*
|
|
799
|
+
* Checks if a Result is an Err (failure) instance.
|
|
800
|
+
*
|
|
801
|
+
* @example
|
|
802
|
+
* ```typescript
|
|
803
|
+
* const err = Err('error');
|
|
804
|
+
* if (isErr(err)) {
|
|
805
|
+
* console.log(err.error); // TypeScript knows it's Err
|
|
806
|
+
* }
|
|
807
|
+
* ```
|
|
808
|
+
*/
|
|
809
|
+
declare function isErr<E>(value: Result<unknown, E>): value is Err<E>;
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* @oxog/types - Event system types
|
|
813
|
+
* @version 1.0.0
|
|
814
|
+
* @author Ersin Koç
|
|
815
|
+
*/
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Base event map interface.
|
|
819
|
+
*
|
|
820
|
+
* Defines the structure for event payloads. Extend this interface
|
|
821
|
+
* to define custom events for your application.
|
|
822
|
+
*
|
|
823
|
+
* @example
|
|
824
|
+
* ```typescript
|
|
825
|
+
* interface MyEvents extends EventMap {
|
|
826
|
+
* 'user:login': { userId: string; timestamp: number };
|
|
827
|
+
* 'user:logout': { userId: string };
|
|
828
|
+
* 'error': Error;
|
|
829
|
+
* }
|
|
830
|
+
* ```
|
|
831
|
+
*/
|
|
832
|
+
interface EventMap {
|
|
833
|
+
[event: string]: unknown;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Event handler for a specific event.
|
|
837
|
+
*
|
|
838
|
+
* Type-safe event handler that receives the correctly typed payload.
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```typescript
|
|
842
|
+
* interface MyEvents extends EventMap {
|
|
843
|
+
* 'user:login': { userId: string; timestamp: number };
|
|
844
|
+
* }
|
|
845
|
+
*
|
|
846
|
+
* const handler: EventHandler<MyEvents, 'user:login'> = (payload) => {
|
|
847
|
+
* console.log(payload.userId, payload.timestamp);
|
|
848
|
+
* };
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
type EventHandler<TEvents extends EventMap, K extends keyof TEvents> = (payload: TEvents[K]) => void;
|
|
852
|
+
/**
|
|
853
|
+
* Typed event emitter interface.
|
|
854
|
+
*
|
|
855
|
+
* Provides type-safe event emission and subscription. Implement this
|
|
856
|
+
* interface to create custom event emitters.
|
|
857
|
+
*
|
|
858
|
+
* @example
|
|
859
|
+
* ```typescript
|
|
860
|
+
* class MyEmitter implements TypedEventEmitter<MyEvents> {
|
|
861
|
+
* private handlers = new Map<string, Set<Function>>();
|
|
862
|
+
*
|
|
863
|
+
* on<K extends keyof MyEvents>(event: K, handler: EventHandler<MyEvents, K>): Unsubscribe {
|
|
864
|
+
* // Implementation
|
|
865
|
+
* return () => {};
|
|
866
|
+
* }
|
|
867
|
+
*
|
|
868
|
+
* off<K extends keyof MyEvents>(event: K, handler: EventHandler<MyEvents, K>): void {
|
|
869
|
+
* // Implementation
|
|
870
|
+
* }
|
|
871
|
+
*
|
|
872
|
+
* emit<K extends keyof MyEvents>(event: K, payload: MyEvents[K]): void {
|
|
873
|
+
* // Implementation
|
|
874
|
+
* }
|
|
875
|
+
*
|
|
876
|
+
* once<K extends keyof MyEvents>(event: K, handler: EventHandler<MyEvents, K>): Unsubscribe {
|
|
877
|
+
* // Implementation
|
|
878
|
+
* return () => {};
|
|
879
|
+
* }
|
|
880
|
+
* }
|
|
881
|
+
* ```
|
|
882
|
+
*/
|
|
883
|
+
interface TypedEventEmitter<TEvents extends EventMap> {
|
|
884
|
+
/** Subscribe to an event */
|
|
885
|
+
on<K extends keyof TEvents>(event: K, handler: EventHandler<TEvents, K>): Unsubscribe;
|
|
886
|
+
/** Unsubscribe from an event */
|
|
887
|
+
off<K extends keyof TEvents>(event: K, handler: EventHandler<TEvents, K>): void;
|
|
888
|
+
/** Emit an event */
|
|
889
|
+
emit<K extends keyof TEvents>(event: K, payload: TEvents[K]): void;
|
|
890
|
+
/** Subscribe to an event exactly once */
|
|
891
|
+
once<K extends keyof TEvents>(event: K, handler: EventHandler<TEvents, K>): Unsubscribe;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* @oxog/types - Well-known symbols and constants
|
|
896
|
+
* @version 1.0.0
|
|
897
|
+
* @author Ersin Koç
|
|
898
|
+
*/
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Well-known symbol for @oxog plugins.
|
|
902
|
+
*
|
|
903
|
+
* Use this symbol to mark objects as @oxog plugins.
|
|
904
|
+
* This enables runtime detection and validation.
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```typescript
|
|
908
|
+
* const myPlugin = {
|
|
909
|
+
* [OXOG_PLUGIN]: true,
|
|
910
|
+
* name: 'my-plugin',
|
|
911
|
+
* version: '1.0.0',
|
|
912
|
+
* install: () => {}
|
|
913
|
+
* };
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
declare const OXOG_PLUGIN: unique symbol;
|
|
917
|
+
/**
|
|
918
|
+
* Well-known symbol for @oxog kernels.
|
|
919
|
+
*
|
|
920
|
+
* Use this symbol to mark objects as @oxog kernels.
|
|
921
|
+
* This enables runtime detection and validation.
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```typescript
|
|
925
|
+
* const myKernel = {
|
|
926
|
+
* [OXOG_KERNEL]: true,
|
|
927
|
+
* use: (plugin) => kernel,
|
|
928
|
+
* // ... other methods
|
|
929
|
+
* };
|
|
930
|
+
* ```
|
|
931
|
+
*/
|
|
932
|
+
declare const OXOG_KERNEL: unique symbol;
|
|
933
|
+
/**
|
|
934
|
+
* @oxog/types package version.
|
|
935
|
+
*
|
|
936
|
+
* Current version of the @oxog/types package.
|
|
937
|
+
*
|
|
938
|
+
* @example
|
|
939
|
+
* ```typescript
|
|
940
|
+
* console.log(`Using @oxog/types v${OXOG_VERSION}`);
|
|
941
|
+
* ```
|
|
942
|
+
*/
|
|
943
|
+
declare const OXOG_VERSION = "1.0.0";
|
|
944
|
+
|
|
945
|
+
export { type AnyFunction, type AsyncFunction, type Brand, type Branded, type DeepPartial, type DeepReadonly, type DeepRequired, Err, ErrorCodes, type EventHandler, type EventMap, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type Kernel, type MaybePromise, type NonEmptyArray, type Nullable, OXOG_KERNEL, OXOG_PLUGIN, OXOG_VERSION, Ok, type Optional, OxogError, type Plugin, PluginError, type PluginLogger, type PluginOptions, type Prettify, type Result, type StrictOmit, type StrictPick, type SyncFunction, type TypedEventEmitter, type Unsubscribe, ValidationError, isErr, isKernel, isOk, isOxogError, isPlugin, isPluginError, isResult, isValidationError };
|