generated from VLADIMIR/template_frontend
add get team method
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import type { Team } from './models'
|
||||
import { UnauthorizedError } from './UnauthorizedError'
|
||||
|
||||
export const apiGetTeam = async (login: string, password: string): Promise<Team> => {
|
||||
try {
|
||||
const response = await fetch(getApiUrl('/team'), {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Id': encodeUTF8ToBase64(login),
|
||||
'X-Password': password,
|
||||
},
|
||||
})
|
||||
if (response.status === 401) {
|
||||
throw new UnauthorizedError('Ошибка авторизации')
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`http error status: ${response.status}`)
|
||||
}
|
||||
return await response.json()
|
||||
} catch (error) {
|
||||
console.error('[apiGetTeam] error:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function getApiUrl(path: string) {
|
||||
const url = 'http://' + window.location.host.split(':')[0] + ':8090' + path
|
||||
return url
|
||||
}
|
||||
|
||||
export function encodeUTF8ToBase64(s: string) {
|
||||
return btoa(
|
||||
encodeURIComponent(s).replace(/%([0-9A-F]{2})/g, (_, p1) =>
|
||||
String.fromCharCode(parseInt(p1, 16)),
|
||||
),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user