MCPcopy
hub / github.com/npmx-dev/npmx.dev / fetchFromFixtures

Function fetchFromFixtures

modules/runtime/server/cache.ts:594–771  ·  view source on GitHub ↗

* Shared fixture-backed fetch implementation. * This is used by both cachedFetch and the global $fetch override.

(
  url: string,
  storage: ReturnType<typeof useStorage>,
)

Source from the content-addressed store, hash-verified

592 * This is used by both cachedFetch and the global $fetch override.
593 */
594async function fetchFromFixtures<T>(
595 url: string,
596 storage: ReturnType<typeof useStorage>,
597): Promise<CachedFetchResult<T>> {
598 // Check for mock responses (OSV, JSR)
599 const mockResult = getMockForUrl(url)
600 if (mockResult) {
601 if (VERBOSE) process.stdout.write(`[test-fixtures] Mock: ${url}\n`)
602 return { data: mockResult.data as T, isStale: false, cachedAt: Date.now() }
603 }
604
605 // Check for fast-npm-meta
606 const fastNpmMetaResult = await handleFastNpmMeta(url, storage)
607 if (fastNpmMetaResult) {
608 if (VERBOSE) process.stdout.write(`[test-fixtures] Fast-npm-meta: ${url}\n`)
609 return { data: fastNpmMetaResult.data as T, isStale: false, cachedAt: Date.now() }
610 }
611
612 const jsdelivrResult = await handleJsdelivrDataApi(url, storage)
613 if (jsdelivrResult) {
614 if (VERBOSE) process.stdout.write(`[test-fixtures] jsDelivr Data API: ${url}\n`)
615 return { data: jsdelivrResult.data as T, isStale: false, cachedAt: Date.now() }
616 }
617
618 // Check for GitHub API
619 const githubResult = await handleGitHubApi(url, storage)
620 if (githubResult) {
621 if (VERBOSE) process.stdout.write(`[test-fixtures] GitHub API: ${url}\n`)
622 return { data: githubResult.data as T, isStale: false, cachedAt: Date.now() }
623 }
624
625 const match = matchUrlToFixture(url)
626
627 if (!match) {
628 logUnmockedRequest('NO FIXTURE PATTERN', 'URL does not match any known fixture pattern', url)
629 throw createError({
630 statusCode: 404,
631 statusMessage: 'No test fixture available',
632 message: `No fixture pattern matches URL: ${url}`,
633 })
634 }
635
636 const fixturePath = getFixturePath(match.type, match.name)
637 const rawData = await storage.getItem<any>(fixturePath)
638
639 if (rawData === null) {
640 // For user searches or search queries without fixtures, return empty results
641 if (match.type === 'user' || match.type === 'search') {
642 if (VERBOSE) process.stdout.write(`[test-fixtures] Empty ${match.type}: ${match.name}\n`)
643 return {
644 data: { objects: [], total: 0, time: new Date().toISOString() } as T,
645 isStale: false,
646 cachedAt: Date.now(),
647 }
648 }
649
650 // For org packages without fixtures, return 404
651 if (match.type === 'org') {

Callers 1

cache.tsFile · 0.85

Calls 8

getMockForUrlFunction · 0.85
handleFastNpmMetaFunction · 0.85
handleJsdelivrDataApiFunction · 0.85
handleGitHubApiFunction · 0.85
matchUrlToFixtureFunction · 0.85
logUnmockedRequestFunction · 0.85
getFixturePathFunction · 0.85
writeMethod · 0.80

Tested by

no test coverage detected