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

Core utilities

Reference for the AntelopeJS core functions that declare and implement interfaces, inspect modules and connections, and build typed decorators.

To write a new contract, you need InterfaceFunction and ImplementInterface. The other functions on this page serve module tooling, reflection, and custom decorator systems. An application module does not need them. The examples that use User and UserInput assume these types come from your own contract.

InterfaceFunction

InterfaceFunction creates a callable function backed by an AsyncProxy. It is the main way to declare a function in an interface definition. Consumers call the returned function like any other async function, and the proxy routes the call to the implementation.

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

export const GetUser = InterfaceFunction<(id: string) => Promise<User>>();
export const CreateUser =
  InterfaceFunction<(data: UserInput) => Promise<User>>();

Consumers import these functions and call them directly. At run time, the proxy routes each call to the provider.

typescript
import { GetUser } from "@antelopejs/interface-example";

const user = await GetUser("user-123");

The type parameter defines the function signature. The returned function carries that type, so consumers get autocompletion and type checks at the call site.

ImplementInterface

ImplementInterface connects an interface declaration to its implementation. Call it in the module's construct callback to attach your implementation functions to the matching interface proxies.

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

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

The first argument is the interface module, which holds the InterfaceFunction declarations. The second argument is the implementation module, which exports functions with the same names. ImplementInterface matches each declaration to its implementation by name and attaches the implementation to the proxy. It throws a TypeError when a declared function has no implementation.

For a standalone interface package, import the package name 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"),
  );
}

GetResponsibleModule

GetResponsibleModule returns the ID of the module whose work is running. It reads the ID from the async module context that the core enters for every module lifecycle step. Outside any module context, for example in the core itself, it returns undefined.

  • A provider's callback runs in the provider's context.
  • An event handler runs in the context it was registered in.
  • While an interface package's own code runs, it returns the interface package name.

The core uses this attribution for proxy cleanup and logging. Use it for diagnostics or ownership tracking. It is not an authorization boundary, because all modules share one process.

typescript
import { GetResponsibleModule } from "@antelopejs/interface-core";

const moduleId = GetResponsibleModule();

GetMetadata

GetMetadata returns the metadata that a metadata class attaches to a target object. Use it for reflection, when a system reads at run time the configuration that decorators applied.

typescript
import { GetMetadata } from "@antelopejs/interface-core";

class MyMeta {
  static key = Symbol("MyMeta");
  constructor(target: object) {
    /* initialize from target */
  }
}

declare const myInstance: object;

const metadata = GetMetadata(myInstance, MyMeta);

The first argument is the target object. The second argument is a metadata class with a static key symbol. The optional third argument, inherit, controls inheritance from the prototype chain and defaults to true. The return value is the instance of the metadata class for the target. When the target has none yet, GetMetadata creates one.

GetInterfaceInstances and GetInterfaceInstance

These functions return the connections to the implementations of an interface, as the module whose work is running sees them. Use them when several modules implement one interface and you need to list or pick its connections.

typescript
import {
  GetInterfaceInstances,
  GetInterfaceInstance,
} from "@antelopejs/interface-core";

// All connections for an interface
const allConnections = GetInterfaceInstances("@antelopejs/interface-email");

// One connection, by interface ID and connection ID
const connection = GetInterfaceInstance(
  "@antelopejs/interface-email",
  "brevo-module",
);

GetInterfaceInstances takes an interface ID and returns every connection to an implementation of that interface. Outside a module context, it returns an empty array. GetInterfaceInstance takes an interface ID and a connection ID and returns the matching connection. It returns undefined when no connection matches, and outside a module context.

A connection's path is what to require() to get the copy of the interface bound to that connection's provider. For the connection that a plain import reaches, path is the package name and selected is true. For the others, path is a connection path. In a module that implements the interface, the plain import reaches the module's own implementation, so the selected connection is the module's own. A module that disables its export of the interface has no selected connection. Objects built from that copy stay bound to that provider wherever you use them.

Decorator factories

AntelopeJS has factory functions that create typed decorators for interface definitions. Each factory takes a handler function whose arguments come in two groups. The first arguments are the decorator target arguments: the class, the property key, the descriptor, or the parameter index, depending on the decorator type. The remaining arguments are the factory parameters that users pass when they apply the decorator.

Basic factories

Each basic factory creates a decorator for one target type. The type parameter T lists the factory parameter types as a tuple.

typescript
import {
  MakeClassDecorator,
  MakePropertyDecorator,
  MakeMethodDecorator,
  MakeParameterDecorator,
} from "@antelopejs/interface-core/decorators";

// Class decorator: the handler receives (target, ...factoryArgs)
const MyDecorator = MakeClassDecorator<[name: string]>((target, name) => {
  // target is the class constructor, name is the factory parameter
});

// Property decorator: the handler receives (target, key, ...factoryArgs)
const MyPropDecorator = MakePropertyDecorator<[required: boolean]>(
  (target, key, required) => {
    // target is the prototype, key is the property name
  },
);

// Method decorator: the handler receives (target, key, descriptor, ...factoryArgs)
const MyMethodDecorator = MakeMethodDecorator<[route: string]>(
  (target, key, descriptor, route) => {
    // target is the prototype, key is the method name, descriptor is the property descriptor
  },
);

// Parameter decorator: the handler receives (target, key, index, ...factoryArgs)
const MyParamDecorator = MakeParameterDecorator<[validate: boolean]>(
  (target, key, index, validate) => {
    // target is the prototype, key is the method name, index is the parameter position
  },
);

The handler receives the standard decorator arguments first, then the factory parameters. When users apply the decorator, they pass only the factory parameters:

typescript
@MyDecorator("MyClassName")
class Example {
  @MyPropDecorator(true)
  name!: string;

  @MyMethodDecorator("/api/users")
  getUsers(@MyParamDecorator(true) id: string) {}
}

Combined factories

A combined factory creates one decorator that applies to more than one target type. Use one when a single decorator must work on several kinds of declaration. The module exports a combined factory for each combination of class, property, method, and parameter targets, such as MakePropertyAndClassDecorator, MakeMethodAndClassDecorator, and MakeParameterAndMethodAndPropertyAndClassDecorator. The example shows two of them.

typescript
import {
  MakeMethodAndClassDecorator,
  MakeParameterAndMethodDecorator,
} from "@antelopejs/interface-core/decorators";

// Applies to methods and classes
const Transactional = MakeMethodAndClassDecorator<[isolation?: string]>(
  (target, key, descriptor, isolation) => {
    // key and descriptor are undefined when applied to a class
  },
);

// Applies to parameters and methods
const Validate = MakeParameterAndMethodDecorator<[schema: string]>(
  (target, key, indexOrDescriptor, schema) => {
    // indexOrDescriptor is a number for parameters, a PropertyDescriptor for methods
  },
);
Tip

Interfaces that ask consumers to annotate their classes use decorator factories most. For example, an HTTP interface can provide a Route method decorator and a Controller class decorator that consumers apply to their handler classes.