-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.ts
More file actions
162 lines (138 loc) · 3.97 KB
/
basic.ts
File metadata and controls
162 lines (138 loc) · 3.97 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Basic fetch examples demonstrating core functionality.
*
* Run with: npx tsx examples/basic.ts
* Or with Deno: deno run --allow-net examples/basic.ts
*/
import { fetchT } from '../src/mod.ts';
const API_BASE = 'https://jsonplaceholder.typicode.com';
/**
* Example 1: Simple GET request with JSON response
*/
async function fetchJson() {
console.log('--- Example 1: GET JSON ---');
const result = await fetchT(`${API_BASE}/posts/1`, {
responseType: 'json',
});
result
.inspect((data) => {
if (data == null) {
console.log('No body');
return;
}
console.log('Post title:', (data as { title: string; }).title);
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 2: GET request with text response
*/
async function fetchText() {
console.log('\n--- Example 2: GET Text ---');
const result = await fetchT(`${API_BASE}/posts/1`, {
responseType: 'text',
});
result
.inspect((text) => {
console.log('Response length:', text.length, 'characters');
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 3: POST request with JSON body
*/
async function postJson() {
console.log('\n--- Example 3: POST JSON ---');
const result = await fetchT(`${API_BASE}/posts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'Hello World',
body: 'This is a test post',
userId: 1,
}),
responseType: 'json',
});
result
.inspect((data) => {
if (data == null) {
console.log('No body');
return;
}
console.log('Created post with id:', (data as { id: number; }).id);
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 4: GET request returning raw Response object
*/
async function fetchRawResponse() {
console.log('\n--- Example 4: Raw Response ---');
const result = await fetchT(`${API_BASE}/posts/1`);
result
.inspect((response) => {
console.log('Status:', response.status);
console.log('Content-Type:', response.headers.get('content-type'));
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 5: GET request with ArrayBuffer response
*/
async function fetchArrayBuffer() {
console.log('\n--- Example 5: ArrayBuffer Response ---');
const result = await fetchT(`${API_BASE}/posts/1`, {
responseType: 'arraybuffer',
});
result
.inspect((buffer) => {
console.log('Buffer size:', buffer.byteLength, 'bytes');
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 6: GET request with Stream response
*/
async function fetchStream() {
console.log('\n--- Example 6: Stream Response ---');
const result = await fetchT(`${API_BASE}/posts/1`, {
responseType: 'stream',
});
if (result.isOk()) {
const stream = result.unwrap();
if (!stream) {
console.log('No body (stream is null)');
return;
}
const reader = stream.getReader();
let totalBytes = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
totalBytes += value.byteLength;
}
console.log('Stream completed, total bytes:', totalBytes);
} else {
console.error('Failed:', result.unwrapErr().message);
}
}
// Run all examples
console.log('=== Basic Fetch Examples ===\n');
await fetchJson();
await fetchText();
await postJson();
await fetchRawResponse();
await fetchArrayBuffer();
await fetchStream();