@@ -3,71 +3,70 @@ import { ABORT_ERROR, TIMEOUT_ERROR } from "../src/fetch/defines.ts";
3
3
import { fetchT } from '../src/mod.ts' ;
4
4
5
5
Deno . test ( 'fetch' , async ( t ) => {
6
- const mockServer = 'https://16a6dafa-2258-4a83-88fa-31a409e42b17.mock.pstmn.io' ;
7
- const mockTodos = `${ mockServer } /todos` ;
8
- const mockTodo1 = `${ mockTodos } /1` ;
9
- const mockInvalidJson = `${ mockServer } /invalid_json` ;
6
+ const mockServer = 'https://fakestoreapi.com' ;
7
+ const mockAll = `${ mockServer } /products` ;
8
+ const mockSingle = `${ mockAll } /1` ;
10
9
const mockNotFound = `${ mockServer } /not_found` ;
10
+ const mockInvalidJson = `https://github.com/JiangJie/fetch-t` ;
11
11
12
12
await t . step ( 'Invalid url will throw' , ( ) => {
13
13
const url = { } ;
14
14
assertThrows ( ( ) => fetchT ( ( url as string ) ) , Error ) ;
15
15
} ) ;
16
16
17
17
await t . step ( 'Get Response by default' , async ( ) => {
18
- const res = ( await fetchT ( mockTodos ) ) . unwrap ( ) ;
18
+ const res = ( await fetchT ( mockSingle ) ) . unwrap ( ) ;
19
19
const data = await res . text ( ) ;
20
20
21
- assert ( data . includes ( `"id": 1` ) ) ;
21
+ assert ( typeof data === 'string' ) ;
22
22
} ) ;
23
23
24
24
await t . step ( 'Get Response by RequestInit' , async ( ) => {
25
- const res = ( await fetchT ( mockTodo1 , {
25
+ const res = ( await fetchT ( mockSingle , {
26
26
mode : 'no-cors' ,
27
27
} as RequestInit ) ) . unwrap ( ) ;
28
28
const data = await res . text ( ) ;
29
29
30
- assert ( data . includes ( `"id": 1` ) ) ;
30
+ assert ( typeof data === 'string' ) ;
31
31
} ) ;
32
32
33
33
await t . step ( 'Get text by response type' , async ( ) => {
34
- const data = ( await fetchT ( mockTodo1 , {
34
+ const data = ( await fetchT ( mockSingle , {
35
35
responseType : 'text' ,
36
36
} ) ) . unwrap ( ) ;
37
37
38
- assert ( data . includes ( `"id": 1` ) ) ;
38
+ assert ( typeof data === 'string' ) ;
39
39
} ) ;
40
40
41
41
await t . step ( 'Get arraybuffer by response type' , async ( ) => {
42
- const data = ( await fetchT ( mockTodo1 , {
42
+ const data = ( await fetchT ( mockSingle , {
43
43
responseType : 'arraybuffer' ,
44
44
} ) ) . unwrap ( ) ;
45
45
46
- assert ( data . byteLength === 37 ) ;
46
+ assert ( data instanceof ArrayBuffer ) ;
47
47
} ) ;
48
48
49
49
await t . step ( 'Get blob by response type' , async ( ) => {
50
- const data = ( await fetchT ( mockTodo1 , {
50
+ const data = ( await fetchT ( mockSingle , {
51
51
responseType : 'blob' ,
52
52
} ) ) . unwrap ( ) ;
53
53
54
- assert ( data . size === 37 ) ;
54
+ assert ( data instanceof Blob ) ;
55
55
} ) ;
56
56
57
57
await t . step ( 'Get json by response type' , async ( ) => {
58
- const data = ( await fetchT < { id : number } [ ] > ( mockTodos , {
58
+ const data = ( await fetchT < { id : number } > ( mockSingle , {
59
59
responseType : 'json' ,
60
60
method : 'GET' ,
61
61
} ) ) . unwrap ( ) ;
62
62
63
- assert ( data . length === 1 ) ;
64
- assert ( data [ 0 ] . id === 1 ) ;
63
+ assert ( 'id' in data ) ;
65
64
} ) ;
66
65
67
66
await t . step ( 'Post json' , async ( ) => {
68
67
const data = ( await fetchT < {
69
- id : number ;
70
- } > ( mockTodos , {
68
+ title : string ;
69
+ } > ( mockAll , {
71
70
responseType : 'json' ,
72
71
method : 'POST' ,
73
72
body : JSON . stringify ( {
@@ -78,33 +77,30 @@ Deno.test('fetch', async (t) => {
78
77
} ,
79
78
} ) ) . unwrap ( ) ;
80
79
81
- assert ( data . id === 2 ) ;
80
+ assert ( data . title === 'happy-2' ) ;
82
81
} ) ;
83
82
84
83
await t . step ( 'Put json' , async ( ) => {
85
84
const data = ( await fetchT < {
86
- id : number ;
87
85
title : string ;
88
- } > ( mockTodo1 , {
86
+ } > ( mockSingle , {
89
87
responseType : 'json' ,
90
88
method : 'PUT' ,
91
89
body : JSON . stringify ( {
92
- id : 100 ,
93
90
title : 'happy-new' ,
94
91
} ) ,
95
92
headers : {
96
93
'Content-type' : 'application/json; charset=UTF-8' ,
97
94
} ,
98
95
} ) ) . unwrap ( ) ;
99
96
100
- assert ( data . id === 100 ) ;
101
97
assert ( data . title === 'happy-new' ) ;
102
98
} ) ;
103
99
104
100
await t . step ( 'Patch json' , async ( ) => {
105
101
const data = ( await fetchT < {
106
102
title : string ;
107
- } > ( mockTodo1 , {
103
+ } > ( mockSingle , {
108
104
responseType : 'json' ,
109
105
method : 'PATCH' ,
110
106
body : JSON . stringify ( {
@@ -120,13 +116,13 @@ Deno.test('fetch', async (t) => {
120
116
121
117
await t . step ( 'Delete json' , async ( ) => {
122
118
const data = ( await fetchT < {
123
- success : boolean ;
124
- } > ( mockTodo1 , {
119
+ title : boolean ;
120
+ } > ( mockSingle , {
125
121
responseType : 'json' ,
126
122
method : 'DELETE' ,
127
123
} ) ) . unwrap ( ) ;
128
124
129
- assert ( data . success ) ;
125
+ assert ( 'title' in data ) ;
130
126
} ) ;
131
127
132
128
await t . step ( 'Get invalid json' , async ( ) => {
@@ -138,7 +134,7 @@ Deno.test('fetch', async (t) => {
138
134
} ) ;
139
135
140
136
await t . step ( 'Abort fetch by default' , async ( ) => {
141
- const fetchTask = fetchT ( mockTodo1 , {
137
+ const fetchTask = fetchT ( mockSingle , {
142
138
abortable : true ,
143
139
timeout : 1000 ,
144
140
} ) ;
@@ -153,7 +149,7 @@ Deno.test('fetch', async (t) => {
153
149
} ) ;
154
150
155
151
await t . step ( 'Abort fetch by custom' , async ( ) => {
156
- const fetchTask = fetchT ( mockTodo1 , {
152
+ const fetchTask = fetchT ( mockSingle , {
157
153
abortable : true ,
158
154
timeout : 1000 ,
159
155
} ) ;
@@ -173,13 +169,13 @@ Deno.test('fetch', async (t) => {
173
169
} ) ;
174
170
175
171
await t . step ( 'Invalid timeout' , ( ) => {
176
- assertThrows ( ( ) => fetchT ( mockTodo1 , {
172
+ assertThrows ( ( ) => fetchT ( mockSingle , {
177
173
timeout : - 1 ,
178
174
} ) , Error ) ;
179
175
} ) ;
180
176
181
177
await t . step ( 'Abort fetch by timeout' , async ( ) => {
182
- const res = await fetchT ( mockTodo1 , {
178
+ const res = await fetchT ( mockSingle , {
183
179
timeout : 1 ,
184
180
} ) ;
185
181
0 commit comments