39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import {
|
|
UserApi,
|
|
Configuration,
|
|
type CreateUserDto,
|
|
type UserDto
|
|
} from '~/.api-client'
|
|
import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
|
|
|
|
export function useUserApi() {
|
|
const appBaseUrl = useRuntimeConfig().app.baseURL
|
|
const { serverApiBaseUrl, serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
|
const { jwt } = useAuth()
|
|
|
|
const basePath = withoutTrailingSlash(
|
|
cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : serverApiBaseUrl + serverApiBasePath)
|
|
)
|
|
|
|
const userApiClient = new UserApi(
|
|
new Configuration({ basePath, headers: { Authorization: jwt.value ? `Bearer ${jwt.value}` : '' } })
|
|
)
|
|
|
|
async function createUser(createUserDto: CreateUserDto): Promise<UserDto> {
|
|
return userApiClient.createUser({ createUserDto })
|
|
}
|
|
|
|
async function getUserById(id: string): Promise<UserDto> {
|
|
return userApiClient.getUserById({ id })
|
|
}
|
|
|
|
async function deleteUser(id: string): Promise<void> {
|
|
return userApiClient.deleteUser({ id })
|
|
}
|
|
|
|
return {
|
|
createUser,
|
|
getUserById,
|
|
deleteUser
|
|
}
|
|
}
|