Skip to content

Commit

Permalink
test useMenu composable with pnpm generate
Browse files Browse the repository at this point in the history
  • Loading branch information
vernaillen committed Apr 10, 2024
1 parent 3fec241 commit 36a74e4
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/runtime/composables/useMenu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useFetch, createError, useRuntimeConfig, ref, useNuxtData, useNuxtApp } from "#imports"
import { useFetch, useRuntimeConfig, ref, useNuxtData, useNuxtApp } from "#imports"
import { useTokens } from "./useTokens"
import { useWPNuxtLogger } from "./useWPNuxtlogger";
import type { GraphqlResponse } from "~/src/types";

const _useMenu = async (name?: string) => {
const menu = ref()
const logger = useWPNuxtLogger()
const config = useRuntimeConfig()
const nuxtApp = useNuxtApp()
const menuName = name && name.length > 0 ? name : config.public.wpNuxt.defaultMenuName
Expand All @@ -15,29 +14,26 @@ const _useMenu = async (name?: string) => {
if (cachedMenu.value) {
menu.value = cachedMenu.value
} else {
const { data, error } = await useFetch<any>('/api/graphql_middleware/query/Menu', {
params: {
name: menuName
},
key: cacheKey,
headers: {
Authorization: tokens.authorizationHeader
},
transform (data: any) {
return data.data.menu.menuItems.nodes;
},
getCachedData(key: string) {
return nuxtApp.payload.data[key] || nuxtApp.static.data[key]
}
});
if (error.value) {
logger.error('useMenu, error: ', error.value)
throw createError({ statusCode: error.value.status, message: error.value.message, fatal: true })
}
menu.value = data.value
}
return {
data: menu.value
return useFetch<GraphqlResponse<any>>("/api/graphql_middleware/query/Menu", {
params: {
name: menuName
},
key: cacheKey,
headers: {
Authorization: tokens.authorizationHeader
},
transform (data: any) {
return data.data.menu.menuItems.nodes;
},
getCachedData(key: string) {
return nuxtApp.payload.data[key] || nuxtApp.static.data[key]
}
}).then((v: GraphqlResponse<any>) => {
return {
data: v.data.value,
errors: v.errors || [],
}
})
}
}

Expand Down

0 comments on commit 36a74e4

Please sign in to comment.