Ohey is a promise-based JavaScript library built on top of XMLHttpRequest. It simplifies making HTTP requests by providing a modern, promise-based API for GET, POST, PUT, and DELETE methods.
npm i ohey
import ohey from 'ohey';
To make a simple GET request:
const fetchData = async () => {
const res = await ohey("https://jsonplaceholder.typicode.com/todos");
console.log(res);
};
fetchData();
You can pass an optional configuration object to customize your request:
const fetchData = async () => {
const res = await ohey("https://jsonplaceholder.typicode.com/todos", {
headers: {
"Content-Type": "application/json"
},
timeout: 5000,
method: "GET"
});
console.log(res);
};
fetchData();
To make a POST request with a payload:
const postData = async () => {
const res = await ohey("https://jsonplaceholder.typicode.com/todos", {
headers: {
"Content-Type": "application/json"
},
timeout: 5000,
method: "POST",
body: JSON.stringify({
title: "foo",
body: "bar",
userId: 1
})
});
console.log(res);
};
postData();
To update data using a PUT request:
const updateData = async () => {
const res = await ohey("https://jsonplaceholder.typicode.com/todos/1", {
headers: {
"Content-Type": "application/json"
},
timeout: 5000,
method: "PUT",
body: JSON.stringify({
id: 1,
title: "foo",
body: "bar",
userId: 1
})
});
console.log(res);
};
updateData();
To delete data using a DELETE request:
const deleteData = async () => {
const res = await ohey("https://jsonplaceholder.typicode.com/todos/1", {
headers: {
"Content-Type": "application/json"
},
timeout: 5000,
method: "DELETE"
});
console.log(res);
};
deleteData();
The configuration object supports the following options:
-
headers: An object representing custom headers to be sent with the request.
-
timeout: The time in milliseconds before the request times out.
-
method: The HTTP method to use (e.g., GET, POST, PUT, DELETE).
-
body: The body of the request. Typically used with POST and PUT requests to send data.
Ohey will reject the promise with an error if the request fails or times out. You can handle errors using a try-catch block:
const fetchData = async () => {
try {
const res = await ohey("https://jsonplaceholder.typicode.com/todos");
console.log(res);
} catch (error) {
console.error("Error fetching data:", error);
}
};
fetchData();
Ohey is licensed under the MIT License.