Skip to content

Commit c2fe8f5

Browse files
committed
fix!: Rename Matchers to MatchersV2 to to avoid type overlap. remove AnyTemplate from V3 types
1 parent f99e01b commit c2fe8f5

File tree

5 files changed

+36
-58
lines changed

5 files changed

+36
-58
lines changed

src/dsl/interaction.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
import { isNil, keys } from 'lodash';
77
import { reject } from 'ramda';
88
import { HTTPMethods, HTTPMethod } from '../common/request';
9-
import { Matcher, isMatcher, AnyTemplate } from './matchers';
9+
import { MatcherV2, isMatcher, AnyTemplate } from './matchers';
1010
import ConfigurationError from '../errors/configurationError';
1111

1212
interface QueryObject {
13-
[name: string]: string | Matcher<string> | string[];
13+
[name: string]: string | MatcherV2<string> | string[];
1414
}
1515
export type Query = string | QueryObject;
1616

1717
export type Headers = {
18-
[header: string]: string | Matcher<string> | (Matcher<string> | string)[];
18+
[header: string]: string | MatcherV2<string> | (MatcherV2<string> | string)[];
1919
};
2020

2121
export interface RequestOptions {
2222
method: HTTPMethods | HTTPMethod;
23-
path: string | Matcher<string>;
23+
path: string | MatcherV2<string>;
2424
query?: Query;
2525
headers?: Headers;
2626
body?: AnyTemplate;

src/dsl/matchers.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ export const IPV6_FORMAT =
2828
'^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$';
2929
export const HEX_FORMAT = '^[0-9a-fA-F]+$';
3030

31-
export interface Matcher<T> {
31+
export interface MatcherV2<T> {
3232
value?: T;
3333
'pact:matcher:type': string;
3434
getValue(): T;
3535
}
3636

37-
export interface ArrayMatcher<T> extends Matcher<T> {
37+
export interface ArrayMatcher<T> extends MatcherV2<T> {
3838
'pact:matcher:type': string;
3939
min?: number;
4040
max?: number;
4141
}
4242

43-
export function isMatcher(x: unknown): x is Matcher<AnyTemplate> {
44-
return x != null && (x as Matcher<AnyTemplate>).getValue !== undefined;
43+
export function isMatcher(x: unknown): x is MatcherV2<AnyTemplate> {
44+
return x != null && (x as MatcherV2<AnyTemplate>).getValue !== undefined;
4545
}
4646

4747
export type AnyTemplate =
4848
| AnyJson
49-
| Matcher<AnyTemplate>
49+
| MatcherV2<AnyTemplate>
5050
| ArrayMatcher<AnyTemplate>
5151
| TemplateMap
5252
| ArrayTemplate;
@@ -105,7 +105,7 @@ export function eachLike<T>(
105105
* The somethingLike matcher
106106
* @param {any} value - the value to be somethingLike
107107
*/
108-
export function somethingLike<T>(value: T): Matcher<T> {
108+
export function somethingLike<T>(value: T): MatcherV2<T> {
109109
if (isNil(value) || isFunction(value)) {
110110
throw new MatcherError(
111111
'Error creating a Pact somethingLike Match. Value cannot be a function or undefined'
@@ -119,7 +119,7 @@ export function somethingLike<T>(value: T): Matcher<T> {
119119
};
120120
}
121121

122-
export interface RegexMatcher<T> extends Matcher<T> {
122+
export interface RegexMatcher<T> extends MatcherV2<T> {
123123
regex: string;
124124
}
125125

@@ -158,7 +158,7 @@ export function term(opts: {
158158
* Email address matcher.
159159
* @param {string} address - a email address to use as an example
160160
*/
161-
export function email(address?: string): Matcher<string> {
161+
export function email(address?: string): MatcherV2<string> {
162162
return term({
163163
generate: address || '[email protected]',
164164
matcher: EMAIL_FORMAT,
@@ -169,7 +169,7 @@ export function email(address?: string): Matcher<string> {
169169
* UUID v4 matcher.
170170
* @param {string} id - a UUID to use as an example.
171171
*/
172-
export function uuid(id?: string): Matcher<string> {
172+
export function uuid(id?: string): MatcherV2<string> {
173173
return term({
174174
generate: id || 'ce118b6e-d8e1-11e7-9296-cec278b6b50a',
175175
matcher: UUID_V4_FORMAT,
@@ -180,7 +180,7 @@ export function uuid(id?: string): Matcher<string> {
180180
* IPv4 matcher.
181181
* @param {string} ip - an IPv4 address to use as an example. Defaults to `127.0.0.13`
182182
*/
183-
export function ipv4Address(ip?: string): Matcher<string> {
183+
export function ipv4Address(ip?: string): MatcherV2<string> {
184184
return term({
185185
generate: ip || '127.0.0.13',
186186
matcher: IPV4_FORMAT,
@@ -191,7 +191,7 @@ export function ipv4Address(ip?: string): Matcher<string> {
191191
* IPv6 matcher.
192192
* @param {string} ip - an IPv6 address to use as an example. Defaults to '::ffff:192.0.2.128'
193193
*/
194-
export function ipv6Address(ip?: string): Matcher<string> {
194+
export function ipv6Address(ip?: string): MatcherV2<string> {
195195
return term({
196196
generate: ip || '::ffff:192.0.2.128',
197197
matcher: IPV6_FORMAT,
@@ -203,7 +203,7 @@ export function ipv6Address(ip?: string): Matcher<string> {
203203
* @param {string} date - an ISO8601 Date and Time string
204204
* e.g. 2015-08-06T16:53:10+01:00 are valid
205205
*/
206-
export function iso8601DateTime(date?: string): Matcher<string> {
206+
export function iso8601DateTime(date?: string): MatcherV2<string> {
207207
return term({
208208
generate: date || '2015-08-06T16:53:10+01:00',
209209
matcher: ISO8601_DATETIME_FORMAT,
@@ -214,7 +214,7 @@ export function iso8601DateTime(date?: string): Matcher<string> {
214214
* ISO8601 DateTime matcher with required millisecond precision
215215
* @param {string} date - an ISO8601 Date and Time string, e.g. 2015-08-06T16:53:10.123+01:00
216216
*/
217-
export function iso8601DateTimeWithMillis(date?: string): Matcher<string> {
217+
export function iso8601DateTimeWithMillis(date?: string): MatcherV2<string> {
218218
return term({
219219
generate: date || '2015-08-06T16:53:10.123+01:00',
220220
matcher: ISO8601_DATETIME_WITH_MILLIS_FORMAT,
@@ -225,18 +225,18 @@ export function iso8601DateTimeWithMillis(date?: string): Matcher<string> {
225225
* ISO8601 Date matcher.
226226
* @param {string} date - a basic yyyy-MM-dd date string e.g. 2000-09-31
227227
*/
228-
export function iso8601Date(date?: string): Matcher<string> {
228+
export function iso8601Date(date?: string): MatcherV2<string> {
229229
return term({
230230
generate: date || '2013-02-01',
231231
matcher: ISO8601_DATE_FORMAT,
232232
});
233233
}
234234

235235
/**
236-
* ISO8601 Time Matcher, matches a pattern of the format "'T'HH:mm:ss".
236+
* ISO8601 Time MatcherV2, matches a pattern of the format "'T'HH:mm:ss".
237237
* @param {string} date - a ISO8601 formatted time string e.g. T22:44:30.652Z
238238
*/
239-
export function iso8601Time(time?: string): Matcher<string> {
239+
export function iso8601Time(time?: string): MatcherV2<string> {
240240
return term({
241241
generate: time || 'T22:44:30.652Z',
242242
matcher: ISO8601_TIME_FORMAT,
@@ -248,51 +248,51 @@ export function iso8601Time(time?: string): Matcher<string> {
248248
*
249249
* @param {string} date - an RFC1123 Date and Time string, e.g. Mon, 31 Oct 2016 15:21:41 -0400
250250
*/
251-
export function rfc1123Timestamp(timestamp?: string): Matcher<string> {
251+
export function rfc1123Timestamp(timestamp?: string): MatcherV2<string> {
252252
return term({
253253
generate: timestamp || 'Mon, 31 Oct 2016 15:21:41 -0400',
254254
matcher: RFC1123_TIMESTAMP_FORMAT,
255255
});
256256
}
257257

258258
/**
259-
* Hexadecimal Matcher.
259+
* Hexadecimal MatcherV2.
260260
* @param {string} hex - a hex value.
261261
*/
262-
export function hexadecimal(hex?: string): Matcher<string> {
262+
export function hexadecimal(hex?: string): MatcherV2<string> {
263263
return term({
264264
generate: hex || '3F',
265265
matcher: HEX_FORMAT,
266266
});
267267
}
268268

269269
/**
270-
* Decimal Matcher.
270+
* Decimal MatcherV2.
271271
* @param {float} float - a decimal value.
272272
*/
273-
export function decimal(float?: number): Matcher<number> {
273+
export function decimal(float?: number): MatcherV2<number> {
274274
return somethingLike<number>(isNil(float) ? 13.01 : float);
275275
}
276276

277277
/**
278-
* Integer Matcher.
278+
* Integer MatcherV2.
279279
* @param {integer} int - an int value.
280280
*/
281-
export function integer(int?: number): Matcher<number> {
281+
export function integer(int?: number): MatcherV2<number> {
282282
return somethingLike<number>(isNil(int) ? 13 : int);
283283
}
284284

285285
/**
286-
* Boolean Matcher.
286+
* Boolean MatcherV2.
287287
*/
288-
export function boolean(value = true): Matcher<boolean> {
288+
export function boolean(value = true): MatcherV2<boolean> {
289289
return somethingLike<boolean>(value);
290290
}
291291

292292
/**
293-
* String Matcher.
293+
* String MatcherV2.
294294
*/
295-
export function string(value = 'iloveorange'): Matcher<string> {
295+
export function string(value = 'iloveorange'): MatcherV2<string> {
296296
return somethingLike<string>(value);
297297
}
298298

src/dsl/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AnyJson } from '../common/jsonTypes';
2-
import { Matcher as MatcherV2 } from './matchers';
2+
import { MatcherV2 } from './matchers';
33
import { Matcher } from '../v3/matchers';
44

55
/**

src/httpPact/ffi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Headers,
88
Query,
99
} from '../dsl/interaction';
10-
import { Matcher, matcherValueOrString } from '../dsl/matchers';
10+
import { MatcherV2, matcherValueOrString } from '../dsl/matchers';
1111
import logger from '../common/logger';
1212

1313
enum InteractionPart {
@@ -22,7 +22,7 @@ export const contentTypeFromHeaders = (
2222
headers: Headers | undefined,
2323
defaultContentType: string
2424
): string => {
25-
let contentType: string | Matcher<string> = defaultContentType;
25+
let contentType: string | MatcherV2<string> = defaultContentType;
2626
forEachObjIndexed((v, k) => {
2727
if (`${k}`.toLowerCase() === CONTENT_TYPE_HEADER) {
2828
contentType = matcherValueOrString(v);
@@ -81,7 +81,7 @@ export const setBody = (
8181
}
8282
};
8383

84-
type TemplateHeaderArrayValue = (string | Matcher<string>)[];
84+
type TemplateHeaderArrayValue = (string | MatcherV2<string>)[];
8585

8686
export const setHeaders = (
8787
part: InteractionPart,

src/v3/types.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
import { AnyJson, JsonMap } from '../common/jsonTypes';
1+
import { JsonMap } from '../common/jsonTypes';
22

33
export enum SpecificationVersion {
44
SPECIFICATION_VERSION_V2 = 3,
55
SPECIFICATION_VERSION_V3 = 4,
66
SPECIFICATION_VERSION_V4 = 5,
77
}
88

9-
interface TemplateMap {
10-
[key: string]: AnyJson | AnyTemplate;
11-
}
12-
type TemplateArray = Array<AnyTemplate>;
13-
14-
/**
15-
* The `AnyTemplate` type is no longer used anywhere in Pact.
16-
*
17-
* @deprecated This type never really worked, so it has been removed from the Pact API surface. It is still exported to avoid breaking changes with anyone who is importing it. See {@link https://github.com/pact-foundation/pact-js/issues/1054} for details
18-
*/
19-
export type AnyTemplate =
20-
| AnyJson
21-
| TemplateMap
22-
| TemplateArray
23-
| Matcher<unknown>;
24-
25-
// TODO: In the next major release, these types should be renamed so they
26-
// don't clash with the V2 ones. Typescript merges interfaces with the same
27-
// name, so the calculated type for any Matcher that exists in both V2 and
28-
// V3 will be wrong. This isn't a major problem as is only likely to affect
29-
// maintainers, and contributors but it definitely should be corrected.
30-
319
/**
3210
* Pact Matcher
3311
*/

0 commit comments

Comments
 (0)