-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
fetch.ts
79 lines (65 loc) 路 1.84 KB
/
fetch.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
import test from 'ava';
import ky from '../source/index.js';
const fixture = 'https://example.com/unicorn';
test('fetch option takes a custom fetch function', async t => {
t.plan(6);
const customFetch: typeof fetch = async input => {
if (!(input instanceof Request)) {
throw new TypeError('Expected to have input as request');
}
return new Response(input.url);
};
t.is(await ky(fixture, {fetch: customFetch}).text(), fixture);
t.is(
await ky(fixture, {
fetch: customFetch,
searchParams: {foo: 'bar'},
}).text(),
`${fixture}?foo=bar`,
);
t.is(
await ky(`${fixture}#hash`, {
fetch: customFetch,
searchParams: 'foo',
}).text(),
`${fixture}?foo#hash`,
);
t.is(
await ky(`${fixture}?old`, {
fetch: customFetch,
searchParams: 'new',
}).text(),
`${fixture}?new`,
);
t.is(
await ky(`${fixture}?old#hash`, {
fetch: customFetch,
searchParams: 'new',
}).text(),
`${fixture}?new#hash`,
);
t.is(await ky('unicorn', {fetch: customFetch, prefixUrl: `${fixture}/api/`}).text(), `${fixture}/api/unicorn`);
});
test('options are correctly passed to Fetch #1', async t => {
t.plan(1);
const cache = 'no-store';
const customFetch: typeof fetch = async request => {
t.is(request.cache, cache);
return new Response(request.url);
};
await ky(fixture, {cache, fetch: customFetch}).text();
});
test('options are correctly passed to Fetch #2', async t => {
const fixture = {x: true};
const json = await ky.post('https://httpbin.org/anything', {json: fixture}).json();
t.deepEqual(json.json, fixture);
});
test('unknown options are passed to fetch', async t => {
t.plan(1);
const options = {next: {revalidate: 3600}};
const customFetch: typeof fetch = async (request, init) => {
t.is(init.next, options.next);
return new Response(request.url);
};
await ky(fixture, {...options, fetch: customFetch}).text();
});