What is @algolia/transporter?
@algolia/transporter is a low-level HTTP client for making requests to Algolia's API. It provides a set of utilities to handle retries, timeouts, and other network-related concerns, making it easier to interact with Algolia's services.
What are @algolia/transporter's main functionalities?
Basic Request
This code demonstrates how to create a basic transporter instance and make a GET request to list all indexes in an Algolia application.
const { createTransporter } = require('@algolia/transporter');
const transporter = createTransporter({
hosts: [{ url: 'https://your-algolia-instance.algolia.net' }],
headers: { 'X-Algolia-API-Key': 'your-api-key', 'X-Algolia-Application-Id': 'your-app-id' }
});
transporter.read({ method: 'GET', path: '/1/indexes' }).then(response => {
console.log(response);
});
Handling Retries
This code shows how to configure the transporter to handle retries with specific timeout settings and retry policies.
const { createTransporter } = require('@algolia/transporter');
const transporter = createTransporter({
hosts: [{ url: 'https://your-algolia-instance.algolia.net' }],
headers: { 'X-Algolia-API-Key': 'your-api-key', 'X-Algolia-Application-Id': 'your-app-id' },
timeouts: { connect: 1, read: 2, write: 30 },
retry: { maxRetries: 3, delay: 1000 }
});
transporter.read({ method: 'GET', path: '/1/indexes' }).then(response => {
console.log(response);
});
Custom Headers
This example demonstrates how to add custom headers to the transporter instance for making requests.
const { createTransporter } = require('@algolia/transporter');
const transporter = createTransporter({
hosts: [{ url: 'https://your-algolia-instance.algolia.net' }],
headers: { 'X-Algolia-API-Key': 'your-api-key', 'X-Algolia-Application-Id': 'your-app-id', 'Custom-Header': 'custom-value' }
});
transporter.read({ method: 'GET', path: '/1/indexes' }).then(response => {
console.log(response);
});
Other packages similar to @algolia/transporter
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and handling responses, including support for interceptors, automatic JSON transformation, and more. Compared to @algolia/transporter, Axios is more general-purpose and not specifically tailored for Algolia's API.
node-fetch
Node-fetch is a lightweight module that brings `window.fetch` to Node.js. It allows you to make HTTP requests in a similar way to the Fetch API in the browser. While it is more low-level compared to @algolia/transporter, it can be used to interact with any HTTP API, including Algolia's.
request-promise-native
Request-promise-native is a simplified HTTP request client for Node.js with Promise support. It is built on top of the `request` library and provides a more modern API for making HTTP requests. Like Axios, it is more general-purpose and not specifically designed for Algolia's API.