(* Copyright 2023 Yawar Amin

   This file is part of dream-html.

   dream-html is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation, either version 3 of the License, or (at your option) any
   later version.

   dream-html is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
   details.

   You should have received a copy of the GNU General Public License along with
   dream-html. If not, see <https://www.gnu.org/licenses/>. *)

include module type of Pure_html

(** {2 Output} *)

val respond :
  ?status:[< Dream.status] ->
  ?code:int ->
  ?headers:(string * string) list ->
  node ->
  Dream.response Dream.promise

val send :
  ?text_or_binary:[< Dream.text_or_binary] ->
  ?end_of_message:[< Dream.end_of_message] ->
  Dream.websocket ->
  node ->
  unit Dream.promise
(** Type-safe wrapper for [Dream.send].

    @since 3.2.0. *)

val set_body : Dream.response -> node -> unit
(** Type-safe wrapper for [Dream.set_body]. Sets the body to the given [node] and
    sets the [Content-Type] header to [text/html]. *)

val write : Dream.stream -> node -> unit Dream.promise
(** Type-safe wrapper for [Dream.write]. *)

val csrf_tag : Dream.request -> node
(** Convenience to add a CSRF token generated by Dream into your form. Type-safe
    wrapper for [Dream.csrf_tag].

    {[form
        [action "/foo"]
        [csrf_tag req; input [name "bar"]; input [type_ "submit"]]]} *)

(** {2 Live reload support}*)

(** Live reload script injection and handling. Adapted from [Dream.livereload]
    middleware. This version is not a middleware so it's not as plug-and-play as
    that, but on the other hand it's much simpler to implement because it uses
    type-safe dream-html nodes rather than parsing and printing raw HTML. See
    below for the 3-step process to use it.

    This module is adapted from Dream, released under the MIT license. For
    details, visit {{: https://github.com/aantron/dream}}.

    Copyright 2021-2023 Thibaut Mattio, Anton Bachin.

    @since 3.4.0. *)
module Livereload : sig
  val route : Dream.route
  (** (1) Put this in your top-level router:

      {[let () = Dream.run
        @@ Dream.logger
        @@ Dream.router [
          Dream_html.Livereload.route;
          (* ...other routes... *)
      ]]} *)

  val script : node
  (** (2) Put this inside your [head]:

      {[head [] [
        Livereload.script;
        (* ... *)
      ]]} *)

  (** (3) And run the server with environment variable [LIVERELOAD=1].

      {b ⚠️ If this env var is not set, then livereload is turned off.} This means
      that the [route] will respond with [404] status and the script will be
      omitted from the rendered HTML. *)
end
