diff --git a/docs/tsconfig.json b/docs/tsconfig.json index a746f2a..c09b41b 100644 --- a/docs/tsconfig.json +++ b/docs/tsconfig.json @@ -1,4 +1,9 @@ { // https://nuxt.com/docs/guide/concepts/typescript - "extends": "./.nuxt/tsconfig.json" + "extends": "./.nuxt/tsconfig.json", + "exclude": [ + "node_modules", + "dist", + ".nuxt", + ] } diff --git a/package.json b/package.json index 104b8df..8ef3383 100644 --- a/package.json +++ b/package.json @@ -18,15 +18,16 @@ "dist" ], "scripts": { - "build": "npm run prepack", + "build": "nuxt-module-build build && pnpm build:web-types", + "build:web-types": "vue-docgen-web-types src/runtime/components/ ./dist/web-types.json", "prepare": "nuxi prepare .", "prepack": "nuxt-module-build build", "generate": "pnpm --filter ./playground/ run generate", "dev": "pnpm --filter ./playground/ run dev --host app.local", "dev:build": "pnpm --filter ./playground/ run build", "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground", - "release": "npm run lint && npm run prepack && changelogen --release && npm publish && git push --follow-tags", - "release:major": "npm run lint && npm run prepack && changelogen --release --major && npm publish && git push --follow-tags", + "release": "npm run lint && nuxi prepare && nuxt-module-build build && changelogen --release && pnpm publish && git push --follow-tags", + "release:major": "npm run lint && nuxi prepare && nuxt-module-build build && changelogen --release --major && pnpm publish && git push --follow-tags", "lint": "eslint .", "lint:fix": "eslint . --fix", "test": "vitest run", @@ -72,6 +73,7 @@ "typescript": "^5.4.5", "untyped": "1.4.2", "vitest": "^1.6.0", + "vue-docgen-web-types": "^0.1.8", "vue-tsc": "^2.0.16" } -} \ No newline at end of file +} diff --git a/playground/app.vue b/playground/app.vue index bd10267..5afcbce 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,63 +1,16 @@ diff --git a/playground/components/HeaderComponent.vue b/playground/components/HeaderComponent.vue new file mode 100644 index 0000000..1127a05 --- /dev/null +++ b/playground/components/HeaderComponent.vue @@ -0,0 +1,36 @@ + + + diff --git a/playground/error.vue b/playground/error.vue new file mode 100644 index 0000000..c6b3d29 --- /dev/null +++ b/playground/error.vue @@ -0,0 +1,34 @@ + + + diff --git a/playground/pages/[...slug].vue b/playground/pages/[...slug].vue index 5fd1026..eaffbf2 100644 --- a/playground/pages/[...slug].vue +++ b/playground/pages/[...slug].vue @@ -1,11 +1,15 @@ + + diff --git a/playground/pages/index.vue b/playground/pages/index.vue index 8b80d55..d5d5ea7 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -1,12 +1,11 @@ diff --git a/playground/pages/pages.vue b/playground/pages/pages.vue new file mode 100644 index 0000000..ba80335 --- /dev/null +++ b/playground/pages/pages.vue @@ -0,0 +1,32 @@ + + + diff --git a/playground/pages/test.vue b/playground/pages/test.vue index fc7d3eb..1fd193c 100644 --- a/playground/pages/test.vue +++ b/playground/pages/test.vue @@ -1,10 +1,9 @@ @@ -67,15 +66,15 @@ if (post?.data?.title) {
- Edit + Edit
diff --git a/src/runtime/composables/useGeneralSettings.ts b/src/runtime/composables/useGeneralSettings.ts index 128748d..34eb07c 100644 --- a/src/runtime/composables/useGeneralSettings.ts +++ b/src/runtime/composables/useGeneralSettings.ts @@ -1,7 +1,9 @@ +import type { FetchError } from 'ofetch' import { getContentNodes } from './useWPContent' import type { GeneralSettings } from '#graphql-operations' +import type { AsyncData } from '#app' -const _useGeneralSettings = async (): Promise => { +const _useGeneralSettings = async (): Promise> => { return getContentNodes('GeneralSettings', 'generalSettings') } diff --git a/src/runtime/composables/useMenu.ts b/src/runtime/composables/useMenu.ts index ab285be..55adcf3 100644 --- a/src/runtime/composables/useMenu.ts +++ b/src/runtime/composables/useMenu.ts @@ -1,11 +1,13 @@ +import type { FetchError } from 'ofetch' import { getContentNodes } from './useWPContent' import { useRuntimeConfig } from '#imports' import type { Menu } from '#graphql-operations' +import type { AsyncData } from '#app' -const _useMenu = async (name?: string): Promise => { +const _useMenu = async (name?: string): Promise> => { const config = useRuntimeConfig() const menuName = name && name.length > 0 ? name : config.public.wpNuxt.defaultMenuName - return await getContentNodes('Menu', 'menu', 'menuItems', 'nodes', { + return getContentNodes('Menu', 'menu', 'menuItems', 'nodes', { name: menuName, }) } diff --git a/src/runtime/composables/useNode.ts b/src/runtime/composables/useNode.ts index f893c30..267e8d9 100644 --- a/src/runtime/composables/useNode.ts +++ b/src/runtime/composables/useNode.ts @@ -1,12 +1,13 @@ +import type { FetchError } from 'ofetch' import { getContentNode } from './useWPContent' import type { Page, Post } from '#graphql-operations' +import type { AsyncData } from '#app' -const _useNodeByUri = async (uri: string): Promise => { - if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return - const node = await getContentNode('NodeByUri', 'nodeByUri', { +const _useNodeByUri = async (uri: string): Promise> => { + if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return new Promise(() => null) + return await getContentNode('NodeByUri', 'nodeByUri', { uri: uri, }) - return node } export const useNodeByUri = _useNodeByUri diff --git a/src/runtime/composables/usePages.ts b/src/runtime/composables/usePages.ts index ddfc917..07654d9 100644 --- a/src/runtime/composables/usePages.ts +++ b/src/runtime/composables/usePages.ts @@ -1,18 +1,20 @@ +import type { FetchError } from 'ofetch' import { getContentNode, getContentNodes } from './useWPContent' import type { Page } from '#graphql-operations' +import type { AsyncData } from '#app' -const _usePages = async (): Promise => { +const _usePages = async (): Promise> => { return await getContentNodes('Pages', 'pages', 'nodes') } -const _usePageByUri = async (uri: string): Promise => { - if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return +const _usePageByUri = async (uri: string): Promise> => { + if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return new Promise(() => null) return await getContentNode('PageByUri', 'nodeByUri', { uri: uri, }) } -const _usePageById = async (id: number, asPreview?: boolean): Promise => { +const _usePageById = async (id: number, asPreview?: boolean): Promise> => { return await getContentNode('PageById', 'page', { id: id, asPreview: asPreview ? true : false, diff --git a/src/runtime/composables/usePosts.ts b/src/runtime/composables/usePosts.ts index 8c95383..0201448 100644 --- a/src/runtime/composables/usePosts.ts +++ b/src/runtime/composables/usePosts.ts @@ -1,26 +1,30 @@ +import type { FetchError } from 'ofetch' import { getContentNode, getContentNodes } from './useWPContent' import type { Post } from '#graphql-operations' +import type { AsyncData } from '#app' +import { ref } from '#imports' -const _usePosts = async (): Promise => { - return await getContentNodes('Posts', 'posts', 'nodes') +const _usePosts = async (): Promise> => { + return await getContentNodes>('Posts', 'posts', 'nodes') } -const _useLatestPost = async (): Promise => { - const posts = await getContentNodes('LatestPost', 'posts', 'nodes') - if (!posts || !posts.length) { - return undefined +const _useLatestPost = async (): Promise> => { + const response = await getContentNodes>('LatestPost', 'posts', 'nodes') + if (response.data && response.data.value && response.data.value[0]) { + response.data = ref(response.data.value[0]) + return response } - return posts[0] + return new Promise(() => null) } -const _usePostByUri = async (uri: string): Promise => { - if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return +const _usePostByUri = async (uri: string): Promise> => { + if (!uri || uri === 'undefined' || uri === '_nuxt' || uri === '__nuxt') return new Promise(() => null) return await getContentNode('PostByUri', 'nodeByUri', { uri: uri, }) } -const _usePostById = async (id: number, asPreview?: boolean): Promise => { +const _usePostById = async (id: number, asPreview?: boolean): Promise> => { return await getContentNode('PostById', 'post', { id: id, asPreview: asPreview ? true : false, diff --git a/src/runtime/composables/useRevisions.ts b/src/runtime/composables/useRevisions.ts index ba1ca38..30ab326 100644 --- a/src/runtime/composables/useRevisions.ts +++ b/src/runtime/composables/useRevisions.ts @@ -1,7 +1,9 @@ +import type { FetchError } from 'ofetch' import { getContentNodes } from './useWPContent' import type { Revisions } from '#graphql-operations' +import type { AsyncData } from '#app' -const _useRevisions = async () => { +const _useRevisions = async (): Promise> => { return await getContentNodes('Revisions') } diff --git a/src/runtime/composables/useViewer.ts b/src/runtime/composables/useViewer.ts index 52d18e6..15eb43b 100644 --- a/src/runtime/composables/useViewer.ts +++ b/src/runtime/composables/useViewer.ts @@ -1,22 +1,10 @@ +import type { FetchError } from 'ofetch' import { getContentNode } from './useWPContent' -import type { ViewerQuery } from '#graphql-operations' +import type { Viewer } from '#graphql-operations' +import type { AsyncData } from '#app' -const _useViewer = async (): Promise => { - const viewer = await getContentNode('Viewer', 'viewer') - if (!viewer) return undefined - - return { - username: viewer?.username, - userId: viewer?.userId, - id: viewer?.id, - email: viewer?.email, - description: viewer?.description, - firstName: viewer?.firstName, - lastName: viewer?.lastName, - locale: viewer?.locale, - url: viewer?.url, - uri: viewer?.uri, - } +const _useViewer = async (): Promise> => { + return await getContentNode('Viewer', 'viewer') } export const useViewer = _useViewer diff --git a/src/runtime/composables/useWPContent.ts b/src/runtime/composables/useWPContent.ts index b39a3eb..2cc0493 100644 --- a/src/runtime/composables/useWPContent.ts +++ b/src/runtime/composables/useWPContent.ts @@ -1,26 +1,26 @@ -import { useWPNuxtLogger } from '../composables/wpNuxtLogger' +import type { FetchError } from 'ofetch' import { getRelativeImagePath } from '../util/images' import { useTokens } from './useTokens' import { useFetch, useNuxtApp } from '#imports' +import type { AsyncData } from '#app' -const _getContentNodes = async (queryName: string, node1Name?: string, node2Name?: string, node3Name?: string, params?: T): Promise => { +const _getContentNodes = async (queryName: string, node1Name?: string, node2Name?: string, node3Name?: string, params?: T): Promise> => { const node1 = node1Name ? node1Name : queryName.toLowerCase() const node2 = node2Name ? node2Name : undefined const node3 = node3Name || node3Name == null ? node3Name : undefined return await _fetchContentNode(queryName, node1, node2, node3, false, params) } -const _getContentNode = async (queryName: string, nodeName?: string, params?: T): Promise => { +const _getContentNode = async (queryName: string, nodeName?: string, params?: T): Promise> => { const node = nodeName ? nodeName : queryName.toLowerCase() return await _fetchContentNode(queryName, node, undefined, undefined, true, params) } -const _fetchContentNode = async (queryName: string, node1: string, node2: string | undefined, node3: string | undefined, fixImagePaths: boolean, params?: T): Promise => { +const _fetchContentNode = async (queryName: string, node1: string, node2: string | undefined, node3: string | undefined, fixImagePaths: boolean, params?: T) => { const nuxtApp = useNuxtApp() const tokens = useTokens() - const logger = useWPNuxtLogger() const cacheKey = `wp-${queryName}-${node1}-${node2}-${node3}-${JSON.stringify(params)}` - const { data, error } = await useFetch('/api/wpContent', { + return useFetch('/api/wpContent', { method: 'POST', body: { queryName: queryName, @@ -54,10 +54,6 @@ const _fetchContentNode = async (queryName: string, node1: string, node2: str return nuxtApp.payload.data[key] || nuxtApp.static.data[key] }, }) - if (error.value) { - logger.error('Error fetching content', error.value) - } - return data.value } export const getContentNodes = _getContentNodes diff --git a/src/runtime/composables/user.ts b/src/runtime/composables/user.ts index b99c28f..877819e 100644 --- a/src/runtime/composables/user.ts +++ b/src/runtime/composables/user.ts @@ -1,12 +1,12 @@ import { useLocalStorage } from '@vueuse/core' import { useViewer } from './useViewer' -import type { ViewerQuery } from '#graphql-operations' +import type { Viewer } from '#graphql-operations' const currentUserId = useLocalStorage('CURRENT_USER_ID', null) const currentUserName = useLocalStorage('CURRENT_USER_NAME', null) export async function loginUser(): Promise { - const viewer = await useViewer() + const { data: viewer } = await useViewer() currentUserId.value = viewer.userId if (viewer.firstName === undefined || viewer.firstName === null || viewer.firstName === '') { currentUserName.value = viewer.username diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts deleted file mode 100644 index cef188f..0000000 --- a/src/runtime/plugin.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { defineNuxtPlugin } from 'nuxt/app' - -/** - * This is only called when performing a query or mutation from within the nuxt - * app (e.g. not via custom server routes). - */ -export default defineNuxtPlugin(() => { -}) diff --git a/src/runtime/queries/fragments/NodeWithExcerpt.fragment.gql b/src/runtime/queries/fragments/NodeWithExcerpt.fragment.gql index 9b444aa..863497a 100644 --- a/src/runtime/queries/fragments/NodeWithExcerpt.fragment.gql +++ b/src/runtime/queries/fragments/NodeWithExcerpt.fragment.gql @@ -1,4 +1,3 @@ fragment NodeWithExcerpt on NodeWithExcerpt { excerpt - id }