@pact-foundation/pact
Version:
Pact for all things Javascript
138 lines • 5.66 kB
JavaScript
;
/**
* An Interaction is where you define the state of your interaction with a Provider.
* @module Interaction
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.interactionToInteractionObject = exports.Interaction = void 0;
const lodash_1 = require("lodash");
const ramda_1 = require("ramda");
const request_1 = require("../common/request");
const configurationError_1 = __importDefault(require("../errors/configurationError"));
const matchers_1 = require("./matchers");
/**
* Returns valid if object or matcher only contains string values
* @param query
*/
const throwIfQueryObjectInvalid = (query) => {
if ((0, matchers_1.isMatcher)(query)) {
return;
}
Object.values(query).forEach((value) => {
if (!((0, matchers_1.isMatcher)(value) || Array.isArray(value) || typeof value === 'string')) {
throw new configurationError_1.default(`Query must only contain strings.`);
}
});
};
class Interaction {
state = {};
/**
* Gives a state the provider should be in for this interaction.
* @param {string} providerState - The state of the provider.
* @returns {Interaction} interaction
*/
given(providerState) {
if (providerState) {
this.state.providerState = providerState;
}
return this;
}
/**
* A free style description of the interaction.
* @param {string} description - A description of the interaction.
* @returns {Interaction} interaction
*/
uponReceiving(description) {
if ((0, lodash_1.isNil)(description)) {
throw new configurationError_1.default('You must provide a description for the interaction.');
}
this.state.description = description;
return 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) {
if ((0, lodash_1.isNil)(requestOpts.method)) {
throw new configurationError_1.default('You must provide an HTTP method.');
}
if ((0, lodash_1.keys)(request_1.HTTPMethods).indexOf(requestOpts.method.toString()) < 0) {
throw new configurationError_1.default(`You must provide a valid HTTP method: ${(0, lodash_1.keys)(request_1.HTTPMethods).join(', ')}.`);
}
if ((0, lodash_1.isNil)(requestOpts.path)) {
throw new configurationError_1.default('You must provide a path.');
}
if (typeof requestOpts.query === 'object') {
throwIfQueryObjectInvalid(requestOpts.query);
}
this.state.request = (0, ramda_1.reject)(lodash_1.isNil, requestOpts);
return 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) {
if ((0, lodash_1.isNil)(responseOpts.status) ||
responseOpts.status.toString().trim().length === 0) {
throw new configurationError_1.default('You must provide a status code.');
}
this.state.response = (0, ramda_1.reject)(lodash_1.isNil, {
body: responseOpts.body,
headers: responseOpts.headers || undefined,
status: responseOpts.status,
});
return this;
}
/**
* Returns the interaction object created.
* @returns {Object}
*/
json() {
if ((0, lodash_1.isNil)(this.state.description)) {
throw new configurationError_1.default('You must provide a description for the Interaction');
}
if ((0, lodash_1.isNil)(this.state.request) ||
(0, lodash_1.isNil)(this.state?.request?.method) ||
(0, lodash_1.isNil)(this.state?.request?.path)) {
throw new configurationError_1.default('You must provide a request with at least a method and path for the Interaction');
}
if ((0, lodash_1.isNil)(this.state.response) || (0, lodash_1.isNil)(this.state?.response?.status)) {
throw new configurationError_1.default('You must provide a response with a status for the Interaction');
}
return this.state;
}
}
exports.Interaction = Interaction;
const interactionToInteractionObject = (interaction) => ({
state: interaction.providerState,
uponReceiving: interaction.description,
withRequest: {
method: interaction.request?.method,
path: interaction.request?.path,
query: interaction.request?.query,
body: interaction.request?.body,
headers: interaction.request?.headers,
},
willRespondWith: {
status: interaction.response?.status,
body: interaction.response?.body,
headers: interaction.response?.headers,
},
});
exports.interactionToInteractionObject = interactionToInteractionObject;
//# sourceMappingURL=interaction.js.map