Skip to content

Commit

Permalink
Add another example
Browse files Browse the repository at this point in the history
  • Loading branch information
n2o committed Sep 5, 2024
1 parent 15b6553 commit a4c7b04
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pnpm install @schnaq/strapi4-utils

Query a Strapi API and flatten the response. It's always JSON what we are receiving from the API, and we are getting rid of the `attributes` property.

This function is formatted in a way that it can be used within nextjs and benefit from the fetcher function.
This function is formatted as a vector to be supported with hooks, like `useSWR`.

Example:

Expand All @@ -45,6 +45,27 @@ export async function getEvent(eventId: Event["id"]) {
}
```

Example: Usage in a react context

```ts
async function getMyBoxes() {
const { data: session, status } = useSession()
const [boxes, setBoxes] = useState<Box[]>()

useSWR(
status === "authenticated"
? [`/boxes`, undefined, "GET", session?.accessToken]
: null,
queryAPI<Box[]>,
{
onSuccess: (boxes) => {
setBoxes(boxes?.data)
},
}
)
}
```

### `getImageUrlForSize`

Provide a valid image from a Strapi response. This functions provides the nearest image size to the requested one.
Expand Down

0 comments on commit a4c7b04

Please sign in to comment.