Home / Function/ fetchRegistry() — ui Function Reference

fetchRegistry() — ui Function Reference

Architecture documentation for the fetchRegistry() function in fetcher.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  9f199ac7_f8ed_fb51_bdb3_647d06922390["fetchRegistry()"]
  16316120_c278_1dd6_844b_6f1683d22437["fetcher.ts"]
  9f199ac7_f8ed_fb51_bdb3_647d06922390 -->|defined in| 16316120_c278_1dd6_844b_6f1683d22437
  style 9f199ac7_f8ed_fb51_bdb3_647d06922390 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/registry/fetcher.ts lines 29–121

export async function fetchRegistry(
  paths: string[],
  options: { useCache?: boolean } = {}
) {
  options = {
    useCache: true,
    ...options,
  }

  try {
    const results = await Promise.all(
      paths.map(async (path) => {
        const url = resolveRegistryUrl(path)

        // Check cache first if caching is enabled
        if (options.useCache && registryCache.has(url)) {
          return registryCache.get(url)
        }

        // Store the promise in the cache before awaiting if caching is enabled.
        const fetchPromise = (async () => {
          // Get headers from context for this URL.
          const headers = getRegistryHeadersFromContext(url)

          const response = await fetch(url, {
            agent,
            headers: {
              ...headers,
            },
          })

          if (!response.ok) {
            let messageFromServer = undefined

            if (
              response.headers.get("content-type")?.includes("application/json")
            ) {
              const json = await response.json()
              const parsed = z
                .object({
                  // RFC 7807.
                  detail: z.string().optional(),
                  title: z.string().optional(),
                  // Standard error response.
                  message: z.string().optional(),
                  error: z.string().optional(),
                })
                .safeParse(json)

              if (parsed.success) {
                // Prefer RFC 7807 detail field, then message field.
                messageFromServer = parsed.data.detail || parsed.data.message

                if (parsed.data.error) {
                  messageFromServer = `[${parsed.data.error}] ${messageFromServer}`
                }
              }
            }

            if (response.status === 401) {
              throw new RegistryUnauthorizedError(url, messageFromServer)
            }

            if (response.status === 404) {
              throw new RegistryNotFoundError(url, messageFromServer)
            }

            if (response.status === 403) {
              throw new RegistryForbiddenError(url, messageFromServer)
            }

            throw new RegistryFetchError(
              url,
              response.status,
              messageFromServer
            )
          }

          return response.json()
        })()

Subdomains

Frequently Asked Questions

What does fetchRegistry() do?
fetchRegistry() is a function in the ui codebase, defined in packages/shadcn/src/registry/fetcher.ts.
Where is fetchRegistry() defined?
fetchRegistry() is defined in packages/shadcn/src/registry/fetcher.ts at line 29.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free