Giter Site home page Giter Site logo

mdmahfuzrp / react-api-call Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 46 KB

Say Goodbye to Api Headaches: Effortless Api Call With React-Api-Call

Home Page: https://reactapicall.vercel.app

License: MIT License

JavaScript 100.00%
api api-call apicall apicalls apis mahfuzrp mdmahfuzrp mrp next react

react-api-call's Introduction

Say Goodbye to Api Headaches:
Effortless Api Call With react-api-call

drawing

Introducing react-api-call, the ultimate npm package for effortless API requests. Simplify your development with just 2-3 lines of code to handle GET, POST, PUT, and DELETE operations. This user-friendly API request library enhances your productivity by streamlining best practices for making API calls. Perfect for developers, react api call is the efficient solution you need to boost your projects and improve your coding experience.

Install react-api-call:

npm i react-api-call

You can follow this steps for your usecase.

Get Method:
import { useGetMethod } from "react-api-call";

const App = () => {
  const { isLoading, refetch, response } = useGetMethod({
    apiUrl: "example/api/v1/users",
    token: "your-token",
    tokenType: "Bearer",
    headersConfig: {
      "Content-type": "application/json",
    },
  });

  console.log(response);
  return (
    <div>
      <h1>Hello World</h1>
    </div>
  );
};

export default App;
Submit Method:
import { useSubmitMethod } from "react-api-call";

const App = () => {
  const { handleSubmit, isLoading } = useSubmitMethod({
    token: "your-token",
    tokenType: "Bearer",
    headersConfig: {
      "Content-type": "application/json",
    },
  });

  const handlePostStatus = async () => {
    const postData = {
      name: "John Smith",
      status: true,
    };

    const { error, response } = await handleSubmit({
      url: "example/api/v1/users",
      data: postData,
    });

    console.log(error, response);
  };
  return (
    <div>
      <button onClick={handlePostStatus}>Add Status</button>
    </div>
  );
};

export default App;
Delete Method:
import { useDeleteMethod } from "react-api-call";

const App = () => {
  const { handleDelete, isDeleting } = useDeleteMethod({
    token: "your-token",
    headersConfig: {
      "Content-type": "application/json",
    },
    tokenType: "Bearer",
  });

  const handleDeleteStatus = async () => {
    const apiUrl = "example.com/status/1";
    await handleDelete({ url: apiUrl });
  };
  return (
    <div>
      <button onClick={handleDeleteStatus}>Delete</button>
    </div>
  );
};

export default App;

Now it's time for manage your cookies

Set Cookies:
import { useCookies } from "react-api-call";

const App = () => {
  const { setCookies } = useCookies();

  const handleSetCookies = () => {
    // in set cookies you need to pass: name, value, expires in day
    setCookies("admin-token", token, 7);
  };
  return (
    <div>
      <button onClick={handleSetCookies}>Set Token</button>
    </div>
  );
};

export default App;
Get Cookies:
import { useCookies } from "react-api-call";

const App = () => {
  const { getCookies } = useCookies();

  const handleGetCookies = () => {
    // for get cookies value, you need to pass "name" only
    getCookies("admin-token");
  };
  return (
    <div>
      <button onClick={handleGetCookies}>Get Token</button>
    </div>
  );
};

export default App;
Delete Cookies:
import { useCookies } from "react-api-call";

const App = () => {
  const { deleteCookie } = useCookies();

  const handleDeleteCookies = () => {
    // for delete cookies you need to pass: "name" only
    deleteCookie("admin-token");
  };
  return (
    <div>
      <button onClick={handleDeleteCookies}>Delete Token</button>
    </div>
  );
};

export default App;

List of parameters, that you can pass if needed:

Parameters Description Usecase Status Type Default Value
token If you need to pass token in your api calls then you can pass this otherwise not. useGetMethod, useSubmitMethod, useDeleteMethod Optional string null
tokenType Default tokenType is Bearer but if you need to change, then you can pass this parameters with value. useGetMethod, useSubmitMethod, useDeleteMethod Optional string Bearer
headersConfig Default is application/json but if needed you change pass this with values. useGetMethod, useSubmitMethod, useDeleteMethod Optional object application/json
apiUrl Pass your api url in useGetMethod Hooks. useGetMethod Required string null
onError After a submit or delete request, (errors) if you need to show error then you can pass a function in this parameters, and you also get error response in your function what you have pass in this parameters. useSubmitMethod, useDeleteMethod Optional function null
onSuccess After a submit or delete request, (success) if you need to show success or showing a alert or something then you can pass a function in this parameters, and you also get success response in your function what you have pass in this parameter. useSubmitMethod, useDeleteMethod Optional function null
refetch If after submit or delete you need to refetch or call some api then you can pass function in this parameters. useSubmitMethod, useDeleteMethod Optional function null
method For submit request default method is post and for delete request default method is delete so if you need to change you can pass this parameters with your methods. useSubmitMethod, useDeleteMethod Optional string post & delete

Contribution From Your End

If you have confidence to contribute in this package, YOU ARE WELCOME.

Todo

  • Component mount unmount loading handle for get api
  • Cache in for 5 minutes
Author

drawing

MD MAHFUZ RP
Software Engineer & Tech Entrepreneur
Developer & Creator of @react-api-call
mdmahfuzrp mdmahfuzrp

react-api-call's People

Contributors

mdmahfuzrp avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.