simpler-api v1.0.1
Simpler API
Simpler API is a simple and intuitive library to make HTTP requests like GET, POST, PUT, and DELETE. The goal of this package is to simplify the process of working with APIs by using easy-to-remember function names such as fetch
, send
, modify
, and remove
.
Features
- Easy-to-use functions for making HTTP requests.
- Supports GET, POST, PUT, and DELETE HTTP methods.
- Includes options for query parameters, custom headers, and timeout handling.
- Returns parsed JSON data for the responses.
Installation
To install the simpler-api
package, run the following command in your project directory:
npm install simpler-api
Usage
Importing the package
After installing the package, you can import the functions into your project like so:
import { fetch, send, modify, remove } from 'simpler-api';
Example Usage
GET Request (fetch)
To perform a GET request with optional query parameters:
import { fetch } from 'simpler-api';
fetch('https://jsonplaceholder.typicode.com/posts', {
params: { userId: 1 } // Add query parameters if needed
})
.then(data => console.log('Fetched Data:', data))
.catch(error => console.error('Error:', error));
POST Request (send)
To make a POST request with a JSON body:
import { send } from 'simpler-api';
send('https://jsonplaceholder.typicode.com/posts', {
title: 'New Post',
body: 'This is a new post!'
})
.then(data => console.log('Posted Data:', data))
.catch(error => console.error('Error:', error));
PUT Request (modify)
To make a PUT request (or PATCH if needed) to update an existing resource:
import { modify } from 'simpler-api';
modify('https://jsonplaceholder.typicode.com/posts/1', {
title: 'Updated Title'
})
.then(data => console.log('Modified Post:', data))
.catch(error => console.error('Error:', error));
DELETE Request (remove)
To delete a resource:
import { remove } from 'simpler-api';
remove('https://jsonplaceholder.typicode.com/posts/1')
.then(data => console.log('Deleted Post:', data))
.catch(error => console.error('Error:', error));
Options
Each request function (fetch, send, modify, remove) accepts an optional options
parameter. The options
object can contain:
- params: An object to append query parameters to the URL.
- headers: Additional headers for the request (e.g., authentication tokens).
- timeout: Time in milliseconds before the request is aborted (default is 5000ms).
Example with Options:
import { fetch } from 'simpler-api';
fetch('https://jsonplaceholder.typicode.com/posts', {
params: { userId: 1 },
timeout: 7000,
headers: { 'Authorization': 'Bearer YOUR_TOKEN' }
})
.then(data => console.log('Fetched Data with Options:', data))
.catch(error => console.error('Error:', error));
Contributing
We welcome contributions to this project. If you have any ideas, bug fixes, or improvements, feel free to fork the repository, create a new branch, and submit a pull request. If you encounter any issues, please open an issue in the repository.
Steps to Contribute:
- Fork the repository.
- Clone your forked repository to your local machine.
- Create a new branch for your changes.
- Make your changes and commit them with clear messages.
- Push your changes to your forked repository.
- Submit a pull request to the main repository.
License
This project is licensed under the ISC License - see the LICENSE file for details.
Example Test Code
Here’s a simple example to test the simpler-api functions:
import { fetch, send, modify, remove } from 'simpler-api';
// Example GET request
fetch('https://jsonplaceholder.typicode.com/posts', {
params: { userId: 1 }, // Adding query parameters
})
.then((data) => console.log('Fetched Data:', data))
.catch((error) => console.error('Error:', error));
// Example POST request
send('https://jsonplaceholder.typicode.com/posts', {
title: 'New Post',
body: 'This is a new post!',
})
.then((data) => console.log('Posted Data:', data))
.catch((error) => console.error('Error:', error));
// Example PUT request
modify('https://jsonplaceholder.typicode.com/posts/1', {
title: 'Updated Title',
})
.then((data) => console.log('Modified Post:', data))
.catch((error) => console.error('Error:', error));
// Example DELETE request
remove('https://jsonplaceholder.typicode.com/posts/1')
.then((data) => console.log('Deleted Post:', data))
.catch((error) => console.error('Error:', error));