App API

primate/app creates an application facade from your configuration. Export the result from config/app.ts to use it throughout your app.

import app from "primate/app";
import type { AppFacade } from "primate/app";

See the Configuration guide for deployment advice and examples, the Application guide for facade usage, and the Modules guide for writing extensions.

app()

function app<
  P extends EnvSchema = EnvSchema,
  const I extends I18NConfig | undefined = undefined,
  const T extends Record<string, string> = Record<string, string>,
  D extends DB = DB,
>(input?: ConfigInput<P, I, T, D>): AppFacade<P, I, D>;

Parameters: Optional configuration. Omitted settings use their defaults. Invalid input throws a validation error.

Returns: An AppFacade<P, I, D> with the inferred environment, localization, and migration-database types.

// config/app.ts
import app from "primate/app";

export default app({
  http: { port: 8080 },
});

ConfigInput

The following shape summarizes the configuration accepted by app(). TypeScript infers partition names, environment values, localization, and the migration database from your input.

type ConfigInput<P, I, T, D> = {
  build?: {
    external?: string[];
  };
  conditions?: string[];
  client?: {
    partitions: T;
    routes: Record<string, keyof NoInfer<T> & string>;
  };
  http?: {
    csp?: Record<string, string[]>;
    headers?: Record<string, string>;
    host?: string;
    port?: number;
    timeout?: {
      inactivity?: number;
      request?: number;
      headers?: number;
    };
    ssl?: {
      cert?: string;
      key?: string;
    };
    static?: {
      root?: string;
    };
  };
  livereload?: {
    exclude?: string[];
    host?: string;
    port?: number;
  };
  migrations?: {
    table: string;
    db: D;
  };
  env?: {
    schema?: ObjectType<P>;
  };
  i18n?: I;
  loaders?: Record<string, "file">;
  entrypoints?: Record<string, string>;
  modules?: Module[];
};

input.build.external

build?: {
  external?: string[];
};

Packages or import paths excluded from the production server bundle. The default is []; duplicate entries are rejected. External packages must be available in the production installation.

input.conditions

conditions?: string[];

Additional package export conditions used by client and server builds. Defaults to []; entries must be unique.

input.client

client?: {
  partitions: Record<string, string>;
  routes: Record<string, string>;
};

Defines client partitions and maps route prefixes to partition names. Both maps are required when client is present, and each route must name a declared partition. See Client partitions for prefix and isolation rules.

input.http.csp

csp?: Record<string, string[]>;

Content-Security-Policy directives. No policy is emitted by default.

input.http.headers

headers?: Record<string, string>;

Default response headers for rendered application views.

input.http.host

host?: string;

Server host. Defaults to HOST from the environment, then "localhost".

input.http.port

port?: number;

TCP port. Defaults to PORT from the environment, then 6161.

input.http.timeout

timeout?: {
  inactivity?: number;
  request?: number;
  headers?: number;
};

Network timeouts in whole milliseconds. inactivity accepts 0–255000; request and headers accept 0–2147483647. Omitted clocks use runtime defaults. See HTTP timeouts for how they interact.

input.http.ssl

ssl?: {
  cert?: string;
  key?: string;
};

Certificate and private-key paths. HTTPS is active when both are set.

input.http.static.root

static?: {
  root?: string;
};

URL root under which files from the application static directory are served. Defaults to "/".

input.livereload

livereload?: {
  exclude?: string[];
  host?: string;
  port?: number;
};

Development live-reload exclusions and endpoint overrides. host defaults to the configured HTTP host and port defaults to one less than the configured HTTP port when Primate resolves the live-reload endpoint.

input.migrations

migrations?: {
  table: string;
  db: DB;
};

Migration ledger table and database. The database type is preserved in app.migrate() declarations. No ledger is configured by default.

input.env.schema

env?: {
  schema?: ObjectType<EnvSchema>;
};

Pema object schema for environment values. With a schema, app.env() is typed by key and parsed value. Omit it to read untyped environment values.

input.i18n

i18n?: I18NConfig;

Localization configuration. Without it, localization operations are unavailable. See the primate/i18n API and Internationalization guide.

input.loaders

loaders?: Record<string, "file">;

Maps file extensions to esbuild loaders. The only accepted value is "file". The default is no additional loader mapping.

input.entrypoints

entrypoints?: Record<string, string>;

Named files from client bundled for matching HTML template placeholders. The default is no named entrypoints. app, head, and body are reserved.

input.modules

modules?: Module[];

Extension modules initialized in array order. The default is []; module names must be unique. See the Module API.

Returned application facade

The value returned by app() gives server code access to configuration, environment variables, views, and logging. Export it from config/app.ts, then import it where you need it. Views, the project root, and the logger become available when the server starts.

AppFacade

declare class AppFacade<
  T extends EnvSchema = EnvSchema,
  I extends I18NConfig | undefined = undefined,
  D extends DB = DB,
> {
  get i18n(): I18NRuntime<I>;
  config(path: string): unknown;
  migrate(value: MigrationDeclaration<D>): MigrationDeclaration<D>;
  env<K extends keyof T>(key: K): T[K]["infer"];
  get views(): ViewBag;
  get root(): FileRef;
  get log(): AppLogger;
}

app.i18n

get i18n(): I18NRuntime<I>;

Localization facade inferred from input.i18n. Available in both server and browser builds. Configure i18n before using its operations. See the translator API and Internationalization guide.

app.config()

config(path: string): unknown;

Reads parsed configuration at a dot-separated path. The result type is inferred for known paths; for example, http.port returns a number. Server-only.

import app from "@/config/app";

const port = app.config("http.port"); // number

app.migrate()

migrate(
  value: MigrationDeclaration<D>,
): MigrationDeclaration<D>;

Validates a migration declaration and preserves the configured database type. The id and parent IDs must be eight lowercase hexadecimal characters; parent IDs must be unique. run is optional. Server-only.

MigrationDeclaration

Declarations passed to and returned from app.migrate() have this shape:

type MigrationDeclaration<D extends DB = DB> = {
  id: string;
  parents: readonly string[];
  run?: (db: D) => MaybePromise<unknown>;
};

app.env()

env<K extends keyof T>(key: K): T[K]["infer"];

With env.schema, returns a typed, parsed value and throws if the key is missing. Without a schema, returns the environment value as unknown. Server-only.

app.views

get views(): ViewBag;

Registry of loaded server views. Available after the facade is attached to the server; absent in browser builds.

const render = app.views.get<
  (props: { title: string }) => string | Promise<string>
>("docs/home/index.md");

ViewBag

app.views exposes loaded views through this registry:

type ViewBag = {
  get<T>(name: string): T;
  try(name: string): unknown | undefined;
  has(name: string): boolean;
  keys(): string[];
  values(): unknown[];
  entries(): [string, unknown][];
};

get() resolves registered frontend extensions and throws when no view exists. try() returns undefined instead. The collection methods return snapshots of the current registry.

app.root

get root(): FileRef;

Project-root filesystem reference from @rcompat/fs. Available after the facade is attached to the server. Server-only.

app.log

get log(): AppLogger;

Application logger. Available after the facade is attached to the server. Server-only.

AppLogger

app.log provides these logging methods and properties:

type AppLogger = {
  readonly deprecations: readonly {
    deprecated: string;
    message: string;
    replacement: string;
  }[];
  readonly level: string;
  print(strings: TemplateStringsArray, ...params: unknown[]): void;
  system(strings: TemplateStringsArray, ...params: unknown[]): void;
  trace(strings: TemplateStringsArray, ...params: unknown[]): void;
  info(strings: TemplateStringsArray, ...params: unknown[]): void;
  warn(strings: TemplateStringsArray, ...params: unknown[]): void;
  deprecate(deprecated: string, replacement: string): void;
  error(error: unknown): void;
};

trace, info, and warn are tagged-template methods; registered log hooks receive their messages even when the console level hides them. print and system write unclassified output. error() accepts a thrown value. deprecate() reports use of an API and its replacement. deprecations contains the diagnostics collected in the active build scope.

Browser availability

In browser builds, app.i18n is available. config(), migrate(), env(), root, and log throw server-only errors; views is absent. Keep server configuration and environment access in server code.

Previous
Overview
Next
Module