Skip to content

data-fetch-ts is a TypeScript-based library for making HTTP requests with support for different methods (GET, POST, PUT, DELETE, PATCH). It simplifies fetching data from APIs by providing a unified interface for various HTTP methods and optional token-based authentication.

Notifications You must be signed in to change notification settings

Soum-ik/data-fetch

Repository files navigation

data-fetch-ts

Description

data-fetch-ts is a TypeScript-based library for making HTTP requests with support for different methods (GET, POST, PUT, DELETE, PATCH). It simplifies fetching data from APIs by providing a unified interface for various HTTP methods and optional token-based authentication.

Installation

To install data-fetch-ts, run the following command:

npm install data-fetch-ts
bun install data-fetch-ts

Dependencies

This package does not have any external dependencies.

Changelog

v1.5.2

  • Initial release with support for GET, POST, PUT, DELETE, and PATCH methods.

Benefits

  • TypeScript Support: Strong typing ensures better code quality and fewer runtime errors.
  • Unified Interface: Simplifies HTTP requests by providing a single function to handle various methods.
  • Token-based Authentication: Easily include authorization tokens in your requests.
  • Flexible Options: Support for optional body and headers, making it adaptable to different API requirements.
  • Caching Control: Allows specifying caching strategies for improved performance.

API Doc

fetchData

Parameters

  • endpoint : string - The URL endpoint to send the request to.
  • method : Methods - The HTTP method to use (GET, POST, PUT, DELETE, PATCH). Default is GET
  • token : string - (optional) - The token for authorization (if required).
  • body : string - (optional) - The body of the request. Required for POST, PUT, and PATCH methods.
  • cash : cache - (optional) - The caching mode to use.

Returns

  • Promise<any> - A promise that resolves with the response data in JSON format.

Usecase

Here is an example of using the fetchData function to fetch and log the length of the data:

Simple Get Request

import fetchData from 'data-fetch-ts';

const data = async () => {
    const endpoint = 'https://jsonplaceholder.typicode.com/todos';
    const res = await fetchData({ endpoint });
    console.log(res.length);
};

data();

Post Request

import fetchData from 'data-fetch-ts';

const data = async () => {
    const endpoint = 'https://jsonplaceholder.typicode.com/todos';
    const body = { 'name' : 'soumik', 'email' : `01754759169`, };
    const res = await fetchData({ endpoint, method : 'POST', body });
    console.log(res.length);
}

when you use post request the body required

PUT Request

import fetchData from 'data-fetch-ts';

const data = async () => {
    const endpoint = 'https://jsonplaceholder.typicode.com/todos';
    const body = { 'name' : 'soumik', 'email' : `01754759169`, 'age' : '19' };
    const token = 'dsjlfjalkfjdalskdfjaskldfjklutwiueiweojflaskdjfasdfjsdfk_jsldfjskldfj' 
    const res = await fetchData({ endpoint, method : 'PUT', body, token });
    console.log(res.length);
}

If a token is required, you don't need to do a lot of work; just pass the token. Authorization and Bearer are already set, so you just need to pass the token.

Recive your token

# extract token from headers
const extractToken = (req, res, next) => {
    const authHeader = req.headers['authorization'];

    if (authHeader) {
        const token = authHeader.split(' ')[1]; // Assuming 'Bearer <token>'
        req.token = token; // Attach the token to the request object
    } else {
        req.token = null;
    }

    next();
};

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Maintainers

Soumik Sarkar - [email protected]

Like

If you like this package, please give it a star.

About

data-fetch-ts is a TypeScript-based library for making HTTP requests with support for different methods (GET, POST, PUT, DELETE, PATCH). It simplifies fetching data from APIs by providing a unified interface for various HTTP methods and optional token-based authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published