CachingTokenProvider
Caches the token from an access-token provider until shortly before it expires, so the provider is consulted once per token lifetime instead of once per request.
Expiry comes from the token itself: Ledger API tokens are JWTs, and the standard exp claim is read from the payload. A token without a parseable exp is cached until refreshIfChanged forces a fetch — the right behaviour for non-expiring development tokens.
refreshIfChanged exists for the failure path: when the server rejects a connection for authentication reasons, the caller fetches fresh and learns whether retrying can possibly help. A provider that returns the same token it returned before has nothing new to offer, and the rejection is final.
Functions
The cached token, refreshed via fetch when within refreshLeeway of expiry.