Skip to content

Commit

Permalink
refactor: use URL constructor (freeCodeCamp#328)
Browse files Browse the repository at this point in the history
Co-authored-by: yoko <[email protected]>
  • Loading branch information
ojeytonwilliams and sidemt authored Nov 20, 2023
1 parent 6d29d4c commit 38e899e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 105 deletions.
2 changes: 1 addition & 1 deletion apps/cron/src/jobs/check-and-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require("dotenv").config();

(async () => {
const res = await fetch(
`${process.env.STRAPI_URL}/api/posts/check-and-publish`,
new URL("/api/posts/check-and-publish", process.env.STRAPI_URL),
{
method: "GET",
headers: {
Expand Down
36 changes: 19 additions & 17 deletions apps/frontend/src/lib/invite-user.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import qs from "qs";

const api_root = `${process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL}/api`;
const base = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

export async function getInvitedUsers(token, queryParams) {
const res = await fetch(
`${api_root}/invited-users?${qs.stringify(queryParams, {
encodeValuesOnly: true,
})}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
const url = new URL("/api/invited-users", base);
url.search = qs.stringify(queryParams, {
encodeValuesOnly: true,
});
const res = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
);
});

try {
const data = await res.json();
Expand All @@ -32,7 +31,8 @@ export async function getInvitedUsers(token, queryParams) {
}

export async function inviteUser(token, data) {
const res = await fetch(`${api_root}/invited-users`, {
const url = new URL("/api/invited-users", base);
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -58,7 +58,8 @@ export async function inviteUser(token, data) {
}

export async function invitedUserExists(token, email) {
const endpoint = `${api_root}/invited-users?${qs.stringify(
const url = new URL("/api/invited-users", base);
url.search = qs.stringify(
{
filters: {
email: {
Expand All @@ -69,7 +70,7 @@ export async function invitedUserExists(token, email) {
{
encodeValuesOnly: true,
},
)}`;
);

const options = {
method: "GET",
Expand All @@ -79,7 +80,7 @@ export async function invitedUserExists(token, email) {
},
};

const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
throw new Error("invitedUserExists Failed");
Expand All @@ -90,7 +91,8 @@ export async function invitedUserExists(token, email) {
}

export async function deleteInvitedUser(token, id) {
const res = await fetch(`${api_root}/invited-users/${id}`, {
const url = new URL(`/api/invited-users/${id}`, base);
const res = await fetch(url, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Expand Down
37 changes: 16 additions & 21 deletions apps/frontend/src/lib/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import qs from "qs";

// Post API calls

const api_root = `${process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL}/api`;
const base = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

export async function getAllPosts(token, queryParams) {
const endpoint = `${api_root}/posts?${qs.stringify(queryParams, {
const url = new URL("/api/posts", base);
url.search = qs.stringify(queryParams, {
encodeValuesOnly: true,
})}`;
});

const options = {
method: "GET",
Expand All @@ -18,7 +19,7 @@ export async function getAllPosts(token, queryParams) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
console.error("getAllPosts responded with error. Status: ", res.status);
Expand All @@ -35,9 +36,10 @@ export async function getAllPosts(token, queryParams) {
}

export async function getUserPosts(token, queryParams) {
const endpoint = `${api_root}/posts?${qs.stringify(queryParams, {
const url = new URL("/api/posts", base);
url.search = qs.stringify(queryParams, {
encodeValuesOnly: true,
})}`;
});

const options = {
method: "GET",
Expand All @@ -48,7 +50,7 @@ export async function getUserPosts(token, queryParams) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
console.error("getUserPosts responded with error. Status: ", res.status);
Expand All @@ -65,7 +67,8 @@ export async function getUserPosts(token, queryParams) {
}

export async function getPost(postId, token) {
const endpoint = `${api_root}/posts/${postId}?populate=*`;
const url = new URL(`/api/posts/${postId}`, base);
url.search = "populate=*";

const options = {
method: "GET",
Expand All @@ -76,7 +79,7 @@ export async function getPost(postId, token) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
console.error(
Expand All @@ -95,22 +98,18 @@ export async function getPost(postId, token) {
}

export async function createPost(data, token) {
// API endpoint where we send form data.
const endpoint = `${api_root}/posts`;
const url = new URL("/api/posts", base);

// Form the request for sending data to the server.
const options = {
method: "POST",
// Tell the server we're sending JSON.
headers: {
"Content-Type": "application/json",
accept: "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
};
// Send the form data to our forms API and get a response.
const res = await fetch(endpoint, options);
const res = await fetch(url, options);
if (!res.ok) {
throw new Error("createPost Failed");
}
Expand All @@ -119,22 +118,18 @@ export async function createPost(data, token) {
}

export async function updatePost(postId, data, token) {
// API endpoint where we send form data.
const endpoint = `${api_root}/posts/${postId}`;
const url = new URL(`/api/posts/${postId}`, base);

// Form the request for sending data to the server.
const options = {
method: "PUT",
// Tell the server we're sending JSON.
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
};

// Send the form data to our forms API and get a response.
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
throw new Error("updatePost Failed");
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/src/lib/roles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const api_root = `${process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL}/api`;
const base = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

export async function getRoles(token) {
const endpoint = `${api_root}/users-permissions/roles`;
const url = new URL("/api/users-permissions/roles", base);

const options = {
method: "GET",
Expand All @@ -12,7 +12,7 @@ export async function getRoles(token) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

return res.json();
} catch (error) {
Expand Down
52 changes: 27 additions & 25 deletions apps/frontend/src/lib/tags.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import qs from "qs";

// Tag API calls

const api_root = `${process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL}/api`;
const base = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

export async function getTags(token, queryParams) {
const endpoint = `${api_root}/tags?${qs.stringify(queryParams, {
const url = new URL("/api/tags", base);
url.search = qs.stringify(queryParams, {
encodeValuesOnly: true,
})}`;
});

const options = {
method: "GET",
Expand All @@ -18,17 +18,17 @@ export async function getTags(token, queryParams) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

return res.json();
} catch (error) {
console.error("getTags responded with error. Status: ", res?.body);
throw new Error(`getTags responded with error. Status: ${res?.body}`);
console.error("getTags responded with error. Status: ", res.body);
throw new Error(`getTags responded with error. Status: ${res.body}`);
}
}

export async function createTag(token, data) {
const endpoint = `${api_root}/tags`;
const url = new URL("/api/tags", base);

const options = {
method: "POST",
Expand All @@ -40,11 +40,11 @@ export async function createTag(token, data) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

if (!res.ok) {
console.error("createTag responded with error. Status: ", res?.status);
throw new Error(`createTag responded with error. Status: ${res?.status}`);
console.error("createTag responded with error. Status: ", res.status);
throw new Error(`createTag responded with error. Status: ${res.status}`);
}

return res.json();
Expand All @@ -55,14 +55,15 @@ export async function createTag(token, data) {
}

export async function getTag(token, tagId) {
const endpoint = `${api_root}/tags/${tagId}?${qs.stringify(
const url = new URL(`/api/tags/${tagId}`, base);
url.search = qs.stringify(
{
populate: "*",
},
{
encodeValuesOnly: true,
},
)}`;
);

const options = {
method: "GET",
Expand All @@ -73,24 +74,25 @@ export async function getTag(token, tagId) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

return res.json();
} catch (error) {
console.error("getTag responded with error. Status: ", res?.body);
throw new Error(`getTag responded with error. Status: ${res?.body}`);
console.error("getTag responded with error. Status: ", res.body);
throw new Error(`getTag responded with error. Status: ${res.body}`);
}
}

export async function updateTag(token, tagId, data) {
const endpoint = `${api_root}/tags/${tagId}?${qs.stringify(
const url = new URL(`/api/tags/${tagId}`, base);
url.search = qs.stringify(
{
populate: "*",
},
{
encodeValuesOnly: true,
},
)}`;
);

const options = {
method: "PUT",
Expand All @@ -102,17 +104,17 @@ export async function updateTag(token, tagId, data) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

return res.json();
} catch (error) {
console.error("updateTag responded with error. Status: ", res?.body);
throw new Error(`updateTag responded with error. Status: ${res?.body}`);
console.error("updateTag responded with error. Status: ", res.body);
throw new Error(`updateTag responded with error. Status: ${res.body}`);
}
}

export async function deleteTag(token, tagId) {
const endpoint = `${api_root}/tags/${tagId}`;
const url = new URL(`/api/tags/${tagId}`, base);

const options = {
method: "DELETE",
Expand All @@ -123,11 +125,11 @@ export async function deleteTag(token, tagId) {
};

try {
const res = await fetch(endpoint, options);
const res = await fetch(url, options);

return res.json();
} catch (error) {
console.error("deleteTag responded with error. Status: ", res?.body);
throw new Error(`deleteTag responded with error. Status: ${res?.body}`);
console.error("deleteTag responded with error. Status: ", res.body);
throw new Error(`deleteTag responded with error. Status: ${res.body}`);
}
}
Loading

0 comments on commit 38e899e

Please sign in to comment.