-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
index.test-d.ts
86 lines (72 loc) · 1.68 KB
/
index.test-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
import {expectType} from 'tsd-check';
import createTestServer from 'create-test-server';
import ky, {Ky, HTTPError, TimeoutError, ResponsePromise, JSONValue} from '.';
const server = await createTestServer();
server.get('/', (request, response) => {
response.end();
});
server.get('/timeout', (request, response) => {
setTimeout(() => response.end(), 11000);
});
// Test Ky
expectType<ResponsePromise>(ky(server.url));
const requestMethods = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
const requestBodyMethods = [
'post',
'put',
'delete'
];
// Test Ky HTTP methods
for (const method of requestMethods) {
expectType<ResponsePromise>(await ky[method](server.url));
}
// Test Ky HTTP methods with `body`
for (const method of requestBodyMethods) {
expectType<ResponsePromise>(await ky[method](server.url, {body: 'x'}));
}
expectType<Ky>(ky.extend({}));
expectType<HTTPError>(new HTTPError());
expectType<TimeoutError>(new TimeoutError);
ky(server.url, {
hooks: {
beforeRequest: [
options => {
expectType<Object>(options);
}
],
afterResponse: [
response => {
expectType<Response>(response);
return new Response('Test');
}
]
}
});
ky(new URL(server.url));
ky(new Request(server.url));
// `searchParams` option
ky(server.url, {searchParams: 'foo=bar'});
ky(server.url, {searchParams: {foo: 'bar'}});
ky(server.url, {searchParams: {foo: 1}});
ky(server.url, {searchParams: new URLSearchParams({foo: 'bar'})});
// `json` option
ky.post(server.url, {
json: {
foo: true
}
});
ky.post(server.url, {
json: 'x'
});
expectType<JSONValue>(await ky(server.url).json());
interface Result {
value: number;
}
expectType<Result>(await ky(server.url).json<Result>());