Skip to content

Commit 7056369

Browse files
committed
chore: passing typechecking
1 parent eaf7412 commit 7056369

File tree

21 files changed

+84
-71
lines changed

21 files changed

+84
-71
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ jobs:
3434
- name: Build
3535
run: pnpm run build
3636

37+
- name: Lint
38+
run: pnpm run lint
39+
40+
- name: Typecheck
41+
run: pnpm run typecheck
42+
3743
- name: Test
3844
run: pnpm run test

client/composables/rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NuxtDevtoolsClient, NuxtDevtoolsIframeClient } from '@nuxt/devtool
22
import type { $Fetch } from 'nitropack'
33
import { onDevtoolsClientConnected } from '@nuxt/devtools-kit/iframe-client'
44
import { ref, watchEffect } from 'vue'
5-
import { path, query, refreshSources } from '~/util/logic'
5+
import { path, query, refreshSources } from '../util/logic'
66

77
export const appFetch = ref<$Fetch>()
88

@@ -13,7 +13,7 @@ export const devtoolsClient = ref<NuxtDevtoolsIframeClient>()
1313
export const colorMode = ref<'dark' | 'light'>('dark')
1414

1515
onDevtoolsClientConnected(async (client) => {
16-
appFetch.value = client.host.app.$fetch
16+
appFetch.value = client.host.app.$fetch as $Fetch
1717
watchEffect(() => {
1818
colorMode.value = client.host.app.colorMode.value
1919
})

src/i18n.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NuxtI18nOptions } from '@nuxtjs/i18n/dist/module'
1+
import type { ModuleOptions as NuxtI18nOptions } from '@nuxtjs/i18n'
22
import type { AutoI18nConfig, NormalisedLocales } from './runtime/types'
33
import { getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from '@nuxt/kit'
44
import { getNuxtModuleOptions } from './kit'
@@ -30,7 +30,7 @@ export async function resolveI18nConfig() {
3030
if (!await hasNuxtModuleCompatibility('@nuxtjs/i18n', '>=8'))
3131
logger.warn(`You are using @nuxtjs/i18n v${i18nVersion}. For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.`)
3232
nuxtI18nConfig = (await getNuxtModuleOptions('@nuxtjs/i18n') || {}) as NuxtI18nOptions
33-
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map(locale => typeof locale === 'string' ? { code: locale } : locale), 'code')
33+
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: Required<NuxtI18nOptions>['locales'][number]) => typeof locale === 'string' ? { code: locale } : locale), 'code')
3434
const usingI18nPages = Object.keys(nuxtI18nConfig.pages || {}).length
3535
const hasI18nConfigForAlternatives = nuxtI18nConfig.differentDomains || usingI18nPages || (nuxtI18nConfig.strategy !== 'no_prefix' && nuxtI18nConfig.locales)
3636
if (hasI18nConfigForAlternatives) {

src/module.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Arrayable, AutoI18nConfig, RobotsGroupInput, RobotsGroupResolved } from './runtime/types'
1+
import type { Arrayable, AutoI18nConfig, NuxtRobotsRuntimeConfig, RobotsGroupInput, RobotsGroupResolved } from './runtime/types'
22
import fsp from 'node:fs/promises'
33
import {
44
addImports,
@@ -449,7 +449,7 @@ export default defineNuxtModule<ModuleOptions>({
449449
}
450450
}
451451

452-
nuxt.options.runtimeConfig['nuxt-robots'] = {
452+
const robotsRuntimeConfig: NuxtRobotsRuntimeConfig = {
453453
version: version || '',
454454
isNuxtContentV2,
455455
debug: config.debug,
@@ -459,26 +459,21 @@ export default defineNuxtModule<ModuleOptions>({
459459
header: config.header,
460460
robotsEnabledValue: config.robotsEnabledValue,
461461
robotsDisabledValue: config.robotsDisabledValue,
462-
// @ts-expect-error untyped
463-
cacheControl: config.cacheControl,
462+
cacheControl: config.cacheControl ?? 'max-age=14400, must-revalidate',
464463
}
464+
nuxt.options.runtimeConfig['nuxt-robots'] = robotsRuntimeConfig as any
465465
})
466466

467467
addTypeTemplate({
468468
filename: 'module/nuxt-robots.d.ts',
469469
getContents: (data) => {
470470
const typesPath = relative(resolve(data.nuxt!.options.rootDir, data.nuxt!.options.buildDir, 'module'), resolve('runtime/types'))
471-
return `// Generated by nuxt-robots
472-
473-
import type { RobotsContext } from '#robots/types'
474-
475-
declare module 'nitropack/types' {
476-
interface NitroApp {
471+
const types = ` interface NitroApp {
477472
_robots: {
478473
ctx: import('${typesPath}').HookRobotsConfigContext
479474
nuxtContentUrls?: Set<string>
480475
},
481-
_robotsRuleMatcher: (url: string) => string
476+
_robotsRuleMatcher: (url: string) => any
482477
}
483478
interface NitroRouteRules {
484479
robots?: boolean | string | {
@@ -495,33 +490,17 @@ declare module 'nitropack/types' {
495490
interface NitroRuntimeHooks {
496491
'robots:config': (ctx: import('${typesPath}').HookRobotsConfigContext) => void | Promise<void>
497492
'robots:robots-txt': (ctx: import('${typesPath}').HookRobotsTxtContext) => void | Promise<void>
498-
}
493+
}`
494+
return `// Generated by nuxt-robots
495+
496+
import type { RobotsContext } from '#robots/types'
497+
498+
declare module 'nitropack/types' {
499+
${types}
499500
}
500501
501502
declare module 'nitropack' {
502-
interface NitroApp {
503-
_robots: {
504-
ctx: import('${typesPath}').HookRobotsConfigContext
505-
nuxtContentUrls?: Set<string>
506-
},
507-
_robotsRuleMatcher: (url: string) => string
508-
}
509-
interface NitroRouteRules {
510-
robots?: boolean | string | {
511-
indexable: boolean
512-
rule: string
513-
}
514-
}
515-
interface NitroRouteConfig {
516-
robots?: boolean | string | {
517-
indexable: boolean
518-
rule: string
519-
}
520-
}
521-
interface NitroRuntimeHooks {
522-
'robots:config': (ctx: import('${typesPath}').HookRobotsConfigContext) => void | Promise<void>
523-
'robots:robots-txt': (ctx: import('${typesPath}').HookRobotsTxtContext) => void | Promise<void>
524-
}
503+
${types}
525504
}
526505
527506
declare module 'h3' {
@@ -535,6 +514,7 @@ export {}
535514
},
536515
}, {
537516
nitro: true,
517+
nuxt: true,
538518
})
539519

540520
// only prerender for `nuxi generate`

src/runtime/app/composables/useRobotsRule.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { MaybeRef } from 'vue'
2-
// @ts-expect-error untyped
32
import { devRootDir } from '#build/nuxt.config.mjs'
43
import { injectHead, useHead } from '#imports'
54
import { setHeader } from 'h3'
@@ -45,7 +44,7 @@ export function useRobotsRule(rule?: MaybeRef<boolean | string>) {
4544
{
4645
'name': 'robots',
4746
'content': _rule,
48-
'data-hint': import.meta.dev ? ['useRobotsRule', `.${vmFile ? vm.type?.__file?.split(devRootDir)[1] : ''}`].filter(Boolean).join(',') : undefined,
47+
'data-hint': import.meta.dev ? ['useRobotsRule', `.${vmFile ? vm.type?.__file?.split(devRootDir || '')[1] || '' : ''}`].filter(Boolean).join(',') : undefined,
4948
},
5049
],
5150
}, {

src/runtime/server/composables/getPathRobotConfig.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { matchPathToRule } from '../../util'
77
import { createNitroRouteRuleMatcher } from '../kit'
88
import { normaliseRobotsRouteRule } from '../nitro'
99
import { getSiteRobotConfig } from './getSiteRobotConfig'
10+
import { useRuntimeConfigNuxtRobots } from './useRuntimeConfigNuxtRobots'
1011

1112
export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, skipSiteIndexable?: boolean, path?: string }): RobotsContext {
1213
const runtimeConfig = useRuntimeConfig(e)
1314
// has already been resolved
14-
const { robotsDisabledValue, robotsEnabledValue, isNuxtContentV2 } = runtimeConfig['nuxt-robots']
15+
const { robotsDisabledValue, robotsEnabledValue, isNuxtContentV2 } = useRuntimeConfigNuxtRobots(e)
1516
if (!options?.skipSiteIndexable) {
1617
if (!getSiteRobotConfig(e).indexable) {
1718
return {
@@ -57,7 +58,7 @@ export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, s
5758
},
5859
}
5960
}
60-
const robotsTxtRule = matchPathToRule(path, group._rules)
61+
const robotsTxtRule = matchPathToRule(path, group._rules || [])
6162
if (robotsTxtRule) {
6263
if (!robotsTxtRule.allow) {
6364
return {
@@ -90,7 +91,9 @@ export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, s
9091
let routeRulesPath = path
9192
// if we're using i18n we need to strip leading prefixes so the rule will match
9293
if (runtimeConfig.public?.i18n?.locales) {
93-
const { locales } = runtimeConfig.public.i18n
94+
const { locales } = runtimeConfig.public.i18n as {
95+
locales: { code: string }[]
96+
}
9497
const locale = locales.find(l => routeRulesPath.startsWith(`/${l.code}`))
9598
if (locale) {
9699
routeRulesPath = routeRulesPath.replace(`/${locale.code}`, '')
@@ -99,7 +102,7 @@ export function getPathRobotConfig(e: H3Event, options?: { userAgent?: string, s
99102
const routeRules = normaliseRobotsRouteRule(nitroApp._robotsRuleMatcher(routeRulesPath))
100103
if (routeRules && (typeof routeRules.allow !== 'undefined' || typeof routeRules.rule !== 'undefined')) {
101104
return {
102-
indexable: routeRules.allow,
105+
indexable: routeRules.allow ?? false,
103106
rule: routeRules.rule || (routeRules.allow ? robotsEnabledValue : robotsDisabledValue),
104107
debug: {
105108
source: 'Route Rules',

src/runtime/server/composables/getSiteRobotConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import type { ParsedRobotsTxt } from '../../types'
33
import { getSiteIndexable } from '#site-config/server/composables/getSiteIndexable'
44
import { useSiteConfig } from '#site-config/server/composables/useSiteConfig'
55
import { getQuery } from 'h3'
6-
import { useRuntimeConfig } from 'nitropack/runtime'
6+
import { useRuntimeConfigNuxtRobots } from './useRuntimeConfigNuxtRobots'
77

88
export function getSiteRobotConfig(e: H3Event): { indexable: boolean, hints: string[] } {
99
// move towards deprecating indexable
1010
const query = getQuery(e)
1111
const hints: string[] = []
12-
const { groups, debug } = useRuntimeConfig(e)['nuxt-robots']
12+
const { groups, debug } = useRuntimeConfigNuxtRobots(e)
1313
let indexable = getSiteIndexable(e)
1414
// allow previewing with ?mockProductionEnv
1515
const queryIndexableEnabled = String(query.mockProductionEnv) === 'true' || query.mockProductionEnv === ''
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { H3Event } from 'h3'
2+
import type { NuxtRobotsRuntimeConfig } from '../../types'
3+
import { useRuntimeConfig } from 'nitropack/runtime'
4+
5+
export function useRuntimeConfigNuxtRobots(event?: H3Event): NuxtRobotsRuntimeConfig {
6+
return useRuntimeConfig(event)['nuxt-robots'] as NuxtRobotsRuntimeConfig
7+
}

src/runtime/server/middleware/injectContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { defineEventHandler, getQuery, setHeader } from 'h3'
2-
import { useRuntimeConfig } from 'nitropack/runtime'
32
import { getPathRobotConfig } from '../composables/getPathRobotConfig'
3+
import { useRuntimeConfigNuxtRobots } from '../composables/useRuntimeConfigNuxtRobots'
44

55
export default defineEventHandler(async (e) => {
66
if (e.path === '/robots.txt' || e.path.startsWith('/__') || e.path.startsWith('/api') || e.path.startsWith('/_nuxt'))
77
return
8-
const nuxtRobotsConfig = useRuntimeConfig(e)['nuxt-robots']
8+
const nuxtRobotsConfig = useRuntimeConfigNuxtRobots(e)
99
if (nuxtRobotsConfig) {
1010
const { header } = nuxtRobotsConfig
1111
const robotConfig = getPathRobotConfig(e, { skipSiteIndexable: Boolean(getQuery(e)?.mockProductionEnv) })

src/runtime/server/plugins/initContext.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NitroApp } from 'nitropack/types'
2-
import { defineNitroPlugin, getRouteRules, useRuntimeConfig } from 'nitropack/runtime'
2+
import { defineNitroPlugin, getRouteRules } from 'nitropack/runtime'
33
import { withoutTrailingSlash } from 'ufo'
4+
import { useRuntimeConfigNuxtRobots } from '../composables/useRuntimeConfigNuxtRobots'
45
import { logger } from '../logger'
56
import { resolveRobotsTxtContext } from '../util'
67

@@ -9,7 +10,7 @@ const PRERENDER_NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html'
910
// we need to init our state using a nitro plugin so the user doesn't throttle the resolve context hook
1011
// important when we integrate with nuxt-simple-sitemap and we're checking thousands of URLs
1112
export default defineNitroPlugin(async (nitroApp: NitroApp) => {
12-
const { isNuxtContentV2, robotsDisabledValue } = useRuntimeConfig()['nuxt-robots']
13+
const { isNuxtContentV2, robotsDisabledValue } = useRuntimeConfigNuxtRobots()
1314
nitroApp._robots = {} as typeof nitroApp._robots
1415
await resolveRobotsTxtContext(undefined, nitroApp)
1516
const nuxtContentUrls = new Set<string>()
@@ -36,6 +37,7 @@ export default defineNitroPlugin(async (nitroApp: NitroApp) => {
3637
const isIsland = (process.env.NUXT_COMPONENT_ISLANDS && event.path.startsWith('/__nuxt_island'))
3738
const noSSR = !!(process.env.NUXT_NO_SSR)
3839
|| event.context.nuxt?.noSSR
40+
// @ts-expect-error upstream type issue
3941
|| (routeOptions.ssr === false && !isIsland)
4042
|| (import.meta.prerender ? PRERENDER_NO_SSR_ROUTES.has(event.path) : false)
4143
if (noSSR) {

0 commit comments

Comments
 (0)