Adding a New Registry Adapter
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Adding a new registry adapter to regxa involves implementing the Registry interface to normalize metadata from a specific ecosystem (e.g., npm, PyPI, Cargo) into a shared set of core types. Adapters act as plugin-based bridges that handle ecosystem-specific API quirks, name normalization, and data mapping, ensuring that the rest of the library can interact with any registry using a unified API.
The process requires creating a new implementation file in src/registries/, defining the data mapping logic, and registering the adapter with the core factory. This architecture ensures that the core system remains agnostic to individual registry implementations while providing a consistent interface for the CLI and other library consumers.
Sources: AGENTS.md:73-77, src/registries/AGENTS.md:5-10
Architecture and Contract
Registry adapters must implement the Registry interface defined in the core types. This interface dictates a set of asynchronous methods for fetching package details, versions, dependencies, and maintainers, as well as a urls() method for generating standardized links and Package URLs (PURLs).
The Registry Interface
| Method | Purpose |
|---|---|
ecosystem() | Returns the unique identifier for the ecosystem (e.g., "npm", "pypi"). |
fetchPackage(name, signal?) | Retrieves top-level package metadata. |
fetchVersions(name, signal?) | Lists all available versions with status and integrity hashes. |
fetchDependencies(name, version, signal?) | Resolves requirements for a specific package version. |
fetchMaintainers(name, signal?) | Lists people or entities with ownership/contribution roles. |
urls() | Returns a URLBuilder for generating registry, download, and PURL links. |
Sources: src/core/types.ts, src/registries/AGENTS.md:27-32
Data Flow Overview
The following diagram illustrates how a registry adapter interacts with the core Client and the registration factory.
Implementation Steps
1. Define the Adapter Class
Create a new file in src/registries/<ecosystem>.ts. The class should accept a baseURL and a Client instance in its constructor. All network calls MUST use the provided Client to ensure consistent retry, timeout, and rate-limiting behavior.
class MyRegistry implements Registry {
constructor(readonly baseURL: string, readonly client: Client) {}
ecosystem(): string {
return "my-ecosystem";
}
// ... implementation of other methods
}
Sources: src/registries/npm.ts:84-93, AGENTS.md:88-89
2. Implementation of Fetch Methods
Each fetch method should map the upstream JSON response to the corresponding core interface. For example, fetchPackage must return a Package object.
- Error Handling: Remote API failures (like 404 Not Found) should be mapped to core error classes like
NotFoundError. - Normalization: Use core utilities like
normalizeLicenseandnormalizeRepositoryURLto clean up upstream data.
Sources: src/registries/npm.ts:114-117, src/registries/pypi.ts:84-88, src/core/AGENTS.md:33-35
3. URL Building
The urls() method must return a URLBuilder that generates various links. Crucially, the purl() method should use buildPURL to ensure the identifier is ECMA-427 compliant.
Sources: src/registries/npm.ts:255-274, src/registries/pypi.ts:205-225
Factory Registration
To make the new adapter available to the system, it must be registered via the register() function. This typically happens at the bottom of the adapter file as a side effect.
Registration Pattern
Manual Wiring
After creating the file, you MUST import it in src/registries/index.ts. This ensures the side-effect registration occurs when the library is loaded.
Sources: src/registries/AGENTS.md:35-37, AGENTS.md:73-74
Best Practices and Anti-Patterns
| Category | Guideline |
|---|---|
| HTTP | Do not use fetch or ofetch directly. Always use this.client.getJSON. |
| Payloads | Do not return raw upstream payloads; always map to Core Types. |
| PURLs | Always use buildPURL for generating PURL strings. |
| Isolation | Avoid importing one adapter into another. Keep implementation self-contained. |
| Types | Use strict TypeScript; avoid as any or @ts-ignore. |
Sources: AGENTS.md:104-110, src/registries/AGENTS.md:35-37, src/core/AGENTS.md:38-40
Conclusion
Adding a new registry adapter expands the reach of regxa while maintaining a predictable interface for developers. By adhering to the normalization standards and utilizing the core Client and registration factory, new ecosystems can be integrated with minimal friction, automatically gaining features like caching and standardized PURL resolution.
Sources: README.md:14-20, AGENTS.md:73-77