resolveEndpointKey function
Resolves the API key for baseUrl given the catalog env envNames and
the spec's defaultBaseUrl. envRead reads genuine environment values;
storeRead reads the secure store (null store = env-only resolution,
e.g. tests or the web build). activeCustomKeyName is the active custom
registry entry's own key name, when known (wins over the host-scoped
entry for non-default endpoints).
Implementation
String? resolveEndpointKey({
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 name = resolveEndpointKeyName(
envNames: envNames,
defaultBaseUrl: defaultBaseUrl,
baseUrl: baseUrl,
envRead: envRead,
storeRead: storeRead,
activeCustomKeyName: activeCustomKeyName,
);
if (name == null) return null;
// A name from the env leg resolves from the environment (the name step
// already verified the value is genuine); every other leg reads the
// store. One chain — the value can never disagree with the slot.
if (envNames.contains(name)) {
final env = envRead(name);
if (env != null && env.isNotEmpty && env != storeRead?.call(name)) {
return env;
}
}
return storeRead?.call(name);
}