Skip to main content Link Search Menu Expand Document (external link)

Request overview

Added in v2.0.0


Table of contents


constructors

Class

Provides a constructor for a Request Class.

Signature

export declare const Class: new <Success, Error, A extends Record<string, any>>(
  args: Types.Equals<Omit<A, keyof Request<unknown, unknown>>, {}> extends true
    ? void
    : { readonly [P in keyof A as P extends keyof Request<unknown, unknown> ? never : P]: A[P] }
) => Request<Success, Error> & Readonly<A>

Example

import { Request } from "effect"

type Success = string
type Error = never

class MyRequest extends Request.Class<
  Success,
  Error,
  {
    readonly id: string
  }
> {}

Added in v2.0.0

TaggedClass

Provides a Tagged constructor for a Request Class.

Signature

export declare const TaggedClass: <Tag extends string>(
  tag: Tag
) => new <Success, Error, A extends Record<string, any>>(
  args: Types.Equals<Omit<A, keyof Request<unknown, unknown>>, {}> extends true
    ? void
    : { readonly [P in keyof A as P extends "_tag" | keyof Request<unknown, unknown> ? never : P]: A[P] }
) => Request<Success, Error> & Readonly<A> & { readonly _tag: Tag }

Example

import { Request } from "effect"

type Success = string
type Error = never

class MyRequest extends Request.TaggedClass("MyRequest")<
  Success,
  Error,
  {
    readonly name: string
  }
> {}

Added in v2.0.0

makeEntry

Signature

export declare const makeEntry: <A extends Request<any, any>>(options: {
  readonly request: A
  readonly result: Deferred<Request.Success<A>, Request.Error<A>>
  readonly listeners: Listeners
  readonly ownerId: FiberId
  readonly state: { completed: boolean }
}) => Entry<A>

Added in v2.0.0

of

Constructs a new Request.

Signature

export declare const of: <R extends Request<any, any>>() => Request.Constructor<R>

Added in v2.0.0

tagged

Constructs a new Request.

Signature

export declare const tagged: <R extends Request<any, any> & { _tag: string }>(
  tag: R["_tag"]
) => Request.Constructor<R, "_tag">

Added in v2.0.0

guards

isEntry

Signature

export declare const isEntry: (u: unknown) => u is Entry<unknown>

Added in v2.0.0

models

Cache (interface)

Signature

export interface Cache
  extends _Cache.ConsumerCache<
    Request<any, any>,
    {
      listeners: Listeners
      handle: Deferred<unknown, unknown>
    }
  > {}

Added in v2.0.0

Entry (interface)

A Entry<A> keeps track of a request of type A along with a Ref containing the result of the request, existentially hiding the result type. This is used internally by the library to support data sources that return different result types for different requests while guaranteeing that results will be of the type requested.

Signature

export interface Entry<out R> extends Entry.Variance<R> {
  readonly request: R
  readonly result: Deferred<
    [R] extends [Request<infer _A, infer _E>] ? _A : never,
    [R] extends [Request<infer _A, infer _E>] ? _E : never
  >
  readonly listeners: Listeners
  readonly ownerId: FiberId
  readonly state: {
    completed: boolean
  }
}

Added in v2.0.0

Entry (namespace)

Added in v2.0.0

Variance (interface)

Signature

export interface Variance<out R> {
  readonly [EntryTypeId]: {
    readonly _R: Types.Covariant<R>
  }
}

Added in v2.0.0

Listeners (interface)

Signature

export interface Listeners {
  readonly count: number
  readonly observers: Set<(count: number) => void>
  interrupted: boolean
  addObserver(f: (count: number) => void): void
  removeObserver(f: (count: number) => void): void
  increment(): void
  decrement(): void
}

Added in v2.0.0

Request (interface)

A Request<A, E> is a request from a data source for a value of type A that may fail with an E.

Signature

export interface Request<out A, out E = never> extends Request.Variance<A, E> {}

Added in v2.0.0

makeCache

Signature

export declare const makeCache: (options: {
  readonly capacity: number
  readonly timeToLive: DurationInput
}) => Effect.Effect<Cache>

Added in v2.0.0

refinements

isRequest

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

Signature

export declare const isRequest: (u: unknown) => u is Request<unknown, unknown>

Added in v2.0.0

request completion

complete

Complete a Request with the specified result.

Signature

export declare const complete: {
  <A extends Request<any, any>>(result: Request.Result<A>): (self: A) => Effect.Effect<void>
  <A extends Request<any, any>>(self: A, result: Request.Result<A>): Effect.Effect<void>
}

Added in v2.0.0

completeEffect

Complete a Request with the specified effectful computation, failing the request with the error from the effect workflow if it fails, and completing the request with the value of the effect workflow if it succeeds.

Signature

export declare const completeEffect: {
  <A extends Request<any, any>, R>(
    effect: Effect.Effect<Request.Success<A>, Request.Error<A>, R>
  ): (self: A) => Effect.Effect<void, never, R>
  <A extends Request<any, any>, R>(
    self: A,
    effect: Effect.Effect<Request.Success<A>, Request.Error<A>, R>
  ): Effect.Effect<void, never, R>
}

Added in v2.0.0

fail

Complete a Request with the specified error.

Signature

export declare const fail: {
  <A extends Request<any, any>>(error: Request.Error<A>): (self: A) => Effect.Effect<void>
  <A extends Request<any, any>>(self: A, error: Request.Error<A>): Effect.Effect<void>
}

Added in v2.0.0

failCause

Complete a Request with the specified cause.

Signature

export declare const failCause: {
  <A extends Request<any, any>>(cause: Cause<Request.Error<A>>): (self: A) => Effect.Effect<void>
  <A extends Request<any, any>>(self: A, cause: Cause<Request.Error<A>>): Effect.Effect<void>
}

Added in v2.0.0

interruptWhenPossible

Interrupts the child effect when requests are no longer needed

Signature

export declare const interruptWhenPossible: {
  (all: Iterable<Request<any, any>>): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>
  <A, E, R>(self: Effect.Effect<A, E, R>, all: Iterable<Request<any, any>>): Effect.Effect<void, E, R>
}

Added in v2.0.0

succeed

Complete a Request with the specified value.

Signature

export declare const succeed: {
  <A extends Request<any, any>>(value: Request.Success<A>): (self: A) => Effect.Effect<void>
  <A extends Request<any, any>>(self: A, value: Request.Success<A>): Effect.Effect<void>
}

Added in v2.0.0

symbols

EntryTypeId

Signature

export declare const EntryTypeId: typeof EntryTypeId

Added in v2.0.0

EntryTypeId (type alias)

Signature

export type EntryTypeId = typeof EntryTypeId

Added in v2.0.0

RequestTypeId

Signature

export declare const RequestTypeId: typeof RequestTypeId

Added in v2.0.0

RequestTypeId (type alias)

Signature

export type RequestTypeId = typeof RequestTypeId

Added in v2.0.0

utils

Request (namespace)

Added in v2.0.0

Constructor (interface)

Signature

export interface Constructor<R extends Request<any, any>, T extends keyof R = never> {
  (args: Omit<R, T | keyof Request.Variance<Request.Success<R>, Request.Error<R>>>): R
}

Added in v2.0.0

Variance (interface)

Signature

export interface Variance<out A, out E> {
  readonly [RequestTypeId]: {
    readonly _A: Types.Covariant<A>
    readonly _E: Types.Covariant<E>
  }
}

Added in v2.0.0

Error (type alias)

A utility type to extract the error type from a Request.

Signature

export type Error<T extends Request<any, any>> = [T] extends [Request<infer _A, infer _E>] ? _E : never

Added in v2.0.0

OptionalResult (type alias)

A utility type to extract the optional result type from a Request.

Signature

export type OptionalResult<T extends Request<any, any>> =
  T extends Request<infer A, infer E> ? Exit.Exit<Option.Option<A>, E> : never

Added in v2.0.0

Result (type alias)

A utility type to extract the result type from a Request.

Signature

export type Result<T extends Request<any, any>> = T extends Request<infer A, infer E> ? Exit.Exit<A, E> : never

Added in v2.0.0

Success (type alias)

A utility type to extract the value type from a Request.

Signature

export type Success<T extends Request<any, any>> = [T] extends [Request<infer _A, infer _E>] ? _A : never

Added in v2.0.0