-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-practice.js
84 lines (72 loc) · 2.31 KB
/
fetch-practice.js
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
// options gets passed into fetch
// tells the fetch what we want to do for that endpoint (CRUD)
const fetchData = async (url, options = {}) => {
try {
// 1 send the request, get the HTTP response
const response = await fetch(url, options);
// 2. Check to see if the
// Throw an error if the response was not 2xx - let the catch statement handle it
if (!response.ok) throw new Error(`Fetch failed. ${response.status} ${response.statusText}`)
const contentType = response.headers.get('content-type');
if (contentType === null || !contentType.includes('application/json')) {
// If the contentType of the response is not JSON, read the stream as plain text
const textData = await response.text();
return textData;
}
// 3. Read the ReadableStream
const jsonData = await response.json();
return jsonData;
}
catch (error) {
// if there was an error, log it and return a tuple: [data, error]
console.error(error.message);
return null;
}
}
const baseUrl = 'https://test-server-pgma.onrender.com'
// const baseUrl = 'http://localhost:3000'
const password = 'bananabread';
const getPosts = () => {
fetchData(`${baseUrl}/api/posts`).then(posts => {
console.log(posts || 'error getting posts');
});
}
const getPostsByUserName = async (username) => {
const posts = await fetchData(`${baseUrl}/api/posts/${username}`);
console.log(posts || 'error getting posts');
}
const getPostById = async (id) => {
const posts = await fetchData(`${baseUrl}/api/posts/${id}`);
console.log(posts || 'error getting posts');
}
const createPost = async (username, content, img_url) => {
const newPost = {
username,
content,
img_url
}
const options = {
method: "POST",
body: JSON.stringify(newPost),
headers: {
"Content-Type": "application/json"
}
}
const returnedPost = await fetchData(`${baseUrl}/api/posts?password=bananabread`, options);
console.log(returnedPost || 'error creating post');
}
const deletePostById = (id) => {
const options = {
method: 'DELETE'
}
fetchData(`${baseUrl}/api/posts/${id}?password=bananabread`, options)
}
const main = async () => {
console.log('start')
// createPost('Ben', 'Hello Hanat!')
// deletePostById(22);
// getPosts();
// getPostsByUserName('Ben');
console.log('done')
}
main();