UNPKG

@amadeus-it-group/kassette

Version:

Development server, used mainly for testing, which proxies requests and is able to easily manage local mocks.

60 lines (59 loc) 3.36 kB
import { RecursiveArray } from '../array'; import { HarFormatEntry, HarFormat } from './harTypes'; import { StructuredFile } from './structuredFile'; import { FileFormat } from './formats'; export declare const harFileMap: Map<string, HarFile>; export declare const getHarFile: (path: string, keepDelay: number, format: FileFormat) => HarFile; /** * 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. * * @remarks * * 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 {@link IMock.setMockHarKey}, or from {@link IMock.defaultMockHarKey | defaultMockHarKey}. * * In order to compute the {@link IMock.defaultMockHarKey | 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 {@link IMock.setMockHarKey}, it is stored in the * {@link HarFormatEntry._kassetteMockKey | _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. * * @example * * Here is the default har key manager: * ```ts * 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; * }; * ``` * * @public */ export type HarKeyManager = (entry: HarFormatEntry, key?: string) => RecursiveArray<string | null | undefined>; export declare const defaultHarKeyManager: HarKeyManager; export declare const callKeyManager: (keyManager: HarKeyManager, entry: HarFormatEntry, key?: string) => string | undefined; export declare class HarFile extends StructuredFile<HarFormat> { protected _keysMaps: WeakMap<HarKeyManager, Map<string, number>>; protected _afterRead(): void; private _getKeys; getEntry(key: string | undefined, keyManager: HarKeyManager): Promise<HarFormatEntry | undefined>; setEntry(key: string | undefined, entry: HarFormatEntry, keyManager: HarKeyManager): Promise<void>; }