alepha 0.10.7 → 0.11.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/core.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
- import { Validator } from "typebox/compile";
3
2
  import * as TypeBox from "typebox";
4
- import { Static, Static as Static$1, StaticDecode, StaticEncode, TAny, TAny as TAny$1, TArray, TArray as TArray$1, TArrayOptions, TBigInt, TBoolean, TBoolean as TBoolean$1, TInteger, TInteger as TInteger$1, TKeysToIndexer, TNull, TNull as TNull$1, TNumber, TNumber as TNumber$1, TNumberOptions, TNumberOptions as TNumberOptions$1, TObject, TObject as TObject$1, TObjectOptions, TObjectOptions as TObjectOptions$1, TOptional, TOptionalAdd, TOptionalAdd as TOptionalAdd$1, TPick, TProperties, TProperties as TProperties$1, TRecord, TRecord as TRecord$1, TSchema, TSchema as TSchema$1, TSchemaOptions, TString, TString as TString$1, TStringOptions, TStringOptions as TStringOptions$1, TTuple, TUnion, TUnion as TUnion$1, TUnsafe, TVoid } from "typebox";
3
+ import { Static as Static$1, StaticDecode, StaticDecode as Static, StaticDecode as StaticDecode$1, StaticEncode, StaticEncode as StaticEncode$1, TAny, TAny as TAny$1, TArray, TArray as TArray$1, TArrayOptions, TBigInt, TBoolean, TBoolean as TBoolean$1, TInteger, TInteger as TInteger$1, TKeysToIndexer, TKeysToIndexer as TKeysToIndexer$1, TNull, TNull as TNull$1, TNumber, TNumber as TNumber$1, TNumberOptions, TNumberOptions as TNumberOptions$1, TObject, TObject as TObject$1, TObjectOptions, TObjectOptions as TObjectOptions$1, TOmit, TOptional, TOptionalAdd, TOptionalAdd as TOptionalAdd$1, TPick, TPick as TPick$1, TProperties, TProperties as TProperties$1, TRecord, TRecord as TRecord$1, TSchema, TSchema as TSchema$1, TSchemaOptions, TString, TString as TString$1, TStringOptions, TStringOptions as TStringOptions$1, TTuple, TUnion, TUnion as TUnion$1, TUnsafe, TUnsafe as TUnsafe$1, TVoid } from "typebox";
4
+ import { Validator } from "typebox/compile";
5
5
  import TypeBoxFormat from "typebox/format";
6
6
  import * as TypeBoxValue from "typebox/value";
7
7
  import { Readable } from "node:stream";
@@ -28,13 +28,14 @@ declare const MODULE: unique symbol;
28
28
  /**
29
29
  * In Alepha, a service is a class that can be instantiated or an abstract class. Nothing more, nothing less...
30
30
  */
31
- type Service<T$1 extends object = any> = InstantiableClass<T$1> | AbstractClass<T$1>;
32
- type InstantiableClass<T$1 extends object = any> = new (...args: any[]) => T$1;
31
+ type Service<T extends object = any> = InstantiableClass<T> | AbstractClass<T> | RunFunction<T>;
32
+ type RunFunction<T extends object = any> = (...args: any[]) => T | void;
33
+ type InstantiableClass<T extends object = any> = new (...args: any[]) => T;
33
34
  /**
34
35
  * Abstract class is a class that cannot be instantiated directly!
35
36
  * It widely used for defining interfaces.
36
37
  */
37
- type AbstractClass<T$1 extends object = any> = abstract new (...args: any[]) => T$1;
38
+ type AbstractClass<T extends object = any> = abstract new (...args: any[]) => T;
38
39
  /**
39
40
  * Service substitution allows you to register a class as a different class.
40
41
  * Providing class A, but using class B instead.
@@ -42,17 +43,17 @@ type AbstractClass<T$1 extends object = any> = abstract new (...args: any[]) =>
42
43
  *
43
44
  * class A is mostly an AbstractClass, while class B is an InstantiableClass.
44
45
  */
45
- interface ServiceSubstitution<T$1 extends object = any> {
46
+ interface ServiceSubstitution<T extends object = any> {
46
47
  /**
47
48
  * Every time someone asks for this class, it will be provided with the 'use' class.
48
49
  */
49
- provide: Service<T$1>;
50
+ provide: Service<T>;
50
51
  /**
51
52
  * Service to use instead of the 'provide' service.
52
53
  *
53
54
  * Syntax is inspired by Angular's DI system.
54
55
  */
55
- use: Service<T$1>;
56
+ use: Service<T>;
56
57
  /**
57
58
  * If true, if the service already exists -> just ignore the substitution and do not throw an error.
58
59
  * Mostly used for plugins to enforce a substitution without throwing an error.
@@ -68,11 +69,12 @@ interface ServiceSubstitution<T$1 extends object = any> {
68
69
  *
69
70
  * And yes, you declare the *type* of the service, not the *instance*.
70
71
  */
71
- type ServiceEntry<T$1 extends object = any> = Service<T$1> | ServiceSubstitution<T$1>;
72
+ type ServiceEntry<T extends object = any> = Service<T> | ServiceSubstitution<T>;
73
+ declare function isClass(func: any): func is InstantiableClass;
72
74
  //#endregion
73
75
  //#region src/helpers/descriptor.d.ts
74
- interface DescriptorArgs<T$1 extends object = {}> {
75
- options: T$1;
76
+ interface DescriptorArgs<T extends object = {}> {
77
+ options: T;
76
78
  alepha: Alepha;
77
79
  service: InstantiableClass<Service>;
78
80
  module?: Service;
@@ -82,22 +84,22 @@ interface DescriptorConfig {
82
84
  service: InstantiableClass<Service>;
83
85
  module?: Service;
84
86
  }
85
- declare abstract class Descriptor<T$1 extends object = {}> {
87
+ declare abstract class Descriptor<T extends object = {}> {
86
88
  protected readonly alepha: Alepha;
87
- readonly options: T$1;
89
+ readonly options: T;
88
90
  readonly config: DescriptorConfig;
89
- constructor(args: DescriptorArgs<T$1>);
91
+ constructor(args: DescriptorArgs<T>);
90
92
  /**
91
93
  * Called automatically by Alepha after the descriptor is created.
92
94
  */
93
95
  protected onInit(): void;
94
96
  }
95
- type DescriptorFactory<TDescriptor$1 extends Descriptor = Descriptor> = {
96
- (options: TDescriptor$1["options"]): TDescriptor$1;
97
- [KIND]: InstantiableClass<TDescriptor$1>;
97
+ type DescriptorFactory<TDescriptor extends Descriptor = Descriptor> = {
98
+ (options: TDescriptor["options"]): TDescriptor;
99
+ [KIND]: InstantiableClass<TDescriptor>;
98
100
  };
99
- type DescriptorFactoryLike<T$1 extends object = any> = {
100
- (options: T$1): any;
101
+ type DescriptorFactoryLike<T extends object = any> = {
102
+ (options: T): any;
101
103
  [KIND]: any;
102
104
  };
103
105
  declare const createDescriptor: <TDescriptor extends Descriptor>(descriptor: InstantiableClass<TDescriptor> & {
@@ -117,7 +119,7 @@ declare const createDescriptor: <TDescriptor extends Descriptor>(descriptor: Ins
117
119
  */
118
120
  declare const $inject: <T extends object>(type: Service<T>, opts?: InjectOptions<T>) => T;
119
121
  declare class InjectDescriptor extends Descriptor {}
120
- interface InjectOptions<T$1 extends object = any> {
122
+ interface InjectOptions<T extends object = any> {
121
123
  /**
122
124
  * - 'transient' → Always a new instance on every inject. Zero caching.
123
125
  * - 'singleton' → One instance per Alepha runtime (per-thread). Never disposed until Alepha shuts down. (default)
@@ -132,7 +134,7 @@ interface InjectOptions<T$1 extends object = any> {
132
134
  /**
133
135
  * Constructor arguments to pass when creating a new instance.
134
136
  */
135
- args?: ConstructorParameters<InstantiableClass<T$1>>;
137
+ args?: ConstructorParameters<InstantiableClass<T>>;
136
138
  /**
137
139
  * Parent that requested the instance.
138
140
  *
@@ -190,7 +192,7 @@ interface InjectOptions<T$1 extends object = any> {
190
192
  * If we speak with `$actions`, a module should be used when you have more than 30 actions in a single module.
191
193
  */
192
194
  declare const $module: <T extends object = {}>(options: ModuleDescriptorOptions<T>) => Service<Module<T>>;
193
- interface ModuleDescriptorOptions<T$1 extends object> {
195
+ interface ModuleDescriptorOptions<T extends object> {
194
196
  /**
195
197
  * Name of the module.
196
198
  *
@@ -210,16 +212,16 @@ interface ModuleDescriptorOptions<T$1 extends object> {
210
212
  * You can override this behavior by providing a register function.
211
213
  * It's useful when you want to register services conditionally or in a specific order.
212
214
  */
213
- register?: (alepha: Alepha, options: T$1) => void;
215
+ register?: (alepha: Alepha, options: T) => void;
214
216
  }
215
217
  /**
216
218
  * Base class for all modules.
217
219
  */
218
- declare abstract class Module<T$1 extends object = {}> {
219
- abstract readonly config: ModuleDescriptorOptions<T$1>;
220
+ declare abstract class Module<T extends object = {}> {
221
+ abstract readonly config: ModuleDescriptorOptions<T>;
220
222
  abstract register(alepha: Alepha): void;
221
223
  static NAME_REGEX: RegExp;
222
- options: T$1;
224
+ options: T;
223
225
  /**
224
226
  * Check if a Service is a Module.
225
227
  */
@@ -229,7 +231,7 @@ declare abstract class Module<T$1 extends object = {}> {
229
231
  */
230
232
  static of(ctor: Service): Service<Module> | undefined;
231
233
  }
232
- type WithModule<T$1 extends object = any> = T$1 & {
234
+ type WithModule<T extends object = any> = T & {
233
235
  [MODULE]?: Service;
234
236
  };
235
237
  //#endregion
@@ -237,7 +239,7 @@ type WithModule<T$1 extends object = any> = T$1 & {
237
239
  /**
238
240
  * Represents a value that can be either a value or a promise of value.
239
241
  */
240
- type Async<T$1> = T$1 | Promise<T$1>;
242
+ type Async<T> = T | Promise<T>;
241
243
  /**
242
244
  * Represents a function that returns an async value.
243
245
  */
@@ -245,7 +247,7 @@ type AsyncFn = (...args: any[]) => Async<any>;
245
247
  /**
246
248
  * Transforms a type T into a promise if it is not already a promise.
247
249
  */
248
- type MaybePromise<T$1> = T$1 extends Promise<any> ? T$1 : Promise<T$1>;
250
+ type MaybePromise<T> = T extends Promise<any> ? T : Promise<T>;
249
251
  //#endregion
250
252
  //#region src/interfaces/LoggerInterface.d.ts
251
253
  type LogLevel = "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE" | "SILENT";
@@ -257,45 +259,6 @@ interface LoggerInterface {
257
259
  error(message: string, data?: unknown): void;
258
260
  }
259
261
  //#endregion
260
- //#region src/helpers/EventManager.d.ts
261
- declare class EventManager {
262
- protected logFn?: () => LoggerInterface | undefined;
263
- /**
264
- * List of events that can be triggered. Powered by $hook().
265
- */
266
- protected events: Record<string, Array<Hook>>;
267
- constructor(logFn?: () => LoggerInterface | undefined);
268
- protected get log(): LoggerInterface | undefined;
269
- /**
270
- * Registers a hook for the specified event.
271
- */
272
- on<T extends keyof Hooks>(event: T, hookOrFunc: Hook<T> | ((payload: Hooks[T]) => Async<void>)): () => void;
273
- /**
274
- * Emits the specified event with the given payload.
275
- */
276
- emit<T extends keyof Hooks>(func: T, payload: Hooks[T], options?: {
277
- /**
278
- * If true, the hooks will be executed in reverse order.
279
- * This is useful for "stop" hooks that should be executed in reverse order.
280
- *
281
- * @default false
282
- */
283
- reverse?: boolean;
284
- /**
285
- * If true, the hooks will be logged with their execution time.
286
- *
287
- * @default false
288
- */
289
- log?: boolean;
290
- /**
291
- * If true, errors will be caught and logged instead of throwing.
292
- *
293
- * @default false
294
- */
295
- catch?: boolean;
296
- }): Promise<void>;
297
- }
298
- //#endregion
299
262
  //#region src/providers/AlsProvider.d.ts
300
263
  type AsyncLocalStorageData = any;
301
264
  declare class AlsProvider {
@@ -309,36 +272,15 @@ declare class AlsProvider {
309
272
  set<T>(key: string, value: T): void;
310
273
  }
311
274
  //#endregion
312
- //#region src/helpers/StateManager.d.ts
313
- declare class StateManager<S extends Record<string, any> = State> {
314
- protected store: Partial<S>;
315
- protected readonly events?: EventManager;
316
- protected readonly als?: AlsProvider;
317
- constructor(events?: EventManager, als?: AlsProvider);
318
- /**
319
- * Get a value from the state with proper typing
320
- */
321
- get<Key extends keyof S>(key: Key): S[Key] | undefined;
322
- /**
323
- * Set a value in the state
324
- */
325
- set<Key extends keyof S>(key: Key, value: S[Key] | undefined): this;
326
- /**
327
- * Check if a key exists in the state
328
- */
329
- has<Key extends keyof S>(key: Key): boolean;
330
- /**
331
- * Delete a key from the state (set to undefined)
332
- */
333
- del<Key extends keyof S>(key: Key): this;
334
- /**
335
- * Clear all state
336
- */
337
- clear(): this;
338
- /**
339
- * Get all keys that exist in the state
340
- */
341
- keys(): (keyof S)[];
275
+ //#region src/providers/Json.d.ts
276
+ /**
277
+ * Mimics the JSON global object with stringify and parse methods.
278
+ *
279
+ * Used across the codebase via dependency injection.
280
+ */
281
+ declare class Json {
282
+ stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
283
+ parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
342
284
  }
343
285
  //#endregion
344
286
  //#region src/helpers/FileLike.d.ts
@@ -398,20 +340,20 @@ interface FileLike {
398
340
  /**
399
341
  * TypeBox view of FileLike.
400
342
  */
401
- type TFile = TUnsafe<FileLike>;
343
+ type TFile = TUnsafe$1<FileLike>;
402
344
  declare const isTypeFile: (value: TSchema$1) => value is TFile;
403
345
  declare const isFileLike: (value: any) => value is FileLike;
404
346
  type StreamLike = ReadableStream | ReadableStream$1 | Readable | NodeJS.ReadableStream;
405
- type TStream = TUnsafe<StreamLike>;
347
+ type TStream = TUnsafe$1<StreamLike>;
406
348
  //#endregion
407
349
  //#region src/providers/TypeProvider.d.ts
408
350
  declare const isUUID: typeof TypeBoxFormat.IsUuid;
409
- declare const isDateTime: typeof TypeBoxFormat.IsDateTime;
410
- declare const isDate: typeof TypeBoxFormat.IsDate;
411
351
  declare const isEmail: typeof TypeBoxFormat.IsEmail;
412
352
  declare const isURL: typeof TypeBoxFormat.IsUrl;
413
353
  declare class TypeGuard {
414
- isSchema: typeof TypeBox.IsSchema;
354
+ isFile: (value: TSchema$1) => value is TFile;
355
+ isBigInt: (value: TSchema$1) => value is TString$1;
356
+ isUUID: (value: TSchema$1) => value is TString$1;
415
357
  isObject: typeof TypeBox.IsObject;
416
358
  isNumber: typeof TypeBox.IsNumber;
417
359
  isString: typeof TypeBox.IsString;
@@ -427,11 +369,8 @@ declare class TypeGuard {
427
369
  isRecord: typeof TypeBox.IsRecord;
428
370
  isTuple: typeof TypeBox.IsTuple;
429
371
  isVoid: typeof TypeBox.IsVoid;
430
- isFile: (value: TSchema$1) => value is TFile;
431
- isBigInt: (value: TSchema$1) => value is TString$1;
432
- isDate: (value: TSchema$1) => value is TString$1;
433
- isDatetime: (value: TSchema$1) => value is TString$1;
434
- isUUID: (value: TSchema$1) => value is TString$1;
372
+ isLiteral: typeof TypeBox.IsLiteral;
373
+ isSchema: typeof TypeBox.IsSchema;
435
374
  }
436
375
  declare module "typebox" {
437
376
  interface TString {
@@ -445,6 +384,7 @@ declare module "typebox" {
445
384
  }
446
385
  declare class TypeProvider {
447
386
  static format: typeof TypeBoxFormat;
387
+ static setLocale(locale: string): void;
448
388
  static isValidBigInt(value: string | number): boolean;
449
389
  /**
450
390
  * Default maximum length for strings.
@@ -494,12 +434,13 @@ declare class TypeProvider {
494
434
  void: typeof TypeBox.Void;
495
435
  undefined: typeof TypeBox.Undefined;
496
436
  record: typeof TypeBox.Record;
497
- omit: typeof TypeBox.Omit;
498
437
  partial: typeof TypeBox.Partial;
499
438
  union: typeof TypeBox.Union;
500
- pick: typeof TypeBox.Pick;
501
439
  tuple: typeof TypeBox.Tuple;
502
440
  interface: typeof TypeBox.Interface;
441
+ null: typeof TypeBox.Null;
442
+ const: typeof TypeBox.Literal;
443
+ codec: typeof TypeBox.Codec;
503
444
  /**
504
445
  * Type guards to check the type of schema.
505
446
  * This is not a runtime type check, but a compile-time type guard.
@@ -512,6 +453,8 @@ declare class TypeProvider {
512
453
  * ```
513
454
  */
514
455
  readonly schema: TypeGuard;
456
+ pick<T extends TObject$1, Indexer extends PropertyKey[]>(schema: T, keys: [...Indexer], options?: TObjectOptions$1): TPick$1<T, TKeysToIndexer$1<Indexer>>;
457
+ omit<T extends TObject$1, Indexer extends PropertyKey[]>(schema: T, keys: [...Indexer], options?: TObjectOptions$1): TOmit<T, TKeysToIndexer$1<Indexer>>;
515
458
  /**
516
459
  * Create a schema for an object.
517
460
  * By default, additional properties are not allowed.
@@ -536,7 +479,7 @@ declare class TypeProvider {
536
479
  * Create a schema for a string.
537
480
  * For db or input fields, consider using `t.text()` instead, which has length limits.
538
481
  *
539
- * If you need a string with specific format (e.g. email, uuid, date), consider using the corresponding method (e.g. `t.email()`, `t.uuid()`, `t.date()`).
482
+ * If you need a string with specific format (e.g. email, uuid), consider using the corresponding method (e.g. `t.email()`, `t.uuid()`).
540
483
  */
541
484
  string(options?: TStringOptions$1): TString$1;
542
485
  /**
@@ -563,6 +506,10 @@ declare class TypeProvider {
563
506
  * Create a schema for a signed 32-bit integer.
564
507
  */
565
508
  int(options?: TNumberOptions$1): TInteger$1;
509
+ /**
510
+ * @alias `t.int()`
511
+ */
512
+ integer(options?: TNumberOptions$1): TInteger$1;
566
513
  /**
567
514
  * Mimic a signed 64-bit integer.
568
515
  *
@@ -592,26 +539,20 @@ declare class TypeProvider {
592
539
  /**
593
540
  * Create a schema for a string enum.
594
541
  */
595
- enum<T extends string[]>(values: [...T], options?: TStringOptions$1): TUnsafe<T[number]>;
542
+ enum<T extends string[]>(values: [...T], options?: TStringOptions$1): TUnsafe$1<T[number]>;
596
543
  /**
597
544
  * Create a schema for a bigint.
598
545
  * This is NOT a BigInt object, but a string that represents a bigint.
599
546
  */
600
- bigint(options?: TStringOptions$1): TString$1;
601
- /**
602
- * Create a schema for a datetime.
603
- * This is NOT a Date object, but a string in ISO 8601 format.
604
- */
605
- datetime(options?: TStringOptions$1): TString$1;
547
+ bigint(options?: TStringOptions$1): TypeBox.TCodec<TString$1, bigint>;
606
548
  /**
607
- * Create a schema for a date.
608
- * This is NOT a Date object, but a string in ISO 8601 date format (YYYY-MM-DD).
549
+ * Create a schema for a url.
609
550
  */
610
- date(options?: TStringOptions$1): TString$1;
551
+ url(options?: TStringOptions$1): TypeBox.TCodec<TString$1, URL>;
611
552
  /**
612
- * Create a schema for a url.
553
+ * Create a schema for binary data represented as a string.
613
554
  */
614
- url(options?: TStringOptions$1): TString$1;
555
+ binary(options: TStringOptions$1): TypeBox.TCodec<TString$1, Uint8Array<ArrayBuffer>>;
615
556
  /**
616
557
  * Create a schema for uuid.
617
558
  */
@@ -685,6 +626,192 @@ interface TTextOptions extends TStringOptions$1 {
685
626
  }
686
627
  declare const t: TypeProvider;
687
628
  //#endregion
629
+ //#region src/providers/SchemaCodec.d.ts
630
+ declare abstract class SchemaCodec {
631
+ protected cache: Map<TSchema$1, Validator>;
632
+ protected guard: TypeGuard;
633
+ /**
634
+ * Encode the value to a string format.
635
+ */
636
+ abstract encodeToString(schema: TSchema$1, value: any): string;
637
+ /**
638
+ * Encode the value to a binary format.
639
+ */
640
+ abstract encodeToBinary(schema: TSchema$1, value: any): Uint8Array;
641
+ /**
642
+ * Transform a specific type for this codec.
643
+ * Return undefined to use the default schema, or return a new schema to replace it.
644
+ *
645
+ * Override this method to customize how specific types are handled in this codec.
646
+ * For example, a ProtobufCodec might keep BigInt as-is instead of converting to string.
647
+ */
648
+ protected abstract transformType(schema: TSchema$1): TSchema$1 | false | void;
649
+ encode<T extends TSchema$1>(schema: T, value: any): StaticEncode$1<T>;
650
+ decode<T extends TSchema$1>(schema: T, value: any): StaticDecode$1<T>;
651
+ protected validator<T extends TSchema$1>(schema: T): Validator<{}, T>;
652
+ /**
653
+ * Transform the schema for this codec.
654
+ * Override this method to provide custom schema transformations.
655
+ * This method should recursively transform all nested schemas.
656
+ */
657
+ protected transformSchema<T extends TSchema$1>(schema: T): TSchema$1;
658
+ /**
659
+ * Preprocess the value based on the schema before encoding.
660
+ *
661
+ * - Remove `undefined` values from objects. { a: 1, b: undefined } => { a: 1 }
662
+ */
663
+ beforeEncode: (schema: any, value: any) => any;
664
+ /**
665
+ * Preprocess the value based on the schema before validation.
666
+ *
667
+ * - If the value is `null` and the schema does not allow `null`, it converts it to `undefined`.
668
+ * - If the value is a string and the schema has a `~options.trim` flag, it trims whitespace from the string.
669
+ */
670
+ beforeDecode: (schema: any, value: any) => any;
671
+ /**
672
+ * Used by `preprocess` to determine if a schema allows null values.
673
+ */
674
+ protected isSchemaNullable: (schema: any) => boolean;
675
+ }
676
+ //#endregion
677
+ //#region src/providers/JsonSchemaCodec.d.ts
678
+ declare class JsonSchemaCodec extends SchemaCodec {
679
+ protected readonly json: Json;
680
+ protected readonly encoder: TextEncoder;
681
+ protected readonly decoder: TextDecoder;
682
+ protected transformType(schema: TSchema$1): TSchema$1 | false | void;
683
+ encodeToString(schema: TSchema$1, value: any): string;
684
+ encodeToBinary(schema: TSchema$1, value: any): Uint8Array;
685
+ decode<T extends TSchema$1>(schema: T, value: any): StaticDecode$1<T>;
686
+ }
687
+ //#endregion
688
+ //#region src/providers/CodecManager.d.ts
689
+ type Encoding = "object" | "string" | "binary";
690
+ interface EncodeOptions<T extends Encoding = Encoding> {
691
+ /**
692
+ * The output encoding format:
693
+ * - 'object': Returns native types (objects, BigInt, Date, etc.)
694
+ * - 'string': Returns JSON string
695
+ * - 'binary': Returns Uint8Array (for protobuf, msgpack, etc.)
696
+ */
697
+ as?: T;
698
+ /**
699
+ * The encoder to use (e.g., 'json', 'protobuf', 'msgpack')
700
+ * Defaults to 'json'
701
+ */
702
+ encoder?: string;
703
+ }
704
+ type EncodeResult<T extends TSchema$1, E extends Encoding> = E extends "string" ? string : E extends "binary" ? Uint8Array : StaticEncode$1<T>;
705
+ interface DecodeOptions {
706
+ /**
707
+ * The encoder to use (e.g., 'json', 'protobuf', 'msgpack')
708
+ * Defaults to 'json'
709
+ */
710
+ encoder?: string;
711
+ }
712
+ /**
713
+ * CodecManager manages multiple codec formats and provides a unified interface
714
+ * for encoding and decoding data with different formats.
715
+ */
716
+ declare class CodecManager {
717
+ protected readonly codecs: Map<string, SchemaCodec>;
718
+ protected readonly jsonCodec: JsonSchemaCodec;
719
+ default: string;
720
+ constructor();
721
+ /**
722
+ * Register a new codec format.
723
+ * @param name - The name of the codec (e.g., 'json', 'protobuf')
724
+ * @param codec - The codec implementation
725
+ */
726
+ register(name: string, codec: SchemaCodec): void;
727
+ /**
728
+ * Get a specific codec by name.
729
+ * @param name - The name of the codec
730
+ * @returns The codec instance
731
+ * @throws {AlephaError} If the codec is not found
732
+ */
733
+ get(name: string): SchemaCodec;
734
+ /**
735
+ * Encode data using the specified codec and output format.
736
+ */
737
+ encode<T extends TSchema$1, E extends Encoding = "object">(schema: T, value: any, options?: EncodeOptions<E>): EncodeResult<T, E>;
738
+ /**
739
+ * Decode data using the specified codec.
740
+ */
741
+ decode<T extends TSchema$1>(schema: T, value: any, options?: DecodeOptions): StaticDecode$1<T>;
742
+ }
743
+ //#endregion
744
+ //#region src/providers/EventManager.d.ts
745
+ declare class EventManager {
746
+ protected logFn?: () => LoggerInterface | undefined;
747
+ /**
748
+ * List of events that can be triggered. Powered by $hook().
749
+ */
750
+ protected events: Record<string, Array<Hook>>;
751
+ constructor(logFn?: () => LoggerInterface | undefined);
752
+ protected get log(): LoggerInterface | undefined;
753
+ /**
754
+ * Registers a hook for the specified event.
755
+ */
756
+ on<T extends keyof Hooks>(event: T, hookOrFunc: Hook<T> | ((payload: Hooks[T]) => Async<void>)): () => void;
757
+ /**
758
+ * Emits the specified event with the given payload.
759
+ */
760
+ emit<T extends keyof Hooks>(func: T, payload: Hooks[T], options?: {
761
+ /**
762
+ * If true, the hooks will be executed in reverse order.
763
+ * This is useful for "stop" hooks that should be executed in reverse order.
764
+ *
765
+ * @default false
766
+ */
767
+ reverse?: boolean;
768
+ /**
769
+ * If true, the hooks will be logged with their execution time.
770
+ *
771
+ * @default false
772
+ */
773
+ log?: boolean;
774
+ /**
775
+ * If true, errors will be caught and logged instead of throwing.
776
+ *
777
+ * @default false
778
+ */
779
+ catch?: boolean;
780
+ }): Promise<void>;
781
+ }
782
+ //#endregion
783
+ //#region src/providers/StateManager.d.ts
784
+ declare class StateManager<S extends Record<string, any> = State> {
785
+ protected readonly als: AlsProvider;
786
+ protected readonly events: EventManager;
787
+ protected store: Partial<S>;
788
+ constructor(store?: Partial<S>);
789
+ /**
790
+ * Get a value from the state with proper typing
791
+ */
792
+ get<Key extends keyof S>(key: Key): S[Key] | undefined;
793
+ /**
794
+ * Set a value in the state
795
+ */
796
+ set<Key extends keyof S>(key: Key, value: S[Key] | undefined): this;
797
+ /**
798
+ * Check if a key exists in the state
799
+ */
800
+ has<Key extends keyof S>(key: Key): boolean;
801
+ /**
802
+ * Delete a key from the state (set to undefined)
803
+ */
804
+ del<Key extends keyof S>(key: Key): this;
805
+ /**
806
+ * Clear all state
807
+ */
808
+ clear(): this;
809
+ /**
810
+ * Get all keys that exist in the state
811
+ */
812
+ keys(): (keyof S)[];
813
+ }
814
+ //#endregion
688
815
  //#region src/Alepha.d.ts
689
816
  /**
690
817
  * Core container of the Alepha framework.
@@ -819,10 +946,6 @@ declare class Alepha {
819
946
  * If you are not interested about these helpers, you can use the constructor directly.
820
947
  */
821
948
  static create(state?: Partial<State>): Alepha;
822
- /**
823
- * List of all services + how they are provided.
824
- */
825
- protected registry: Map<Service, ServiceDefinition>;
826
949
  /**
827
950
  * Flag indicating whether the App won't accept any further changes.
828
951
  * Pass to true when #start() is called.
@@ -845,33 +968,11 @@ declare class Alepha {
845
968
  */
846
969
  protected starting?: PromiseWithResolvers<this>;
847
970
  /**
848
- * The current state of the App.
849
- *
850
- * It contains the environment variables, logger, and other state-related properties.
851
- *
852
- * You can declare your own state properties by extending the `State` interface.
853
- *
854
- * ```ts
855
- * declare module "alepha" {
856
- * interface State {
857
- * myCustomValue: string;
858
- * }
859
- * }
860
- * ```
861
- *
862
- * Same story for the `Env` interface.
863
- * ```ts
864
- * declare module "alepha" {
865
- * interface Env {
866
- * readonly myCustomValue: string;
867
- * }
868
- * }
869
- * ```
971
+ * Initial state of the container.
870
972
  *
871
- * State values can be function or primitive values.
872
- * However, all .env variables must serializable to JSON.
973
+ * > Used to initialize the StateManager.
873
974
  */
874
- protected store: State;
975
+ protected init: State;
875
976
  /**
876
977
  * During the instantiation process, we keep a list of pending instantiations.
877
978
  * > It allows us to detect circular dependencies.
@@ -882,22 +983,34 @@ declare class Alepha {
882
983
  * > It allows us to avoid parsing the same schema multiple times.
883
984
  */
884
985
  protected cacheEnv: Map<TSchema, any>;
885
- /**
886
- * Cache for TypeBox type checks.
887
- * > It allows us to avoid compiling the same schema multiple times.
888
- */
889
- protected cacheTypeCheck: Map<TSchema, Validator>;
890
986
  /**
891
987
  * List of modules that are registered in the container.
892
988
  *
893
989
  * Modules are used to group services and provide a way to register them in the container.
894
990
  */
895
991
  protected modules: Array<Module>;
992
+ /**
993
+ * List of service substitutions.
994
+ *
995
+ * Services registered here will be replaced by the specified service when injected.
996
+ */
896
997
  protected substitutions: Map<Service, {
897
998
  use: Service;
898
999
  }>;
1000
+ /**
1001
+ * Configuration states for services.
1002
+ *
1003
+ * Used to configure services when they are instantiated.
1004
+ */
899
1005
  protected configurations: Map<Service, object>;
1006
+ /**
1007
+ * Registry of descriptors.
1008
+ */
900
1009
  protected descriptorRegistry: Map<Service<Descriptor<{}>>, Descriptor<{}>[]>;
1010
+ /**
1011
+ * List of all services + how they are provided.
1012
+ */
1013
+ protected registry: Map<Service, ServiceDefinition>;
901
1014
  /**
902
1015
  * Node.js feature that allows to store context across asynchronous calls.
903
1016
  *
@@ -905,15 +1018,21 @@ declare class Alepha {
905
1018
  *
906
1019
  * Mocked for browser environments.
907
1020
  */
908
- readonly context: AlsProvider;
1021
+ get context(): AlsProvider;
909
1022
  /**
910
1023
  * Event manager to handle lifecycle events and custom events.
911
1024
  */
912
- readonly events: EventManager;
1025
+ get events(): EventManager;
913
1026
  /**
914
1027
  * State manager to store arbitrary values.
915
1028
  */
916
- readonly state: StateManager<State>;
1029
+ get state(): StateManager<Record<string, any>>;
1030
+ /**
1031
+ * Codec manager for encoding and decoding data with different formats.
1032
+ *
1033
+ * Supports multiple codec formats (JSON, Protobuf, etc.) with a unified interface.
1034
+ */
1035
+ get codec(): CodecManager;
917
1036
  /**
918
1037
  * Get logger instance.
919
1038
  */
@@ -922,7 +1041,7 @@ declare class Alepha {
922
1041
  * The environment variables for the App.
923
1042
  */
924
1043
  get env(): Readonly<Env>;
925
- constructor(state?: Partial<State>);
1044
+ constructor(init?: Partial<State>);
926
1045
  /**
927
1046
  * True when start() is called.
928
1047
  *
@@ -958,7 +1077,7 @@ declare class Alepha {
958
1077
  /**
959
1078
  * Returns whether the App is running in a serverless environment.
960
1079
  */
961
- isServerless(): boolean | "vercel";
1080
+ isServerless(): boolean;
962
1081
  /**
963
1082
  * Returns whether the App is in test mode. (Running in a test environment)
964
1083
  *
@@ -1082,43 +1201,6 @@ declare class Alepha {
1082
1201
  configure<T extends {
1083
1202
  options?: object;
1084
1203
  }>(service: Service<T>, state: Partial<T["options"]>): this;
1085
- /**
1086
- * Casts the given value to the specified schema.
1087
- *
1088
- * It uses the TypeBox library to validate the value against the schema.
1089
- */
1090
- parse<T extends TSchema>(schema: T, value?: unknown, opts?: {
1091
- /**
1092
- * Clone the value before parsing.
1093
- * @default true
1094
- */
1095
- clone?: boolean;
1096
- /**
1097
- * Apply default values defined in the schema.
1098
- * @default true
1099
- */
1100
- default?: boolean;
1101
- /**
1102
- * Remove all values not defined in the schema.
1103
- * @default true
1104
- */
1105
- clean?: boolean;
1106
- /**
1107
- * Try to cast/convert some data based on the schema.
1108
- * @default true
1109
- */
1110
- convert?: boolean;
1111
- /**
1112
- * Convert `null` to `undefined`
1113
- * @default true
1114
- */
1115
- convertNullToUndefined?: boolean;
1116
- /**
1117
- * Prepare value after being deserialized.
1118
- * @default true
1119
- */
1120
- check?: boolean;
1121
- }): Static$1<T>;
1122
1204
  /**
1123
1205
  * Applies environment variables to the provided schema and state object.
1124
1206
  *
@@ -1144,20 +1226,20 @@ declare class Alepha {
1144
1226
  protected new<T extends object>(service: Service<T>, args?: any[]): T;
1145
1227
  protected processDescriptor(value: Descriptor, propertyKey?: string): void;
1146
1228
  }
1147
- interface Hook<T$1 extends keyof Hooks = any> {
1229
+ interface Hook<T extends keyof Hooks = any> {
1148
1230
  caller?: Service;
1149
1231
  priority?: "first" | "last";
1150
- callback: (payload: Hooks[T$1]) => Async<void>;
1232
+ callback: (payload: Hooks[T]) => Async<void>;
1151
1233
  }
1152
1234
  /**
1153
1235
  * This is how we store services in the Alepha container.
1154
1236
  */
1155
- interface ServiceDefinition<T$1 extends object = any> {
1237
+ interface ServiceDefinition<T extends object = any> {
1156
1238
  /**
1157
1239
  * The instance of the class or type definition.
1158
1240
  * Mostly used for caching / singleton but can be used for other purposes like forcing the instance.
1159
1241
  */
1160
- instance: T$1;
1242
+ instance: T;
1161
1243
  /**
1162
1244
  * List of classes which use this class.
1163
1245
  */
@@ -1229,8 +1311,8 @@ interface Hooks {
1229
1311
  prevValue: any;
1230
1312
  };
1231
1313
  }
1232
- interface Configurable<T$1 extends object = any> {
1233
- options: T$1;
1314
+ interface Configurable<T extends object = any> {
1315
+ options: T;
1234
1316
  }
1235
1317
  //#endregion
1236
1318
  //#region src/interfaces/Run.d.ts
@@ -1387,15 +1469,15 @@ declare const $hook: {
1387
1469
  <T extends keyof Hooks>(options: HookOptions<T>): HookDescriptor<T>;
1388
1470
  [KIND]: typeof HookDescriptor;
1389
1471
  };
1390
- interface HookOptions<T$1 extends keyof Hooks> {
1472
+ interface HookOptions<T extends keyof Hooks> {
1391
1473
  /**
1392
1474
  * The name of the hook. "configure", "start", "ready", "stop", ...
1393
1475
  */
1394
- on: T$1;
1476
+ on: T;
1395
1477
  /**
1396
1478
  * The handler to run when the hook is triggered.
1397
1479
  */
1398
- handler: (args: Hooks[T$1]) => Async<any>;
1480
+ handler: (args: Hooks[T]) => Async<any>;
1399
1481
  /**
1400
1482
  * Force the hook to run first or last on the list of hooks.
1401
1483
  */
@@ -1409,7 +1491,7 @@ interface HookOptions<T$1 extends keyof Hooks> {
1409
1491
  */
1410
1492
  after?: object | Array<object>;
1411
1493
  }
1412
- declare class HookDescriptor<T$1 extends keyof Hooks> extends Descriptor<HookOptions<T$1>> {
1494
+ declare class HookDescriptor<T extends keyof Hooks> extends Descriptor<HookOptions<T>> {
1413
1495
  called: number;
1414
1496
  protected onInit(): void;
1415
1497
  }
@@ -1450,23 +1532,32 @@ declare class TooLateSubstitutionError extends AlephaError {
1450
1532
  declare class TypeBoxError extends AlephaError {
1451
1533
  readonly name = "TypeBoxError";
1452
1534
  readonly cause: TLocalizedValidationError;
1453
- readonly value: any;
1454
- constructor(error: TLocalizedValidationError, value: any);
1535
+ readonly value: {
1536
+ path: string;
1537
+ message: string;
1538
+ };
1539
+ constructor(error: TLocalizedValidationError);
1540
+ }
1541
+ interface TypeBoxErrorParams {
1542
+ requiredProperties?: string[];
1455
1543
  }
1456
1544
  //#endregion
1457
1545
  //#region src/index.d.ts
1458
- declare global {
1459
- interface ImportMetaEnv {
1460
- SSR: boolean;
1461
- }
1462
- interface ImportMeta {
1463
- readonly env: ImportMetaEnv;
1464
- }
1465
- }
1466
1546
  /**
1547
+ * Run Alepha application, trigger start lifecycle.
1467
1548
  *
1549
+ * ```ts
1550
+ * import { Alepha, run } from "alepha";
1551
+ * import { MyService } from "./services/MyService.ts";
1552
+ *
1553
+ * const alepha = new Alepha({ env: { APP_NAME: "MyAlephaApp" } });
1554
+ *
1555
+ * alepha.with(MyService);
1556
+ *
1557
+ * export default run(alepha);
1558
+ * ```
1468
1559
  */
1469
- declare const run: (entry: Alepha | Service | Array<Service>, opts?: RunOptions) => void;
1560
+ declare const run: (entry: Alepha | Service | Array<Service>, opts?: RunOptions) => Alepha;
1470
1561
  //#endregion
1471
- export { $cursor, $env, $hook, $inject, $module, AbstractClass, Alepha, AlephaError, AlsProvider, AppNotStartedError, Async, AsyncFn, AsyncLocalStorageData, CircularDependencyError, Configurable, ContainerLockedError, CursorDescriptor, Descriptor, DescriptorArgs, DescriptorConfig, DescriptorFactory, DescriptorFactoryLike, Env, FileLike, Hook, HookDescriptor, HookOptions, Hooks, InjectDescriptor, InjectOptions, InstantiableClass, KIND, LogLevel, LoggerInterface, MaybePromise, Module, ModuleDescriptorOptions, OPTIONS, Service, ServiceEntry, ServiceSubstitution, State, StateManager, type Static, type StaticDecode, type StaticEncode, StreamLike, type TAny, type TArray, type TBigInt, type TBoolean, TFile, type TInteger, type TKeysToIndexer, type TNull, type TNumber, type TNumberOptions, type TObject, type TObjectOptions, type TOptional, type TOptionalAdd, type TPick, type TProperties, type TRecord, type TSchema, TStream, type TString, type TStringOptions, TTextOptions, type TTuple, type TUnion, type TVoid, TextLength, TooLateSubstitutionError, TypeBox, TypeBoxError, TypeBoxFormat, TypeBoxValue, TypeGuard, TypeProvider, WithModule, __alephaRef, createDescriptor, isDate, isDateTime, isEmail, isFileLike, isTypeFile, isURL, isUUID, run, t };
1562
+ export { $cursor, $env, $hook, $inject, $module, AbstractClass, Alepha, AlephaError, AlsProvider, AppNotStartedError, Async, AsyncFn, AsyncLocalStorageData, CircularDependencyError, CodecManager, Configurable, ContainerLockedError, CursorDescriptor, DecodeOptions, Descriptor, DescriptorArgs, DescriptorConfig, DescriptorFactory, DescriptorFactoryLike, EncodeOptions, EncodeResult, Encoding, Env, FileLike, Hook, HookDescriptor, HookOptions, Hooks, InjectDescriptor, InjectOptions, InstantiableClass, JsonSchemaCodec, KIND, LogLevel, LoggerInterface, MaybePromise, Module, ModuleDescriptorOptions, OPTIONS, RunFunction, SchemaCodec, Service, ServiceEntry, ServiceSubstitution, State, StateManager, type Static, type StaticDecode, type StaticEncode, StreamLike, type TAny, type TArray, type TBigInt, type TBoolean, TFile, type TInteger, type TKeysToIndexer, type TNull, type TNumber, type TNumberOptions, type TObject, type TObjectOptions, type TOptional, type TOptionalAdd, type TPick, type TProperties, type TRecord, type TSchema, TStream, type TString, type TStringOptions, TTextOptions, type TTuple, type TUnion, type TUnsafe, type TVoid, TextLength, TooLateSubstitutionError, TypeBox, TypeBoxError, TypeBoxErrorParams, TypeBoxFormat, TypeBoxValue, TypeGuard, TypeProvider, WithModule, __alephaRef, createDescriptor, isClass, isEmail, isFileLike, isTypeFile, isURL, isUUID, run, t };
1472
1563
  //# sourceMappingURL=index.d.ts.map