Last active
November 3, 2020 13:47
-
-
Save SkinyMonkey/3adc2bb71ad5555a5eec02616ae7d8e9 to your computer and use it in GitHub Desktop.
A rescript binding to the nhost-js-sdk library,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NOTE : This rescript file is a a binding to the nhost-js-sdk library, | |
| // which is a way to communicate with https://github.com/nhost/hasura-backend-plus or services at nhost.io | |
| // see the end of the file for a usage example | |
| // initialization ------------------------------------------------------------- | |
| // placeholder type | |
| type nhost_t | |
| // we import the nhost-js-sdk module | |
| @bs.module("nhost-js-sdk") external nhost : nhost_t = "default" | |
| type auth_config = { | |
| base_url: string, | |
| }; | |
| // we define the initializeApp method on the nhost_t type | |
| @bs.send external initializeApp: (nhost_t, auth_config) => unit = "initializeApp" | |
| // we initialize nhost | |
| initializeApp(nhost, { | |
| base_url: "http://localhost:3000", | |
| }); | |
| // error ---------------------------------------------------------------------- | |
| // FIXME : find a way to cast from Js.Promise.error to axios format: | |
| // instead of using rawJs | |
| /* | |
| type error = { | |
| statusCode: int, | |
| error: string, | |
| message: string | |
| } | |
| */ | |
| let getErrorMessage = %raw(` | |
| function(error) { | |
| return error.response.data.message | |
| } | |
| `) | |
| // auth ----------------------------------------------------------------------- | |
| // placeholder type | |
| type auth_t | |
| // We define the nhostAuth method on the auth_t type | |
| @bs.send external nhostAuth: nhost_t => auth_t = "auth" | |
| // We create the nhost auth object | |
| let auth = nhostAuth(nhost); | |
| // login ---------------------------------------------------------------------- | |
| // loginData is returned by a login call | |
| type loginData = { | |
| mfa: option<bool>, | |
| ticket: option<string>, | |
| }; | |
| @bs.send external nhostLogin : (auth_t, string, string) => Js.Promise.t<loginData> = "login" | |
| // We redefine login to not have to use the auth object outside of this file | |
| let login = (email, password : string) => nhostLogin(auth, email, password) | |
| // register ------------------------------------------------------------------- | |
| type registerUserData = { | |
| display_name: string, | |
| }; | |
| let newRegisterUserData = (displayName: string) : registerUserData => { | |
| {display_name : displayName} | |
| } | |
| @bs.send external nhostRegister : (auth_t, string, string, registerUserData) => Js.Promise.t<unit> = "register" | |
| let register = (email, password: string, userData: registerUserData) : Js.Promise.t<unit> => | |
| nhostRegister(auth, email, password, userData) | |
| // logout --------------------------------------------------------------------- | |
| // FIXME : send profile uuid to cancel refresh token on HBP+? | |
| @bs.send external nhostLogout : auth_t => unit = "logout" | |
| let logout = () => nhostLogout(auth) | |
| // getJWTToken ---------------------------------------------------------------- | |
| @bs.send external nhostGetJWTToken : auth_t => string = "getJWTToken" | |
| let getJWTToken = () : option<string> => { | |
| let token = nhostGetJWTToken(auth) | |
| if token != "" { | |
| Some(token) | |
| } else { | |
| None | |
| } | |
| } | |
| // activate ------------------------------------------------------------------- | |
| @bs.send external nhostActivate : (auth_t, string) => Js.Promise.t<unit> = "activate" | |
| let activate = (ticket: string) : Js.Promise.t<unit> => nhostActivate(auth, ticket) | |
| // onAuthStateChanged --------------------------------------------------------- | |
| type nhostOnAuthStateChangedCb = (bool) => unit | |
| type nhostOnAuthStateUnsuscribed = (unit => unit) | |
| @bs.send external nhostOnAuthStateChanged : | |
| (auth_t, nhostOnAuthStateChangedCb) => nhostOnAuthStateUnsuscribed | |
| = "onAuthStateChanged" | |
| let onAuthStateChanged = (cb : nhostOnAuthStateChangedCb) : nhostOnAuthStateUnsuscribed => nhostOnAuthStateChanged(auth, cb) | |
| // nhostChangePasswordRequest ------------------------------------------------- | |
| @bs.send external nhostChangePasswordRequest : (auth_t, string) => Js.Promise.t<unit> = "changePasswordRequest" | |
| let changePasswordRequest = (email) => nhostChangePasswordRequest(auth, email) | |
| // nhostChangePasswordChange -------------------------------------------------- | |
| @bs.send external nhostChangePasswordChange : (auth_t, string, string) => Js.Promise.t<unit> = "changePasswordChange" | |
| let changePasswordChange = (email, ticket) => nhostChangePasswordChange(auth, email, ticket) | |
| // (simple) EXAMPLE, where this file is called Nhost.res: | |
| /* | |
| let authContext : React.Context.t<bool> = React.createContext(false) | |
| module AuthProvider = { | |
| let makeProps = (~value, ~children, ()) => { | |
| "value": value, | |
| "children": children, | |
| }; | |
| let make = React.Context.provider(authContext); | |
| } | |
| module Provider = { | |
| @react.component | |
| let make = (~children) => { | |
| let (loggedIn, setLoggedIn) = React.useState(() => false) | |
| React.useEffect0(() => { | |
| let unsubscribe = Nhost.onAuthStateChanged((loggedIn: bool) => { | |
| setLoggedIn(_ => loggedIn) | |
| }) | |
| Some(unsubscribe) | |
| }) | |
| <AuthProvider value={}> | |
| {children} | |
| </UserProvider> | |
| } | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment