npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-fetch-ts

v1.5.2

Published

`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 option

Downloads

14

Readme

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.0.0

  • 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.