forked from sindresorhus/ky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
141 lines (126 loc) · 2.99 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
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
import {expectType} from 'tsd';
import ky, {HTTPError, TimeoutError, ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.';
const url = 'https://sindresorhus';
// Test Ky
expectType<ResponsePromise>(ky(url));
const requestMethods = [
'get',
'post',
'put',
'delete',
'patch',
'head',
] as const;
// Test Ky HTTP methods
for (const method of requestMethods) {
expectType<ResponsePromise>(ky[method](url));
ky(url, {method});
}
expectType<typeof ky>(ky.create({}));
expectType<typeof ky>(ky.extend({}));
expectType<HTTPError>(new HTTPError(new Response));
expectType<TimeoutError>(new TimeoutError);
ky(url, {
hooks: {
beforeRequest: [
(request, options) => {
expectType<Request>(request);
expectType<NormalizedOptions>(options);
request.headers.set('foo', 'bar');
},
(_request, _options) => {
return new Request('Test');
},
async (_request, _options) => {
return new Request('Test');
},
(_request, _options) => {
return new Response('Test');
},
async (_request, _options) => {
return new Response('Test');
}
],
beforeRetry: [
(request, options, error, retryCount) => {
expectType<Request>(request);
expectType<NormalizedOptions>(options);
expectType<Error>(error);
expectType<number>(retryCount);
request.headers.set('foo', 'bar');
}
],
afterResponse: [
(request, options, response) => {
expectType<Request>(request);
expectType<NormalizedOptions>(options);
expectType<Response>(response);
},
(_request, _options, _response) => {
return new Response('Test');
},
async (_request, _options, _response) => {
return new Response('Test');
}
]
}
});
ky(new URL(url));
ky(new Request(url));
// Reusable types
const input: Input = new URL('https://sindresorhus');
const options: Options = {
method: 'get',
timeout: 5000,
}
ky(input, options);
// Extending Ky
interface CustomOptions extends Options {
foo?: boolean;
}
async function customKy(input: Input, options?: CustomOptions) {
if (options && options.foo) {
options.json = {foo: options.foo};
}
return ky(input, options);
}
customKy(input, options);
// `searchParams` option
ky(url, {searchParams: 'foo=bar'});
ky(url, {searchParams: {foo: 'bar'}});
ky(url, {searchParams: {foo: 1}});
ky(url, {searchParams: {foo: true}});
ky(url, {searchParams: [['foo', 'bar']]});
ky(url, {searchParams: [['foo', 1]]});
ky(url, {searchParams: [['foo', true]]});
ky(url, {searchParams: new URLSearchParams({foo: 'bar'})});
// `json` option
ky.post(url, {
json: {
foo: true
}
});
ky.post(url, {
json: 'x'
});
expectType<Promise<unknown>>(ky(url).json());
interface Result {
value: number;
}
expectType<Promise<Result>>(ky(url).json<Result>());
// `onDownloadProgress` option
ky(url, {
onDownloadProgress: (progress, chunk) => {
expectType<DownloadProgress>(progress);
expectType<Uint8Array>(chunk);
}
});
// `retry` option
ky(url, {retry: 100});
ky(url, {
retry: {
methods: [],
statusCodes: [],
afterStatusCodes: []
}
});