1.0.1 • Published 7 months ago

simpler-api v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

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:

  1. Fork the repository.
  2. Clone your forked repository to your local machine.
  3. Create a new branch for your changes.
  4. Make your changes and commit them with clear messages.
  5. Push your changes to your forked repository.
  6. 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));
1.0.1

7 months ago

1.0.0

7 months ago