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

Exporting interfaces

Create interface contracts with typed proxies, implement them, and move them to a standalone package when a second implementation appears.

Create an embedded interface

An embedded interface lives inside the module that implements it. The module holds both the contract and the logic, in separate directories.

The example below is an in-memory user directory, so its data disappears when the module reloads. First make its public calls work, then add persistence behind the same contract. Use Testing to check the create, retrieve, and missing-user behavior.

Start from the module and TypeScript configuration in Creating a Module. The examples extend that package. Merge the manifest fields into it, and keep its dependencies and build settings.

text
my-module/
├── src/
│   ├── index.ts
│   ├── interface/
│   │   └── index.ts
│   └── implementations/
│       └── index.ts

The interface/ directory holds the contract: the types, the proxies, and the public wrapper functions. The implementations/ directory holds the logic that fulfills that contract.

Step 1. Define the interface contract

The interface file declares the types, the internal proxy functions, and the public wrapper functions that consumers call. The proxies live inside a namespace internal block. The wrapper functions are the public API.

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

export interface User {
  id: string;
  name: string;
  email: string;
}

export namespace internal {
  export const getUserProxy =
    InterfaceFunction<(id: string) => Promise<User>>();
  export const createUserProxy =
    InterfaceFunction<(name: string, email: string) => Promise<User>>();
  export const deleteUserProxy =
    InterfaceFunction<(id: string) => Promise<void>>();
}

export function GetUser(id: string): Promise<User> {
  return internal.getUserProxy(id);
}

export function CreateUser(name: string, email: string): Promise<User> {
  return internal.createUserProxy(name, email);
}

export function DeleteUser(id: string): Promise<void> {
  return internal.deleteUserProxy(id);
}

InterfaceFunction creates a typed proxy that the core binds to an implementation at runtime. The wrapper functions forward each call to its proxy. Consumers call plain functions and never see the proxy.

Step 2. Write the implementation

The implementation file exports a namespace that matches the interface's internal namespace. Each function in it holds the logic for the proxy of the same name.

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

const users = new Map<string, User>();

export namespace internal {
  export async function getUserProxy(id: string): Promise<User> {
    const user = users.get(id);
    if (!user) throw new Error("User not found");
    return user;
  }

  export async function createUserProxy(
    name: string,
    email: string,
  ): Promise<User> {
    const user: User = { id: crypto.randomUUID(), name, email };
    users.set(user.id, user);
    return user;
  }

  export async function deleteUserProxy(id: string): Promise<void> {
    users.delete(id);
  }
}

The namespace and function names must match exactly. The core walks the interface's exports and binds each proxy to the implementation found under the same path. If a function is missing, ImplementInterface throws Missing or malformed interface handler at implementation.internal.<name>. and binds nothing.

Step 3. Declare the interface in package.json

The implements field in package.json lists the npm package names from which consumers import the interface. For an embedded interface, the module lists its own package name, because consumers import the interface from it.

package.json json
{
  "name": "@your-org/my-module",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": ["dist"],
  "exports": {
    ".": "./dist/index.js",
    "./interface": {
      "types": "./dist/interface/index.d.ts",
      "default": "./dist/interface/index.js"
    }
  },
  "typesVersions": {
    "*": { "interface": ["dist/interface/index.d.ts"] }
  },
  "antelopeJs": {
    "implements": ["@your-org/my-module"]
  }
}

implements declares what the module provides. It does not create an npm subpath. The exports entry exposes the compiled contract, and typesVersions lets consumers that use the classic TypeScript node resolution find its declarations. Keep declaration output enabled when you publish this module.

After a consumer installs the built package as a dependency, it can import the interface subpath:

typescript
import { GetUser, CreateUser } from "@your-org/my-module/interface";

Step 4. Connect the interface and the implementation

The module entry point calls ImplementInterface to bind the interface proxies to their implementations. Place the call inside the construct lifecycle function, so every binding exists before the start phase runs.

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

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

ImplementInterface takes two arguments, the interface module and the implementation module. It matches their exported namespaces and binds each proxy to the function of the same name.

Note

You can call ImplementInterface several times to register implementations for different interfaces. Each call binds one interface to one implementation.

Add utility code to an interface

An interface file can hold more than proxies and wrapper functions. Any code that consumers need and that does not depend on one implementation belongs in the interface file, next to the contract.

Common additions are:

  • Decorator factories, which consumers use to annotate their classes or methods.
  • Helper classes, such as query builders, request formatters, and metadata containers.
  • Utility functions, such as data transformations, validation helpers, and constants.

The user-directory contract above needs no decorators. If consumers need annotations, see Decorator Factories for the factory API. A decorator must record metadata that a handler reads. An empty decorator callback adds no validation or authentication.

Note

Code in the interface file must not depend on one implementation. If the logic needs a specific implementation, such as a database driver or an HTTP library, put it in the implementation file.

Move the interface to a standalone package

An embedded interface fits while one module owns both the contract and the implementation. When a second module needs to implement the same contract, move the interface into its own npm package.

When to move it

Move the interface when one of these is true:

  • A second module needs to implement the same contract.
  • Consumers need to depend on the contract without depending on one implementation.
  • The interface becomes part of a shared ecosystem.

How to move it

1. Create a package for the same user contract, for example @your-org/interface-users. Move src/interface/index.ts into that package's src/index.ts. Keep @antelopejs/interface-core as a dependency, compile the package, and publish its JavaScript and declarations:

package.json (interface package fields) json
{
  "name": "@your-org/interface-users",
  "version": "1.0.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": ["dist"],
  "antelopeJs": {}
}

The antelopeJs key tells the core that the package is an interface. The core does not look for a provider of an ordinary npm package that lacks this key. Test the built package locally before you publish it. Do not set antelopeJs.standalone only because the contract has its own package.

2. Update the implementing module. Add the interface package as a dependency and set "implements" in the antelopeJs configuration:

package.json json
{
  "antelopeJs": {
    "implements": ["@your-org/interface-users"]
  }
}

3. Change the ImplementInterface call to import from the package instead of a local path:

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

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

Also change the implementation's User type import from ../interface to @your-org/interface-users.

4. Update the consumers to depend on and import the interface package instead of the module:

typescript
import { CreateUser } from "@your-org/interface-users";

await CreateUser("Alice", "[email protected]");

Consumers now depend only on the interface package. A replacement provider must implement the contract version and the behavior those consumers rely on. Run the contract tests against the replacement before you change the application configuration.

Tip

Keep the contract embedded while it shares the provider's release cycle. Move it out when several providers or independent consumers need a boundary with its own version.

Best practices

  • Keep interface files to the contract: types, proxies, wrapper functions, and shared utilities.
  • Put logic that depends on one implementation in the implementation file, not in the interface.
  • Use InterfaceFunction for request and response calls, where the caller needs a result.
  • Use EventProxy to broadcast events to many listeners.
  • Use RegisteringProxy when modules add items to a collection.
  • Give wrapper functions clear names, because they are the public API that consumers call.
  • Export shared types from the interface file, so implementations and consumers use the same definitions.

See also

  • Interfaces explains interface contracts.
  • Proxies describes the proxy types that modules use to talk to each other.
  • Interface Registry shows how to list interfaces in the registry.