Commit 7eb42ed6 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Added more documentation

parent 067a0e6bb07e
(* The base URL for fetching all data *)
let baseUrl = "http://localhost:6543";;
(* Headers when fetching schema information *)
let headersForSchema = [|
("Content-Type", "application/schema+json");
("Accept", "application/schema+json")
|];;
(* Headers when fetching data *)
let headersForResources = [|
("Content-Type", "application/json");
("Accept", "application/json")
|];;
type reference = { ref: string }
type schemaLink = {
......@@ -66,6 +51,7 @@ and entityValue =
| ValueObject of entity
(* Module for entity manipulation *)
module Entity = struct
let get entity key = begin
(List.find (fun p -> p.name = key) entity.properties).value
......@@ -83,6 +69,7 @@ module Entity = struct
end
(* Module for decoding a JSON response into OCaml *)
module Decode = struct
exception DecodeError of string
......@@ -185,7 +172,9 @@ module Decode = struct
end
(* Utility module *)
module Utils = struct
(* Renders a template string using the specified entity *)
let produce template entity = begin
let re = [%re "/\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g"] in
let break = ref false in
......@@ -218,13 +207,31 @@ module Utils = struct
end
(* Module for fetching data *)
module Fetch = struct
(* The base URL for fetching all data *)
let baseUrl = "http://localhost:6543";;
(* Headers when fetching schema information *)
let headersForSchema = [|
("Content-Type", "application/schema+json");
("Accept", "application/schema+json")
|];;
(* Headers when fetching data *)
let headersForResources = [|
("Content-Type", "application/json");
("Accept", "application/json")
|];;
(* Take a path as a list of segments and convert it to a string *)
let rec getTarget path =
match path with
| [] -> ""
| [first] -> "/" ^ first
| first :: others -> "/" ^ first ^ (getTarget others)
(* Get a promise for fetching a schema for the specified target path *)
let do_fetch_schema path =
let headers = Fetch.HeadersInit.makeWithArray headersForSchema in
let reqInit = () |> Fetch.RequestInit.make ~headers: headers in
......@@ -237,6 +244,7 @@ module Fetch = struct
|> catch (fun _err -> resolve None)
)
(* Get a promise for fetching data at the specified target path *)
let do_fetch_data path =
let headers = Fetch.HeadersInit.makeWithArray headersForResources in
let reqInit = () |> Fetch.RequestInit.make ~headers: headers in
......@@ -249,6 +257,7 @@ module Fetch = struct
|> catch (fun _err -> resolve None)
)
(* Executes a fetch operation f for a path and react to failure or success *)
let fetch f path on_fail on_success =
let promise = f path in
Js.Promise.(
......@@ -261,9 +270,11 @@ module Fetch = struct
|> ignore
)
(* Fetch the schema for a resource at the path and react to failure or success *)
let fetch_shema path on_fail on_success =
fetch do_fetch_schema path on_fail on_success
(* Fetch the data for a resource at the path and react to failure or success *)
let fetch_data path on_fail on_success =
fetch do_fetch_data path on_fail on_success
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment