@swlkr/req

2.1.3 • Public • Published

req

Zero dependency json http client for the browser

Install

$ yarn add @swlkr/req

Use

import Http from "@swlkr/req";

// Get request
Http
.send({ url: "http://your-api.com/some-url", method: "get" })
.then(res => console.log(res))
.catch(err => console.log(err))

// Post request
Http
.send({ url: "http://your-api.com/some-url", body: { prop: "value", prop1: "value1", prop2: "value2" }, method: "POST" })
.then(res => console.log(res))
.catch(err => console.log(err))

// Custom headers with delete request
Http
.send({ url: "http://your-api.com/some-url", method: "delete", headers: { Authorization: "super secure json web token" } })
.then(res => console.log(res))
.catch(err => console.log(err))

// Using async/await
try {
  const response = await Http.send({ url: "http://your-api.com/some-url", method: "get" });
  // use response
} catch(error) {
  console.log(error.message);
  console.log(error.status);
}

// Handling the unfortunate event of a timeout
// timeout should be an integer that represents milliseconds
Http
.send({ url: "http://your-api.com/some-url", method: "GET", timeout: 1000 })
.then(res => console.log(res))
.catch(err => console.log(err))

Test

yarn
yarn test

Package Sidebar

Install

npm i @swlkr/req

Weekly Downloads

0

Version

2.1.3

License

MIT

Last publish

Collaborators

  • swlkr