Skip to content

Commit

Permalink
feat: support JSON addInteraction function for PactV3
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jul 4, 2022
1 parent 33c145a commit 903fd36
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/v3/pact.ts
Expand Up @@ -102,6 +102,13 @@ type TemplateQuery = Record<
| Array<string | MatchersV3.Matcher<string>>
>;

export interface V3Interaction {
states?: V3ProviderState[];
uponReceiving: string;
withRequest: V3Request;
willRespondWith: V3Response;
}

export interface V3Request {
method: string;
path: string | MatchersV3.Matcher<string>;
Expand Down Expand Up @@ -232,6 +239,24 @@ export class PactV3 {
this.setup();
}

// JSON object interface for V3, to aid with migration from the previous major version
public addInteraction(interaction: V3Interaction): PactV3 {
if (interaction.uponReceiving === '') {
throw new Error(
"must provide a valid interaction description via 'uponReceiving'"
);
}
this.uponReceiving(interaction.uponReceiving);

(interaction.states || []).forEach((s) => {
this.given(s.description, s.parameters);
});
this.withRequest(interaction.withRequest);
this.willRespondWith(interaction.willRespondWith);

return this;
}

// TODO: this currently must be called before other methods, else it won't work
public given(providerState: string, parameters?: JsonMap): PactV3 {
if (parameters) {
Expand Down

0 comments on commit 903fd36

Please sign in to comment.