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

codewithdeepak.fetch

v1.0.7

Published

`codewithdeepak.fetch` is a simple HTTP request library built with TypeScript and Axios. It provides an easy-to-use `fetch` function for making HTTP requests with support for different HTTP methods and custom headers.

Downloads

36

Readme

codewithdeepak.fetch

codewithdeepak.fetch is a simple HTTP request library built with TypeScript and Axios. It provides an easy-to-use fetch function for making HTTP requests with support for different HTTP methods and custom headers.

Installation

You can install codewithdeepak.fetch using npm:

npm install codewithdeepak.fetch

Usage

JavaScript

To use the package in a JavaScript environment:

GET Request

const { fetch } = require('codewithdeepak.fetch');

(async () => {
    try {
        const data = await fetch({
            url: 'https://api.github.com/repos/nodejs/node',
            method: 'GET'
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

POST Request with JSON Data

const { fetch } = require('codewithdeepak.fetch');

(async () => {
    try {
        const data = await fetch({
            url: 'https://jsonplaceholder.typicode.com/posts',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                title: 'foo',
                body: 'bar',
                userId: 1
            }
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

POST Request with Form Data

const { fetch } = require('codewithdeepak.fetch');
const FormData = require('form-data');

(async () => {
    try {
        const form = new FormData();
        form.append('username', 'example');
        form.append('password', '123456');

        const data = await fetch({
            url: 'https://example.com/api/login',
            method: 'POST',
            headers: {
                ...form.getHeaders()
            },
            data: form
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

TypeScript

To use the package in a TypeScript environment:

GET Request

import { fetch } from 'codewithdeepak.fetch';

(async () => {
    try {
        const data = await fetch({
            url: 'https://api.github.com/repos/nodejs/node',
            method: 'GET'
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

POST Request with JSON Data

import { fetch } from 'codewithdeepak.fetch';

(async () => {
    try {
        const data = await fetch({
            url: 'https://jsonplaceholder.typicode.com/posts',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                title: 'foo',
                body: 'bar',
                userId: 1
            }
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

POST Request with Form Data

import { fetch } from 'codewithdeepak.fetch';
import FormData from 'form-data';

(async () => {
    try {
        const form = new FormData();
        form.append('username', 'example');
        form.append('password', '123456');

        const data = await fetch({
            url: 'https://example.com/api/login',
            method: 'POST',
            headers: {
                ...form.getHeaders()
            },
            data: form
        });
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

API

fetch(options: FetchOptions): Promise<any>

Makes an HTTP request based on the provided options.

Parameters

  • url (string): The URL to which the request is made.
  • method (string, optional): The HTTP method to use. Defaults to 'GET'. Can be 'POST', 'PUT', or 'DELETE'.
  • headers (Record<string, string>, optional): Custom headers to include in the request.
  • data (any, optional): The request payload for POST or PUT requests.

Returns

A Promise that resolves with the response data. In case of an error, it returns an object with the following properties:

  • error (string): A description of the error.
  • status (number, optional): The HTTP status code.
  • data (any, optional): The error response data.

Development

To build the package from source:

npm run build

To run tests (assuming you have a test file named test-fetch.ts):

npm test

License

This package is licensed under the ISC License.

Contributing

If you'd like to contribute to this project, please fork the repository and submit a pull request with your changes. For detailed contribution guidelines, please refer to the project's repository.


For more information, please refer to the Axios documentation and TypeScript documentation.


### Key Additions

1. **POST Request with JSON Data**: Shows how to send JSON payloads with `POST` requests.
2. **POST Request with Form Data**: Demonstrates sending form data, which is useful for scenarios where you need to send data in `application/x-www-form-urlencoded` or `multipart/form-data` formats.
3. **Headers Management**: Shows how to include custom headers in requests.

This extended README provides more comprehensive examples for using `POST` requests with different types of payloads, which should help users understand how to interact with different types of APIs effectively.