/
types.ts
181 lines (163 loc) · 5.06 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { JsonMap } from '../../common/jsonTypes';
import {
Path,
SpecificationVersion,
TemplateHeaders,
TemplateQuery,
V3MockServer,
V3ProviderState,
V3Request,
V3Response,
} from '../../v3';
// TODO: do we alias all types to V4 or is this yicky??
// These types are all interface types, so any extensions/modifications
// could easily be made compatible
export type V4ProviderState = V3ProviderState;
export type V4MockServer = V3MockServer;
export type V4Request = V3Request;
export type V4Response = V3Response;
export interface PactV4Options {
/**
* Directory to write the pact file to
*/
dir?: string;
/**
* Consumer name
*/
consumer: string;
/**
* Provider name
*/
provider: string;
/**
* Port to run the mock server on. Defaults to a random port
*/
port?: number;
/**
* Specification version to use. Defaults to V4
*/
spec?: SpecificationVersion;
/**
* Specification version to use
*/
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
/**
* Flag to identify if HTTP or HTTPs over TLS should be used (default false, HTTP)
*/
tls?: boolean;
/**
* The host to run the mock service, defaults to 127.0.0.1
*/
host?: string;
}
export interface V4UnconfiguredInteraction {
given(state: string, parameters?: JsonMap): V4UnconfiguredInteraction;
uponReceiving(description: string): V4UnconfiguredInteraction;
withCompleteRequest(request: V4Request): V4InteractionWithCompleteRequest;
withRequest(
method: string,
path: Path,
builder?: V4RequestBuilderFunc
): V4InteractionwithRequest;
usingPlugin(config: PluginConfig): V4InteractionWithPlugin;
}
export interface V4InteractionWithCompleteRequest {
withCompleteResponse(response: V4Response): V4InteractionWithResponse;
}
export interface V4InteractionwithRequest {
willRespondWith(
status: number,
builder?: V4ResponseBuilderFunc
): V4InteractionWithResponse;
}
export type V4RequestBuilderFunc = (builder: V4RequestBuilder) => void;
// TOOD: not sure if the Builder pattern is better or worse from a readibility
// and forcing function.
export interface V4RequestBuilder {
query(query: TemplateQuery): V4RequestBuilder;
headers(headers: TemplateHeaders): V4RequestBuilder;
jsonBody(body: unknown): V4RequestBuilder;
binaryFile(contentType: string, file: string): V4RequestBuilder;
multipartBody(
contentType: string,
filename: string,
mimePartName: string
): V4RequestBuilder;
body(contentType: string, body: Buffer): V4RequestBuilder;
}
export interface V4ResponseBuilder {
headers(headers: TemplateHeaders): V4ResponseBuilder;
jsonBody(body: unknown): V4ResponseBuilder;
binaryFile(contentType: string, file: string): V4ResponseBuilder;
multipartBody(
contentType: string,
filename: string,
mimePartName: string
): V4ResponseBuilder;
body(contentType: string, body: Buffer): V4ResponseBuilder;
}
export type V4ResponseBuilderFunc = (builder: V4ResponseBuilder) => void;
export interface V4InteractionWithResponse {
executeTest<T>(
testFn: (mockServer: V4MockServer) => Promise<T>
): Promise<T | undefined>;
}
export type V4PluginRequestBuilderFunc = (
builder: V4RequestWithPluginBuilder
) => void;
export type V4PluginResponseBuilderFunc = (
builder: V4ResponseWithPluginBuilder
) => void;
export interface PluginConfig {
plugin: string;
version: string;
}
export interface V4InteractionWithPlugin {
// Multiple plugins are allowed
usingPlugin(config: PluginConfig): V4InteractionWithPlugin;
withRequest(
method: string,
path: Path,
builder?: V4PluginRequestBuilderFunc
): V4InteractionWithPluginRequest;
}
export interface V4InteractionWithPluginRequest {
willRespondWith(
status: number,
builder?: V4PluginResponseBuilderFunc
): V4InteractionWithPluginResponse;
}
export interface V4RequestWithPluginBuilder {
query(query: TemplateQuery): V4RequestWithPluginBuilder;
headers(headers: TemplateHeaders): V4RequestWithPluginBuilder;
jsonBody(body: unknown): V4RequestWithPluginBuilder;
binaryFile(contentType: string, file: string): V4RequestWithPluginBuilder;
multipartBody(
contentType: string,
filename: string,
mimePartName: string
): V4RequestWithPluginBuilder;
body(contentType: string, body: Buffer): V4RequestWithPluginBuilder;
pluginContents(
contentType: string,
contents: string
): V4RequestWithPluginBuilder;
}
export interface V4ResponseWithPluginBuilder {
headers(headers: TemplateHeaders): V4ResponseBuilder;
jsonBody(body: unknown): V4ResponseBuilder;
binaryFile(contentType: string, file: string): V4ResponseBuilder;
multipartBody(
contentType: string,
filename: string,
mimePartName: string
): V4ResponseBuilder;
body(contentType: string, body: Buffer): V4ResponseBuilder;
pluginContents(contentType: string, contents: string): V4ResponseBuilder;
}
export interface V4InteractionWithPluginResponse {
executeTest<T>(
testFn: (mockServer: V4MockServer) => Promise<T>
): Promise<T | undefined>;
}
export type TestFunction<T> = (mockServer: V4MockServer) => Promise<T>;