Skip to content

Commit e3530c6

Browse files
authored
Merge pull request #4 from elastic/add-types
Added type definitions
2 parents 75ee704 + c759d80 commit e3530c6

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
import { Connection } from '@elastic/elasticsearch'
6+
7+
declare class ClientMock {
8+
constructor()
9+
add(pattern: MockPattern, resolver: ResolverFn): ClientMock
10+
get(pattern: MockPattern): ResolverFn | null
11+
getConnection(): typeof Connection
12+
}
13+
14+
export declare type ResolverFn = (params: MockPattern) => Record<string, any> | string
15+
16+
export interface MockPattern {
17+
method: string
18+
path: string
19+
querystring?: Record<string, string>
20+
body?: Record<string, any>
21+
}
22+
23+
export default ClientMock

index.test-d.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
import { expectType, expectError } from 'tsd'
6+
import { Client } from '@elastic/elasticsearch'
7+
import Mock, { MockPattern } from './'
8+
9+
const mock = new Mock()
10+
const client = new Client({
11+
node: 'http://localhost:9200',
12+
Connection: mock.getConnection()
13+
})
14+
15+
mock.add({
16+
method: 'GET',
17+
path: '/'
18+
}, params => {
19+
expectType<MockPattern>(params)
20+
return { status: 'ok' }
21+
})
22+
23+
mock.add({
24+
method: 'GET',
25+
path: '/',
26+
querystring: { pretty: 'true' }
27+
}, params => {
28+
expectType<MockPattern>(params)
29+
return { status: 'ok' }
30+
})
31+
32+
mock.add({
33+
method: 'POST',
34+
path: '/',
35+
querystring: { pretty: 'true' },
36+
body: { foo: 'bar' }
37+
}, params => {
38+
expectType<MockPattern>(params)
39+
return { status: 'ok' }
40+
})
41+
42+
mock.add({
43+
method: 'GET',
44+
path: '/'
45+
}, params => {
46+
expectType<MockPattern>(params)
47+
return 'ok'
48+
})
49+
50+
// querystring should only have string values
51+
expectError(
52+
mock.add({
53+
method: 'GET',
54+
path: '/',
55+
querystring: { pretty: true }
56+
}, () => {
57+
return { status: 'ok' }
58+
})
59+
)
60+
61+
// missing resolver function
62+
expectError(
63+
mock.add({
64+
method: 'GET',
65+
path: '/'
66+
})
67+
)

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "0.1.0",
44
"description": "",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
7-
"test": "standard && ava -v",
8+
"test": "standard && ava -v && tsd",
89
"coverage:report": "nyc ava && nyc report --reporter=lcov && echo \"\n==> open coverage/lcov-report/index.html\"",
910
"coverage:check": "nyc ava && nyc check-coverage --branches=100 --lines=100 --functions=100 --statements=100"
1011
},
@@ -23,7 +24,8 @@
2324
"@elastic/elasticsearch": "^7.7.0-rc.2",
2425
"ava": "^3.6.0",
2526
"nyc": "^15.0.1",
26-
"standard": "^14.3.3"
27+
"standard": "^14.3.3",
28+
"tsd": "^0.11.0"
2729
},
2830
"dependencies": {
2931
"fast-deep-equal": "^3.1.1",

0 commit comments

Comments
 (0)