1.3.1 • Published 1 day ago

@dck/rescript-ky v1.3.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 day ago

rescript-ky

ReScript bindings for ky HTTP client (targeted version : ~1.2.0)

Setup

  1. Install the module
bun install @dck/rescript-ky
# or
yarn install @dck/rescript-ky
# or
npm install @dck/rescript-ky
  1. Add it to your rescript.json config
{
  "bsc-dependencies": ["@dck/rescript-ky"]
}

Usage

The functions can be accessed through Ky module.

type data = {anything: string}

let fetchSomething = async () => {
  try {
    let response: data = await Ky.fetch("test", {prefixUrl: "https://fake.com", method: GET}).json()
    // handle response data
  } catch {
    | JsError(err) => {
      // handle err
      Js.log(err)
    }
  }
}

Use shortcut method :

type data = {anything: string}

let fetchSomething = async () => {
  try {
    let response: data = await Ky.get("test", {prefixUrl: "https://fake.com"}).json()
    // handle response data
  } catch {
    | JsError(err) => {
      // handle err
      Js.log(err)
    }
  }
}

Instance

let instance = Ky.Instance.create({prefixUrl: "https://fake.com"})

type data = {anything: string}

let fetchSomething = async () => {
  try {
    let response: data = await (instance->Ky.Instance.get("test")).json()
    // handle response data
  } catch {
    | JsError(err) => {
      // handle err
      Js.log(err)
    }
  }
}

Extend

let instance = Ky.Instance.create({prefixUrl: "https://fake.com"})
let extendedInstance = instance->Ky.Instance.extend({
  prefixUrl: `${mockBasePath}/extend`,
  headers: Ky.Headers.fromObj({
    "custom-header": "test",
  }),
})

type data = {anything: string}

let fetchSomething = async () => {
  try {
    let response: data = await (extendedInstance->Ky.Instance.get("test")).json()
    // handle response data
  } catch {
    | JsError(err) => {
      // handle err
      Js.log(err)
    }
  }
}
1.3.1

1 day ago

1.3.0

1 day ago

1.2.1

3 months ago

1.2.0

3 months ago

1.1.0

3 months ago

1.0.0

3 months ago