Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Oct 26, 2022
1 parent b2033f4 commit f780cd1
Show file tree
Hide file tree
Showing 297 changed files with 1,089 additions and 1,031 deletions.
62 changes: 30 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# xswr

The God tier React data (re)fetching library
![](https://user-images.githubusercontent.com/4405263/198129674-d7beabc9-61ed-43ab-852f-ab317c51f1f9.png)

```bash
npm i @hazae41/xswr
```

[**Read the docs 📚**](https://xswr.hazae41.me)[**Try it online 🚀**](https://test.xswr.hazae41.me)[**Comparison with other libs 💩**](https://xswr.hazae41.me/faq/comparison)
[**Read the docs 📚**](https://xswr.hazae41.me)[**Try it online 🚀**](https://test.xswr.hazae41.me)[**Next.js CodeSandbox 🪣**](https://codesandbox.io/p/sandbox/misty-butterfly-64m881)[**Comparison with other libs 🌐**](https://xswr.hazae41.me/faq/comparison)

## 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 behaviours.
2) Composing features with very simple hooks instead of having bloated configuration and unexpected behaviors.

## Features 🔥

Expand Down Expand Up @@ -64,13 +62,13 @@ function MyWrapper() {
}
```

# Your first mix 🧪
## 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 ⚡️
### Create a fetcher ⚡️

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

Expand All @@ -82,22 +80,23 @@ async function fetchAsJson<T>(url: string) {
}
```

## Create a mix 🌪
### Create a mix 🌪

Then create a mix using a handle and some blocks.
Then create a mix using a query and some blocks.

```tsx
function useHello() {
const handle = XSWR.useSingle<Hello>(`/api/hello`, fetchAsJson)
const query = useSingleQuery<Hello>(`/api/hello`, fetchAsJson)

XSWR.useFetch(handle) // Fetch on mount and on url change
XSWR.useVisible(handle) // Fetch when the page becomes visible
XSWR.useOnline(handle) // Fetch when the browser becomes online
return handle
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 🚀
### Use it in your components 🚀

```tsx
function MyApp() {
Expand All @@ -111,16 +110,16 @@ function MyApp() {
}
```

# Advanced example
## Advanced example 🗿

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

## Making our fetcher cancellable ⚡️
### Making our fetcher cancellable ⚡️

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

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

const res = await fetch(url, { signal })
Expand All @@ -137,43 +136,43 @@ async function fetchAsJson<T>(url: string, more: XSWR.FetcherMore<T>) {

It also returns an error if the request failed.

## Defining schemas 📐
### Defining schemas 📐

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

```tsx
function getHelloSchema() {
return XSWR.single<Hello>("/api/hello", fetchAsJson)
return getSingleSchema<Hello>("/api/hello", fetchAsJson)
}
```

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

## Creating mixtures 🧪
### Creating mixtures 🧪

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

```tsx
function useAutoFetchMixture(handle: XSWR.Handle) {
XSWR.useFetch(handle)
XSWR.useVisible(handle)
XSWR.useOnline(handle)
function useAutoFetchMixture(query: Query) {
useFetch(query)
useVisible(query)
useOnline(query)
}
```

## Mixing it 🌪
### Mixing it 🌪

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

```tsx
function useHelloMix() {
const handle = XSWR.use(getHelloSchema, [])
useAutoFetchMixture(handle)
return handle
const query = useQuery(getHelloSchema, [])
useAutoFetchMixture(query)
return query
}
```

## Use it in your app 🚀
### Use it in your app 🚀

```tsx
function MyApp() {
Expand All @@ -187,5 +186,4 @@ function MyApp() {
}
```

[**Read the docs 📚**](https://xswr.hazae41.me)[**Try it online 🚀**](https://test.xswr.hazae41.me)[**Comparison with other libs 💩**](https://xswr.hazae41.me/faq/comparison)

[**Read the docs 📚**](https://xswr.hazae41.me)[**Try it online 🚀**](https://test.xswr.hazae41.me)[**Next.js CodeSandbox 🪣**](https://codesandbox.io/p/sandbox/misty-butterfly-64m881)[**Comparison with other libs 🌐**](https://xswr.hazae41.me/faq/comparison)
10 changes: 0 additions & 10 deletions dist/cjs/index.cjs

This file was deleted.

1 change: 0 additions & 1 deletion dist/cjs/index.cjs.map

This file was deleted.

85 changes: 85 additions & 0 deletions dist/cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/cjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/cjs/libs/arrays.cjs.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/cjs/libs/arrays.cjs → dist/cjs/libs/arrays.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/cjs/libs/arrays.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions dist/cjs/libs/generator.cjs

This file was deleted.

1 change: 0 additions & 1 deletion dist/cjs/libs/generator.cjs.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/cjs/libs/ortho.cjs.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/cjs/libs/ortho.cjs → dist/cjs/libs/ortho.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/cjs/libs/ortho.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/cjs/libs/react.cjs.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/cjs/libs/react.cjs → dist/cjs/libs/react.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/cjs/libs/react.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/cjs/libs/time.cjs.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/cjs/libs/time.cjs → dist/cjs/libs/time.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

1 comment on commit f780cd1

@vercel
Copy link

@vercel vercel bot commented on f780cd1 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xswr-test – ./

xswr-test-hazae41.vercel.app
test.xswr.hazae41.me
xswr-test-git-master-hazae41.vercel.app

Please sign in to comment.