Skip to main content

Class Signature

class App<E extends Record<string, unknown>>
The App class manages the application lifecycle, dependency injection container, event emitter, and extension bootstrapping.

Type Parameters

E
Record<string, unknown>
The type of the exposed services object. This is inferred from the expose schema passed to createApp.

Properties

name

public readonly name: string
The name of the application.

debug

public readonly debug: boolean
Whether debug mode is enabled. Defaults to false.

timezone

public readonly timezone: string
The application’s timezone. Defaults to "UTC". This value is also set to process.env.timezone.

emitter

public readonly emitter: Emitter
The application’s event emitter instance. Used for application-wide events like 'app:ready'.

$

public readonly $: E
Object containing exposed services as defined in the expose schema. Provides type-safe access to registered services.

Methods

init()

async init(): Promise<void>
Initializes the application by resolving and exposing services defined in the expose schema. This method is called automatically by createApp() and typically doesn’t need to be called manually.
void
Promise<void>
Returns a Promise that resolves when initialization is complete.
Behavior:
  • Iterates through the expose schema
  • Resolves each token from the dependency injection container
  • Assigns the resolved instances to the $ object

rootPath()

rootPath(...paths: string[]): string
Resolves paths relative to the application’s root directory (current working directory).
...paths
string[]
Path segments to join. Backslashes are automatically converted to forward slashes for cross-platform compatibility.
string
string
The absolute path joined from the root directory and provided segments.
Examples:
app.rootPath(); // Returns cwd()
app.rootPath('config'); // Returns /path/to/app/config
app.rootPath('config', 'database.json'); // Returns /path/to/app/config/database.json
app.rootPath('config\\log'); // Returns /path/to/app/config/log (backslashes converted)

runtimePath()

runtimePath(...paths: string[]): string
Resolves paths relative to the application’s runtime directory ({root}/runtime).
...paths
string[]
Path segments to join. Backslashes are automatically converted to forward slashes.
string
string
The absolute path joined from the runtime directory and provided segments.
Examples:
app.runtimePath(); // Returns /path/to/app/runtime
app.runtimePath('cache'); // Returns /path/to/app/runtime/cache
app.runtimePath('logs', 'app.log'); // Returns /path/to/app/runtime/logs/app.log

get()

get<T>(token: Token<T>): T
Retrieves a service from the dependency injection container by its token.
token
Token<T>
required
The dependency injection token for the service to retrieve.
T
T
The resolved service instance.
Example:
const LOGGER = Symbol('LOGGER') as Token<LoggerService>;

const app = await createApp({
  name: 'MyApp',
  providers: [
    { token: LOGGER, factory: () => new LoggerService() }
  ]
});

const logger = app.get(LOGGER);
logger.log('Hello');

run()

async run(): Promise<void>
Starts the application by executing all extension bootstrap functions and emitting the 'app:ready' event.
void
Promise<void>
Returns a Promise that resolves when all bootstrap functions complete.
Behavior:
  • Executes all bootstrap functions from registered extensions in parallel
  • Emits the 'app:ready' event when bootstrapping completes
  • Is idempotent - calling multiple times has no additional effect
  • If any bootstrap function throws an error, the Promise rejects
Example:
const app = await createApp({
  name: 'MyApp',
  extensions: [
    {
      name: 'database',
      bootstrap: async (context) => {
        // Connect to database
        console.log('Database connected');
      }
    }
  ]
});

await app.run(); // Logs: "Database connected"
await app.run(); // No effect - bootstrap functions only run once

dispose()

async dispose(): Promise<void>
Cleans up application resources by disposing the dependency injection container and removing all event listeners.
void
Promise<void>
Returns a Promise that resolves when cleanup is complete.
Behavior:
  • Calls dispose() on the dependency injection container (which calls dispose() on all services that implement it)
  • Removes all event listeners from the emitter using emitter.offAll()
Example:
const DB = Symbol('DB') as Token<DatabaseService>;

const app = await createApp({
  name: 'MyApp',
  providers: [
    {
      token: DB,
      factory: () => ({
        connection: connectToDb(),
        dispose: () => {
          console.log('Disconnecting from database');
        }
      })
    }
  ]
});

await app.run();

// When shutting down:
await app.dispose(); // Logs: "Disconnecting from database"

See Also

  • createApp - Factory function to create App instances
  • AppContext - Context object provided to extensions
  • Extension - Extension interface for extending application functionality

Build docs developers (and LLMs) love