[data-reveal]{opacity:1!important;transform:none!important}
DocumentationInterfaces

Interfaces

An interface is the contract modules use to communicate. It declares functions, events, and registrations without tying them to one implementation.

What is an interface

An interface is a contract that defines how modules communicate. The contract declares functions, events, or registrations without implementing them. Producers fulfill the contract and consumers call it. Neither side knows about the other directly.

The AntelopeJS core binds each interface import to a provider. A module that needs email delivery imports the email interface, not the email provider. To replace that provider, the new one needs a compatible contract and compatible behavior. Matching TypeScript types alone are not enough.

The binding diagram separates the runtime call path from the application's provider choice.

Choose your role

You are buildingWhat you need
A consumer of an existing capabilityInstall the interface dependency, configure a provider, and call its public API.
A provider for an existing contractDeclare antelopeJs.implements and attach implementations during construct().
A new shared capabilityDefine the contract below, then follow Exporting interfaces.

Before startup, the core checks that every required interface package has a provider. A contract package on its own does not supply an implementation.

What an interface contains

An interface file holds more than type definitions and proxy declarations. It is the complete public API that both producers and consumers rely on.

An interface can contain:

  • Type definitions, the TypeScript types and interfaces that producer and consumer share.
  • Proxy instances, the InterfaceFunction, EventProxy, and RegisteringProxy declarations that define the contract points.
  • Wrapper functions, the public functions that call the underlying proxies and give consumers a cleaner API.
  • Decorator factories, the decorators that consumers use, such as @Controller or @Authentication.
  • Utility code, such as helper classes, query builders, or metadata management. Any generic code that stays the same for every implementation belongs here.

The following example shows an interface file with shared types, internal proxies, and public wrapper functions:

src/interface/index.ts typescript
import { InterfaceFunction } from "@antelopejs/interface-core";

export interface EmailParams {
  to: string;
  subject: string;
  body: string;
}

export interface EmailResult {
  messageId: string;
  status: string;
}

export namespace internal {
  export const sendEmailProxy =
    InterfaceFunction<(params: EmailParams) => Promise<EmailResult>>();
  export const getStatusProxy =
    InterfaceFunction<(messageId: string) => Promise<string>>();
}

export function SendEmail(params: EmailParams): Promise<EmailResult> {
  return internal.sendEmailProxy(params);
}

export function GetEmailStatus(messageId: string): Promise<string> {
  return internal.getStatusProxy(messageId);
}

Consumers call SendEmail(params) instead of the proxy. The wrapper functions are the stable, readable API, and the proxies find the provider at run time.

Note

An interface file can contain generic code that does not change between implementations. Decorator factories, utility functions, and helper classes belong in the interface when all implementations share the same behavior.

Embedded vs standalone interfaces

An interface takes one of two forms, depending on how many modules implement it.

Embedded interface

An embedded interface lives inside the module that implements the contract, in a src/interface/ directory. The module contains both the contract and its implementation. When you create a new module with a new interface, start with an embedded interface.

text
my-module/
├── src/
│   ├── index.ts
│   ├── interface/
│   │   └── index.ts          # Interface contract
│   └── implementations/
│       └── index.ts          # Implementation

The module entry point connects the contract to its implementation:

src/index.ts typescript
import { ImplementInterface } from "@antelopejs/interface-core";

export async function construct() {
  ImplementInterface(
    await import("./interface"),
    await import("./implementations"),
  );
}

Standalone interface package

When an interface needs several implementations, move the contract into its own npm package. The standalone interface package contains only the contract: types, proxies, wrappers, and utilities. Each implementing module is a separate package and lists the interface in its implements field.

text
@antelopejs/interface-email/     # Standalone interface package
├── src/
│   └── index.ts                 # Types, proxies, wrappers

@antelopejs/email-brevo/         # Implementation module A
├── src/
│   ├── index.ts
│   └── implementations/
│       └── email/
│           └── index.ts

@antelopejs/email-sendgrid/      # Implementation module B
├── src/
│   ├── index.ts
│   └── implementations/
│       └── email/
│           └── index.ts

Each implementing module names the interface package in its package.json:

package.json json
{
  "antelopeJs": {
    "implements": ["@antelopejs/interface-email"]
  }
}

The module entry point imports the interface from the standalone package instead of a local path:

src/index.ts typescript
import { ImplementInterface } from "@antelopejs/interface-core";

export async function construct() {
  ImplementInterface(
    await import("@antelopejs/interface-email"),
    await import("./implementations/email"),
  );
}

When to extract

Start with an embedded interface. Move it to a standalone package when one of these is true:

  • A second module needs to implement the same interface.
  • Consumers should depend on the contract without depending on a specific implementation.
  • The interface becomes part of the public ecosystem and needs its own release cycle.
Tip

Extract earlier when consumers need a contract with its own release cycle, or must not depend on the provider package.

Complete example

The following example follows a storage interface from declaration to use. It covers three files: the interface contract, the producer module that implements it, and a consumer module that calls it.

Declare the interface

The interface defines a key-value storage contract with get and set operations. InterfaceFunction creates typed proxies, and wrapper functions are the public API.

src/interface/index.ts typescript
import { InterfaceFunction } from "@antelopejs/interface-core";

export interface StorageEntry {
  key: string;
  value: string;
  updatedAt: number;
}

export namespace internal {
  export const getProxy =
    InterfaceFunction<(key: string) => Promise<StorageEntry | null>>();
  export const setProxy =
    InterfaceFunction<(key: string, value: string) => Promise<StorageEntry>>();
}

export function Get(key: string): Promise<StorageEntry | null> {
  return internal.getProxy(key);
}

export function Set(key: string, value: string): Promise<StorageEntry> {
  return internal.setProxy(key, value);
}

Implement it in the producer module

The producer module exports a namespace that mirrors the interface's internal namespace. ImplementInterface attaches each function to the proxy with the same name. If a declared function has no implementation under that name, ImplementInterface throws a TypeError that names the missing path, such as Missing or malformed interface handler at implementation.internal.getProxy.

src/implementations/index.ts typescript
import type { StorageEntry } from "../interface";

const store = new Map<string, StorageEntry>();

export namespace internal {
  export async function getProxy(key: string): Promise<StorageEntry | null> {
    return store.get(key) ?? null;
  }

  export async function setProxy(
    key: string,
    value: string,
  ): Promise<StorageEntry> {
    const entry: StorageEntry = { key, value, updatedAt: Date.now() };
    store.set(key, entry);
    return entry;
  }
}

The module entry point connects the interface to its implementation in construct:

src/index.ts typescript
import { ImplementInterface } from "@antelopejs/interface-core";

export async function construct() {
  ImplementInterface(
    await import("./interface"),
    await import("./implementations"),
  );
}

Consume it from another module

A consumer imports the public contract subpath and calls its wrapper functions. In this embedded example, the contract still ships in the producer's package. A standalone package removes that packaging dependency.

src/index.ts typescript
import { Get, Set } from "@your-org/storage-module/interface";

export async function start() {
  await Set("greeting", "hello world");

  const entry = await Get("greeting");
  if (entry) {
    console.log(entry.value);
  }
}

If no implementation is attached yet, the call waits in a queue until one attaches. The queue does not fix a missing provider or a circular startup dependency. Do not await another module's interface from provide(). Avoid construction cycles where each module waits for the other before it attaches its implementation.

Interface instances

This section matters when you configure several providers for one contract. With one provider, all consumers share the same interface instance. With two database providers, consumers bound to mongodb-module and consumers bound to postgres-module import separate copies of @antelopejs/interface-database.

Each copy's proxies belong to exactly one provider. An object built from a copy, such as a query or a schema, carries that copy's proxies, so it reaches that provider wherever it ends up. A standalone interface built on other interfaces is copied the same way, once per combination of the bindings it is built on. Interface bindings describes how a module is bound.

A module that lists an interface in its disabledExports gets a copy of its own, which only its own imports reach. Every other import, including one from a module that does not declare the interface and one from host code, reaches another provider's copy. When no other module provides the interface, that import gets a copy with no provider, and its async calls reject.

When a project configures two instances of one module package, the core evaluates each instance and its dependencies separately. Neither instance sees the other's module-level state. The second instance runs from a copy of the module folder. A dependency linked from outside that folder, such as a pnpm workspace member, is copied without the packages installed next to it, so its own dependencies can fail to resolve in the copy. The core logs a warning that names those links.

How the core tells the two kinds apart

This section is for publishing a package or diagnosing provider discovery. An interface package and a runtime module both carry an antelopeJs key in package.json. Within that key, implements separates the usual cases:

antelopeJs.implementsThe package isDepending on it means
absent, or []an interface packagethe contract needs a provider
lists other packagesa runtime modulea plain package dependency
lists only the package itselfa runtime module that ships its own interfacethe contract needs a provider

Declare implements only for capabilities the module provides. A package does not need to name itself because another package depends on it. The check never treats a package that the project loads as a module as an interface dependency.

The manifest flag antelopeJs.standalone: true is a different thing from a standalone interface package. The flag lets an interface run without an external provider, on its own implementation. Do not add it to an ordinary contract that only declares proxies, because the flag does not create the missing implementation. Optional interface dependencies also do not block startup, so a caller must handle a capability that has no provider.

See also

  • Proxies explains how each proxy type carries calls between modules.
  • Exporting interfaces shows how to export interfaces from your module.