2.0.0-94 โ€ข Published 2 years ago

@hazae41/xswr v2.0.0-94

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago
npm i @hazae41/xswr

Node Package ๐Ÿ“ฆ โ€ข Read the docs ๐Ÿ“š โ€ข Next.js Example ๐Ÿชฃ โ€ข Expo Example ๐Ÿชฃ โ€ข Comparison with other libs ๐ŸŒ

Philosophy ๐Ÿง 

xswr uses two new approaches compared to other data fetching libraries like swr or react-query: 1) Encapsulating key+fetcher+params in a single abstraction called schema. 2) Composing features with very simple hooks instead of having bloated configuration and unexpected behaviors.

Features ๐Ÿ”ฅ

Current features

  • 100% TypeScript and ESM
  • Composition-based hooks
  • Very easy learning curve
  • No dependency except React
  • Not over-engineered (hello react-query)
  • No unexpected behaviour (hello swr)
  • Backend agnostic fetching (REST, GraphQL, WebSocket)
  • Storage agnostic caching (new Map(), LocalStorage, IndexedDB)
  • Automatic refetching
  • Dependent and conditional queries
  • Request deduplication, cooldown, timeout, and expiration
  • Page-based and cursor-based pagination
  • Exponential backoff retry
  • SSR & ISR support
  • Optimistic mutations
  • Cancellable requests
  • Automatic cancellation
  • Automatic garbage collection
  • Per-query persistent storage
  • Out of the box IndexedDB and LocalStorage
  • Out of the box store normalization
  • Super natural React Suspense
  • React Native support

Upcoming features

  • Transport agnostic streaming (ethers.js, WebSockets, Socket.io)
  • Bidirectional scrolling

Installation ๐Ÿ”ง

Just install @hazae41/xswr using your favorite package manager.

npm i @hazae41/xswr

Then, wrap your app in a CoreProvider component.

import { CoreProvider } from "@hazae41/xswr"

function MyWrapper() {
  return <CoreProvider>
    <MyAwesomeApp />
  </CoreProvider>
}

Your first mix ๐Ÿงช

When using xswr and its composition-based hooks, you create a mix and only include the ingredients you want.

We'll do a request at /api/data using JSON, display it with a loading, and automatically refetch it.

Create a fetcher โšก๏ธ

It will just take an url, fetch it, and return the data.

async function fetchAsJson<T>(url: string) {
  const res = await fetch(url)
  const data = await res.json() as T
  return { data }
}

Create a mix ๐ŸŒช

Then create a mix using a query and some blocks.

function useHello() {
  const query = useQuery<Hello>(`/api/hello`, fetchAsJson)
  
  useFetch(query) // Fetch on mount and on url change
  useVisible(query) // Fetch when the page becomes visible
  useOnline(query) // Fetch when the browser becomes online

  return query
}

Use it in your components ๐Ÿš€

function MyApp() {
  const { data, error } = useHello()

  if (error)
    return <MyError error={error} />
  if (!data)
    return <MyLoading />
  return <MyPage data={data} />
}

Advanced example ๐Ÿ—ฟ

Last example was good, but here is the best way to use XSWR.

Making our fetcher cancellable โšก๏ธ

Our fetcher was good, but this one can be aborted.

async function fetchAsJson<T>(url: string, more: FetcherMore<T>) {
  const { signal } = more

  const res = await fetch(url, { signal })

  if (!res.ok) {
    const error = new Error(await res.text())
    return { error }
  }

  const data = await res.json() as T
  return { data }
}

It also returns an error if the request failed.

Defining schemas ๐Ÿ“

Using schemas may seems boilerplate, but it will save you a lot of time later.

function getHelloSchema() {
  return getSchema<Hello>("/api/hello", fetchAsJson)
}

It allows you to reuse the same set of key+fetcher+params in multiple places, including imperative code.

Creating mixtures ๐Ÿงช

The mixtures pattern allows you to reuse the same group of blocks.

function useAutoFetchMixture(query: Query) {
  useFetch(query)
  useVisible(query)
  useOnline(query)
}

Mixing it ๐ŸŒช

Once you got a schema and a mixture, you just have to mix it.

function useHelloMix() {
  const query = useSchema(getHelloSchema, [])
  useAutoFetchMixture(query)
  return query
}

Use it in your app ๐Ÿš€

function MyApp() {
  const { data, error } = useHelloMix()

  if (error)
    return <MyError error={error} />
  if (!data)
    return <MyLoading />
  return <MyPage data={data} />
}
2.0.0-94

2 years ago

2.0.0-93

2 years ago

2.0.0-92

2 years ago

2.0.0-91

2 years ago

2.0.0-90

2 years ago

2.0.0-69

2 years ago

2.0.0-68

2 years ago

2.0.0-67

2 years ago

2.0.0-66

2 years ago

2.0.0-65

2 years ago

2.0.0-79

2 years ago

2.0.0-78

2 years ago

2.0.0-77

2 years ago

2.0.0-76

2 years ago

2.0.0-75

2 years ago

2.0.0-74

2 years ago

2.0.0-73

2 years ago

2.0.0-72

2 years ago

2.0.0-71

2 years ago

2.0.0-70

2 years ago

2.0.0-89

2 years ago

2.0.0-88

2 years ago

2.0.0-87

2 years ago

2.0.0-86

2 years ago

2.0.0-85

2 years ago

2.0.0-84

2 years ago

2.0.0-83

2 years ago

2.0.0-82

2 years ago

2.0.0-81

2 years ago

2.0.0-80

2 years ago

2.0.0-64

2 years ago

2.0.0-63

2 years ago

2.0.0-19

2 years ago

2.0.0-18

2 years ago

2.0.0-17

2 years ago

2.0.0-16

2 years ago

2.0.0-15

2 years ago

2.0.0-14

2 years ago

2.0.0-13

2 years ago

2.0.0-12

2 years ago

2.0.0-11

2 years ago

2.0.0-10

2 years ago

2.0.0-29

2 years ago

2.0.0-28

2 years ago

2.0.0-27

2 years ago

2.0.0-26

2 years ago

2.0.0-25

2 years ago

2.0.0-24

2 years ago

2.0.0-23

2 years ago

2.0.0-22

2 years ago

2.0.0-21

2 years ago

2.0.0-20

2 years ago

2.0.0-39

2 years ago

2.0.0-38

2 years ago

2.0.0-37

2 years ago

2.0.0-36

2 years ago

2.0.0-35

2 years ago

2.0.0-34

2 years ago

2.0.0-33

2 years ago

2.0.0-32

2 years ago

2.0.0-31

2 years ago

2.0.0-30

2 years ago

2.0.0-49

2 years ago

2.0.0-48

2 years ago

2.0.0-47

2 years ago

2.0.0-46

2 years ago

2.0.0-45

2 years ago

2.0.0-44

2 years ago

2.0.0-43

2 years ago

2.0.0-42

2 years ago

2.0.0-41

2 years ago

2.0.0-40

2 years ago

2.0.0-59

2 years ago

2.0.0-58

2 years ago

2.0.0-57

2 years ago

2.0.0-56

2 years ago

2.0.0-55

2 years ago

2.0.0-54

2 years ago

2.0.0-53

2 years ago

2.0.0-52

2 years ago

2.0.0-51

2 years ago

2.0.0-50

2 years ago

2.0.0-6

2 years ago

2.0.0-5

2 years ago

2.0.0-4

2 years ago

2.0.0-3

2 years ago

2.0.0-2

2 years ago

2.0.0-1

2 years ago

2.0.0-0

2 years ago

2.0.0-62

2 years ago

2.0.0-61

2 years ago

2.0.0-60

2 years ago

2.0.0-8

2 years ago

2.0.0-7

2 years ago

1.5.14

2 years ago

1.5.16

2 years ago

1.5.15

2 years ago

1.5.18

2 years ago

1.5.17

2 years ago

1.5.19

2 years ago

1.5.21

2 years ago

1.5.20

2 years ago

1.5.22

2 years ago

1.5.1-9

2 years ago

1.5.1-8

2 years ago

1.5.1-7

2 years ago

1.5.1-6

2 years ago

1.5.1-5

2 years ago

1.5.1-4

2 years ago

1.5.1-3

2 years ago

1.5.1-2

2 years ago

1.5.1-0

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.2-10

2 years ago

1.4.2-11

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.10

2 years ago

1.5.12

2 years ago

1.5.11

2 years ago

1.5.13

2 years ago

1.2.6

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.3.1-rc.2

2 years ago

1.3.1-rc.4

2 years ago

1.3.1-rc.3

2 years ago

1.3.1-rc.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.4.1-0

2 years ago

1.3.0

2 years ago

1.4.2-7

2 years ago

1.4.2-6

2 years ago

1.4.2-9

2 years ago

1.4.2-8

2 years ago

1.4.2-3

2 years ago

1.4.2-5

2 years ago

1.4.2-4

2 years ago

1.4.2-1

2 years ago

1.4.2-0

2 years ago

1.2.0

3 years ago

1.2.1

2 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.84

3 years ago

1.0.83

3 years ago

1.0.82

3 years ago

1.0.88

3 years ago

1.0.87

3 years ago

1.0.86

3 years ago

1.0.85

3 years ago

1.0.89

3 years ago

1.0.91

3 years ago

1.0.90

3 years ago

1.0.95

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.92

3 years ago

1.0.96

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.62

3 years ago

1.0.61

3 years ago

1.0.60

3 years ago

1.0.66

3 years ago

1.0.22

3 years ago

1.0.65

3 years ago

1.0.21

3 years ago

1.0.64

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.69

3 years ago

1.0.25

3 years ago

1.0.68

3 years ago

1.0.24

3 years ago

1.0.67

3 years ago

1.0.23

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.77

3 years ago

1.0.33

3 years ago

1.0.76

3 years ago

1.0.32

3 years ago

1.0.75

3 years ago

1.0.31

3 years ago

1.0.74

3 years ago

1.0.30

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.79

3 years ago

1.0.35

3 years ago

1.0.78

3 years ago

1.0.34

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.80

3 years ago

1.0.40

3 years ago

1.0.81

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.59

3 years ago

1.0.15

3 years ago

1.0.58

3 years ago

1.0.14

3 years ago

1.0.57

3 years ago

1.0.13

3 years ago

1.0.56

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago