A rescript library to query remote api

Clone this repo:

Branches

  1. 1d2f904 Catch network exception and improve error message by Tristan Cacqueray · 2 years ago master
  2. d182613 Re-Export RemoteData and improve the documentation by Tristan Cacqueray · 2 years ago
  3. 04fd531 usePost: drop support for optional decoder by Tristan Cacqueray · 2 years ago
  4. 5e17bee package.json: define peerDependencies to ensure consistent react version by Tristan Cacqueray · 2 years ago
  5. 889bbeb useGet: refresh when the url change by Tristan Cacqueray · 2 years ago

res-remoteapi

Remote API React hook for rescript

npm add @softwarefactory-project/res-remoteapi

When using pnpm, you also need to add the dependencies to your package:

pnpm add bs-fetch remotedata-re @rescript/react react@^16.14.0

Add to your bsconfig.json:

"bs-dependencies": [
+  "@softwarefactory-project/res-remoteapi"
]

Usage

// Your application:
module Main = (CLIENT: RemoteAPI.HTTPClient) => {
  module Hook = RemoteAPI.Hook(CLIENT)

  @react.component
  let make = () => {
    let state = Hook.useAutoGet("/api", api_decoder)
    switch (state) {
      | RemoteData.NotAsked
      | RemoteData.Loading(None) => <p> {"Loading..." |> str} </p>
      | RemoteData.Loading(Some(info))
      | RemoteData.Success(info) => <p> {("Loaded: " ++ info.version) -> React.string} </p>
      | RemoteData.Failure(title) => <p> {("Failure: " ++ title) -> React.string} </p>
    };
  }
}

Contribute

Contributions are most welcome.

Get started by running:

git clone https://github.com/softwarefactory-project/res-remoteapi
cd re-ansi
pnpm install
pnpm start

Then build and run tests with pnpm test.

Make sure to read about [React][reason-react] and ReScript too.

Changes

0.1.0

  • Initial release