Hyper Fetch

Backend agnostic data-exchange framework for any javascript environment. Ta...

README



Hyper Fetch


Hyper Fetch is a data-exchange framework. What makes it unique is the typesafe design and the ease of use.
This library is backend and framework agnostic, with aim to provide as many great and useful features as possible. In
particular caching, queuing, persistence, offline first support, request deduplication, authentication,
progress tracking, structure and architecture guidelines.


Key Features


🔮 Simple setup - Read more

🎯 Request cancellation - Read more

Window Focus/Blur Events - Read more

🚀 Queueing - Read more

💎 Automatic caching - Read more

🪄 Persistence - Read more

🎊 SSR Support - Read more

🔋 Offline First - Read more

📡 Built-in client - Read more

🧪 Easy to test - Read more

🎟 Authentication - Read more

💡 Prefetching - Read more

Sources


- #### Docs
- #### API
- #### Guides

Installation


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

  1. ``` sh
  2. npm install --save @hyper-fetch/core
  3. or
  4. yarn add @hyper-fetch/core
  5. ```

React Hooks


  1. ``` sh
  2. npm install --save @hyper-fetch/core @hyper-fetch/react
  3. or
  4. yarn add @hyper-fetch/core @hyper-fetch/react
  5. ```

Packages


Package Stats
⚡ Hyper Fetch
⚛️ React Hyper Fetch

Examples


Simple Setup


  1. ```tsx
  2. import { Builder } from "@hyper-fetch/core";

  3. // Create global setup
  4. export const builder = new Builder({ baseUrl: "http://localhost:3000" });

  5. // Create reusable commands to trigger requests
  6. export const postData = builder.createCommand<ResponseType, RequestType, LocalErrorType, QueryParamsType>()({
  7.   method: "POST",
  8.   endpoint: "/data/:accountId",
  9. });
  10. export const getData = builder.createCommand<ResponseType, RequestType, LocalErrorType, QueryParamsType>()({
  11.   method: "GET",
  12.   endpoint: "/user",
  13. });
  14. ```

Triggering request


  1. ```tsx
  2. // Set the information to command (methods return command clone - NOT mutating the source)
  3. const command = postData
  4.   .setParams({ accountId: 104 }) // Set Params
  5.   .setQueryParams({ paramOne: "test", paramTwo: "test2" })
  6.   .setData({ name: "My new entity", description: "Some description" }); // Add payload data

  7. // Use command directly
  8. const [data, error, status] = await command.send();

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

React


Use prepared hooks

Fetch with lifecycle


  1. ```tsx
  2. import { useFetch } from "@hyper-fetch/react";

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

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

  8. onError((error) => {
  9.   console.log(error);
  10. });
  11. ```

Manually trigger requests


  1. ```tsx
  2. import { useSubmit } from "@hyper-fetch/react";

  3. const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(command);

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

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

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

Pass dynamic data to submit method


  1. ```tsx
  2. import { useSubmit } from "@hyper-fetch/react";

  3. const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(command);

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

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

  10. return (
  11.   <button
  12.     onClick={() =>
  13.       submit({
  14.         params: { accountId: 104 },
  15.         data: { name: "My new entity", description: "Some description" },
  16.         queryParams: { paramOne: "test", paramTwo: "test2" },
  17.       })
  18.     }
  19.   >
  20.     Trigger request!
  21.   </button>
  22. );
  23. ```

Use submit promise response


  1. ```tsx
  2. import { useSubmit } from "@hyper-fetch/react";

  3. // Manual triggering
  4. const { submit, data, error, submitting, onSubmitSuccess, onSubmitError } = useSubmit(command);

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

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

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

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