Module

Yoga.Fastify.Om.Route.OmHandler

Package
purescript-yoga-fastify-om
Repository
rowtype-yoga/purescript-yoga-fastify-om

#handle Source

handle :: forall @route pathParams queryParams reqHeaders body respVariant successRow errorRow extraCtx. RouteHandler route pathParams queryParams reqHeaders body respVariant => SplitResponse respVariant successRow errorRow => Lacks "_respondNow" errorRow => Lacks "path" extraCtx => Lacks "query" extraCtx => Lacks "headers" extraCtx => Lacks "body" extraCtx => Om { body :: body, headers :: Record reqHeaders, path :: Record pathParams, query :: Record queryParams | extraCtx } (_respondNow :: Variant successRow | errorRow) (Variant successRow) -> Handler route extraCtx

Create a Handler from an Om computation.

The Om computation receives the request context (path, query, headers, body) plus any extra dependencies via ask. Request field types are verified against the route at compile time.

Example:

putUserHandler :: Handler PutUser (userRepo :: UserRepo)
putUserHandler = handle do
  { path, body, userRepo } <- ask
  existing <- userRepo.findByName path.name # liftAff
  case existing of
    Just user -> respond @"ok" user
    Nothing -> do
      user <- userRepo.create path.name body.email # liftAff
      respond @"created" user

#Handler Source

newtype Handler :: Type -> Row Type -> Typenewtype Handler route (ctx :: Row Type)

A handler with deferred dependency injection. The ctx row tracks what extra dependencies are needed beyond the request. Use handle to create one, and registerAPILayer to provide the deps.

Constructors

#respond Source

respond :: forall @labelOrCode label body r1 r2 ctx err. ToLabel (Proxy labelOrCode) label => IsSymbol label => Cons label (Response () body) r1 r2 => body -> Om ctx err (Variant r2)

Return a response with a specific reason label or status code

Example:

respond @"ok" { id: 1, name: "Alice" }
respond @200 { id: 1, name: "Alice" }
respond @"created" newUser
respond @201 newUser

#respondWith Source

respondWith :: forall @labelOrCode label headers body r1 r2 ctx err. ToLabel (Proxy labelOrCode) label => IsSymbol label => Cons label (Response headers body) r1 r2 => Record headers -> body -> Om ctx err (Variant r2)

Return a response with a specific reason label or status code and custom headers

Example:

respondWith @"created" { "Location": "/users/123" } user
respondWith @201 { "Location": "/users/123" } user

#respondNoContent Source

respondNoContent :: forall ctx err r. Om ctx err (Variant (noContent :: Response () Unit | r))

Return a 204 No Content response (no headers, no body).

Example:

respondNoContent

#respondNotModified Source

respondNotModified :: forall ctx err r. Om ctx err (Variant (notModified :: Response () Unit | r))

Return a 304 Not Modified response (no headers, no body).

Example:

respondNotModified

#reject Source

reject :: forall @labelOrCode label body _r1 err _r2 ctx a. ToLabel (Proxy labelOrCode) label => IsSymbol label => Cons label (Response () body) _r1 err => Cons label (Response () body) _r2 (exception :: Error | err) => body -> Om ctx err a

Throw a non-2xx error response (short-circuits the handler)

Example:

reject @"notFound" { error: "User not found" }
reject @404 { error: "User not found" }
reject @"badRequest" { error: "Invalid input" }
reject @400 { error: "Invalid input" }

#rejectWith Source

rejectWith :: forall @labelOrCode label headers body _r1 err _r2 ctx a. ToLabel (Proxy labelOrCode) label => IsSymbol label => Cons label (Response headers body) _r1 err => Cons label (Response headers body) _r2 (exception :: Error | err) => Record headers -> body -> Om ctx err a

Throw a non-2xx error response with custom headers (short-circuits)

Example:

rejectWith @"unauthorized" { "WWW-Authenticate": "Bearer" } { error: "Invalid token" }
rejectWith @401 { "WWW-Authenticate": "Bearer" } { error: "Invalid token" }

#mapReject Source

mapReject :: forall @from @toLabelOrCode toLabel tyIn body ctx errIn errMid errOut a. IsSymbol from => ToLabel (Proxy toLabelOrCode) toLabel => IsSymbol toLabel => Cons from tyIn (exception :: Error | errMid) (exception :: Error | errIn) => Cons toLabel (Response () body) (exception :: Error | errMid) (exception :: Error | errOut) => (tyIn -> body) -> Om ctx errIn a -> Om ctx errOut a

#ToLabel Source

class ToLabel :: Type -> Symbol -> Constraintclass ToLabel labelOrCode label | labelOrCode -> label

Convert either a status code (Int) or label (Symbol) to a label (Symbol). This allows functions to accept both @404 and @"notFound" polymorphically.

Instances

#Is2xxStatus Source

class Is2xxStatus :: Symbol -> Boolean -> Constraintclass Is2xxStatus (label :: Symbol) (is2xx :: Boolean) | label -> is2xx

Determine whether a variant label corresponds to a 2xx HTTP status code.

Instances

#SplitResponse Source

class SplitResponse :: Row Type -> Row Type -> Row Type -> Constraintclass SplitResponse (respVariant :: Row Type) (successRow :: Row Type) (errorRow :: Row Type) | respVariant -> successRow errorRow

Split a response variant row into success (2xx) and error (non-2xx) sub-rows.

Instances

#SplitResponseRL Source

class SplitResponseRL :: RowList Type -> Row Type -> Row Type -> Constraintclass SplitResponseRL (rl :: RowList Type) (successRow :: Row Type) (errorRow :: Row Type) | rl -> successRow errorRow

Instances

#SplitResponseEntry Source

class SplitResponseEntry :: Boolean -> Symbol -> Type -> RowList Type -> Row Type -> Row Type -> Constraintclass SplitResponseEntry (is2xx :: Boolean) (label :: Symbol) (ty :: Type) (tail :: RowList Type) (successRow :: Row Type) (errorRow :: Row Type) | is2xx label ty tail -> successRow errorRow

Dispatch based on Is2xxStatus result.

Instances

#BuildErrorHandlers Source

class BuildErrorHandlers :: RowList Type -> Row Type -> Row Type -> Constraintclass BuildErrorHandlers (rl :: RowList Type) (respVariant :: Row Type) (handlers :: Row Type) | rl respVariant -> handlers where

Build a record of handlers for Variant.onMatch that convert each error variant label into the full response variant.

Members

Instances

#RouteResponseVariant Source

class RouteResponseVariant :: Type -> Row Type -> Constraintclass RouteResponseVariant (route :: Type) (respVariant :: Row Type) | route -> respVariant

Instances