Skip to content
This repository was archived by the owner on Aug 13, 2023. It is now read-only.

Conversation

@nisten711
Copy link
Collaborator

@nisten711 nisten711 commented May 15, 2021

No description provided.

@nisten711 nisten711 changed the title methods to call API endpoints frontend methods to call API endpoints May 15, 2021
@nisten711
Copy link
Collaborator Author

I don't know why it recreated with different spacing, maybe it was that recently pushed config, which uses 2 spacing?

return fetch(`${base_path}${endpoint}`, {
method,
mode: "cors",
...(additional !== undefined && { ...additional }),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...additional should be enough even if it is undefined. OR, if you're afraid of undefined, you can provide a default value: const makeApiRequest = async (base_path, endpoint, method, additional = {}) => ...

const uploadVideo = async (video) => {
const formData = new FormData();
formData.append('formFile', video);
const getUntrashedUserVideos = async (userId) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this function async, considering you're not using await ? Same applies to other functions in this file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function receives a response from makeApiRequest. response is a promise which is then delegated (passed) to the caller of this function. Function that returns a promise has to be marked with async to indicate that it is awaitable and does not return the 'raw result' but rather - a promise.

const formData = new FormData();
formData.append('formFile', video);
const getUntrashedUserVideos = async (userId) => {
const response = makeApiRequest(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it could be simplified to:

return makeApiRequest(...)

});
}
if (typeof response.text === "function") {
return response.text().then((text) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you assume that text() returns a promise/thenable?

return response;
}).catch(error => {
console.log(error);
return fetch(`${API_URL_PREFIX}${endpoint}`, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function would probably read better with await and try-catch. An idea - it could take onSuccess and onError callbacks as parameters to make it more reusable

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants