UNPKG

@pinax/graph-networks-registry

Version:

TypeScript types and helpers for The Graph Networks Registry

175 lines (174 loc) 6.61 kB
import { type NetworksRegistryInner, type Network, type APIURLKind } from "./types.js"; /** * Client for interacting with The Graph Networks Registry. * Provides methods to load the registry from json, file or URL. */ export declare class NetworksRegistry { private registry; /** * Creates a new NetworksRegistry instance. * @param registry - The parsed NetworksRegistry data */ constructor(registry: NetworksRegistryInner); /** * Gets all networks in the registry. * @returns Array of all network elements */ get networks(): Network[]; /** * Gets the version of the loaded registry. * @returns Version string */ get version(): string; /** * Gets the date of the last update of the registry. * @returns Date object */ get updatedAt(): Date; /** * Attempts to fetch the registry from a given URL, returns null if fetch fails * @internal */ private static tryFetchRegistry; /** * Fetches and loads the latest version of the networks registry. First tries to fetch from * the primary registry URL at networks-registry.thegraph.com, then falls back to the fallback URL at GitHub * Uses the library version to determine the latest compatible registry URL. * Library version 0.5.x will use the latest registry version 0.5.y even if 0.6.z is available * * @returns Promise that resolves to a new NetworksRegistry instance * @throws Error if the registry fetch fails * * @example * ```typescript * const registry = await NetworksRegistry.fromLatestVersion(); * ``` */ static fromLatestVersion(): Promise<NetworksRegistry>; /** * Fetches and loads a specific version of the networks registry. First tries to fetch from * the primary registry URL at networks-registry.thegraph.com, then falls back to the fallback URL at GitHub * * @param version - The exact version to fetch (e.g. "0.5.0") * @returns Promise that resolves to a new NetworksRegistry instance * @throws Error if the registry fetch fails * * @example * ```typescript * const registry = await NetworksRegistry.fromExactVersion("0.5.0"); * ``` */ static fromExactVersion(version: string): Promise<NetworksRegistry>; /** * Loads the networks registry from a URL. * * @param url - The URL to fetch the registry from * @returns Promise that resolves to a new NetworksRegistry instance * @throws Error if the fetch fails or the response is invalid */ static fromUrl(url: string): Promise<NetworksRegistry>; /** * Creates a new registry instance from a JSON string. * * @param json - The JSON string containing registry data * @returns A new NetworksRegistry instance * @throws Error if the JSON is invalid */ static fromJson(json: string): NetworksRegistry; /** * Loads the networks registry from a local JSON file. * * @param path - Path to the JSON file * @returns A new NetworksRegistry instance * @throws Error if the file cannot be read or contains invalid data */ static fromFile(path: string): NetworksRegistry; /** * Gets the URL for the latest compatible version of the registry. * Uses the major and minor version from package.json. * * @returns The URL string for the latest version */ static getLatestVersionUrl(): string; /** * Gets the URL for the latest compatible version of the registry at GitHub. * Uses the major and minor version from package.json. * * @returns The URL string for the latest version */ static getLatestVersionFallbackUrl(): string; /** * Gets the URL for a specific version of the registry. * * @param version - The exact version (e.g. "0.5.0") * @returns The URL string for the specified version */ static getExactVersionUrl(version: string): string; /** * Gets the URL for a specific version of the registry at GitHub. * * @param version - The exact version (e.g. "0.5.0") * @returns The URL string for the specified version */ static getExactVersionFallbackUrl(version: string): string; /** * Finds a network by its unique identifier. * * @param id - The network ID (e.g. "mainnet", "optimism") * @returns The network if found, undefined otherwise * * @example * ```typescript * const mainnet = registry.getNetworkById("mainnet"); * ``` */ getNetworkById(id: string): Network | undefined; /** * Finds a network by its ID or one of its aliases. * * @param alias - The network ID or alias (e.g. "eth" for Ethereum mainnet) * @returns The network if found, undefined otherwise * * @example * ```typescript * const ethereum = registry.getNetworkByAlias("eth"); * ``` */ getNetworkByAlias(alias: string): Network | undefined; /** * Gets API URLs for a network, filtered by kind and with environment variables applied. * Environment variable placeholders in the format {VARIABLE_NAME} will be replaced with * actual environment variable values. URLs that reference non-existent environment * variables will be omitted from the result. * * @param networkId - The network ID or alias * @param kinds - Optional array of API URL kinds to filter by. If not provided or empty, returns all kinds * @returns Array of API URLs with environment variables applied * * @example * ```typescript * // Get all Etherscan API URLs * const etherscanUrls = registry.getApiUrls("mainnet", [APIURLKind.Etherscan]); * * // Get all API URLs for the network * const allUrls = registry.getApiUrls("mainnet"); * ``` */ getApiUrls(networkId: string, kinds?: APIURLKind[]): string[]; /** * Gets RPC URLs for a network with environment variables applied. * Environment variable placeholders in the format {VARIABLE_NAME} will be replaced with * actual environment variable values. URLs that reference non-existent environment * variables will be omitted from the result. * * @param networkId - The network ID or alias * @returns Array of RPC URLs with environment variables applied * * @example * ```typescript * // Get all RPC URLs for ethereum mainnet * const rpcUrls = registry.getRpcUrls("mainnet"); * ``` */ getRpcUrls(networkId: string): string[]; }