85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
|
|
import axiosInstance from '@/lib/axios'
|
||
|
|
import type { ApiResponse, LoginResponse } from '@/types/api'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Auth API Service
|
||
|
|
*/
|
||
|
|
export const authApi = {
|
||
|
|
/**
|
||
|
|
* Login with email and password
|
||
|
|
*/
|
||
|
|
login: async (email: string, password: string): Promise<ApiResponse<LoginResponse>> => {
|
||
|
|
const response = await axiosInstance.post<ApiResponse<LoginResponse>>('/v1/login', {
|
||
|
|
email,
|
||
|
|
password,
|
||
|
|
})
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Logout current user
|
||
|
|
*/
|
||
|
|
logout: async (): Promise<ApiResponse<null>> => {
|
||
|
|
const response = await axiosInstance.post<ApiResponse<null>>('/v1/logout')
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get current user profile
|
||
|
|
*/
|
||
|
|
me: async (): Promise<ApiResponse<any>> => {
|
||
|
|
const response = await axiosInstance.get<ApiResponse<any>>('/v1/users/me')
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Tenant API Service
|
||
|
|
*/
|
||
|
|
export const tenantApi = {
|
||
|
|
/**
|
||
|
|
* Switch to different tenant
|
||
|
|
*/
|
||
|
|
switch: async (tenantId: number): Promise<ApiResponse<any>> => {
|
||
|
|
const response = await axiosInstance.post<ApiResponse<any>>(`/v1/tenants/${tenantId}/switch`)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get list of tenants for current user
|
||
|
|
*/
|
||
|
|
list: async (): Promise<ApiResponse<any>> => {
|
||
|
|
const response = await axiosInstance.get<ApiResponse<any>>('/v1/users/me/tenants')
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generic API helper for CRUD operations
|
||
|
|
*/
|
||
|
|
export const createCrudApi = <T>(basePath: string) => ({
|
||
|
|
list: async (params?: Record<string, any>): Promise<ApiResponse<T[]>> => {
|
||
|
|
const response = await axiosInstance.get<ApiResponse<T[]>>(basePath, { params })
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
get: async (id: number | string): Promise<ApiResponse<T>> => {
|
||
|
|
const response = await axiosInstance.get<ApiResponse<T>>(`${basePath}/${id}`)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
create: async (data: Partial<T>): Promise<ApiResponse<T>> => {
|
||
|
|
const response = await axiosInstance.post<ApiResponse<T>>(basePath, data)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
update: async (id: number | string, data: Partial<T>): Promise<ApiResponse<T>> => {
|
||
|
|
const response = await axiosInstance.put<ApiResponse<T>>(`${basePath}/${id}`, data)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
|
||
|
|
delete: async (id: number | string): Promise<ApiResponse<null>> => {
|
||
|
|
const response = await axiosInstance.delete<ApiResponse<null>>(`${basePath}/${id}`)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
})
|