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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nextclient

v1.1.15

Published

use http request in very easy way

Downloads

101

Readme

NextClient

NextClient is a TypeScript-based HTTP client designed for making API requests with support for JSON and FormData. It provides a simple and intuitive interface for interacting with RESTful APIs.

Table of Contents

Installation

You can install NextClient using npm or yarn. Run the following command in your terminal:

npm install nextclient

or

yarn add nextclient

Usage

Creating an Instance

To create an instance of NextClient, you need to provide a configuration object with the base URL and optional headers.

import { NextClient } from "nextclient";

const nextClient = new NextClient({
  baseUrl: "https://api.example.com",
  debug: true,
  headers: {
    "Content-Type": "application/json",
  },
});

Making Requests

You can make various types of HTTP requests using the methods provided by NextClient.

GET Request

nextClient
  .get("/endpoint")
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

POST Request

nextClient
  .post("/upload")
  .json({ key: "value" })
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

PUT Request

nextClient
  .put("/update/1")
  .json({ key: "newValue" })
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

PATCH Request

nextClient
  .patch("/update/1")
  .json({ key: "updatedValue" })
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

DELETE Request

nextClient
  .delete("/delete/1")
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

File Uploads

To upload files, you can use the form method to create a form data request. Here's how to do it:

const fileInput = document.querySelector(
  'input[type="file"]'
) as HTMLInputElement;

if (fileInput.files) {
=
  nextClient
    .post("/upload")
    .form({
      file: fileInput.files[0],
    })
    .send()
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      handleNextError(error, (res) => {
        console.error(res);
      });
    });
}

Setting Headers

You can set custom headers for your requests using the headers method:

nextClient
  .post("/upload")
  .json({ key: "value" })
  .headers({
    Authorization: "Bearer your_token_here",
    "Custom-Header": "CustomValue",
  })
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

Handling Form Data

To send form data, you can use the form method. This is useful for submitting forms that include files and other data types:

nextClient
  .post("/submit-form")
  .form({
    name: "John Doe",
    age: 30,
    file: new File(["content"], "example.txt", { type: "text/plain" }),
  })
  .send()
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    handleNextError(error, (res) => {
      console.error(res);
    });
  });

Handling Errors

You can handle errors using the handleNextError utility function. This function takes an error and a callback to process the error details.

import { handleNextError } from "nextclient";

nextClient
  .get("/non-existent-endpoint")
  .send()
  .catch((error) => {
    handleNextError(
      error,
      (res) => {
        console.error("Error details:", res);
      },
      () => {
        console.error("An unexpected error occurred.");
      }
    );
  });

API Reference

NextClient

Constructor

constructor(config: NextClientConfig)
  • Parameters:
    • config: Configuration object for the client.
      • baseUrl: The base URL for the API.
      • debug: (optional) Enable debug logging.
      • headers: (optional) Default headers for requests.

Methods

  • get(path: string, query?: Record<string, any>)
  • post(path: string, query?: Record<string, any>)
  • put(path: string, query?: Record<string, any>)
  • patch(path: string, query?: Record<string, any>)
  • delete(path: string, query?: Record<string, any>)
  • send<R>(params?: ParamsSend)

NextApiError

NextApiError is an error class that extends the built-in Error class. It provides additional properties for handling API errors.

Properties

  • status: HTTP status code.
  • statusText: HTTP status text.
  • url: The URL of the request.
  • response: The response object containing error details.

handleNextError

handleNextError<T>(
  error: any,
  callback: (e: { data?: Partial<T>; statusText: string; url: string; message: string }) => void,
  pass?: () => void
)
  • Parameters:
    • error: The error object to handle.
    • callback: A function to process the error details.
    • pass: An optional function to call if the error is not an instance of NextApiError.

Examples

You can find more examples in the examples directory of the repository.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

License

This project is licensed under the MIT License.


Conclusion

This documentation provides a comprehensive guide on how to use your NextClient package, including detailed sections on file uploads, setting headers, handling form data, and error handling. You can further enhance it by adding more examples, usage scenarios, or specific details about the API endpoints you are working with. Make sure to keep the documentation updated as you make changes to the package.