Skip to content

Commit

Permalink
fix composable
Browse files Browse the repository at this point in the history
Etienne Moureton committed Mar 15, 2024
1 parent eb39131 commit 25dd365
Showing 2 changed files with 7 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/composables/useWpPost.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# useWpPost

`useWpPost` is a composable function that fetches a WordPress post by its slug. It returns a Promise that resolves to a `Post` object.
`useWpPost` is a composable function that fetches a WordPress post by its id. It returns a Promise that resolves to a `Post` object.

## Usage
```ts
const post = await useWpPost({ type, slug })
const post = await useWpPost({ type, id })
```

## Parameters

- `type` (string, optional): The type of the WordPress post to fetch. Defaults to 'posts'.
- `slug` (string, optional): The slug of the WordPress post to fetch. If not provided, the current route path will be used.
- `id` (number, optional): The number of the WordPress post to fetch. If not provided, the current route path will be used.
12 changes: 4 additions & 8 deletions src/runtime/composables/useWpPost.ts
Original file line number Diff line number Diff line change
@@ -4,21 +4,17 @@ import type { Post } from '../types'

interface Options {
type?: string,
slug?: string,
id?: SVGAnimatedNumberList,
}

const useWpPost = async ({ type = 'posts', slug }: Options = {}) => {
const useWpPost = async ({ type = 'posts', id }: Options = {}) => {
const route = useRoute()
const query = slug || route.path.substring(1)
const query = id || route.path.substring(1)

const { data, error } = await useAsyncData<Array<Post>>('post', async () => {
const { apiEndpoint, additonnalQueryParams } = useRuntimeConfig().public.wordpress

const params = new URLSearchParams({
slug: String(slug),
});

return $fetch(`${apiEndpoint}/${type}?${params.toString()}${additonnalQueryParams}`)
return $fetch(`${apiEndpoint}/${type}/${query}${additonnalQueryParams}`)
})

if(error.value) {

0 comments on commit 25dd365

Please sign in to comment.