Skip to content

Commit

Permalink
docs: copy
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Aug 26, 2024
1 parent c80d74b commit 8c94373
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/posts/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ When using an endpoint, this method simply stores whatever is put inside
`ctx.json`. Then you can access that data via `useCache`.

```tsx
import { createApi } from "starfx";
import { useCache } from "starfx/react";

const api = createApi();
const fetchUsers = api.get("/users", api.cache());

function App() {
const { data = [] } = useCache(fetchUsers());
return <div>{data.map((user) => <div>{user.name}</div>)}</div>;
}
```

`api.cache()` opts into automatic caching. This is really just an alias for:
Expand All @@ -42,6 +51,16 @@ function*(ctx, next) {
}
```

The state slice for `cache` is simple, every thunk action has special properties
of one is a `key` field that is a hash of the entire user-defined action
payload:

```js
{
[action.payload.key]: {},
}
```

# `timer` supervisor

This supervisor can help us with how often we refetch data. This will help us
Expand Down
19 changes: 19 additions & 0 deletions docs/posts/thunks.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ A thunk action adheres to the
> While not strictly necessary, it is highly recommended to keep actions JSON
> serializable
For thunks we have a more strict payload type signature with additional
properties:

```ts
interface CreateActionPayload<P = any, ApiSuccess = any> {
name: string; // the user-defined name
options: P; // thunk payload described below
key: string; // hash of entire thunk payload
}

interface ThunkAction<P> {
type: string;
payload: CreateActionPayload<P>;
}
```

This is the type signature for every action created automatically by
`createThunks` or `createApi`.

# Thunk payload

When calling a thunk, the user can provide a payload that is strictly enforced
Expand Down

0 comments on commit 8c94373

Please sign in to comment.