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

Router overview

Added in v1.0.0


Table of contents


combinators

catchAll

Signature

export declare const catchAll: {
  <E, R2, E2>(
    f: (e: E) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R>(self: Router<R, E>) => Router<R | Exclude<R2, any>, E2>
  <R, E, R2, E2>(
    self: Router<R, E>,
    f: (e: E) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R2, any>, E2>
}

Added in v1.0.0

catchAllCause

Signature

export declare const catchAllCause: {
  <E, R2, E2>(
    f: (
      e: Cause.Cause<E>
    ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R>(self: Router<R, E>) => Router<R | Exclude<R2, any>, E2>
  <R, E, R2, E2>(
    self: Router<R, E>,
    f: (
      e: Cause.Cause<E>
    ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R2, any>, E2>
}

Added in v1.0.0

catchTag

Signature

export declare const catchTag: {
  <K extends E extends { _tag: string } ? E["_tag"] : never, E, R1, E1>(
    k: K,
    f: (
      e: Extract<E, { _tag: K }>
    ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | Exclude<E, { _tag: K }>>
  <R, E, K extends E extends { _tag: string } ? E["_tag"] : never, R1, E1>(
    self: Router<R, E>,
    k: K,
    f: (
      e: Extract<E, { _tag: K }>
    ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E1 | Exclude<E, { _tag: K }>>
}

Added in v1.0.0

catchTags

Signature

export declare const catchTags: {
  <
    E,
    Cases extends E extends { _tag: string }
      ? {
          [K in E["_tag"]]+?:
            | ((
                error: Extract<E, { _tag: K }>
              ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>)
            | undefined
        }
      : {}
  >(
    cases: Cases
  ): <R>(
    self: Router<R, E>
  ) => Router<
    | R
    | Exclude<
        {
          [K in keyof Cases]: Cases[K] extends (...args: Array<any>) => Effect.Effect<any, any, infer R> ? R : never
        }[keyof Cases],
        any
      >,
    | Exclude<E, { _tag: keyof Cases }>
    | {
        [K in keyof Cases]: Cases[K] extends (...args: Array<any>) => Effect.Effect<any, infer E, any> ? E : never
      }[keyof Cases]
  >
  <
    R,
    E,
    Cases extends E extends { _tag: string }
      ? {
          [K in E["_tag"]]+?:
            | ((
                error: Extract<E, { _tag: K }>
              ) => Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>)
            | undefined
        }
      : {}
  >(
    self: Router<R, E>,
    cases: Cases
  ): Router<
    | R
    | Exclude<
        {
          [K in keyof Cases]: Cases[K] extends (...args: Array<any>) => Effect.Effect<any, any, infer R> ? R : never
        }[keyof Cases],
        any
      >,
    | Exclude<E, { _tag: keyof Cases }>
    | {
        [K in keyof Cases]: Cases[K] extends (...args: Array<any>) => Effect.Effect<any, infer E, any> ? E : never
      }[keyof Cases]
  >
}

Added in v1.0.0

concat

Signature

export declare const concat: {
  <R1, E1>(that: Router<R1, E1>): <R, E>(self: Router<R, E>) => Router<R1 | R, E1 | E>
  <R, E, R1, E1>(self: Router<R, E>, that: Router<R1, E1>): Router<R | R1, E | E1>
}

Added in v1.0.0

prefixAll

Signature

export declare const prefixAll: {
  (prefix: PathInput): <R, E>(self: Router<R, E>) => Router<R, E>
  <R, E>(self: Router<R, E>, prefix: PathInput): Router<R, E>
}

Added in v1.0.0

provideService

Signature

export declare const provideService: {
  <T extends Context.Tag<any, any>>(
    tag: T,
    service: Context.Tag.Service<T>
  ): <R, E>(self: Router<R, E>) => Router<Exclude<R, Context.Tag.Identifier<T>>, E>
  <R, E, T extends Context.Tag<any, any>>(
    self: Router<R, E>,
    tag: T,
    service: Context.Tag.Service<T>
  ): Router<Exclude<R, Context.Tag.Identifier<T>>, E>
}

Added in v1.0.0

provideServiceEffect

Signature

export declare const provideServiceEffect: {
  <T extends Context.Tag<any, any>, R1, E1>(
    tag: T,
    effect: Effect.Effect<Context.Tag.Service<T>, E1, R1>
  ): <R, E>(
    self: Router<R, E>
  ) => Router<Exclude<R, Context.Tag.Identifier<T>> | Exclude<Exclude<R1, any>, Context.Tag.Identifier<T>>, E1 | E>
  <R, E, T extends Context.Tag<any, any>, R1, E1>(
    self: Router<R, E>,
    tag: T,
    effect: Effect.Effect<Context.Tag.Service<T>, E1, R1>
  ): Router<Exclude<R, Context.Tag.Identifier<T>> | Exclude<Exclude<R1, any>, Context.Tag.Identifier<T>>, E | E1>
}

Added in v1.0.0

use

Signature

export declare const use: {
  <R, E, R1, E1>(
    f: (
      self: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
    ) => App.Default<R1, E1>
  ): (self: Router<R, E>) => Router<Exclude<R1, any>, E1>
  <R, E, R1, E1>(
    self: Router<R, E>,
    f: (
      self: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
    ) => App.Default<R1, E1>
  ): Router<Exclude<R1, any>, E1>
}

Added in v1.0.0

constructors

empty

Signature

export declare const empty: Router<never, never>

Added in v1.0.0

fromIterable

Signature

export declare const fromIterable: <R extends Route<any, any>>(
  routes: Iterable<R>
) => Router<R extends Route<infer Env, infer _> ? Env : never, R extends Route<infer _, infer E> ? E : never>

Added in v1.0.0

makeRoute

Signature

export declare const makeRoute: <R, E>(
  method: Method.Method,
  path: PathInput,
  handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>,
  prefix?: any
) => Route<Exclude<R, any>, E>

Added in v1.0.0

models

PathInput (type alias)

Signature

export type PathInput = `/${string}` | "*"

Added in v1.0.0

Route (interface)

Signature

export interface Route<R, E> extends Inspectable {
  readonly [RouteTypeId]: RouteTypeId
  readonly method: Method.Method | "*"
  readonly path: PathInput
  readonly handler: Route.Handler<R, E>
  readonly prefix: Option.Option<string>
}

Added in v1.0.0

RouteContext (interface)

Signature

export interface RouteContext {
  readonly [RouteContextTypeId]: RouteContextTypeId
  readonly params: Readonly<Record<string, string | undefined>>
  readonly route: Route<unknown, unknown>
  readonly searchParams: Readonly<Record<string, string>>
}

Added in v1.0.0

Router (interface)

Signature

export interface Router<R, E> extends App.Default<Exclude<R, RouteContext>, E | Error.RouteNotFound>, Inspectable {
  readonly [TypeId]: TypeId
  readonly routes: Chunk.Chunk<Route<R, E>>
  readonly mounts: Chunk.Chunk<
    readonly [
      prefix: string,
      httpApp: App.Default<R, E>,
      options?: { readonly includePrefix?: boolean | undefined } | undefined
    ]
  >
}

Added in v1.0.0

route context

RouteContext

Signature

export declare const RouteContext: Context.Tag<RouteContext, RouteContext>

Added in v1.0.0

params

Signature

export declare const params: Effect.Effect<Readonly<Record<string, string | undefined>>, never, RouteContext>

Added in v1.0.0

schemaJson

Signature

export declare const schemaJson: <
  R,
  I extends Partial<{
    readonly method: Method.Method
    readonly url: string
    readonly cookies: Readonly<Record<string, string>>
    readonly headers: Readonly<Record<string, string>>
    readonly pathParams: Readonly<Record<string, string>>
    readonly searchParams: Readonly<Record<string, string>>
    readonly body: any
  }>,
  A
>(
  schema: Schema.Schema<A, I, R>,
  options?: ParseOptions | undefined
) => Effect.Effect<A, any, ServerRequest.ServerRequest | RouteContext | R>

Added in v1.0.0

schemaNoBody

Signature

export declare const schemaNoBody: <
  R,
  I extends Partial<{
    readonly method: Method.Method
    readonly url: string
    readonly cookies: Readonly<Record<string, string>>
    readonly headers: Readonly<Record<string, string>>
    readonly pathParams: Readonly<Record<string, string>>
    readonly searchParams: Readonly<Record<string, string>>
  }>,
  A
>(
  schema: Schema.Schema<A, I, R>,
  options?: ParseOptions | undefined
) => Effect.Effect<A, ParseResult.ParseError, ServerRequest.ServerRequest | RouteContext | R>

Added in v1.0.0

schemaParams

Signature

export declare const schemaParams: <R, I extends Readonly<Record<string, string>>, A>(
  schema: Schema.Schema<A, I, R>,
  options?: ParseOptions | undefined
) => Effect.Effect<A, ParseResult.ParseError, RouteContext | R>

Added in v1.0.0

schemaPathParams

Signature

export declare const schemaPathParams: <R, I extends Readonly<Record<string, string>>, A>(
  schema: Schema.Schema<A, I, R>,
  options?: ParseOptions | undefined
) => Effect.Effect<A, ParseResult.ParseError, RouteContext | R>

Added in v1.0.0

schemaSearchParams

Signature

export declare const schemaSearchParams: <R, I extends Readonly<Record<string, string>>, A>(
  schema: Schema.Schema<A, I, R>,
  options?: ParseOptions | undefined
) => Effect.Effect<A, ParseResult.ParseError, RouteContext | R>

Added in v1.0.0

searchParams

Signature

export declare const searchParams: Effect.Effect<Readonly<Record<string, string>>, never, RouteContext>

Added in v1.0.0

routing

all

Signature

export declare const all: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

del

Signature

export declare const del: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

get

Signature

export declare const get: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

Signature

export declare const head: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

mount

Signature

export declare const mount: {
  <R1, E1>(path: `/${string}`, that: Router<R1, E1>): <R, E>(self: Router<R, E>) => Router<R1 | R, E1 | E>
  <R, E, R1, E1>(self: Router<R, E>, path: `/${string}`, that: Router<R1, E1>): Router<R | R1, E | E1>
}

Added in v1.0.0

mountApp

Signature

export declare const mountApp: {
  <R1, E1>(
    path: `/${string}`,
    that: App.Default<R1, E1>,
    options?: { readonly includePrefix?: boolean | undefined } | undefined
  ): <R, E>(self: Router<R, E>) => Router<Exclude<R1, any> | Exclude<R, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: `/${string}`,
    that: App.Default<R1, E1>,
    options?: { readonly includePrefix?: boolean | undefined } | undefined
  ): Router<Exclude<R, any> | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

options

Signature

export declare const options: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

patch

Signature

export declare const patch: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

post

Signature

export declare const post: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

put

Signature

export declare const put: {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

route

Signature

export declare const route: (method: Method.Method | "*") => {
  <R1, E1>(
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): <R, E>(self: Router<R, E>) => Router<R | Exclude<R1, any>, E1 | E>
  <R, E, R1, E1>(
    self: Router<R, E>,
    path: PathInput,
    handler: Effect.Effect<ServerResponse.ServerResponse, E, ServerRequest.ServerRequest | RouteContext | R>
  ): Router<R | Exclude<R1, any>, E | E1>
}

Added in v1.0.0

type ids

RouteContextTypeId

Signature

export declare const RouteContextTypeId: typeof RouteContextTypeId

Added in v1.0.0

RouteContextTypeId (type alias)

Signature

export type RouteContextTypeId = typeof RouteContextTypeId

Added in v1.0.0

RouteTypeId

Signature

export declare const RouteTypeId: typeof RouteTypeId

Added in v1.0.0

RouteTypeId (type alias)

Signature

export type RouteTypeId = typeof RouteTypeId

Added in v1.0.0

TypeId

Signature

export declare const TypeId: typeof TypeId

Added in v1.0.0

TypeId (type alias)

Signature

export type TypeId = typeof TypeId

Added in v1.0.0

utils

Route (namespace)

Added in v1.0.0

Handler (type alias)

Signature

export type Handler<R, E> = Effect.Effect<
  ServerResponse.ServerResponse,
  E,
  R | RouteContext | ServerRequest.ServerRequest
>

Added in v1.0.0

Router (namespace)

Added in v1.0.0

ExcludeProvided (type alias)

Signature

export type ExcludeProvided<A> = Exclude<A, RouteContext | ServerRequest.ServerRequest | Scope.Scope>

Added in v1.0.0