What is get-it?
get-it is a JavaScript HTTP client for making requests to APIs. It is designed to be modular and extensible, allowing developers to customize its behavior through plugins.
What are get-it's main functionalities?
Basic GET Request
This feature allows you to make a basic GET request to a specified URL. The response is handled using Promises.
const getIt = require('get-it');
const request = getIt();
request({ url: 'https://api.example.com/data' })
.then(response => console.log(response))
.catch(error => console.error(error));
Using Plugins
get-it supports middleware plugins to extend its functionality. In this example, the jsonResponse plugin is used to automatically parse JSON responses.
const getIt = require('get-it');
const jsonResponse = require('get-it/lib/middleware/jsonResponse');
const request = getIt([jsonResponse()]);
request({ url: 'https://api.example.com/data' })
.then(response => console.log(response))
.catch(error => console.error(error));
Custom Headers
You can add custom headers to your requests. This is useful for adding authentication tokens or other custom headers required by the API.
const getIt = require('get-it');
const request = getIt();
request({
url: 'https://api.example.com/data',
headers: { 'Authorization': 'Bearer token' }
})
.then(response => console.log(response))
.catch(error => console.error(error));
Other packages similar to get-it
axios
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and supports interceptors, which are similar to get-it's plugins. Axios is widely used and has a large community.
node-fetch
node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is a minimalistic library that focuses on providing a Fetch API in Node.js environments. Unlike get-it, it does not have built-in support for plugins or middleware.
superagent
Superagent is a small progressive client-side HTTP request library, and Node.js module with a lot of features. It supports a wide range of HTTP methods and has a plugin system similar to get-it. Superagent is known for its flexibility and ease of use.
get-it
Wanted features
Options
rawBody
- Set to true
to return the raw value of the response body, instead of a string. Important note: The returned body will be different in Node and browser environments. In Node, it will return a Buffer
, while in browsers it will return an ArrayBuffer
.
Middleware
Each middleware is an object of hook => action bindings. They are called in the order they are added to the request instance using request.use()
. For instance, if you want to always set a certain header on outgoing responses, you could do:
const isAwesome = {
processOptions: options => Object.assign({
headers: Object.assign({}, options.headers, {
'X-Is-Awesome': 'Absolutely'
})
})
}
request.use(isAwesome)
The available hooks, their arguments and expected return values are as following:
processOptions
Called once a new request is instantiated. Can be used to alter options before they are validated and turned into an actual HTTP request. This hook is used by the base
middleware, which prefixes the passed URL if it is not an absolute URI already.
Arguments:
options
- Object of request options passed to the request. Should be cloned if modifications are to be performed, to prevent unexpected side-effects.
Should return: Plain object of options to pass on to the rest of the middlewares.
parseResponse
Called once a response has been received and the response body is ready.
@todo document
Prior work
This module was inspired by the great work of others:
License
MIT-licensed. See LICENSE.