diff --git a/.gitignore b/.gitignore index e912497..31dec1a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,7 +18,7 @@ *.annot *.cmj *.bak -lib/bs +lib/ *.mlast *.mliast .vscode diff --git a/bsconfig.json b/bsconfig.json index 9dca841..686ed89 100644 --- a/bsconfig.json +++ b/bsconfig.json @@ -5,7 +5,9 @@ "src", "examples" ], + "bsc-flags": ["-bs-super-errors"], "bs-dependencies" : [ // add your bs-dependencies here - ] + ], + "refmt": 3 } diff --git a/examples/examples.ml b/examples/examples.ml deleted file mode 100644 index 3ddf84b..0000000 --- a/examples/examples.ml +++ /dev/null @@ -1,28 +0,0 @@ -open Chrome.Identity - -let testGetAuthToken () = getAuthToken - (mkAuthOptions - ~interactive:Js.true_ () - ~scopes:["scope"] - ~account:(mkAccountInfo ~id:"id" ())) - (fun v -> - Js.log v) - -let testGetProfileUserInfo = getProfileUserInfo (fun userInfo -> - Js.log userInfo##email) - -let testRemoveCachedAuthToken = removeCachedAuthToken [%bs.obj { token = "token" } ] (fun () -> - Js.log "Success") - -let testLaunchWebAuthFlow = launchWebAuthFlow - (mkWebFlowOptions - ~url:"https://example.com" ()) - (fun mResponseURL () -> - match (Js.Null.to_opt mResponseURL) with - | Some responesUrl -> Js.log responesUrl - | None -> Js.log "No url returned!") - -let testGetRedirectURL = getRedirectURL (Js.Null.from_opt (Some "path")) - -let testOnSignInChanged = OnSignInChanged.addListener (fun accountInfo signedIn -> - Js.log signedIn |> (fun _ -> Js.log accountInfo##id)) diff --git a/examples/examples.re b/examples/examples.re new file mode 100644 index 0000000..c4f9117 --- /dev/null +++ b/examples/examples.re @@ -0,0 +1,33 @@ +open Chrome.Identity; + +let testGetAuthToken = () => + getAuthToken( + mkAuthOptions( + ~interactive=Js.true_, + (), + ~scopes=["scope"], + ~account=mkAccountInfo(~id="id", ()) + ), + (v) => Js.log(v) + ); + +let testGetProfileUserInfo = getProfileUserInfo((userInfo) => Js.log(userInfo##email)); + +let testRemoveCachedAuthToken = removeCachedAuthToken({"token": "token"}, () => Js.log("Success")); + +let testLaunchWebAuthFlow = + launchWebAuthFlow( + mkWebFlowOptions(~url="https://example.com", ()), + (mResponseURL, ()) => + switch (Js.Null.to_opt(mResponseURL)) { + | Some(responseUrl) => Js.log(responseUrl) + | None => Js.log("No url returned!") + } + ); + +let testGetRedirectURL = getRedirectURL(Js.Null.from_opt(Some("path"))); + +let testOnSignInChanged = + OnSignInChanged.addListener( + (accountInfo, signedIn) => Js.log(signedIn) |> ((_) => Js.log(accountInfo##id)) + ); diff --git a/src/IdentityRe.re b/src/IdentityRe.re new file mode 100644 index 0000000..c4bcd14 --- /dev/null +++ b/src/IdentityRe.re @@ -0,0 +1,88 @@ +/* chrome.identity.getAuthToken */ +class type _accountInfo = + [@bs] + { + pub id: string + }; + +type accountInfo = Js.t(_accountInfo); + +[@bs.obj] external mkAccountInfo : (~id: string=?, unit) => accountInfo = ""; + +class type _getAuthTokenOptions = + [@bs] + { + pub interactive: Js.boolean; + pub scopes: list(string); + pub account: accountInfo + }; + +type getAuthTokenOptions = Js.t(_getAuthTokenOptions); + +[@bs.obj] +external mkAuthOptions : + (~interactive: Js.boolean=?, ~scopes: list(string)=?, ~account: accountInfo=?, unit) => + getAuthTokenOptions = + ""; + +[@bs.scope ("chrome", "identity")] [@bs.val] +external getAuthToken : (getAuthTokenOptions, string => 'a) => unit = + "getAuthToken"; + +/* chrome.identity.getProfileUserInfo */ +class type _profileUserInfo = + [@bs] + { + pub id: string; + pub email: string + }; + +type profileUserInfo = Js.t(_profileUserInfo); + +[@bs.scope ("chrome", "identity")] [@bs.val] +external getProfileUserInfo : (profileUserInfo => 'a) => unit = + "getProfileUserInfo"; + +/* chrome.identity.removeCachedAuthToken */ +class type _rmCachedToken = + [@bs] + { + pub token: string + }; + +type rmCachedTokenOptions = Js.t(_rmCachedToken); + +[@bs.obj] external mkRmCachedTokenOptions : (~token: string, unit) => rmCachedTokenOptions = ""; + +[@bs.scope ("chrome", "identity")] [@bs.val] +external removeCachedAuthToken : (rmCachedTokenOptions, unit => 'a) => unit = + "removeCachedAuthToken"; + +/* chrome.identity.launchWebAuthFlow */ +class type _webAuthFlowOptions = + [@bs] + { + pub url: string; + pub interactive: Js.boolean + }; + +type webAuthFlowOptions = Js.t(_webAuthFlowOptions); + +[@bs.obj] +external mkWebFlowOptions : (~url: string, ~interactive: Js.boolean=?, unit) => webAuthFlowOptions = + ""; + +[@bs.scope ("chrome", "identity")] [@bs.val] +external launchWebAuthFlow : (webAuthFlowOptions, (Js.null(string), unit) => 'a) => unit = + "launchWebAuthFlow"; + +/* chrome.identity.getRedirectURL */ +[@bs.scope ("chrome", "identity")] [@bs.val] external getRedirectURL : Js.null(string) => string = + "getRedirectURL"; + +/* chrome.identity.onSignInChanged.addListener */ +module OnSignInChanged = { + [@bs.scope ("chrome", "identity", "onSignInChanged")] [@bs.val] + external addListener : ((accountInfo, Js.boolean) => 'a) => unit = + "addListener"; +}; \ No newline at end of file diff --git a/src/chrome.ml b/src/chrome.ml deleted file mode 100644 index 5a02826..0000000 --- a/src/chrome.ml +++ /dev/null @@ -1,65 +0,0 @@ -module Identity = struct - (* chrome.identity.getAuthToken *) - - class type _accountInfo = object - method id : string - end [@bs] - - type accountInfo = _accountInfo Js.t - external mkAccountInfo : ?id:string -> unit -> accountInfo = "" [@@bs.obj] - - class type _getAuthTokenOptions = object - method interactive : Js.boolean - method scopes : string list - method account : accountInfo - end [@bs] - - type getAuthTokenOptions = _getAuthTokenOptions Js.t - external mkAuthOptions : ?interactive:Js.boolean -> ?scopes:string list -> ?account:accountInfo -> unit -> getAuthTokenOptions = "" [@@bs.obj] - - external getAuthToken : getAuthTokenOptions -> (string -> 'a) -> unit = "getAuthToken" [@@bs.scope "chrome", "identity"] [@@bs.val] - - (* chrome.identity.getProfileUserInfo *) - - class type _profileUserInfo = object - method id : string - method email : string - end [@bs] - - type profileUserInfo = _profileUserInfo Js.t - - external getProfileUserInfo : (profileUserInfo -> 'a) -> unit = "getProfileUserInfo" [@@bs.scope "chrome", "identity"] [@@bs.val] - - (* chrome.identity.removeCachedAuthToken *) - - class type _rmCachedToken = object - method token : string - end [@bs] - - type rmCachedTokenOptions = _rmCachedToken Js.t - external mkRmCachedTokenOptions : token:string -> unit -> rmCachedTokenOptions = "" [@@bs.obj] - - external removeCachedAuthToken : rmCachedTokenOptions -> (unit -> 'a) -> unit = "removeCachedAuthToken" [@@bs.scope "chrome", "identity"] [@@bs.val] - - (* chrome.identity.launchWebAuthFlow *) - - class type _webAuthFlowOptions = object - method url : string - method interactive : Js.boolean - end [@bs] - - type webAuthFlowOptions = _webAuthFlowOptions Js.t - external mkWebFlowOptions : url:string -> ?interactive:Js.boolean -> unit -> webAuthFlowOptions = "" [@@bs.obj] - - external launchWebAuthFlow : webAuthFlowOptions -> (string Js.null -> unit -> 'a) -> unit = "launchWebAuthFlow" [@@bs.scope "chrome", "identity"] [@@bs.val] - - (* chrome.identity.getRedirectURL *) - - external getRedirectURL : string Js.null -> string = "getRedirectURL" [@@bs.scope "chrome", "identity"] [@@bs.val] - - (* chrome.identity.onSignInChanged.addListener *) - - module OnSignInChanged = struct - external addListener : (accountInfo -> Js.boolean -> 'a) -> unit = "addListener" [@@bs.scope "chrome", "identity", "onSignInChanged"] [@@bs.val] - end -end diff --git a/src/chrome.re b/src/chrome.re new file mode 100644 index 0000000..8e46bd1 --- /dev/null +++ b/src/chrome.re @@ -0,0 +1 @@ +module Identity = IdentityRe;