Skip to content

Taxum / @taxum/core / layer / layerFn

Function: layerFn()

layerFn<Out, In>(f): Layer<Out, In>

Defined in: layer/layer-fn.ts:40

Returns a new LayerFn that implements Layer by calling a given function.

Type Parameters

Out

Out extends AnyService = UnknownService

In

In extends AnyService = UnknownService

Parameters

f

LayerFnClosure<Out, In>

Returns

Layer<Out, In>

Example

ts
import { layerFn } from "@taxum/core/layer";
import { serviceFn, type Service } from "@taxum/core/service";

// A middleware that logs requests before forwarding them to another service.
class LogService implements Service<string, string> {
    public constructor(private readonly inner: Service) {}

    public async invoke(req: string): Promise<string> {
        console.log("Request received", req);
        return await this.inner.invoke(req);
    }
}

// A `Layer` that wraps services in `LogService`.
const logLayer = layerFn(
    (inner: Service<string, string>) => new LogService(inner)
);

// An example service.
const service = serviceFn((req: string) => req.toUpperCase());

// Wrap our service in a `LogService` so requests are logged.
const wrappedService = logLayer.layer(service);