1.0.7 • Published 6 months ago
nextjs-api-lib v1.0.7
Axios API Client Library
This library provides a robust and flexible API client built on top of Axios, designed to handle HTTP requests with features like encryption, authentication, and error handling. It is suitable for use in a variety of projects, including web applications, Node.js services, and more.
Features
- Axios Instance Creation: Easily create and configure Axios instances with custom settings.
- Interceptors: Request and response interceptors for handling encryption and retry logic.
- Token Management: Manage authentication tokens and handshake tokens.
- Error Handling: Centralized error handling for API requests.
- Debugging: Detailed logging for development and debugging.
Installation
To install the library, use npm or yarn:
npm install nextjs-api-lib
# or
yarn add nextjs-api-lib
Usage
Setting Up the API Client
To set up the API client, provide a configuration object to customize the client's behavior to suit your project's needs.
import { ApiClient, ApiClientConfig } from 'your-library-name';
const config: ApiClientConfig = {
baseURL: 'https://api.example.com',
timeout: 5000,
headers: {
'Custom-Header': 'value'
},
handshake: {
enabled: true,
type: 'user',
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
},
encryption: {
enabled: true,
secretKey: 'your-secret-key',
ivKey: 'your-iv-key'
},
storage: {
enabled: true,
type: 'localStorage'
},
debug: true
};
const apiClient = new ApiClient(config);
Making HTTP Requests
Once the client is set up, you can make HTTP requests using the provided methods. These methods return promises, making them easy to integrate with async/await syntax.
import { ApiClient, ApiClientConfig } from 'your-library-name';
const config: ApiClientConfig = {
baseURL: 'https://api.example.com',
timeout: 5000,
headers: {
'Custom-Header': 'value'
},
handshake: {
enabled: true,
type: 'user',
clientId: 'your-client-id',
clientSecret: 'your-client-secret'
},
encryption: {
enabled: true,
secretKey: 'your-secret-key',
ivKey: 'your-iv-key'
},
storage: {
enabled: true,
type: 'localStorage'
},
debug: true
};
const apiClient = new ApiClient(config);
Authentication
The library provides methods to manage authentication tokens. Set an authentication token like this.
apiClient.setAuthToken('your-auth-token');