What is ofetch?
ofetch is a modern, lightweight HTTP client for making network requests in JavaScript. It is designed to be simple and easy to use, while providing powerful features for handling HTTP requests and responses.
What are ofetch's main functionalities?
Basic GET Request
This feature allows you to make a basic GET request to a specified URL and handle the response. The response data is logged to the console.
const { $fetch } = require('ofetch');
(async () => {
const data = await $fetch('https://api.example.com/data');
console.log(data);
})();
POST Request with JSON Body
This feature allows you to make a POST request with a JSON body. The response data is logged to the console.
const { $fetch } = require('ofetch');
(async () => {
const data = await $fetch('https://api.example.com/data', {
method: 'POST',
body: JSON.stringify({ key: 'value' })
});
console.log(data);
})();
Custom Headers
This feature allows you to add custom headers to your HTTP requests. The response data is logged to the console.
const { $fetch } = require('ofetch');
(async () => {
const data = await $fetch('https://api.example.com/data', {
headers: {
'Authorization': 'Bearer token',
'Content-Type': 'application/json'
}
});
console.log(data);
})();
Handling Errors
This feature demonstrates how to handle errors that may occur during an HTTP request. If an error occurs, it is caught and logged to the console.
const { $fetch } = require('ofetch');
(async () => {
try {
const data = await $fetch('https://api.example.com/data');
console.log(data);
} catch (error) {
console.error('Error:', error);
}
})();
Other packages similar to ofetch
axios
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a rich set of features for making HTTP requests, including support for interceptors, request and response transformation, and automatic JSON parsing. Compared to ofetch, Axios has a larger community and more extensive documentation.
node-fetch
node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is a minimalistic library that provides a simple API for making HTTP requests. While it is similar to ofetch in terms of simplicity, node-fetch is more focused on providing a polyfill for the Fetch API in Node.js environments.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It supports a wide range of features, including retries, streams, and advanced error handling. Compared to ofetch, Got offers more advanced features and is designed for more complex use cases.
oFetch
Add type-safety to your function calls, but only when you feel like it.
oFetch(person, "firstName")
is the same as person.firstName
, except that it will throw an exception if person.firstName
is undefined.
(I don't think I'll be adding support for checking specific types, but you can try using Joi.)
Installation:
Install oFetch using NPM: npm install o-fetch --save
More examples
var oFetch = require("o-fetch");
// Fetch multiple keys (using ES2015 destructuring syntax)
var [firstName, lastName] = oFetch(person, "firstName", "lastName");
// Fetch a nested value
var countryCode = oFetch(person, "address.country.countryCode");
Contributing
Install jasmine-node (npm install -g jasmine-node
), then run jasmine-node spec/
.