An Interaction object defines how the mock server should respond to specific incoming requests. It consists of a request and a response.
Request Configuration (InteractionRequest)
Required fields:
method: The HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE').path: The endpoint path.
Optional fields:
headers: Object containing request headers.body: The request body.pathParams: Object for path parameters.queryParams: Object for query parameters.cookies: Object for cookies.form: Object for form data.graphQL: An object containing a query (string) and optional variables (object).
Response Configuration (InteractionResponse)
A response can be a simple static response or a conditional response using onCall.
Static Response:
Requires a status (number) and can include:
headers: Object of response headers.cookies: Object of response cookies.body: The response body.file: Path to a file to be returned as the body.fixedDelay: A number representing a delay in milliseconds.randomDelay: An object with { min: number, max: number } for a randomized delay.onCall: An object used for stateful/sequential responses.
Conditional Response (onCall):
If onCall is provided, the response can change based on the call count. The onCall object uses the call number as a key to define subsequent responses.
Interaction Metadata
id: Unique identifier for the interaction.provider: Name of the provider.flow: The flow of the provider.strict: Boolean to enforce strict matching.expects: InteractionExpectations to verify if an interaction was exercised or check its callCount.
const interaction: Interaction = {
request: {
method: 'POST',
path: '/login',
body: { username: 'admin' }
},
response: {
status: 200,
body: { token: 'secret-token' },
onCall: {
1: { status: 200, body: { token: 'first-token' } },
2: { status: 401, body: { error: 'Too many attempts' } }
}
}
};