Skip to content

Commit

Permalink
fix: types for nuxt runtime hook schema-org:meta
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
harlan-zw committed Dec 13, 2023
1 parent c496f6c commit 17a8448
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { addTemplate, createResolver, useNuxt } from '@nuxt/kit'
import { relative } from 'pathe'

export function extendTypes(module: string, template: (options: { typesPath: string }) => string | Promise<string>) {
const nuxt = useNuxt()
const { resolve } = createResolver(import.meta.url)
// paths.d.ts
addTemplate({
filename: `module/${module}.d.ts`,
getContents: async () => {
const typesPath = relative(resolve(nuxt!.options.rootDir, nuxt!.options.buildDir, 'module'), resolve('runtime/types'))
const s = await template({ typesPath })
return `// Generated by ${module}
${s}
export {}
`
},
})

nuxt.hooks.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, `module/${module}.d.ts`) })
})
}
14 changes: 11 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
import { schemaOrgAutoImports, schemaOrgComponents } from '@unhead/schema-org/vue'
import type { NuxtModule } from '@nuxt/schema'
import { installNuxtSiteConfig } from 'nuxt-site-config-kit'
import type { MetaInput } from '@unhead/schema-org'
import type { ScriptBase, TagUserProperties } from '@unhead/schema'
import { version } from '../package.json'
import { setupDevToolsUI } from './devtools'
import type { ModuleRuntimeConfig } from './runtime/types'
import type { ModuleRuntimeConfig, MetaInput } from './runtime/types'
import {extendTypes} from "./kit";

export interface ModuleOptions {
/**
Expand Down Expand Up @@ -52,7 +52,6 @@ export interface ModuleOptions {
}

export interface ModuleHooks {
'schema-org:meta': (meta: MetaInput) => void | Promise<void>
}

export interface ModulePublicRuntimeConfig {
Expand Down Expand Up @@ -138,6 +137,15 @@ export default defineNuxtModule<ModuleOptions>({
autoImports.unshift(...schemaOrgAutoImports)
})

extendTypes('nuxt-schema-org', ({ typesPath }) => {
return `
declare module '@nuxt/schema' {
export interface RuntimeNuxtHooks {
'schema-org:meta': (meta: import('${typesPath}').MetaInput) => void | Promise<void>
}
}`
})

if (config.debug || nuxt.options.dev) {
addServerHandler({
route: '/__schema-org__/debug.json',
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { ModuleOptions } from '../module'
import type { MetaInput } from '@unhead/schema-org'

export type ModuleRuntimeConfig = Pick<ModuleOptions, 'scriptAttributes' | 'reactive' | 'minify'> & { version: string }

export type { MetaInput }

0 comments on commit 17a8448

Please sign in to comment.