-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const BASE_PATH = "http://localhost:44366"; | ||
|
|
||
| export const EP_GET_HEALTH = "/health"; | ||
| export const EP_GET_TRASHED_USER_VIDEOS = (userId) => | ||
| `/video/trashed?userId=${userId}`; | ||
| export const EP_GET_UNTRASHED_USER_VIDEOS = (userId) => | ||
| `/video/untrashed?userId=${userId}`; | ||
| export const EP_PUT_TRASH_USER_VIDEO = (userId, videoId) => | ||
| `/video/trash?videoId=${videoId}`; | ||
| export const EP_DELETE_USER_VIDEO = (videoId) => `/video?videoId=${videoId}`; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,69 @@ | ||
| import { API_URL_PREFIX } from '../constants'; | ||
| import { API_URL_PREFIX } from "../constants"; | ||
|
|
||
| const makeRequest = async (endpoint, method, additional) => { | ||
| return fetch( | ||
| `${API_URL_PREFIX}${endpoint}`, | ||
| { | ||
| method, | ||
| mode: 'cors', | ||
| ...(additional !== undefined && { ...additional }), | ||
| } | ||
| ).then(response => { | ||
| if (response.ok) { | ||
| return response.json().catch(error => { | ||
| return {}; | ||
| }); | ||
| } | ||
| if (typeof response.text === 'function') { | ||
| return response.text().then(text => { | ||
| return { | ||
| 'error': text, | ||
| 'statusCode': response.status, | ||
| } | ||
| }); | ||
| } | ||
| return { | ||
| 'error': response.statusText, | ||
| 'statusCode': response.status, | ||
| } | ||
| }).then(response => { | ||
| return response; | ||
| }).catch(error => { | ||
| console.log(error); | ||
| return fetch(`${API_URL_PREFIX}${endpoint}`, { | ||
| method, | ||
| mode: "cors", | ||
| ...(additional !== undefined && { ...additional }), | ||
| }) | ||
| .then((response) => { | ||
| if (response.ok) { | ||
| return response.json().catch((error) => { | ||
| return {}; | ||
| }); | ||
| } | ||
| if (typeof response.text === "function") { | ||
| return response.text().then((text) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you assume that |
||
| return { | ||
| error: text, | ||
| statusCode: response.status, | ||
| }; | ||
| }); | ||
| } | ||
| return { | ||
| error: response.statusText, | ||
| statusCode: response.status, | ||
| }; | ||
| }) | ||
| .then((response) => { | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export default makeRequest; | ||
| const makeApiRequest = async (base_path, endpoint, method, additional) => { | ||
| return fetch(`${base_path}${endpoint}`, { | ||
| method, | ||
| mode: "cors", | ||
| ...(additional !== undefined && { ...additional }), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }) | ||
| .then((response) => { | ||
| if (response.ok) { | ||
| return response.json().catch((error) => { | ||
| return {}; | ||
| }); | ||
| } | ||
| if (typeof response.text === "function") { | ||
| return response.text().then((text) => { | ||
| return { | ||
| error: text, | ||
| statusCode: response.status, | ||
| }; | ||
| }); | ||
| } | ||
| return { | ||
| error: response.statusText, | ||
| statusCode: response.status, | ||
| }; | ||
| }) | ||
| .then((response) => { | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| }); | ||
| }; | ||
|
|
||
| export { makeRequest, makeApiRequest }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,53 @@ | ||
| import makeRequest from "./baseClient"; | ||
| import { makeRequest, makeApiRequest } from "./baseClient"; | ||
| import { | ||
| BASE_PATH, | ||
| EP_DELETE_USER_VIDEO, | ||
| EP_GET_TRASHED_USER_VIDEOS, | ||
| EP_GET_UNTRASHED_USER_VIDEOS, | ||
| EP_PUT_TRASH_USER_VIDEO, | ||
| } from "../api/api.constants"; | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Why is this function
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function receives a |
||
| const response = makeApiRequest( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like it could be simplified to: |
||
| BASE_PATH, | ||
| EP_GET_UNTRASHED_USER_VIDEOS(userId), | ||
| "GET" | ||
| ); | ||
| return response; | ||
| }; | ||
|
|
||
| const getTrashedUserVideos = async (userId) => { | ||
| const response = makeApiRequest( | ||
| BASE_PATH, | ||
| EP_GET_TRASHED_USER_VIDEOS(userId), | ||
| "GET" | ||
| ); | ||
| return response; | ||
| }; | ||
|
|
||
| const trashUserVideo = async (userId, videoId) => { | ||
| const response = makeApiRequest( | ||
| BASE_PATH, | ||
| EP_PUT_TRASH_USER_VIDEO(userId, videoId), | ||
| "PUT" | ||
| ); | ||
| return response; | ||
| }; | ||
|
|
||
| return makeRequest('video', 'POST', { body: formData }); | ||
| } | ||
| const deleteUserVideo = async (userId, videoId) => { | ||
| const response = makeApiRequest( | ||
| BASE_PATH, | ||
| EP_DELETE_USER_VIDEO(videoId), | ||
| "DELETE" | ||
| ); | ||
| return response; | ||
| }; | ||
|
|
||
| export { | ||
| uploadVideo, | ||
| const uploadVideo = async (video) => { | ||
| const formData = new FormData(); | ||
| formData.append("formFile", video); | ||
|
|
||
| return makeRequest("video", "POST", { body: formData }); | ||
| }; | ||
|
|
||
| export { uploadVideo }; | ||
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
awaitand try-catch. An idea - it could takeonSuccessandonErrorcallbacks as parameters to make it more reusable