The har key manager is a function that is called either to get the key of an entry (when the key parameter is undefined) or to set it (when the key parameter is defined).
It should not modify the entry when the key parameter is undefined.
When the key parameter is defined, the har key manager is supposed to change the provided entry, in order to store the key in it, because after the call, the entry will be persisted in the har file. In this case, the key parameter either comes from a call to IMock.setMockHarKey, or from defaultMockHarKey.
In order to compute the defaultMockHarKey property, the har key manager is called with an entry that includes the request but not the response (and with an undefined key parameter).
In all cases, the har key manager is expected to return the key of the entry. If an array is returned (which can be nested), it is flattened with null items removed, and joined with forward slashes to produce a string.
The default har key manager is expected to work fine for most use cases, especially when working with a har file recorded with kassette. With the default har key manager, if a key is set with IMock.setMockHarKey, it is stored in the _kassetteMockKey field. Otherwise, the default key is the concatenation of the request method and url, with a separating forward slash. It can be useful to replace the default har key manager with a custom one especially when working with har files that are produced by other applications than kassette, if the default key is not convenient.
Here is the default har key manager:
export const defaultHarKeyManager: HarKeyManager = (entry: HarFormatEntry, key?: string) => {
const defaultKey = joinPath(entry._kassetteMockKey ?? [entry.request?.method, entry.request?.url]);
if (key && key !== defaultKey) {
entry._kassetteMockKey = key;
return key;
}
return defaultKey;
};
Each entry in a har file is supposed to have a corresponding unique key (a string). The har key manager is both a getter and a setter for the key of an entry.