/** * @since 1.0.0 */ import * as Context from "@effect/data/Context" import type * as Either from "@effect/data/Either" import type * as Equal from "@effect/data/Equal" import type { Pipeable } from "@effect/data/Pipeable" import * as Effect from "@effect/io/Effect" import type { FiberRef } from "@effect/io/FiberRef" import * as core from "@effect/io/internal/core" import * as internal from "@effect/io/internal/dataSource" import type * as Request from "@effect/io/Request" /** * @since 1.0.0 * @category symbols */ export const RequestResolverTypeId: unique symbol = core.RequestResolverTypeId /** * @since 1.0.0 * @category symbols */ export type RequestResolverTypeId = typeof RequestResolverTypeId /** * A `RequestResolver` requires an environment `R` and is capable of executing * requests of type `A`. * * Data sources must implement the method `runAll` which takes a collection of * requests and returns an effect with a `RequestCompletionMap` containing a * mapping from requests to results. The type of the collection of requests is * a `Chunk>`. The outer `Chunk` represents batches of requests that * must be performed sequentially. The inner `Chunk` represents a batch of * requests that can be performed in parallel. This allows data sources to * introspect on all the requests being executed and optimize the query. * * Data sources will typically be parameterized on a subtype of `Request`, * though that is not strictly necessarily as long as the data source can map * the request type to a `Request`. Data sources can then pattern match on * the collection of requests to determine the information requested, execute * the query, and place the results into the `RequestCompletionMap` using * `RequestCompletionMap.empty` and `RequestCompletionMap.insert`. Data * sources must provide results for all requests received. Failure to do so * will cause a query to die with a `QueryFailure` when run. * * @since 1.0.0 * @category models */ export interface RequestResolver extends Equal.Equal, Pipeable { /** * Execute a collection of requests. The outer `Chunk` represents batches * of requests that must be performed sequentially. The inner `Chunk` * represents a batch of requests that can be performed in parallel. */ runAll(requests: Array>>): Effect.Effect /** * Identify the data source using the specific identifier */ identified(...identifiers: Array): RequestResolver } /** * @since 1.0.0 */ export declare namespace RequestResolver { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [RequestResolverTypeId]: { readonly _R: (_: never) => R readonly _A: (_: never) => A } } } /** * @since 1.0.0 * @category utils */ export const contextFromEffect = >(self: RequestResolver) => Effect.contextWith((_: Context.Context) => provideContext(self, _)) /** * @since 1.0.0 * @category utils */ export const contextFromServices = >>(...services: Services) => >( self: RequestResolver ): Effect.Effect< { [k in keyof Services]: Effect.Effect.Context }[number], never, RequestResolver }[number]>> > => Effect.contextWith((_) => provideContext(self as any, Context.pick(...services)(_ as any))) /** * Returns `true` if the specified value is a `RequestResolver`, `false` otherwise. * * @since 1.0.0 * @category refinements */ export const isRequestResolver: (u: unknown) => u is RequestResolver = core.isRequestResolver /** * Constructs a data source with the specified identifier and method to run * requests. * * @since 1.0.0 * @category constructors */ export const make: ( runAll: (requests: Array>) => Effect.Effect ) => RequestResolver = internal.make /** * Constructs a data source with the specified identifier and method to run * requests. * * @since 1.0.0 * @category constructors */ export const makeWithEntry: ( runAll: (requests: Array>>) => Effect.Effect ) => RequestResolver = internal.makeWithEntry /** * Constructs a data source from a function taking a collection of requests * and returning a `RequestCompletionMap`. * * @since 1.0.0 * @category constructors */ export const makeBatched: >( run: (requests: Array) => Effect.Effect ) => RequestResolver = internal.makeBatched /** * A data source aspect that executes requests between two effects, `before` * and `after`, where the result of `before` can be used by `after`. * * @since 1.0.0 * @category combinators */ export const around: { ( before: Effect.Effect, after: (a: A2) => Effect.Effect ): (self: RequestResolver) => RequestResolver ( self: RequestResolver, before: Effect.Effect, after: (a: A2) => Effect.Effect ): RequestResolver } = internal.around /** * Returns a data source that executes at most `n` requests in parallel. * * @since 1.0.0 * @category combinators */ export const batchN: { (n: number): (self: RequestResolver) => RequestResolver (self: RequestResolver, n: number): RequestResolver } = internal.batchN /** * Provides this data source with part of its required context. * * @since 1.0.0 * @category context */ export const mapInputContext: { ( f: (context: Context.Context) => Context.Context ): >(self: RequestResolver) => RequestResolver , R0>( self: RequestResolver, f: (context: Context.Context) => Context.Context ): RequestResolver } = internal.mapInputContext /** * 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. * * @since 1.0.0 * @category combinators */ export const eitherWith: { , R2, B extends Request.Request, C extends Request.Request>( that: RequestResolver, f: (_: Request.Entry) => Either.Either, Request.Entry> ): (self: RequestResolver) => RequestResolver < R, A extends Request.Request, R2, B extends Request.Request, C extends Request.Request >( self: RequestResolver, that: RequestResolver, f: (_: Request.Entry) => Either.Either, Request.Entry> ): RequestResolver } = internal.eitherWith /** * Constructs a data source from a pure function. * * @since 1.0.0 * @category constructors */ export const fromFunction: >( f: (request: A) => Request.Request.Success ) => RequestResolver = internal.fromFunction /** * 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. * * @since 1.0.0 * @category constructors */ export const fromFunctionBatched: >( f: (chunk: Array) => Array> ) => RequestResolver = internal.fromFunctionBatched /** * Constructs a data source from an effectual function. * * @since 1.0.0 * @category constructors */ export const fromFunctionEffect: >( f: (a: A) => Effect.Effect, Request.Request.Success> ) => RequestResolver = internal.fromFunctionEffect /** * A data source that never executes requests. * * @since 1.0.0 * @category constructors */ export const never: RequestResolver = internal.never /** * Provides this data source with its required context. * * @since 1.0.0 * @category context */ export const provideContext: { ( context: Context.Context ): >(self: RequestResolver) => RequestResolver >( self: RequestResolver, context: Context.Context ): RequestResolver } = internal.provideContext /** * 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. * * @since 1.0.0 * @category combinators */ export const race: { >( that: RequestResolver ): >(self: RequestResolver) => RequestResolver , R2, A2 extends Request.Request>( self: RequestResolver, that: RequestResolver ): RequestResolver } = internal.race /** * Returns a new data source with a localized FiberRef * * @since 1.0.0 * @category combinators */ export const locally: { ( self: FiberRef, value: A ): >(use: RequestResolver) => RequestResolver , A>( use: RequestResolver, self: FiberRef, value: A ): RequestResolver } = core.resolverLocally