This guide provides instructions on how to use the APIClass for making HTTP requests. The class utilizes Request(Fetch) for handling requests and includes methods for GET, POST, PUT, PATCH, and DELETE operations.
Before using the APIClass, ensure you have @koadz/fetch installed:
npm install @koadz/fetch@latest
Import the APIClass or API in your project:
import { API, APIClass } from "@koadz/fetch";
To retrieve data from a specified URL:
API.get("https://api.example.com/data")
.then((response) => console.log(response.data))
.catch((error) => console.error(error));
To send data to a specified URL:
const data = { key: "value" };
API.post("https://api.example.com/data", data)
.then((response) => console.log(response.data))
.catch((error) => console.error(error));
To update data at a specified URL:
const data = { key: "new value" };
API.put("https://api.example.com/data/1", data)
.then((response) => console.log(response.data))
.catch((error) => console.error(error));
To partially update data at a specified URL:
interface MyData {
name: string;
}
const data: MyData = { key: "new value" };
API.patch<MyData>("https://api.example.com/data/1", data)
.then((response) => console.log(response.data)) // response.data is of type MyData
.catch((error) => console.error(error));
To delete data at a specified URL:
API.delete("https://api.example.com/data/1")
.then((response) => console.log("Deleted successfully"))
.catch((error) => console.error(error));
Errors are handled using the Custom Fetch error handling mechanisms.
You can customize default settings by passing configuration options to the APIClass constructor:
Order of usage:
BaseConfig>RequestSpecificConfig>InterceptorsConfig
const customConfig = {
baseURL: "https://api.example.com",
headers: {
"Content-Type": "application/json",
},
};
const API = new APIClass(customConfig);
The APIClass supports interceptors for request and response manipulation. You can define your own interceptors and add them to the interceptors property of the APIClass instance.
Note: Interceptors will be executed in the order they are defined.
This example shows how to add interceptors from the configuration object:
const customConfig: FetchRequestOptions = {
baseURL: "https://api.example.com",
interceptors: {
request: (config) => {
console.log(`Requesting ${config.method} ${config.url}`);
return config;
},
response: (response) => {
console.log(`Response status: ${response.status}`);
console.log(`Response data: ${JSON.stringify(response.data)}`);
return response;
},
},
};
const API = new APIClass(customConfig);
This example shows how to add a request interceptor that logs the request URL and method:
API.interceptors.addRequestInterceptor((config) => {
console.log(`Requesting ${config.method} ${config.url}`);
return config;
});
This example shows how to add a response interceptor that logs the response status and data:
API.interceptors.addResponseInterceptor((response) => {
console.log(`Response status: ${response.status}`);
console.log(`Response data: ${JSON.stringify(response.data)}`);
return response;
});
The APIClass also provides features like:
The APIClass offers a convenient and flexible way to make HTTP requests in your JavaScript applications. It simplifies the process of making requests and provides various features for customization and error handling. With TypeScript support and the ability to use interceptors, the APIClass is a powerful tool for building robust and maintainable applications.