Skip to content

RequestResolver

23 exports Added in v2.0.0 Source

Combinators

around

Added in v2.0.0 Source

A data source aspect that executes requests between two effects, before and after, where the result of before can be used by after.

Signature

declare const around: {
  <A2, R2, X, R3>(
    before: Effect<A2, never, R2>,
    after: (a: A2) => Effect<X, never, R3>,
  ): <A, R>(self: RequestResolver<A, R>) => RequestResolver<A, R2 | R3 | R>;
  <A, R, A2, R2, X, R3>(
    self: RequestResolver<A, R>,
    before: Effect<A2, never, R2>,
    after: (a: A2) => Effect<X, never, R3>,
  ): RequestResolver<A, R | R2 | R3>;
};

A data source aspect that executes requests between two effects, before and after, where the result of before can be used by after.

The before and after effects are provided with the requests being executed.

Signature

declare const aroundRequests: {
  <A, A2, R2, X, R3>(before: (requests: readonly Array<NoInfer<A>>) => Effect<A2, never, R2>, after: (requests: readonly Array<NoInfer<A>>, _: A2) => Effect<X, never, R3>): <R>(self: RequestResolver<A, R>) => RequestResolver<A, R2 | R3 | R>;
  <A, R, A2, R2, X, R3>(self: RequestResolver<A, R>, before: (requests: readonly Array<NoInfer<A>>) => Effect<A2, never, R2>, after: (requests: readonly Array<NoInfer<A>>, _: A2) => Effect<X, never, R3>): RequestResolver<A, R | R2 | R3>;
}

Example

import { Effect, Request, RequestResolver } from "effect"

interface GetUserById extends Request.Request<unknown> {
  readonly id: number
}

const resolver = RequestResolver.fromFunction((request: GetUserById) => ({
  id: request.id,
  name: "John",
}))

RequestResolver.aroundRequests(
  resolver,
  (requests) => Effect.log(`got ${requests.length} requests`),
  (requests, _) => Effect.log(`finised running ${requests.length} requests`),
)

batchN

Added in v2.0.0 Source

Returns a data source that executes at most n requests in parallel.

Signature

declare const batchN: {
  (n: number): <A, R>(self: RequestResolver<A, R>) => RequestResolver<A, R>;
  <A, R>(self: RequestResolver<A, R>, n: number): RequestResolver<A, R>;
};

eitherWith

Added in v2.0.0 Source

Returns a new data source that executes requests of type C using the specified function to transform C requests into requests that either this data source or that data source can execute.

Signature

declare const eitherWith: {
  <A extends Request<any, any>, R2, B extends Request<any, any>, C extends Request<any, any>>(
    that: RequestResolver<B, R2>,
    f: (_: Entry<C>) => Either<Entry<B>, Entry<A>>,
  ): <R>(self: RequestResolver<A, R>) => RequestResolver<C, R2 | R>;
  <R, A extends Request<any, any>, R2, B extends Request<any, any>, C extends Request<any, any>>(
    self: RequestResolver<A, R>,
    that: RequestResolver<B, R2>,
    f: (_: Entry<C>) => Either<Entry<B>, Entry<A>>,
  ): RequestResolver<C, R | R2>;
};

locally

Added in v2.0.0 Source

Returns a new data source with a localized FiberRef

Signature

declare const locally: {
  <A>(
    self: FiberRef<A>,
    value: A,
  ): <R, B extends Request<any, any>>(use: RequestResolver<B, R>) => RequestResolver<B, R>;
  <R, B extends Request<any, any>, A>(
    use: RequestResolver<B, R>,
    self: FiberRef<A>,
    value: A,
  ): RequestResolver<B, R>;
};

race

Added in v2.0.0 Source

Returns a new data source that executes requests by sending them to this data source and that data source, returning the results from the first data source to complete and safely interrupting the loser.

Signature

declare const race: {
  <A2 extends Request<any, any>, R2>(
    that: RequestResolver<A2, R2>,
  ): <A extends Request<any, any>, R>(
    self: RequestResolver<A, R>,
  ) => RequestResolver<A2 | A, R2 | R>;
  <A extends Request<any, any>, R, A2 extends Request<any, any>, R2>(
    self: RequestResolver<A, R>,
    that: RequestResolver<A2, R2>,
  ): RequestResolver<A | A2, R | R2>;
};

Constructors

fromEffect

Added in v2.0.0 Source

Constructs a data source from an effectual function.

Signature

declare const fromEffect: <R, A extends Request.Request<any, any>>(
  f: (a: A) => Effect.Effect<Request.Request.Success<A>, Request.Request.Error<A>, R>,
) => RequestResolver<A, R>;

Constructs a data source from a list of tags paired to functions, that takes a list of requests and returns a list of results of the same size. Each item in the result list must correspond to the item at the same index in the request list.

Signature

declare const fromEffectTagged: <
  A extends Request.Request<any, any> & {
    readonly _tag: string;
  },
>() => <
  Fns extends {
    [Tag in A["_tag"]]: [
      Extract<
        A,
        {
          readonly _tag: Tag;
        }
      >,
    ] extends [infer Req]
      ? Req extends Request.Request<infer ReqA, infer ReqE>
        ? (requests: Array<Req>) => Effect.Effect<Iterable<ReqA>, ReqE, any>
        : never
      : never;
  },
>(
  fns: Fns,
) => RequestResolver<
  A,
  ReturnType<Fns[keyof Fns]> extends Effect.Effect<infer _A, infer _E, infer R> ? R : never
>;

fromFunction

Added in v2.0.0 Source

Constructs a data source from a pure function.

Signature

declare const fromFunction: <A extends Request.Request<any>>(
  f: (request: A) => Request.Request.Success<A>,
) => RequestResolver<A>;

Constructs a data source from a pure function that takes a list of requests and returns a list of results of the same size. Each item in the result list must correspond to the item at the same index in the request list.

Signature

declare const fromFunctionBatched: <A extends Request.Request<any>>(
  f: (chunk: NonEmptyArray<A>) => Iterable<Request.Request.Success<A>>,
) => RequestResolver<A>;

make

Added in v2.0.0 Source

Constructs a data source with the specified identifier and method to run requests.

Signature

declare const make: <A, R>(
  runAll: (requests: Array<Array<A>>) => Effect.Effect<void, never, R>,
) => RequestResolver<A, R>;

makeBatched

Added in v2.0.0 Source

Constructs a data source from a function taking a collection of requests.

Signature

declare const makeBatched: <A extends Request.Request<any, any>, R>(
  run: (requests: NonEmptyArray<A>) => Effect.Effect<void, never, R>,
) => RequestResolver<A, R>;

Constructs a data source with the specified identifier and method to run requests.

Signature

declare const makeWithEntry: <A, R>(
  runAll: (requests: Array<Array<Request.Entry<A>>>) => Effect.Effect<void, never, R>,
) => RequestResolver<A, R>;

never

Added in v2.0.0 Source

A data source that never executes requests.

Signature

declare const never: RequestResolver<never>;

Context

Provides this data source with part of its required context.

Signature

declare const mapInputContext: {
  <R0, R>(
    f: (context: Context<R0>) => Context<R>,
  ): <A extends Request<any, any>>(self: RequestResolver<A, R>) => RequestResolver<A, R0>;
  <R, A extends Request<any, any>, R0>(
    self: RequestResolver<A, R>,
    f: (context: Context<R0>) => Context<R>,
  ): RequestResolver<A, R0>;
};

Provides this data source with its required context.

Signature

declare const provideContext: {
  <R>(
    context: Context<R>,
  ): <A extends Request<any, any>>(self: RequestResolver<A, R>) => RequestResolver<A>;
  <R, A extends Request<any, any>>(
    self: RequestResolver<A, R>,
    context: Context<R>,
  ): RequestResolver<A>;
};

Models

RequestResolver interface

Added in v2.0.0 Source

The RequestResolver<A, R> interface requires an environment R and handles the execution of requests of type A.

Implementations must provide a runAll method, which processes a collection of requests and produces an effect that fulfills these requests. Requests are organized into a Array<Array<A>>, where the outer Array groups requests into batches that are executed sequentially, and each inner Array contains requests that can be executed in parallel. This structure allows implementations to analyze all incoming requests collectively and optimize query execution accordingly.

Implementations are typically specialized for a subtype of Request<A, E>. However, they are not strictly limited to these subtypes as long as they can map any given request type to Request<A, E>. Implementations should inspect the collection of requests to identify the needed information and execute the corresponding queries. It is imperative that implementations resolve all the requests they receive. Failing to do so will lead to a QueryFailure error during query execution.

Signature

interface RequestResolver<in A, out R = never> extends Variance<A, R>, Equal, Pipeable {
  identified(...identifiers: Array<unknown>): RequestResolver<A, R>;
  runAll(requests: Array<Array<Entry<A>>>): Effect<void, never, R>;
}

Other

Refinements

Returns true if the specified value is a RequestResolver, false otherwise.

Signature

declare const isRequestResolver: (u: unknown) => u is RequestResolver<unknown, unknown>;

Symbols

Signature

declare const RequestResolverTypeId: unique symbol;

Signature

type RequestResolverTypeId = typeof RequestResolverTypeId;

Utils

Signature

declare function contextFromEffect<R, A extends Request<any, any>>(
  self: RequestResolver<A, R>,
): Effect<RequestResolver<A, never>, never, R>;

Signature

declare function contextFromServices<Services extends Array<Tag<any, any>>>(
  ...services: Services
): <R, A extends Request<any, any>>(
  self: RequestResolver<A, R>,
) => Effect<
  RequestResolver<A, Exclude<R, { [k in string | number | symbol]: Context<Services[k]> }[number]>>,
  never,
  { [k in string | number | symbol]: Context<Services[k]> }[number]
>;