43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { UserApi, Configuration, type CreateUserDto, type UserDto } from '~/.api-client'
|
|
import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
|
|
import { useAuthState } from '~/composables/auth/useAuthState'
|
|
|
|
export function useUserApi() {
|
|
const appBaseUrl = useRuntimeConfig().app.baseURL
|
|
const { serverApiBaseUrl, serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
|
const { jwt } = useAuthState()
|
|
|
|
const basePath = withoutTrailingSlash(
|
|
cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : serverApiBaseUrl + serverApiBasePath)
|
|
)
|
|
|
|
// Track changing JWT of user who accepts the invitation
|
|
const userApiClient = computed(
|
|
() =>
|
|
new UserApi(new Configuration({ basePath, headers: { Authorization: jwt.value ? `Bearer ${jwt.value}` : '' } }))
|
|
)
|
|
|
|
async function createUser(createUserDto: CreateUserDto): Promise<UserDto> {
|
|
return userApiClient.value.createUser({ createUserDto })
|
|
}
|
|
|
|
async function getUserById(id: string): Promise<UserDto> {
|
|
return userApiClient.value.getUserById({ id })
|
|
}
|
|
|
|
async function updateUser(id: string, userDto?: UserDto): Promise<UserDto> {
|
|
return userApiClient.value.updateUser({ id, userDto })
|
|
}
|
|
|
|
async function deleteUser(id: string): Promise<void> {
|
|
return userApiClient.value.deleteUser({ id })
|
|
}
|
|
|
|
return {
|
|
createUser,
|
|
getUserById,
|
|
updateUser,
|
|
deleteUser
|
|
}
|
|
}
|