The public interface exposed to the end user to handle a given request and the associated mock and response.

An object implementing this interface is passed to the hook function, under property mock of the single argument object.

interface IMock {
    checksumContent: null | string;
    defaultLocalPath: string;
    defaultMockHarKey?: string;
    delay: number;
    harMimeTypesParseJson: string[];
    localPath: string;
    mockFolderFullPath: string;
    mockHarKey?: string;
    mocksFolder: string;
    mocksFormat: MocksFormat;
    mocksHarFile: string;
    mocksHarKeyManager: HarKeyManager;
    mode: Mode;
    options: MockingOptions;
    remoteURL: null | string;
    request: IFetchedRequest;
    response: IResponse;
    saveChecksumContent: boolean;
    saveDetailedTimings: boolean;
    saveForwardedRequestBody: boolean;
    saveForwardedRequestData: boolean;
    saveInputRequestBody: boolean;
    saveInputRequestData: boolean;
    sourcePayload: undefined | PayloadWithOrigin<PayloadOrigin>;
    checksum(spec: ChecksumArgs): Promise<string>;
    createPayload(payload: Payload): PayloadWithOrigin<"user">;
    downloadPayload(): Promise<RemotePayload>;
    fetchPayload(): Promise<RemotePayload>;
    fillResponseFromPayload(payload: PayloadWithOrigin<PayloadOrigin>): void;
    getPayloadAndFillResponse(): Promise<void>;
    hasLocalFiles(): Promise<boolean>;
    hasLocalMock(): Promise<boolean>;
    hasNoLocalFiles(): Promise<boolean>;
    hasNoLocalMock(): Promise<boolean>;
    persistPayload(payload: PayloadWithOrigin<PayloadOrigin>): Promise<void>;
    process(): Promise<void>;
    readLocalPayload(): Promise<undefined | PayloadWithOrigin<"user" | "local">>;
    readLocalPayloadAndFillResponse(): Promise<boolean>;
    readOrDownloadPayload(): Promise<PayloadWithOrigin<"user" | "local"> | RemotePayload>;
    readOrFetchPayload(): Promise<PayloadWithOrigin<"user" | "local"> | RemotePayload>;
    sendResponse(): Promise<void>;
    setDelay(delay: null | Delay): void;
    setHarMimeTypesParseJson(value: string[]): void;
    setLocalPath(pathParts: RecursiveArray<undefined | null | string>): void;
    setMockHarKey(value: RecursiveArray<undefined | null | string>): void;
    setMocksFolder(value: RecursiveArray<undefined | null | string>): void;
    setMocksFormat(value: null | MocksFormat): void;
    setMocksHarFile(value: RecursiveArray<undefined | null | string>): void;
    setMocksHarKeyManager(value: null | HarKeyManager): void;
    setMode(mode: null | Mode): void;
    setPayload(payload: PayloadWithOrigin<"user" | "local">): void;
    setRemoteURL(url: null | string): void;
    setSaveChecksumContent(value: null | boolean): void;
    setSaveDetailedTimings(value: null | boolean): void;
    setSaveForwardedRequestBody(value: null | boolean): void;
    setSaveForwardedRequestData(value: null | boolean): void;
    setSaveInputRequestBody(value: null | boolean): void;
    setSaveInputRequestData(value: null | boolean): void;
}

Properties

checksumContent: null | string

Content produced by the last call to the checksum method, as it was passed to the hash algorithm.

defaultLocalPath: string

Used only when the mocks format is 'folder', specifies the default local path of the mock, relative to mocksFolder, . It uses the URL pathname to build an equivalent folders hierarchy, and appends the HTTP method as a leaf folder.

defaultMockHarKey?: string

Used only when the mocks format is 'har', specifies the default mock har key to use in case setMockHarKey is not called with a non-null value. It is computed by calling the mocks har key manager with the current request.

Note that it is lazily computed, but once computed, it is not re-computed even if mocksHarKeyManager changes.

delay: number

The currently computed delay that will be applied, configured either by a call to setDelay, or by the global setting. Note that if the delay is set to recorded and the local mock to use is not yet loaded, the value returned by this getter will be the default delay and not the recorded delay.

harMimeTypesParseJson: string[]

Used only when the mocks format is 'har', specifies a list of mime types that will attempt to parse the request/response body as JSON. This will only be applicable to request bodies if saveInputRequestBody is set to true Default value will be [] and will only be overridden by setHarMimeTypesParseJson

localPath: string

Used only when the mocks format is 'folder', specifies the local path of the mock, relative to mocksFolder. It is either the one set by the user through setLocalPath or defaultLocalPath.

mockFolderFullPath: string

Used only when the mocks format is 'folder', specifies the full, absolute path of the mock, built from localPath/defaultLocalPath, mocksFolder and possibly options.root if mocksFolder is not absolute.

mockHarKey?: string

Used only when the mocks format is 'har', specifies the key to use inside the har file to either read or write a mock.

The way the key is stored inside the har file depends on the value of mocksHarKeyManager.

mocksFolder: string

Used only when the mocks format is 'folder', specifies the root folder of all mocks, from which specific mocks paths will be resolved (resolved against options.root).

mocksFormat: MocksFormat

The mocks format used for this request.

mocksHarFile: string

Used only when the mocks format is 'har', contains the full path of the the har file to use. If the file name has the .yml or .yaml extension, the YAML format is used instead of the standard JSON format to read and write the file.

mocksHarKeyManager: HarKeyManager

Used only when the mocks format is 'har', specifies the har key manager to use.

mode: Mode

The current mode, configured either by a call to setMode, or by the global setting.

Link to global configuration options

remoteURL: null | string

The current remote URL, configured either by a call to setRemoteURL, or by the global setting.

The wrapper around the input request

response: IResponse

The wrapper around the output response

saveChecksumContent: boolean

Whether to save the content used to create a checksum when creating a new mock with a checksum for this request.

saveDetailedTimings: boolean

Whether to save detailed timings when creating a new mock for this request.

saveForwardedRequestBody: boolean

Whether to save the forwarded request body when creating a new mock for this request.

saveForwardedRequestData: boolean

Whether to save the forwarded request data (headers, method, URL) when creating a new mock for this request.

saveInputRequestBody: boolean

Whether to save the content of the input request body when creating a new mock for this request.

saveInputRequestData: boolean

Whether to save the input request data (headers, method, URL) when creating a new mock for this request.

sourcePayload: undefined | PayloadWithOrigin<PayloadOrigin>

As soon as response is filled with a payload, this property holds the reference to that payload's wrapper. The wrapper is useful here to know where the payload comes from. Before that, this property is undefined.

Methods

  • Compute a checksum using content from the request.

    Parameters

    • spec: ChecksumArgs

      specifies which data from the request to include in the checksum

    Returns Promise<string>

    The actual checksum value, that you can then use for instance to add to the mock's path.

    The computed checksum is intended to be added to the path of the mock so that semantically different requests use different mocks.

    It is difficult to predict what will actually be relevant to include in the checksum or not for your use case, and that's why we provide many options to include/exclude/transform data (cf ChecksumArgs).

    Note that we designed the API so that it is usually not needed to call the checksum method more than once for a given request/mock.

    The method stores the computed content (which is passed to the hash algorithm) in property checksumContent (in the mock object), as a string. It is built according to your options and the request's data. It is also persisted, so that you can debug more easily, especially by committing it into your SCM to analyze changes across versions of your code. File is along with the other files of the mock under file name checksum.

  • Returns true if the mock exists locally.

    Returns Promise<boolean>

    hasNoLocalMock returns the opposite boolean value.

  • Returns true if the mock does not exist locally.

    Returns Promise<boolean>

    hasLocalMock returns the opposite boolean value.

  • Returns Promise<void>

    If mode is manual, this method does nothing. It also uses a private guard to make sure it is executed only once. Therefore, if you call it from the hook method, the automatic call made for you after the hook execution will actually not do anything (and the same if you call it yourself multiple times).

  • Returns a wrapped payload built from data persisted in local files. If no local file is present, returns undefined.

    Returns Promise<undefined | PayloadWithOrigin<"user" | "local">>

  • Sends the response back to the client, with the previously specified delay if the payload origin is not remote.

    Returns Promise<void>

  • Sets the delay that will be used to send the response to the client when the data is taken from the local mock.

    Parameters

    • delay: null | Delay

      can be either a number (to directly specify the delay in milliseconds), 'recorded' (to read the delay from the time property of an already recorded mock if any) or null (to remove the effect of any previous call of setDelay and use the default value from the global setting instead).

    Returns void

  • Sets the localPath value.

    Parameters

    • pathParts: RecursiveArray<undefined | null | string>

      Any combination of values and array of values, which will eventually all be flattened, converted to strings and joined to build a path.

    Returns void

    The following example will use the HTTP method followed by the URL pathname:

    mock.setLocalPath([mock.request.method, mock.request.pathname])
    

    The following example will concatenate prefix, all portions of the URL pathname except the first one (also excluding the very first one which is empty since IFetchedRequest.pathname has a leading slash) and optionally a suffix sequence depending on a boolean.

    mock.setLocalPath([prefix, mock.request.pathname.split('/').slice(2), addSuffix ? [suffix, '-static-suffix'] : null])
    
  • Sets the mockHarKey value.

    Parameters

    • value: RecursiveArray<undefined | null | string>

      specifies the key to use inside the har file to either read or write a mock. If an array is set (which can be nested), it is flattened with null items removed, and joined with forward slashes to produce a string. Passing null resets the value to the default value.

    Returns void

  • Sets the current local payload, with a custom one you would have created.

    Parameters

    Returns void

  • Sets the saveChecksumContent value.

    Parameters

    • value: null | boolean

      whether to save the content used to create a checksum when creating a new mock with a checksum for this request. Passing null resets the value to the default coming from the global setting.

    Returns void

  • Sets the saveForwardedRequestData value.

    Parameters

    • value: null | boolean

      whether to save the forwarded request data (headers, method, URL) when creating a new mock for this request. Passing null resets the value to the default coming from the global setting.

    Returns void

  • Sets the saveInputRequestBody value.

    Parameters

    • value: null | boolean

      whether to save the content of the input request body when creating a new mock for this request. Passing null resets the value to the default coming from the global setting.

    Returns void

  • Sets the saveInputRequestData value.

    Parameters

    • value: null | boolean

      whether to save the input request data (headers, method, URL) when creating a new mock for this request. Passing null resets the value to the default coming from the global setting.

    Returns void