Skip to content

Commit 59b345c

Browse files
authored
fix(types): data matcher for body and query. (#1725)
* fix(types): data matcher for body and query. Fixes: #1724 Closes: #1723 Updates the JSONish type using inspiration from the thread on adding a JSON type to TS. microsoft/TypeScript#1897 (comment) This change does a couple sublet things: - Adds `undefined` to scalar values allowed in arrays or as values in objects. This allows for interfaces with optional keys to be passed in. ref #1723 - Splits out top-level array and objects into their interfaces so they could be used directly. Allows enforcing non-arrays for `.query`. - `RequestBodyMatcher` now extends the array and map types instead of the raw `DataMatcher`. Allowing arrays fixes #1724. This also ensures that booleans, numbers, and `null` are not considered valid.
1 parent 6b88dff commit 59b345c

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

types/index.d.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,29 @@ declare namespace nock {
3737
interceptorOptions?: Options
3838
) => Interceptor
3939

40-
// essentially valid decoded JSON with the addition of possible RegExp
41-
interface DataMatcher {
42-
[k: string]:
43-
| boolean
44-
| null
45-
| number
46-
| string
47-
| RegExp
48-
| DataMatcher
49-
| Array<boolean | null | number | string | RegExp | DataMatcher>
40+
// Essentially valid, decoded JSON with the addition of possible RegExp. TS doesn't currently have
41+
// a great way to represent JSON type data, this data matcher design is based off this comment.
42+
// https://github.com/microsoft/TypeScript/issues/1897#issuecomment-338650717
43+
type DataMatcher =
44+
| boolean
45+
| number
46+
| string
47+
| null
48+
| undefined
49+
| RegExp
50+
| DataMatcherArray
51+
| DataMatcherMap
52+
interface DataMatcherArray extends Array<DataMatcher> {}
53+
interface DataMatcherMap {
54+
[key: string]: DataMatcher
5055
}
5156

5257
type RequestBodyMatcher =
5358
| string
5459
| Buffer
5560
| RegExp
56-
| DataMatcher
61+
| DataMatcherArray
62+
| DataMatcherMap
5763
| { (body: any): boolean }
5864

5965
type RequestHeaderMatcher =
@@ -128,7 +134,7 @@ declare namespace nock {
128134
matcher:
129135
| boolean
130136
| string
131-
| DataMatcher
137+
| DataMatcherMap
132138
| URLSearchParams
133139
| { (parsedObj: ParsedUrlQuery): boolean }
134140
): this

types/tests.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { URL, URLSearchParams } from 'url'
55
let scope: nock.Scope = nock('http://example.test')
66
let inst: nock.Interceptor
77
let str = 'foo'
8-
let strings: string[]
8+
let strings = ['foo', 'bar']
99
let defs: nock.Definition[]
1010
let options: nock.Options = {}
1111

12+
const buffer = Buffer.from('')
1213
const num = 42
1314
const obj: { [k: string]: any } = {}
15+
const objWithUndefinedValue: { a: string; b?: string } = { a: 'a' }
1416
const regex = /test/
1517

1618
scope.head(str) // $ExpectType Interceptor
@@ -34,6 +36,16 @@ inst = scope.post(str, str)
3436
inst = scope.post(str, str, options)
3537
inst = scope.post(str, obj)
3638
inst = scope.post(str, regex)
39+
inst = scope.post(str, objWithUndefinedValue)
40+
inst = scope.post(str, str)
41+
inst = scope.post(str, strings)
42+
inst = scope.post(str, [num, str, regex])
43+
inst = scope.post(str, [num, num, num])
44+
inst = scope.post(str, regex)
45+
inst = scope.post(str, buffer)
46+
inst = scope.post(str, true) // $ExpectError
47+
inst = scope.post(str, null) // $ExpectError
48+
inst = scope.post(str, num) // $ExpectError
3749

3850
inst = scope.put(str)
3951
inst = scope.put(str, str)
@@ -53,8 +65,13 @@ inst = scope.merge(str, str, options)
5365
inst = scope.merge(str, obj)
5466
inst = scope.merge(str, regex)
5567

56-
inst = inst.query(obj)
5768
inst = inst.query(true)
69+
inst = inst.query(obj)
70+
inst = inst.query(objWithUndefinedValue)
71+
inst = inst.query({ foo: regex })
72+
inst = inst.query(strings) // $ExpectError
73+
inst = inst.query(buffer) // $ExpectError
74+
inst = inst.query(regex) // $ExpectError
5875

5976
inst = scope.intercept(str, str)
6077
inst = scope.intercept(str, str, str)

0 commit comments

Comments
 (0)