Skip to content

Commit f5c8dd8

Browse files
committed
fix(llms-txt): resolve type errors in middleware and utils
Fix strict TypeScript errors: add fallback for split()[0] possibly undefined, narrow regex match group types, use non-null assertions for Record lookups after in-check, and use Nuxt's auto-generated Packument type instead of @npm/types import.
1 parent dd07537 commit f5c8dd8

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

server/middleware/llms-txt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const CACHE_HEADER = 's-maxage=3600, stale-while-revalidate=86400'
2626
* - /package/@:org/:name/v/:version/llms_full.txt (scoped, versioned, full)
2727
*/
2828
export default defineEventHandler(async event => {
29-
const path = event.path.split('?')[0]
29+
const path = event.path.split('?')[0] ?? '/'
3030

3131
if (!path.endsWith('/llms.txt') && !path.endsWith('/llms_full.txt')) return
3232

@@ -70,12 +70,12 @@ export default defineEventHandler(async event => {
7070
// Versioned path
7171
if (inner.startsWith('@')) {
7272
const match = inner.match(/^(@[^/]+\/[^/]+)\/v\/(.+)$/)
73-
if (!match) return
73+
if (!match?.[1] || !match[2]) return
7474
rawPackageName = match[1]
7575
rawVersion = match[2]
7676
} else {
7777
const match = inner.match(/^([^/]+)\/v\/(.+)$/)
78-
if (!match) return
78+
if (!match?.[1] || !match[2]) return
7979
rawPackageName = match[1]
8080
rawVersion = match[2]
8181
}

server/utils/llms-txt.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Packument } from '@npm/types'
21
import type { JsDelivrFileNode, AgentFile, LlmsTxtResult } from '#shared/types'
32
import { NPM_MISSING_README_SENTINEL, NPM_REGISTRY } from '#shared/utils/constants'
43

@@ -72,11 +71,12 @@ export function discoverAgentFiles(files: JsDelivrFileNode[]): string[] {
7271
* Get the display name for an agent file path.
7372
*/
7473
function getDisplayName(filePath: string): string {
75-
if (filePath in ROOT_AGENT_FILES) return ROOT_AGENT_FILES[filePath]
76-
if (filePath in DIRECTORY_AGENT_FILES) return DIRECTORY_AGENT_FILES[filePath]
74+
if (filePath in ROOT_AGENT_FILES) return ROOT_AGENT_FILES[filePath]!
75+
if (filePath in DIRECTORY_AGENT_FILES) return DIRECTORY_AGENT_FILES[filePath]!
7776

7877
for (const [dirPath, displayName] of Object.entries(RULE_DIRECTORIES)) {
79-
if (filePath.startsWith(`${dirPath}/`)) return `${displayName}: ${filePath.split('/').pop()}`
78+
if (filePath.startsWith(`${dirPath}/`))
79+
return `${displayName}: ${filePath.split('/').pop() ?? filePath}`
8080
}
8181

8282
return filePath
@@ -181,7 +181,10 @@ async function fetchReadmeFromCdn(packageName: string, version: string): Promise
181181
}
182182

183183
/** Extract README from packument data */
184-
function getReadmeFromPackument(packageData: Packument, requestedVersion?: string): string | null {
184+
function getReadmeFromPackument(
185+
packageData: Awaited<ReturnType<typeof fetchNpmPackage>>,
186+
requestedVersion?: string,
187+
): string | null {
185188
const readme = requestedVersion
186189
? packageData.versions[requestedVersion]?.readme
187190
: packageData.readme

0 commit comments

Comments
 (0)