package sihl
Install
Dune Dependency
Authors
Maintainers
Sources
md5=3dd7fcf3b9f0cf99c1ce0ceed278aef6
sha512=aeb289a71186b7b0b429d3869bb4eab632eec5ab403353dc13d8a18c5b04af5fda7c2a74bacf98364a8e0b1b3e843fb5a0b28a0c38652bed2e9432ee6659b53a
doc/sihl/Sihl/Web/Rest/index.html
Module Web.Rest
Source
This module allows you to build RESTful web pages quickly.
A query
describes the search terms of an `Index
action and it represents a partial view on a collection that is sorted and filtered.
of_request request
returns the query by parsing the query string of the request
.
to_query_string query
returns the query string representation of a query
that can be used safely in URIs. Note that the question mark ?
is also part of it.
next_page query total
returns a query that represents the result of the next page given the query
of the current page.
total
is the total number of items in the collection.
None
is returned if the current page is already the last page and there is no next page.
previous_page query
returns a query that represents the previous page given the query
of the current page.
None
is returned if the current page is the first page and there is no previous page.
last_page query total
returns a query that represents the result of the last page given the query
of the current page.
total
is the total number of items in the collection.
None
is returned if the current page is already the last page.
first_page query total
returns a query that represents the result of the first page given the query
of the current page.
None
is returned if the current page is already the first page.
query_filter query
returns the string representation of the filter keyword if it exists.
query_sort query
returns the string representation of the sort keyword if it exists.
query_limit query
returns the string representation of the limit if it exists.
query_offset query
returns the string representation of the offset if it exists.
form
represents a validated and decoded form. One element consists of (name, input, error)
.
name
is the schema field name and the HTML input name.
input
is the input of the form that was submitted, it might not be present. Use it to restore form values after it was submitted and validation or decoding failed.
error
is the error message of the validation or decoding.
find_form name form
returns the (value, error)
of a form
input element with the name
.
The value
is the submitted value of the input element. The value is set even if the submitted form failed to decode or validate. Use the submitted values to populate the form that can be fixed and re-submitted by the user.
The error
message comes from either the decoding, validation or CRUD service. It can be shown to the user.
The SERVICE
interface has to be implemented by a CRUD service that drives the resource with its business logic.
The VIEW
interface needs to be implemented by a module that renders HTML.
A module of type CONTROLLER
can be used to create a resource with resource_of_controller
. Use a controller instead of a service and a view if you need low level control.
val resource_of_service :
?only:action list ->
string ->
('meta, 'ctor, 'resource) Conformist.t ->
view:(module VIEW with type t = 'resource) ->
(module SERVICE with type t = 'resource) ->
router list
resource_of_service ?only name schema ~view service
returns a list of routers that can be combined with choose
that represent a resource.
A resource pizzas
creates following 7 routers:
GET /pizzas `Index Display a list of all pizzas GET /pizzas/new `New Return an HTML form for creating a new pizza POST /pizzas `Create Create a new pizza GET /pizzas/:id `Show Display a specific pizza GET /pizzas/:id/edit `Edit Return an HTML form for editing a pizza PATCH/PUT /pizzas/:id `Update Update a specific pizza DELETE /pizzas/:id `Delete Delete a specific pizza
only
is an optional list of action
s. If only is set, routers are created only for the actions listed.
name
is the name of the resource. It is good practice to use plural as the name is used to build the resource path of the URL.
schema
is the conformist schema of the resource.
view
is the view service of type VIEW
of the resource. The view renders HTML.
service
is the underlying CRUD service of type SERVICE
of the resource.
val resource_of_controller :
?only:action list ->
string ->
('meta, 'ctor, 'resource) Conformist.t ->
(module CONTROLLER with type t = 'resource) ->
router list
resource_of_controller ?only name schema controller
returns the same list of routers as resource_of_service
. resource_of_controller
takes one module of type CONTROLLER
instead of a view and a service.
If you implement your own controller you have to do all the wiring yourself, but you gain more control.