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
#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 UnitRegister 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
#httpRequest Source
httpRequest :: forall err rest. Om { httpRequest :: RequestContext | rest } err RequestContextGet 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 HTTPMethodGet HTTP method from Om context (no parameter needed!)
#requestUrl Source
requestUrl :: forall err rest. Om { httpRequest :: RequestContext | rest } err RouteURLGet 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) StringGet a required path parameter, throws MissingParam if not found
Example: userId <- requiredParam "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 whereParse a String to a specific type (used internally by requiredParams)
Members
parseParam :: String -> Either String a
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
parseParams :: Proxy rl -> Object String -> Either { invalid :: Array String, missing :: Array String } (Record r)
Instances
ParseParams Nil ()(IsSymbol name, ParseParam ty, ParseParams tail tailRow, Cons name ty tailRow r, Lacks name tailRow) => ParseParams (Cons name ty tail) r
#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
parseOptionalParams :: Proxy rl -> Object String -> Record r
Instances
ParseOptionalParams Nil ()(IsSymbol name, ParseParam ty, ParseOptionalParams tail tailRow, Cons name (Maybe ty) tailRow r, Lacks name tailRow) => ParseOptionalParams (Cons name ty tail) r
#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
parseQueryParams :: Proxy rl -> Object Foreign -> Either { invalid :: Array String, missing :: Array String } (Record r)
Instances
ParseQueryParams Nil ()(IsSymbol name, ParseParam ty, ParseQueryParams tail tailRow, Cons name ty tailRow r, Lacks name tailRow) => ParseQueryParams (Cons name ty tail) r
#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
parseOptionalQueryParams :: Proxy rl -> Object Foreign -> Record r
Instances
ParseOptionalQueryParams Nil ()(IsSymbol name, ParseParam ty, ParseOptionalQueryParams tail tailRow, Cons name (Maybe ty) tailRow r, Lacks name tailRow) => ParseOptionalQueryParams (Cons name ty tail) r
#requiredHeader Source
requiredHeader :: forall rest. String -> Om { httpRequest :: RequestContext | rest } (missingHeader :: String) StringGet 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) ForeignGet 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
#RouteHandler Source
type RouteHandler = FastifyRequest -> FastifyReply -> Aff UnitRoute handler type: takes request and reply, returns Aff Unit
#FastifyRequest Source
data FastifyRequestOpaque request object
#FastifyReply Source
data FastifyReplyOpaque reply object