NexuJS-Client is a lightweight and secure HTTP client library designed to work seamlessly with the Nexu backend library. It handles encrypted communication between the frontend and backend, ensuring your data remains secure.
- Built-in Encryption/Decryption: Automatically encrypts requests and decrypts responses to ensure secure communication.
- Extended Axios Methods: Provides
get
,post
,put
,delete
, andpatch
methods, extended with built-in encryption. - Lightweight and Easy to Use: A simple API to integrate secure communication in your frontend applications.
- Customizable Axios Interceptors: Supports configuring request and response interceptors using
createApiClient
.
Install NexuJS-Client using npm:
npm install nexujs-client
To get started, ensure you have set up the nexu.client
configuration in your project.
Run the following command to generate a basic nexu.client
configuration file:
npx md-config
This will create a constants/nexu.client.js
file in your project directory.
import { NexuClient } from "nexujs-client";
const publicKey = String("your_publicKey");
const privateKey = String("your_privateKey");
const client = new NexuClient({
privateKey,
publicKey,
});
export default client;
To ensure secure communication, use the same encryption keys that were set during the NexuJS server setup. These keys should be stored in environment variables (.env
) for security.
PUBLIC_KEY=your_public_key
PRIVATE_KEY=your_private_key
Here’s an example of using the Post method for making a login request:
import client from "../constants/nexu.client";
const login = async () => {
try {
const response = await client.Post({
url: "http://localhost:8000/auth/login",
data: {
email: "user@example.com",
password: "password123",
},
});
console.log(response);
} catch (error) {
console.error("Error:", error);
}
};
- url: The endpoint to which the request will be sent.
- data: The request body (object).
- config: Optional Axios configuration object.
- url: The endpoint to which the request will be sent.
- data: The request body (object).
- url: The endpoint to which the request will be sent.
- data: The request body (object).
- config: Optional Axios configuration object.
- url: The endpoint to which the request will be sent.
- data: The request body (object).
- config: Optional Axios configuration object.
The createApiClient
function is a utility for setting up Axios interceptors. It allows you to add custom request and response interceptors to handle encryption and decryption automatically.
import { NexuClient } from "nexujs-client";
const publicKey = String("your_publicKey");
const privateKey = String("your_privateKey");
const client = new NexuClient({
privateKey,
publicKey,
});
const apiClient = client.createApiClient({
baseURL: "http://localhost:8000",
onRequest: (config) => {
console.log("Request Interceptor:", config);
return config;
},
onResponse: (response) => {
console.log("Response Interceptor:", response);
return response;
},
onResponseError(error) {
console.log("Something went wrong", error.message);
},
});
export default apiClient;
const fetchUsers = async () => {
const users = await apiClient.Get({ url: "/users" });
console.log(users);
};