Skip to content

Commit

Permalink
Add Json response helper to response-helpers.mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
rpivo authored and atilafassina committed Aug 21, 2024
1 parent 5f29293 commit 2ced0a4
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/routes/solid-router/reference/data-apis/response-helpers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ title: Response helpers
## Redirect

Signature: `redirect(path, options)`
Redirects to the next route

Redirects to the next route.

```js
const getUser = cache(() => {
const user = await api.getCurrentUser()
const user = await api.getCurrentUser();
if (!user) throw redirect("/login");
return user;
})
Expand All @@ -19,16 +20,34 @@ const getUser = cache(() => {

Signature: `reload(options)`

Reloads the data on the current page
Reloads the data at the given cache key on the current route.

```js
const getTodo = cache(async (id: number) => {
const todo = await fetchTodo(id);
return todo;
const todo = await fetchTodo(id);
return todo;
}, "todo");

const updateTodo = action(async (todo: Todo) => {
await updateTodo(todo.id, todo);
reload({ revalidate: getTodo.keyFor(id) });
await putTodo(todo.id, todo);
return reload({ revalidate: getTodo.keyFor(id) });
});
```

## Json

Signature: `json(data, options)`

Returns JSON data from an action while also providing options for controlling revalidation of cache data on the route.

```js
const getTodo = cache(async (id: number) => {
const todo = await fetchTodo(id);
return todo;
}, "todo");

const getCompletedTodos = action(async () => {
const completedTodos = await fetchTodo({ status: 'complete' });
return json(completedTodos, { revalidate: getTodo.keyFor(id) });
});
```

0 comments on commit 2ced0a4

Please sign in to comment.