diff --git a/staff/marti-herms/project/V-HUB/app/logic/index.js b/staff/marti-herms/project/V-HUB/app/logic/index.js index ea7fba272..edddc1f27 100644 --- a/staff/marti-herms/project/V-HUB/app/logic/index.js +++ b/staff/marti-herms/project/V-HUB/app/logic/index.js @@ -4,6 +4,7 @@ import registerUser from './registerUser.js' import isUserLoggedIn from './isUserLoggedIn.js' import getUserUsername from './getUserUsername.js' import registerGame from './registerGame.js' +import searchGame from './searchGame.js' const logic = { loginUser, @@ -11,7 +12,8 @@ const logic = { registerUser, isUserLoggedIn, getUserUsername, - registerGame + registerGame, + searchGame } export default logic \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/app/logic/searchGame.js b/staff/marti-herms/project/V-HUB/app/logic/searchGame.js new file mode 100644 index 000000000..26ced45e7 --- /dev/null +++ b/staff/marti-herms/project/V-HUB/app/logic/searchGame.js @@ -0,0 +1,29 @@ +import { validate, errors } from 'com' + +const { SystemError } = errors + +export default (query) => { + validate.string(query, 'query') + + return fetch(`${import.meta.env.VITE_API_URL}/games/search?q=${query}`, { + headers: { Authorization: `Bearer ${sessionStorage.token}` } + }) + .catch(error => { throw new SystemError(error.message) }) + .then(response => { + const { status } = response + + if (status === 200) { + return response.json() + .then(games => games) + } + + return response.json() + .then(body => { + const { error, message } = body + + const constructor = errors[error] + + throw new constructor(message) + }) + }) +} \ No newline at end of file