IHP Api Reference
Safe HaskellNone

IHP.RouterSupport

Synopsis

Documentation

class HasPath controller => CanRoute controller where Source #

Methods

parseRoute' :: Parser controller Source #

Instances

Instances details
CanRoute ApiController Source # 
Instance details

Defined in IHP.DataSync.REST.Routes

CanRoute SitemapController Source # 
Instance details

Defined in IHP.SEO.Sitemap.Routes

CanRoute WelcomeController Source # 
Instance details

Defined in IHP.Welcome.Controller

(AutoRoute controller, Controller controller) => CanRoute controller Source # 
Instance details

Defined in IHP.RouterSupport

Methods

parseRoute' :: Parser controller Source #

CanRoute (JobsDashboardController authType jobs) Source # 
Instance details

Defined in IHP.Job.Dashboard.Types

class HasPath controller where Source #

Methods

pathTo :: controller -> Text Source #

Returns the path to a given action

>>> pathTo UsersAction
"/Users"
>>> pathTo ShowUserAction { userId = "a32913dd-ef80-4f3e-9a91-7879e17b2ece" }
"/ShowUser?userId=a32913dd-ef80-4f3e-9a91-7879e17b2ece"

Instances

Instances details
HasPath ApiController Source # 
Instance details

Defined in IHP.DataSync.REST.Routes

HasPath SitemapController Source # 
Instance details

Defined in IHP.SEO.Sitemap.Routes

HasPath WelcomeController Source # 
Instance details

Defined in IHP.Welcome.Controller

(Show controller, AutoRoute controller) => HasPath controller Source # 
Instance details

Defined in IHP.RouterSupport

Methods

pathTo :: controller -> Text Source #

HasPath (JobsDashboardController authType jobs) Source # 
Instance details

Defined in IHP.Job.Dashboard.Types

Methods

pathTo :: JobsDashboardController authType jobs -> Text Source #

class Data controller => AutoRoute controller where Source #

Minimal complete definition

Nothing

Methods

autoRouteWithIdType :: (?context :: RequestContext, Data idType) => (ByteString -> Maybe idType) -> Parser controller Source #

autoRoute :: Parser controller Source #

allowedMethodsForAction :: ByteString -> [StdMethod] Source #

Specifies the allowed HTTP methods for a given action

The default implementation does a smart guess based on the usual naming conventions for controllers.

Example (for default implementation):

>>> allowedMethodsForAction @ProjectsController "DeleteProjectAction"
[DELETE]
>>> allowedMethodsForAction @ProjectsController "UpdateProjectAction"
[POST, PATCH]
>>> allowedMethodsForAction @ProjectsController "CreateProjectAction"
[POST]
>>> allowedMethodsForAction @ProjectsController "ShowProjectAction"
[GET, HEAD]
>>> allowedMethodsForAction @ProjectsController "HelloAction"
[GET, POST, HEAD]

Instances

Instances details
(TypeError ((('Text "Looks like you forgot to pass a " ':<>: 'ShowType argument) ':<>: 'Text " to this ") ':<>: 'ShowType controller) :: Constraint, Data argument, Data controller, Data (argument -> controller)) => AutoRoute (argument -> controller) Source #

Display a better error when the user missed to pass an argument to an action.

E.g. when you forgot to pass a projectId to the ShowProjectAction:

<a href={ShowProjectAction}>Show project</a>

The correct code would be this:

<a href={ShowProjectAction projectId}>Show project</a>

See https://github.com/digitallyinduced/ihp/issues/840

Instance details

Defined in IHP.RouterSupport

Methods

autoRouteWithIdType :: (?context :: RequestContext, Data idType) => (ByteString -> Maybe idType) -> Parser (argument -> controller) Source #

autoRoute :: Parser (argument -> controller) Source #

allowedMethodsForAction :: ByteString -> [StdMethod] Source #

runAction :: (Controller controller, ?context :: ControllerContext, ?modelContext :: ModelContext, ?applicationContext :: ApplicationContext, ?requestContext :: RequestContext) => controller -> IO ResponseReceived Source #

get :: (Controller action, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable action) => ByteString -> action -> Parser Application Source #

Routes a given path to an action when requested via GET.

Example:

instance FrontController WebApplication where
    controllers = [
            get "/my-custom-page" NewSessionAction
        ]

The request GET /my-custom-page is now executing NewSessionAction

Also see post.

post :: (Controller action, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable action) => ByteString -> action -> Parser Application Source #

Routes a given path to an action when requested via POST.

Example:

instance FrontController WebApplication where
    controllers = [
            post "/do-something" DoSomethingAction
        ]

The request POST /do-something is now executing DoSomethingAction

Also see get.

startPage :: (Controller action, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable action) => action -> Parser Application Source #

Defines the start page for a router (when / is requested).

frontControllerToWAIApp :: (?applicationContext :: ApplicationContext, FrontController app, WSApp autoRefreshApp, Typeable autoRefreshApp, InitControllerContext ()) => Middleware -> app -> Application -> Application Source #

withPrefix :: ByteString -> [Parser ByteString b] -> Parser ByteString b Source #

class FrontController application where Source #

Minimal complete definition

controllers

Methods

controllers :: [Parser Application] Source #

router :: [Parser Application] -> Parser Application Source #

defaultRouter :: (?applicationContext :: ApplicationContext, ?application :: application, ?context :: RequestContext, FrontController application) => [Parser Application] -> Parser Application Source #

parseRoute :: (?applicationContext :: ApplicationContext, ?context :: RequestContext, Controller controller, CanRoute controller, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => Parser Application Source #

catchAll :: (?applicationContext :: ApplicationContext, ?context :: RequestContext, Controller action, InitControllerContext application, Typeable action, ?application :: application, Typeable application, Data action) => action -> Parser Application Source #

mountFrontController :: (?applicationContext :: ApplicationContext, ?context :: RequestContext, FrontController frontController) => frontController -> Parser Application Source #

createAction :: AutoRoute controller => Maybe controller Source #

Returns the create action for a given controller. Example: `createAction @UsersController == Just CreateUserAction`

updateAction :: AutoRoute controller => Maybe (id -> controller) Source #

Returns the update action when given a controller and id. Example: `updateAction @UsersController == Just (id -> UpdateUserAction id)`

urlTo :: (?context :: context, ConfigProvider context, HasPath action) => action -> Text Source #

Returns the url to a given action.

Uses the baseUrl configured in Config/Config.hs. When no baseUrl is configured in development mode, it will automatically detect the correct baseUrl value.

>>> urlTo UsersAction
"http://localhost:8000/Users"
>>> urlTo ShowUserAction { userId = "a32913dd-ef80-4f3e-9a91-7879e17b2ece" }
"http://localhost:8000/ShowUser?userId=a32913dd-ef80-4f3e-9a91-7879e17b2ece"

parseUUID :: Parser UUID Source #

Parses and returns an UUID

parseId :: forall (table :: Symbol). PrimaryKey table ~ UUID => Parser (Id' table) Source #

Parses an UUID, afterwards wraps it in an Id

parseIntegerId :: Data idType => ByteString -> Maybe idType Source #

remainingText :: Parser Text Source #

Returns all the remaining text until the end of the input

parseText :: Parser Text Source #

Parses until the next /

webSocketApp :: (WSApp webSocketApp, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable webSocketApp) => Parser Application Source #

Routes to a given WebSocket app if the path matches the WebSocket app name

Example:

instance FrontController WebApplication where
    controllers = [
            webSocketApp @AutoRefreshWSApp
        ]

The request /AutoRefreshWSApp will call the AutoRefreshWSApp

webSocketAppWithCustomPath :: (WSApp webSocketApp, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable webSocketApp) => ByteString -> Parser Application Source #

Routes to a given WebSocket app if the path matches

Example:

instance FrontController WebApplication where
    controllers = [
            webSocketAppWithCustomPath @AutoRefreshWSApp "my-ws-app"
        ]

The request /my-ws-app will call the AutoRefreshWSApp

webSocketAppWithHTTPFallback :: (WSApp webSocketApp, InitControllerContext application, ?application :: application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Typeable application, Typeable webSocketApp, Controller webSocketApp) => Parser Application Source #

onlyAllowMethods :: (?context :: RequestContext) => [StdMethod] -> Parser () Source #

Filter methods when writing a custom routing parser

Example:

instance CanRoute ApiController where
   parseRoute' = do
       string "/api/"
       let
           createRecordAction = do
               onlyAllowMethods [POST]

               table <- parseText
               endOfInput
               pure CreateRecordAction { table }

           updateRecordAction = do
               onlyAllowMethods [PATCH]

               table <- parseText
               string "/"
               id <- parseUUID
               pure UpdateRecordAction { table, id }

createRecordAction <|> updateRecordAction

getMethod :: (?context :: RequestContext) => Parser StdMethod Source #

Parses the HTTP Method from the request and returns it.

routeParam :: (?context :: RequestContext, ParamReader paramType) => ByteString -> paramType Source #

Parses and returns an integer parseRational :: (Integral a) => Parser a parseRational = Attoparsec.decimal

Parses a route query parameter

Example:

let showPost = do
    string "/post"
    let postId = routeParam "postId"
    pure ShowPostAction { .. }

Will parse the postId query in `/post?postId=09b545dd-9744-4ef8-87b8-8d227f4faa1e`

Orphan instances

HasPath action => ConvertibleStrings action AttributeValue Source #

This instances makes it possible to write href={MyAction}/ in HSX

Instance details

Methods

convertString :: action -> AttributeValue #