paado
v1.1.0
Published
Paado - Simplify API Requests with Ease Paado is an API helper package designed to make working with APIs more intuitive and efficient by leveraging the power of Axios. Inspired by the metaphor of an API acting like a waiter, the package's name, "Paado,"
Downloads
24
Readme
Paado
Paado is a lightweight JavaScript library designed to simplify making HTTP requests to a server while managing authentication tokens efficiently. It utilizes Axios under the hood for making requests, allowing for a seamless integration into your front-end projects.
Features
- Automatically retrieves authentication tokens from
localStorage
orcookies
. - Configurable base URL and timeout settings.
- Customizable headers for each request.
Installation
You can install Paado using npm:
npm install paado
Usage
Basic Setup
To get started with Paado, you first need to configure it with your desired settings. Here’s a quick example of how to do that:
import { configure, request } from 'paado';
// Set configuration
configure({
baseURL: 'https://api.example.com',
timeout: 15000,
tokenName: 'authToken',
tokenKey: 'Bearer',
tokenLocation: 'localStorage',
headers: {
'Custom-Header': 'value'
}
});
Making Requests
You can make requests using the request
function. Here’s an example of how to perform a GET and POST request:
GET Request
async function fetchData() {
try {
const data = await request('GET', '/data-endpoint');
console.log(data);
} catch (error) {
console.error('Error fetching data:', error);
}
}
fetchData();
POST Request
async function postData() {
try {
const response = await request('POST', '/submit-data', { key: 'value' });
console.log(response);
} catch (error) {
console.error('Error posting data:', error);
}
}
postData();
Configuration
You can customize the configuration using the configure
function. The configuration object can contain the following fields:
baseURL
: (string) The base URL for all requests.timeout
: (number) The timeout for requests in milliseconds (default: 10000).tokenName
: (string) The name of the token to retrieve fromlocalStorage
or cookies (default: 'authToken').tokenKey
: (string) The prefix for the token in the Authorization header (default: 'Bearer').tokenLocation
: (string) Where to retrieve the token from (localStorage
orcookie
) (default: 'localStorage').headers
: (object) Additional headers to be included in every request.
Example Configuration
configure({
baseURL: 'https://api.example.com',
tokenLocation: 'cookie', // or 'localStorage'
});