Effection Logo

interface Api

thefrontside/effection

interface Api<A>

A set of methods and values that can be decorated on a per-scope basis. Apis are ideal for situations that require context sensitivity such as dependency injection, test mocking, and instrumentation.

Examples

Example 1

import { type Operation } from "effection";
import { createApi } from "effection/experimental";

interface DbApi {
  query(sql: string): Operation<{ id: number; title: string }[]>;
}

let DatabaseApi = createApi<DbApi>("database", {
  *query(sql) {
    console.log("running", sql);
    return [];
  },
});

// scope-local decoration: logging, metrics, retries, mocks, etc.
yield* DatabaseApi.around({
  *query(args, next) {
    let [sql] = args;
    console.log("SQL:", sql);
    return yield* next(...args);
  },
});

let rows = yield* DatabaseApi.operations.query("select * from posts");
console.log(rows.length);

Type Parameters

A core shape of the Api

Properties

operations: [K in keyof A]: A[K] extends Operation<unknown> ? A[K] : A[K] extends (...args: ) => ? TReturn extends Operation<unknown> ? A[K] : (...args: TArgs) => Operation<TReturn> : Operation<A[K]>

Every member of A "lifted" into an operation that invokes that member on the current {Scope}

around: (middlewares: Partial<Around<A>>, optionsoptional: {}) => Operation<void>

Enhance an Api within this scope by surrounding it with middleware.

invoke: <K in keyof A>(scope: Scope, key: K, args: A[K] extends (...args: any) => unknown ? Parameters<A[K]> : []) => A[K] extends (...args: any) => unknown ? ReturnType<A[K]> : A[K]

Call an API as it exists on scope.

See