Git Product home page Git Product logo

webdevsharing / hyper-fetch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bettertyped/hyper-fetch

0.0 0.0 0.0 6.65 MB

⚡ Fetching made easy! Take advantage of response caching, request deduplication, authentication features, great progress tracking, smart retries, architecture guidelines and more. Gain full control over requesting in your applications!

Home Page: https://hyperfetch.bettertyped.com/

License: Apache License 2.0

Shell 0.12% JavaScript 1.00% TypeScript 98.88%

hyper-fetch's Introduction


Hyper Fetch

Best of JS Twitter Follow

Hyper Fetch is fetching framework. What makes it unique is the typesafe design and the ease of use. This library can run in the browser and on the server with any framework. With our library fetching, architecture and access to requests lifecycle is fabulously simple, thanks to which you can create new components or functionalities much faster and all of this in accordance with good practices!

DocumentationQuick StartGuidesAPI


Key Features

🔮 Simple setup - Read more

🎯 Easy cancellation - Read more

Deduplicate similar requests - Read more

🚀 Queueing - Read more

💎 Response caching - Read more

🔋 Offline First - Read more

📡 Built-in fetcher - Read more

🎟 Authentication - Read more

🔁 Smart Retries - Read more

Installation

The easiest way to get the latest version of Hyper Fetch is to install it via yarn or npm.

Core

npm install --save @hyper-fetch/core
or
yarn add @hyper-fetch/core

Sockets

npm install --save @hyper-fetch/sockets
or
yarn add @hyper-fetch/sockets

React

npm install --save @hyper-fetch/core @hyper-fetch/react
or
yarn add @hyper-fetch/core @hyper-fetch/react

Packages

Package Stats
⚡ Hyper Fetch
🛰️ Hyper Fetch Sockets
⚛️ React Hyper Fetch

Examples

Simple Setup

import { Client } from "@hyper-fetch/core";

// Setup our connection to the server
export const client = new Client({ url: "http://localhost:3000" });

// Create reusable requests for later use
export const postData = client.createRequest<ResponseType, RequestType, LocalErrorType, QueryParamsType>()({
  method: "POST",
  endpoint: "/data/:accountId",
});

export const getData = client.createRequest<ResponseType, RequestType, LocalErrorType, QueryParamsType>()({
  method: "GET",
  endpoint: "/user",
});

Fetching

Executing previously prepared requests is very simple. We can do this using the send method.

const [data, error, status] = await getData.send();

Mutation request

We can attach the data to the request with methods before sending it to the server. This is helpful for building our request and attaching data to it which can be helpful when we need to create it in a few steps from data obtained during some process.

// Set the information to request (methods return request clone - NOT mutating the source)
const request = postData
  .setParams({ accountId: 104 }) // Set Params
  .setQueryParams({ paramOne: "test", paramTwo: "test2" })
  .setData({ name: "My new entity", description: "Some description" }) // Add payload data
  .send();

We can also pass them directly to the send method, which will add them to the request at once.

// OR pass dynamic data directly to '.send' method
const [data, error, status] = await postData.send({
  params: { accountId: 104 },
  data: { name: "My new entity", description: "Some description" },
  queryParams: { paramOne: "test", paramTwo: "test2" },
});

React

Fetch with lifecycle

Show me example
import { useFetch } from "@hyper-fetch/react";

// Lifecycle fetching
const { data, error, loading, onSuccess, onError } = useFetch(getData);

onSuccess((data) => {
  console.log(data);
});

onError((error) => {
  console.log(error);
});

Manually trigger requests

Show me example
import { useSubmit } from "@hyper-fetch/react";

const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(request);

onSuccess((data) => {
  console.log(data);
});

onError((error) => {
  console.log(error);
});

return <button onClick={() => submit()}>Trigger request!</button>;

Pass dynamic data to submit method

Show me example
import { useSubmit } from "@hyper-fetch/react";

const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(request);

onSuccess((data) => {
  console.log(data);
});

onError((error) => {
  console.log(error);
});

return (
  <button
    onClick={() =>
      submit({
        params: { accountId: 104 },
        data: { name: "My new entity", description: "Some description" },
        queryParams: { paramOne: "test", paramTwo: "test2" },
      })
    }
  >
    Trigger request!
  </button>
);

Use submit promise response

Show me example
import { useSubmit } from "@hyper-fetch/react";

// Manual triggering
const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(request);

onSuccess((data) => {
  console.log(data);
});

onError((error) => {
  console.log(error);
});

const handleSubmit = (values: ValuesType, { setSubmitting }: FormikHelpers) => {
  const [data, error, status] = await submit(); // Submit method returns data!
  setSubmitting(false);
  if (data) {
    notification.success("Done!", data);
  } else {
    notification.success("Error!", error);
  }
};

return <Form onSubmit={handleSubmit}>...</Form>;

Find out more examples

hyper-fetch's People

Contributors

albelewandowski avatar dependabot[bot] avatar gerasnyx avatar prc5 avatar schalkventer avatar snyk-bot avatar spezzino avatar th3fallen 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.