Module

Yoga.Fastify.Om

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

#RequestContext Source

type RequestContext = { body :: Maybe Foreign, headers :: Object String, method :: HTTPMethod, params :: Object String, query :: Object Foreign, url :: RouteURL }

Request context that gets injected automatically into Om context as httpRequest field Contains all request data: headers, params, query, body, method, url

#OmHandler Source

type OmHandler :: Type -> Row Type -> Typetype OmHandler ctx err = FastifyReply -> Om ctx err Unit

Om handler type: receives only reply, request data is in context as httpRequest! Access request data via helpers: hdrs <- requestHeaders or { httpRequest } <- ask

#OmFastify Source

type OmFastify :: Row Type -> Typetype OmFastify appCtx = { contextRef :: Ref (Record appCtx), fastify :: Fastify }

Wrapper for Fastify app with stored application context Used by Om-aware route registration (getOm, postOm, etc.) The app context must not contain an httpRequest field (enforced by Lacks constraint)

#createOmFastify Source

createOmFastify :: forall appCtx. Lacks "httpRequest" appCtx => Record appCtx -> Fastify -> Effect (OmFastify appCtx)

Create Om-aware Fastify wrapper that stores application context

Example: app <- liftEffect $ F.fastify {} omApp <- liftEffect $ createOmFastify { db, logger } app

Note: Your app context must not contain an httpRequest field (compile-time enforced)

#getOm Source

getOm :: forall appCtx. Lacks "httpRequest" appCtx => RouteURL -> OmHandler { httpRequest :: RequestContext | appCtx } () -> OmFastify appCtx -> Effect Unit

Register GET route with Om handler (httpRequest injected into context)

Example: getOm (RouteURL "/users/:id") handler omApp where handler reply = do { httpRequest } <- ask -- Request data in context! -- Or use helpers: hdrs <- requestHeaders

#postOm Source

postOm :: forall appCtx. Lacks "httpRequest" appCtx => RouteURL -> OmHandler { httpRequest :: RequestContext | appCtx } () -> OmFastify appCtx -> Effect Unit

Register POST route with Om handler (httpRequest injected into context)

#putOm Source

putOm :: forall appCtx. Lacks "httpRequest" appCtx => RouteURL -> OmHandler { httpRequest :: RequestContext | appCtx } () -> OmFastify appCtx -> Effect Unit

Register PUT route with Om handler (httpRequest injected into context)

#deleteOm Source

deleteOm :: forall appCtx. Lacks "httpRequest" appCtx => RouteURL -> OmHandler { httpRequest :: RequestContext | appCtx } () -> OmFastify appCtx -> Effect Unit

Register DELETE route with Om handler (httpRequest injected into context)

#patchOm Source

patchOm :: forall appCtx. Lacks "httpRequest" appCtx => RouteURL -> OmHandler { httpRequest :: RequestContext | appCtx } () -> OmFastify appCtx -> Effect Unit

Register PATCH route with Om handler (httpRequest injected into context)

#httpRequest Source

httpRequest :: forall err rest. Om { httpRequest :: RequestContext | rest } err RequestContext

Get full HTTP request context from Om context

Example: req <- httpRequest let hdrs = req.headers

#requestHeaders Source

requestHeaders :: forall err rest. Om { httpRequest :: RequestContext | rest } err (Object String)

Get headers from Om context (no parameter needed!)

Example: hdrs <- requestHeaders case Object.lookup "authorization" hdrs of ...

#requestParams Source

requestParams :: forall err rest. Om { httpRequest :: RequestContext | rest } err (Object String)

Get path params from Om context (no parameter needed!)

Example: ps <- requestParams case Object.lookup "id" ps of ...

#requestQuery Source

requestQuery :: forall err rest. Om { httpRequest :: RequestContext | rest } err (Object Foreign)

Get query params from Om context (no parameter needed!)

Example: q <- requestQuery case Object.lookup "limit" q of ...

#requestBody Source

requestBody :: forall err rest. Om { httpRequest :: RequestContext | rest } err (Maybe Foreign)

Get request body from Om context (no parameter needed!)

Example: bodyMaybe <- requestBody case bodyMaybe of ...

#requestMethod Source

requestMethod :: forall err rest. Om { httpRequest :: RequestContext | rest } err HTTPMethod

Get HTTP method from Om context (no parameter needed!)

#requestUrl Source

requestUrl :: forall err rest. Om { httpRequest :: RequestContext | rest } err RouteURL

Get request URL from Om context (no parameter needed!)

#MissingParam Source

type MissingParam = { missingParam :: String }

Error type for missing path parameter

#ParamErrors Source

type ParamErrors = { invalid :: Array String, missing :: Array String }

Error type for path parameter errors (missing or invalid)

#MissingHeader Source

type MissingHeader = { missingHeader :: String }

Error type for missing header

#HeaderErrors Source

type HeaderErrors = { invalid :: Array String, missing :: Array String }

Error type for header errors (missing or invalid)

#MissingQueryParam Source

type MissingQueryParam = { missingQueryParam :: String }

Error type for missing query parameter

#QueryParamErrors Source

type QueryParamErrors = { invalid :: Array String, missing :: Array String }

Error type for query parameter errors (missing or invalid)

#requiredParam Source

requiredParam :: forall rest. String -> Om { httpRequest :: RequestContext | rest } (missingParam :: String) String

Get a required path parameter, throws MissingParam if not found

Example: userId <- requiredParam "id"

#param Source

param :: forall err rest. String -> Om { httpRequest :: RequestContext | rest } err (Maybe String)

Get an optional path parameter

Example: userIdMaybe <- param "id"

#requiredParams Source

requiredParams :: forall rest r rl. RowToList r rl => ParseParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } (paramErrors :: ParamErrors) (Record r)

Get multiple required path parameters as a typed record Throws ParamErrors with all missing and invalid fields

Example: { userId, postId } <- requiredParams (Proxy :: _ { userId :: Int, postId :: String })

#optionalParams Source

optionalParams :: forall rest r rl. RowToList r rl => ParseOptionalParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } () (Record r)

Get multiple optional path parameters as a typed record with Maybe fields

Example: { userId, postId } <- optionalParams (Proxy :: _ { userId :: Int, postId :: String }) -- Returns { userId :: Maybe Int, postId :: Maybe String }

#ParseParam Source

class ParseParam a  where

Parse a String to a specific type (used internally by requiredParams)

Members

Instances

#ParseParams Source

class ParseParams :: RowList Type -> Row Type -> Constraintclass ParseParams (rl :: RowList Type) (r :: Row Type) | rl -> r where

Typeclass to parse multiple params into a record, accumulating all errors

Members

Instances

#ParseOptionalParams Source

class ParseOptionalParams :: RowList Type -> Row Type -> Constraintclass ParseOptionalParams (rl :: RowList Type) (r :: Row Type) | rl -> r where

Typeclass to parse multiple optional params into a record with Maybe fields

Members

Instances

#ParseQueryParams Source

class ParseQueryParams :: RowList Type -> Row Type -> Constraintclass ParseQueryParams (rl :: RowList Type) (r :: Row Type) | rl -> r where

Typeclass to parse query params (Foreign) into a record, accumulating all errors

Query params are typed as Object Foreign to match TypeScript's unknown type. We use yoga-json's ReadForeign to safely decode Foreign -> String, then ParseParam to convert String -> target type.

Members

Instances

#ParseOptionalQueryParams Source

class ParseOptionalQueryParams :: RowList Type -> Row Type -> Constraintclass ParseOptionalQueryParams (rl :: RowList Type) (r :: Row Type) | rl -> r where

Typeclass to parse optional query params (Foreign) into a record with Maybe fields TODO: Integrate yoga-json's ReadForeign (see ParseQueryParams note)

Members

Instances

#requiredHeader Source

requiredHeader :: forall rest. String -> Om { httpRequest :: RequestContext | rest } (missingHeader :: String) String

Get a required header, throws MissingHeader if not found

Example: authToken <- requiredHeader "authorization"

#requestHeader Source

requestHeader :: forall err rest. String -> Om { httpRequest :: RequestContext | rest } err (Maybe String)

Get an optional header

Example: authTokenMaybe <- requestHeader "authorization"

#requiredHeaders Source

requiredHeaders :: forall rest r rl. RowToList r rl => ParseParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } (headerErrors :: HeaderErrors) (Record r)

Get multiple required headers as a typed record Throws HeaderErrors with all missing and invalid fields

Example: { authorization, contentType } <- requiredHeaders (Proxy :: _ { authorization :: String, contentType :: String })

#optionalHeaders Source

optionalHeaders :: forall rest r rl. RowToList r rl => ParseOptionalParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } () (Record r)

Get multiple optional headers as a typed record with Maybe fields

Example: { authorization, userAgent } <- optionalHeaders (Proxy :: _ { authorization :: String, userAgent :: String }) -- Returns { authorization :: Maybe String, userAgent :: Maybe String }

#requiredQueryParam Source

requiredQueryParam :: forall rest. String -> Om { httpRequest :: RequestContext | rest } (missingQueryParam :: String) Foreign

Get a required query parameter, throws MissingQueryParam if not found Returns Foreign - use yoga-json to decode

Example: limitForeign <- requiredQueryParam "limit" limit <- liftEffect $ J.readJSON limitForeign

#queryParam Source

queryParam :: forall err rest. String -> Om { httpRequest :: RequestContext | rest } err (Maybe Foreign)

Get an optional query parameter

Example: limitMaybe <- queryParam "limit"

#requiredQueryParams Source

requiredQueryParams :: forall rest r rl. RowToList r rl => ParseQueryParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } (queryParamErrors :: QueryParamErrors) (Record r)

Get multiple required query parameters as a typed record Throws QueryParamErrors with all missing and invalid fields

Example: { page, limit } <- requiredQueryParams (Proxy :: _ { page :: Int, limit :: Int })

#optionalQueryParams Source

optionalQueryParams :: forall rest r rl. RowToList r rl => ParseOptionalQueryParams rl r => Proxy r -> Om { httpRequest :: RequestContext | rest } () (Record r)

Get multiple optional query parameters as a typed record with Maybe fields

Example: { page, limit } <- optionalQueryParams (Proxy :: _ { page :: Int, limit :: Int }) -- Returns { page :: Maybe Int, limit :: Maybe Int }

Re-exports from Yoga.Fastify.Fastify

#StatusCode Source

#RouteURL Source

#RouteHandler Source

type RouteHandler = FastifyRequest -> FastifyReply -> Aff Unit

Route handler type: takes request and reply, returns Aff Unit

#Port Source

newtype Port

Instances

#Host Source

newtype Host

Instances

#HTTPMethod Source

#FastifyRequest Source

data FastifyRequest

Opaque request object

#FastifyReply Source

data FastifyReply

Opaque reply object

#Fastify Source

data Fastify

Opaque Fastify instance