/
index.d.ts
276 lines (242 loc) · 7.38 KB
/
index.d.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// TypeScript Version: 3.5
import { ReadStream } from 'fs'
import { ClientRequest, IncomingMessage, RequestOptions } from 'http'
import { ParsedUrlQuery } from 'querystring'
import { Url, URLSearchParams } from 'url'
export = nock
declare function nock(
basePath: string | RegExp | Url,
options?: nock.Options
): nock.Scope
declare namespace nock {
function cleanAll(): void
function activate(): void
function isActive(): boolean
function isDone(): boolean
function pendingMocks(): string[]
function activeMocks(): string[]
function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean
function disableNetConnect(): void
function enableNetConnect(
matcher?: string | RegExp | ((host: string) => boolean)
): void
function load(path: string): Scope[]
function loadDefs(path: string): Definition[]
function define(defs: Definition[]): Scope[]
function restore(): void
function abortPendingRequests(): void
let back: Back
let emitter: NodeJS.EventEmitter
let recorder: Recorder
type InterceptFunction = (
uri: string | RegExp | { (uri: string): boolean },
requestBody?: RequestBodyMatcher,
interceptorOptions?: Options
) => Interceptor
// Essentially valid, decoded JSON with the addition of possible RegExp. TS doesn't currently have
// a great way to represent JSON type data, this data matcher design is based off this comment.
// https://github.com/microsoft/TypeScript/issues/1897#issuecomment-338650717
type DataMatcher =
| boolean
| number
| string
| null
| undefined
| RegExp
| DataMatcherArray
| DataMatcherMap
interface DataMatcherArray extends ReadonlyArray<DataMatcher> {}
interface DataMatcherMap {
[key: string]: DataMatcher
}
type RequestBodyMatcher =
| string
| Buffer
| RegExp
| DataMatcherArray
| DataMatcherMap
| { (body: any): boolean }
type RequestHeaderMatcher =
| string
| RegExp
| { (fieldValue: string): boolean }
type Body = string | Record<string, any> // a string or decoded JSON
type ReplyBody = Body | Buffer | ReadStream
type ReplyHeaderFunction = (
req: ClientRequest,
res: IncomingMessage,
body: string | Buffer
) => string | string[]
type ReplyHeaderValue = string | string[] | ReplyHeaderFunction
type ReplyHeaders =
| Record<string, ReplyHeaderValue>
| Map<string, ReplyHeaderValue>
| ReplyHeaderValue[]
type StatusCode = number
type ReplyFnResult =
| readonly [StatusCode]
| readonly [StatusCode, ReplyBody]
| readonly [StatusCode, ReplyBody, ReplyHeaders]
interface ReplyFnContext extends Interceptor {
req: ClientRequest & {
headers: Record<string, string>
}
}
interface Scope extends NodeJS.EventEmitter {
get: InterceptFunction
post: InterceptFunction
put: InterceptFunction
head: InterceptFunction
patch: InterceptFunction
merge: InterceptFunction
delete: InterceptFunction
options: InterceptFunction
intercept: (
uri: string | RegExp | { (uri: string): boolean },
method: string,
requestBody?: RequestBodyMatcher,
options?: Options
) => Interceptor
defaultReplyHeaders(headers: ReplyHeaders): this
matchHeader(name: string, value: RequestHeaderMatcher): this
filteringPath(regex: RegExp, replace: string): this
filteringPath(fn: (path: string) => string): this
filteringRequestBody(regex: RegExp, replace: string): this
filteringRequestBody(fn: (body: string) => string): this
persist(flag?: boolean): this
replyContentLength(): this
replyDate(d?: Date): this
done(): void
isDone(): boolean
pendingMocks(): string[]
activeMocks(): string[]
}
interface Interceptor {
query(
matcher:
| boolean
| string
| DataMatcherMap
| URLSearchParams
| { (parsedObj: ParsedUrlQuery): boolean }
): this
// tslint (as of 5.16) is under the impression that the callback types can be unified,
// however, doing so causes the params to lose their inherited types during use.
// the order of the overrides is important for determining the param types in the replay fns.
/* tslint:disable:unified-signatures */
reply(
replyFnWithCallback: (
this: ReplyFnContext,
uri: string,
body: Body,
callback: (
err: NodeJS.ErrnoException | null,
result: ReplyFnResult
) => void
) => void
): Scope
reply(
replyFn: (
this: ReplyFnContext,
uri: string,
body: Body
) => ReplyFnResult | Promise<ReplyFnResult>
): Scope
reply(
statusCode: StatusCode,
replyBodyFnWithCallback: (
this: ReplyFnContext,
uri: string,
body: Body,
callback: (err: NodeJS.ErrnoException | null, result: ReplyBody) => void
) => void,
headers?: ReplyHeaders
): Scope
reply(
statusCode: StatusCode,
replyBodyFn: (
this: ReplyFnContext,
uri: string,
body: Body
) => ReplyBody | Promise<ReplyBody>,
headers?: ReplyHeaders
): Scope
reply(responseCode?: StatusCode, body?: Body, headers?: ReplyHeaders): Scope
/* tslint:enable:unified-signatures */
replyWithError(errorMessage: string | object): Scope
replyWithFile(
statusCode: StatusCode,
fileName: string,
headers?: ReplyHeaders
): Scope
matchHeader(name: string, value: RequestHeaderMatcher): this
basicAuth(options: { user: string; pass?: string }): this
times(newCounter: number): this
once(): this
twice(): this
thrice(): this
optionally(): this
delay(opts: number | { head?: number; body?: number }): this
delayBody(timeMs: number): this
delayConnection(timeMs: number): this
}
interface Options {
allowUnmocked?: boolean
reqheaders?: Record<string, RequestHeaderMatcher>
badheaders?: string[]
filteringScope?: { (scope: string): boolean }
encodedQueryParams?: boolean
}
interface Recorder {
rec(options?: boolean | RecorderOptions): void
clear(): void
play(): string[] | Definition[]
}
interface RecorderOptions {
dont_print?: boolean
output_objects?: boolean
enable_reqheaders_recording?: boolean
logging?: (content: string) => void
use_separator?: boolean
}
interface Definition {
scope: string
path: string
port?: number | string
method?: string
status?: number
body?: RequestBodyMatcher
reqheaders?: Record<string, RequestHeaderMatcher>
response?: ReplyBody
headers?: ReplyHeaders
options?: Options
}
type BackMode = 'wild' | 'dryrun' | 'record' | 'lockdown'
interface Back {
currentMode: BackMode
fixtures: string
setMode(mode: BackMode): void
(fixtureName: string, nockedFn: (nockDone: () => void) => void): void
(
fixtureName: string,
options: BackOptions,
nockedFn: (nockDone: () => void) => void
): void
(fixtureName: string, options?: BackOptions): Promise<{
nockDone: () => void
context: BackContext
}>
}
interface BackContext {
isLoaded: boolean
scopes: Scope[]
assertScopesFinished(): void
}
interface BackOptions {
before?: (def: Definition) => void
after?: (scope: Scope) => void
afterRecord?: (defs: Definition[]) => Definition[]
recorder?: RecorderOptions
}
}
type ReqOptions = RequestOptions & { proto?: string }