-
Notifications
You must be signed in to change notification settings - Fork 0
frontend methods to call API endpoints #15
base: main
Are you sure you want to change the base?
Conversation
|
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 }), |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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}`, { |
There was a problem hiding this comment.
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
No description provided.