Effection Logo

Experimental

This API is exported from effection/experimental and may change or be removed in a future release.

function createApi

thefrontside/effection

function createApi<A extends {}>(name: string, core: A): Api<A>

Create an Api whose implementation can be decorated within a scope.

The core implementation defines the API's default behavior. Use Api.around or Scope.around to install middleware that changes that behavior for a scope and its descendants.

Examples

Example 1

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

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

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

export let { query } = Database.operations;

Type Parameters

A extends {} the shape of the API's core implementation

Parameters

name: string

the API identifier used in debug messages

core: A

the default implementation for every API member

Return Type

Api<A>

an API that can be invoked and decorated per scope