From 25dd36590e338b50e5b054d90c1b0dd779df89fc Mon Sep 17 00:00:00 2001 From: Etienne Moureton Date: Fri, 15 Mar 2024 21:32:26 +0100 Subject: [PATCH] fix composable --- docs/composables/useWpPost.md | 6 +++--- src/runtime/composables/useWpPost.ts | 12 ++++-------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/docs/composables/useWpPost.md b/docs/composables/useWpPost.md index 6fe9c8e..0f395dd 100644 --- a/docs/composables/useWpPost.md +++ b/docs/composables/useWpPost.md @@ -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. \ No newline at end of file +- `id` (number, optional): The number of the WordPress post to fetch. If not provided, the current route path will be used. \ No newline at end of file diff --git a/src/runtime/composables/useWpPost.ts b/src/runtime/composables/useWpPost.ts index 7983e9f..f263bdf 100644 --- a/src/runtime/composables/useWpPost.ts +++ b/src/runtime/composables/useWpPost.ts @@ -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>('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) {