import { buildUrlAndHeadersForRegistryItem } from "@/src/registry/builder" import { configWithDefaults } from "@/src/registry/config" import { clearRegistryContext } from "@/src/registry/context" import { RegistryInvalidNamespaceError, RegistryNotFoundError, RegistryParseError, } from "@/src/registry/errors" import { fetchRegistry } from "@/src/registry/fetcher" import { fetchRegistryItems, resolveRegistryTree, } from "@/src/registry/resolver" import { type RegistryItemCategory, registrySchema, } from "@/src/registry/schema" import { isUrl } from "@/src/registry/utils" import type { Config } from "@/src/schema" export async function getRegistry( name: string, type: RegistryItemCategory, options?: { config?: Partial }, ) { const { config } = options || {} if (isUrl(name)) { const [result] = await fetchRegistry([name]) try { return registrySchema.parse(result) } catch (error) { throw new RegistryParseError(name, error) } } if (!name.startsWith("@")) { throw new RegistryInvalidNamespaceError(name) } let registryName = name if (!registryName.endsWith("/registry")) { registryName = `${registryName}/registry` } const urlAndHeaders = buildUrlAndHeadersForRegistryItem( registryName as `@${string}`, type, configWithDefaults(config), ) if (!urlAndHeaders?.url) { throw new RegistryNotFoundError(registryName) } const [result] = await fetchRegistry([urlAndHeaders.url]) try { return registrySchema.parse(result) } catch (error) { throw new RegistryParseError(registryName, error) } } export async function getRegistryItems( items: string[], type: RegistryItemCategory, options?: { config?: Partial }, ) { const { config } = options || {} clearRegistryContext() return fetchRegistryItems(items, type, configWithDefaults(config)) } export async function resolveRegistryItems( items: string[], type: RegistryItemCategory, options?: { config?: Partial }, ) { const { config } = options || {} clearRegistryContext() return resolveRegistryTree(items, type, configWithDefaults(config)) }