@adviser/cement - v0.0.0
    Preparing search index...

    Class AppContext

    Context container for storing and retrieving runtime values.

    AppContext provides a type-safe key-value store for application configuration and runtime state. Useful for passing configuration like URLs, credentials, or other runtime parameters through an application.

    const ctx = new AppContext();
    ctx.set('apiUrl', 'https://api.example.com');
    ctx.set('timeout', 5000);

    const url = ctx.get<string>('apiUrl');
    const timeout = ctx.get<number>('timeout');
    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    ctx: Map<string, unknown> = ...

    Methods

    • Type guard to check if a value is an AppContext instance.

      Parameters

      • ctx: unknown

        Value to check

      Returns ctx is AppContext

      True if value is an AppContext instance

    • Merges multiple contexts or objects into a single AppContext.

      Combines values from multiple AppContext instances or plain objects. Later values overwrite earlier ones for duplicate keys. Undefined values are skipped.

      Parameters

      • ...ctxs: (Record<string, unknown> | AppContext | undefined)[]

        AppContext instances, plain objects, or undefined values to merge

      Returns AppContext

      New AppContext containing merged values

      const ctx1 = new AppContext().set('a', 1).set('b', 2);
      const ctx2 = new AppContext().set('b', 3).set('c', 4);
      const obj = { d: 5 };

      const merged = AppContext.merge(ctx1, ctx2, obj);
      // Contains: { a: 1, b: 3, c: 4, d: 5 }
    • Sets a value in the context.

      Type Parameters

      • T

        Type of the value being stored

      Parameters

      • key: string

        Key to store the value under

      • value: T

        Value to store

      Returns AppContext

      This AppContext instance for chaining

      ctx.set('apiUrl', 'https://api.example.com')
      .set('timeout', 5000)
      .set('retries', 3);
    • Retrieves a value from the context.

      Type Parameters

      • T

        Expected type of the value

      Parameters

      • key: string

        Key to retrieve

      Returns T | undefined

      The value if present, undefined otherwise

      const url = ctx.get<string>('apiUrl');
      const timeout = ctx.get<number>('timeout');
    • Deletes a value from the context.

      Parameters

      • key: string

        Key to delete

      Returns void

      ctx.delete('temporaryKey');
      
    • Converts the context to a plain object.

      Returns Record<string, unknown>

      Plain object containing all key-value pairs

      const ctx = new AppContext()
      .set('a', 1)
      .set('b', 2);

      const obj = ctx.asObj();
      // { a: 1, b: 2 }