resolveEndpointKeyName function

String? resolveEndpointKeyName({
  1. required List<String> envNames,
  2. required String? defaultBaseUrl,
  3. required String baseUrl,
  4. required String? envRead(
    1. String name
    ),
  5. required String? storeRead(
    1. String name
    )?,
  6. String? activeCustomKeyName,
})

Resolves the effective key-slot NAME for baseUrl — the host-facing half of the same chain resolveEndpointKey resolves values through (issue #1322 Gap 2). Order mirrors the value chain exactly:

  1. (default endpoint only) the first envNames entry holding a genuine environment value — the ecosystem convention stays first;
  2. the endpoint-scoped secure-store slot (FA_KEY_<HOST>, or activeCustomKeyName for a non-default endpoint — the entry's own pinned slot wins over the host-scoped one);
  3. (default endpoint only) legacy env-name store entries.

defaultBaseUrl is nullable: a host that does not know the catalog default passes null (the default) and gets store-only resolution — the env-name leg can then never hijack a custom endpoint (issue #40).

Implementation

String? resolveEndpointKeyName({
  required List<String> envNames,
  required String? defaultBaseUrl,
  required String baseUrl,
  required String? Function(String name) envRead,
  required String? Function(String name)? storeRead,
  String? activeCustomKeyName,
}) {
  final defaultEndpoint = defaultBaseUrl != null && baseUrl == defaultBaseUrl;
  if (defaultEndpoint) {
    final genuine = _firstGenuineEnvName(envNames, envRead, storeRead);
    if (genuine != null) return genuine;
  }
  final pinned = _pinnedSlotName(
    defaultEndpoint,
    activeCustomKeyName,
    storeRead,
  );
  if (pinned != null) return pinned;
  final scoped = CustomProviderRegistry.keyNameFor(baseUrl);
  if (_hasStoreValue(scoped, storeRead)) return scoped;
  if (defaultEndpoint) return _legacyEnvSlotName(envNames, storeRead);
  return null;
}