rfetchapi
v1.0.2
Published
A simple utility for making API calls using fetch in JavaScript or TypeScript
Downloads
6
Maintainers
Readme
ApiHelper
ApiHelper is a simple utility for making API calls using fetch in JavaScript or TypeScript. It provides easy-to-use shorthand methods for common HTTP methods such as GET, POST, PUT, and DELETE, and allows you to configure a base URL for your API requests.
Installation
You can install the ApiHelper
package via npm:
npm install rfetchapi
Usage
To use the ApiHelper
in your project, import it as follows:
import ApiHelper from 'rfetchapi';
Configuration
Before making any API calls, you can configure a base URL by calling the config
method:
ApiHelper.config("https://api.example.com/");
Making API Calls
GET Request
To make a GET request, use the get
method:
ApiHelper.get("endpoint", "your_access_token_here")
.then((response) => {
// Handle the response
console.log(response);
})
.catch((error) => {
// Handle errors
console.error(error);
});
POST Request
To make a POST request, use the post
method:
const postData = {
method: "POST",
formData: { key: "value" },
typecontent: false,
token: "your_access_token_here",
};
ApiHelper.post("endpoint", postData)
.then((response) => {
// Handle the response
console.log(response);
})
.catch((error) => {
// Handle errors
console.error(error);
});
PUT Request
To make a PUT request, use the put
method:
const putData = {
method: "PUT",
formData: { key: "updated_value" },
typecontent: false,
token: "your_access_token_here",
};
ApiHelper.put("endpoint", putData)
.then((response) => {
// Handle the response
console.log(response);
})
.catch((error) => {
// Handle errors
console.error(error);
});
DELETE Request
To make a DELETE request, use the del
method:
ApiHelper.del("endpoint", "your_access_token_here")
.then((response) => {
// Handle the response
console.log(response);
})
.catch((error) => {
// Handle errors
console.error(error);
});
License
This package is open-source and available under the MIT License.
Contribution
Contributions are welcome! If you find any bugs or want to suggest improvements, please feel free to create an issue or submit a pull request.