π Nexios: The Ultimate Fetch Library for Next.js π
Welcome to Nexios, a cutting-edge fetch library crafted for seamless integration with Next.js applications. With Nexios, you can enhance your data-fetching capabilities using advanced features like customizable interceptors and default configurations. This library is the perfect companion for modern web development, enabling you to build robust, efficient applications with ease.
π Features
- π§ Customizable Interceptors: Effortlessly modify requests and responses to meet your needs.
- βοΈ Configurable Defaults: Set global and instance-specific defaults for consistent behavior.
- π Secure Token Management: Automatically include authentication tokens for secure API access.
- π Response Transformation: Adjust and manipulate response data as required.
- π Tailored for Next.js: Integrate smoothly with Next.js environments to maximize performance.
π¦ Installation
Add Nexios to your Next.js project using npm or yarn:
npm install nexios-http
yarn add nexios-http
π οΈ Getting Started
1. Create a Nexios Configuration File
Create a configuration file to set up your Nexios instance with default settings and interceptors. This file will manage global configurations and interceptors for all requests.
nexios.config.ts
import { Nexios } from "nexios-http";
import { NexiosOptions } from "nexios-http/types/interfaces";
import { cookies } from "next/headers";
const defaultConfig: NexiosOptions = {
baseURL: "https://jsonplaceholder.typicode.com",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
credentials: "include",
timeout: 10000,
};
const nexiosInstance = new Nexios(defaultConfig);
nexiosInstance.interceptors.request.use((config) => {
const accessToken = cookies().get("accessToken")?.value;
if (accessToken) {
config.headers = {
...config.headers,
Authorization: `Bearer ${accessToken}`,
};
}
return config;
});
nexiosInstance.interceptors.response.use((response) => {
return response;
});
export default nexiosInstance;
2. Use Nexios in Your Next.js Pages
Now that you have a configured Nexios instance, you can use it in your Next.js pages to make HTTP requests.
page.tsx
import nexiosInstance from "@/config/nexios.config";
export default async function Home() {
const response = await nexiosInstance.get("/todos/1", {
cache: "no-store",
});
console.log(response.data);
return <div>Hello</div>;
}
3. Handle Errors Gracefully
Catch and handle errors effectively to enhance user experience.
Example Error Handling
import nexiosInstance from "@/config/nexios.config";
export default async function Home() {
try {
const response = await nexiosInstance.get("/todos/1", {
cache: "no-store",
});
console.log(response.data);
} catch (error) {
console.error("Error fetching data:", error.message);
}
return <div>Hello</div>;
}
π API Documentation
Nexios Class
- Constructor:
constructor(config: NexiosOptions = {})
- config: Optional configuration object to initialize the instance.
Static Methods
- setGlobalDefaults(defaults: NexiosOptions): Set global configuration defaults for all Nexios instances.
- getGlobalDefaults(): NexiosOptions: Retrieve global configuration defaults.
Instance Methods
- setDefaults(defaults: NexiosOptions): Set instance-specific defaults.
- addRequestInterceptor(interceptor: RequestInterceptor): Add a request interceptor.
- addResponseInterceptor(interceptor: ResponseInterceptor): Add a response interceptor.
- get(url: string, options?: NexiosOptions): Perform a GET request.
- post(url: string, body: any, options?: NexiosOptions): Perform a POST request.
- put(url: string, body: any, options?: NexiosOptions): Perform a PUT request.
- delete(url: string, options?: NexiosOptions): Perform a DELETE request.
- patch(url: string, body: any, options?: NexiosOptions): Perform a PATCH request.
- head(url: string, options?: NexiosOptions): Perform a HEAD request.
- options(url: string, options?: NexiosOptions): Perform an OPTIONS request.
π‘ Advanced Usage
Modify headers for specific requests:
nexiosInstance.get("/endpoint", { headers: { "X-Custom-Header": "value" } });
Timeout Handling
Set request-specific timeouts:
nexiosInstance.get("/endpoint", { timeout: 5000 });
Handling Caching
Manage caching with the cache option:
nexiosInstance.get("/endpoint", { cache: "no-store" });
π License
Nexios is licensed under the MIT License.
πββοΈ Support
For support, please open an issue on the GitHub repository.
Thank you for choosing Nexios! We hope it simplifies your data-fetching needs and enhances your Next.js development experience. Happy coding! πβ¨