users.service.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import http from '@/core/api/http'
  2. import type {
  3. GetUserResponse,
  4. GetUserDetailResponse,
  5. CreateUserPayload,
  6. } from '@/types/users.types'
  7. import type { ApiResponse, PaginationParams } from '@/types/common.types'
  8. const BASE_URL = '/admin/users'
  9. export const usersService = {
  10. list: async (params?: PaginationParams): Promise<GetUserResponse> => {
  11. const searchParams = new URLSearchParams()
  12. if (params?.page) searchParams.set('page', String(params.page))
  13. if (params?.per_page) searchParams.set('per_page', String(params.per_page))
  14. const query = searchParams.toString()
  15. const url = query ? `${BASE_URL}?${query}` : BASE_URL
  16. return http.get<GetUserResponse>(url)
  17. },
  18. detail: async (userId: number | string) => {
  19. const res = await http.get<GetUserDetailResponse>(`${BASE_URL}/${userId}`)
  20. return res?.data?.user
  21. },
  22. create: async (payload: CreateUserPayload) => {
  23. const res = await http.post<ApiResponse<void>>(BASE_URL, payload)
  24. return res?.data
  25. },
  26. update: async (id: number, payload: CreateUserPayload) => {
  27. const res = await http.put<ApiResponse<void>>(`${BASE_URL}/${id}`, payload)
  28. return res?.data
  29. },
  30. delete: async (userId: number | string) => {
  31. return http.delete(`${BASE_URL}/${userId}`)
  32. },
  33. }