UNPKG

7.55 kBTypeScriptView Raw
1// TypeScript Version: 3.5
2
3import { ReadStream } from 'fs'
4import { ClientRequest, IncomingMessage, RequestOptions } from 'http'
5import { ParsedUrlQuery } from 'querystring'
6import { Url, URLSearchParams } from 'url'
7
8export = nock
9
10declare function nock(
11 basePath: string | RegExp | Url,
12 options?: nock.Options
13): nock.Scope
14
15declare namespace nock {
16 function cleanAll(): void
17 function activate(): void
18 function isActive(): boolean
19 function isDone(): boolean
20 function pendingMocks(): string[]
21 function activeMocks(): string[]
22 function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean
23 function disableNetConnect(): void
24 function enableNetConnect(
25 matcher?: string | RegExp | ((host: string) => boolean)
26 ): void
27 function load(path: string): Scope[]
28 function loadDefs(path: string): Definition[]
29 function define(defs: Definition[]): Scope[]
30 function restore(): void
31 function abortPendingRequests(): void
32
33 let back: Back
34 let emitter: NodeJS.EventEmitter
35 let recorder: Recorder
36
37 type InterceptFunction = (
38 uri: string | RegExp | { (uri: string): boolean },
39 requestBody?: RequestBodyMatcher,
40 interceptorOptions?: Options
41 ) => Interceptor
42
43 // Essentially valid, decoded JSON with the addition of possible RegExp. TS doesn't currently have
44 // a great way to represent JSON type data, this data matcher design is based off this comment.
45 // https://github.com/microsoft/TypeScript/issues/1897#issuecomment-338650717
46 type DataMatcher =
47 | boolean
48 | number
49 | string
50 | null
51 | undefined
52 | RegExp
53 | DataMatcherArray
54 | DataMatcherMap
55 interface DataMatcherArray extends Array<DataMatcher> {}
56 interface DataMatcherMap {
57 [key: string]: DataMatcher
58 }
59
60 type RequestBodyMatcher =
61 | string
62 | Buffer
63 | RegExp
64 | DataMatcherArray
65 | DataMatcherMap
66 | { (body: any): boolean }
67
68 type RequestHeaderMatcher =
69 | string
70 | RegExp
71 | { (fieldValue: string): boolean }
72
73 type Body = string | Record<string, any> // a string or decoded JSON
74 type ReplyBody = Body | Buffer | ReadStream
75
76 type ReplyHeaderFunction = (
77 req: ClientRequest,
78 res: IncomingMessage,
79 body: string | Buffer
80 ) => string | string[]
81 type ReplyHeaderValue = string | string[] | ReplyHeaderFunction
82 type ReplyHeaders =
83 | Record<string, ReplyHeaderValue>
84 | Map<string, ReplyHeaderValue>
85 | ReplyHeaderValue[]
86
87 type StatusCode = number
88 type ReplyFnResult =
89 | readonly [StatusCode]
90 | readonly [StatusCode, ReplyBody]
91 | readonly [StatusCode, ReplyBody, ReplyHeaders]
92
93 interface ReplyFnContext extends Interceptor {
94 req: ClientRequest & {
95 headers: Record<string, string>
96 }
97 }
98
99 interface Scope extends NodeJS.EventEmitter {
100 get: InterceptFunction
101 post: InterceptFunction
102 put: InterceptFunction
103 head: InterceptFunction
104 patch: InterceptFunction
105 merge: InterceptFunction
106 delete: InterceptFunction
107 options: InterceptFunction
108
109 intercept: (
110 uri: string | RegExp | { (uri: string): boolean },
111 method: string,
112 requestBody?: RequestBodyMatcher,
113 options?: Options
114 ) => Interceptor
115
116 defaultReplyHeaders(headers: ReplyHeaders): this
117 matchHeader(name: string, value: RequestHeaderMatcher): this
118 filteringPath(regex: RegExp, replace: string): this
119 filteringPath(fn: (path: string) => string): this
120 filteringRequestBody(regex: RegExp, replace: string): this
121 filteringRequestBody(fn: (body: string) => string): this
122
123 persist(flag?: boolean): this
124 replyContentLength(): this
125 replyDate(d?: Date): this
126
127 done(): void
128 isDone(): boolean
129 pendingMocks(): string[]
130 activeMocks(): string[]
131 }
132
133 interface Interceptor {
134 query(
135 matcher:
136 | boolean
137 | string
138 | DataMatcherMap
139 | URLSearchParams
140 | { (parsedObj: ParsedUrlQuery): boolean }
141 ): this
142
143 // tslint (as of 5.16) is under the impression that the callback types can be unified,
144 // however, doing so causes the params to lose their inherited types during use.
145 // the order of the overrides is important for determining the param types in the replay fns.
146 /* tslint:disable:unified-signatures */
147 reply(
148 replyFnWithCallback: (
149 this: ReplyFnContext,
150 uri: string,
151 body: Body,
152 callback: (
153 err: NodeJS.ErrnoException | null,
154 result: ReplyFnResult
155 ) => void
156 ) => void
157 ): Scope
158 reply(
159 replyFn: (
160 this: ReplyFnContext,
161 uri: string,
162 body: Body
163 ) => ReplyFnResult | Promise<ReplyFnResult>
164 ): Scope
165 reply(
166 statusCode: StatusCode,
167 replyBodyFnWithCallback: (
168 this: ReplyFnContext,
169 uri: string,
170 body: Body,
171 callback: (err: NodeJS.ErrnoException | null, result: ReplyBody) => void
172 ) => void,
173 headers?: ReplyHeaders
174 ): Scope
175 reply(
176 statusCode: StatusCode,
177 replyBodyFn: (
178 this: ReplyFnContext,
179 uri: string,
180 body: Body
181 ) => ReplyBody | Promise<ReplyBody>,
182 headers?: ReplyHeaders
183 ): Scope
184 reply(responseCode?: StatusCode, body?: Body, headers?: ReplyHeaders): Scope
185 /* tslint:enable:unified-signatures */
186
187 replyWithError(errorMessage: string | object): Scope
188 replyWithFile(
189 statusCode: StatusCode,
190 fileName: string,
191 headers?: ReplyHeaders
192 ): Scope
193
194 matchHeader(name: string, value: RequestHeaderMatcher): this
195 basicAuth(options: { user: string; pass?: string }): this
196
197 times(newCounter: number): this
198 once(): this
199 twice(): this
200 thrice(): this
201 optionally(): this
202
203 delay(opts: number | { head?: number; body?: number }): this
204 delayBody(timeMs: number): this
205 delayConnection(timeMs: number): this
206 }
207
208 interface Options {
209 allowUnmocked?: boolean
210 reqheaders?: Record<string, RequestHeaderMatcher>
211 badheaders?: string[]
212 filteringScope?: { (scope: string): boolean }
213 encodedQueryParams?: boolean
214 }
215
216 interface Recorder {
217 rec(options?: boolean | RecorderOptions): void
218 clear(): void
219 play(): string[] | Definition[]
220 }
221
222 interface RecorderOptions {
223 dont_print?: boolean
224 output_objects?: boolean
225 enable_reqheaders_recording?: boolean
226 logging?: (content: string) => void
227 use_separator?: boolean
228 }
229
230 interface Definition {
231 scope: string
232 path: string
233 port?: number | string
234 method?: string
235 status?: number
236 body?: RequestBodyMatcher
237 reqheaders?: Record<string, RequestHeaderMatcher>
238 response?: ReplyBody
239 headers?: ReplyHeaders
240 options?: Options
241 }
242
243 type BackMode = 'wild' | 'dryrun' | 'record' | 'lockdown'
244
245 interface Back {
246 currentMode: BackMode
247 fixtures: string
248 setMode(mode: BackMode): void
249
250 (fixtureName: string, nockedFn: (nockDone: () => void) => void): void
251 (
252 fixtureName: string,
253 options: BackOptions,
254 nockedFn: (nockDone: () => void) => void
255 ): void
256 (fixtureName: string, options?: BackOptions): Promise<{
257 nockDone: () => void
258 context: BackContext
259 }>
260 }
261
262 interface BackContext {
263 isLoaded: boolean
264 scopes: Scope[]
265 assertScopesFinished(): void
266 }
267
268 interface BackOptions {
269 before?: (def: Definition) => void
270 after?: (scope: Scope) => void
271 afterRecord?: (defs: Definition[]) => Definition[]
272 recorder?: RecorderOptions
273 }
274}
275
276type ReqOptions = RequestOptions & { proto?: string }