@pact-foundation/pact
Version:
Pact for all things Javascript
86 lines (85 loc) • 2.97 kB
TypeScript
/**
* An Interaction is where you define the state of your interaction with a Provider.
* @module Interaction
*/
import { type HTTPMethod, HTTPMethods } from '../common/request';
import { type AnyTemplate, type MatcherV2 } from './matchers';
interface QueryObject {
[name: string]: string | MatcherV2<string> | string[];
}
export type Query = string | QueryObject;
export type Headers = {
[header: string]: string | MatcherV2<string> | (MatcherV2<string> | string)[];
};
export interface RequestOptions {
method: HTTPMethods | HTTPMethod;
path: string | MatcherV2<string>;
query?: Query;
headers?: Headers;
body?: AnyTemplate;
}
export interface ResponseOptions {
status: number;
headers?: Headers;
body?: AnyTemplate;
}
export interface InteractionObject {
state: string | undefined;
uponReceiving: string;
withRequest: RequestOptions;
willRespondWith: ResponseOptions;
}
export interface InteractionState {
providerState?: string;
description?: string;
request?: RequestOptions;
response?: ResponseOptions;
}
export interface InteractionStateComplete {
providerState?: string;
description: string;
request: RequestOptions;
response: ResponseOptions;
}
export declare class Interaction {
state: InteractionState;
/**
* Gives a state the provider should be in for this interaction.
* @param {string} providerState - The state of the provider.
* @returns {Interaction} interaction
*/
given(providerState: string): this;
/**
* A free style description of the interaction.
* @param {string} description - A description of the interaction.
* @returns {Interaction} interaction
*/
uponReceiving(description: string): this;
/**
* The request that represents this interaction triggered by the consumer.
* @param {Object} requestOpts
* @param {string} requestOpts.method - The HTTP method
* @param {string} requestOpts.path - The path of the URL
* @param {string} requestOpts.query - Any query string in the interaction
* @param {Object} requestOpts.headers - A key-value pair object of headers
* @param {Object} requestOpts.body - The body, in {@link String} format or {@link Object} format
* @returns {Interaction} interaction
*/
withRequest(requestOpts: RequestOptions): this;
/**
* The response expected by the consumer.
* @param {Object} responseOpts
* @param {string} responseOpts.status - The HTTP status
* @param {string} responseOpts.headers
* @param {Object} responseOpts.body
* @returns {Interaction} interaction
*/
willRespondWith(responseOpts: ResponseOptions): this;
/**
* Returns the interaction object created.
* @returns {Object}
*/
json(): InteractionStateComplete;
}
export declare const interactionToInteractionObject: (interaction: InteractionStateComplete) => InteractionObject;
export {};