api.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import axios, { isAxiosError } from 'axios';
  2. import { hc } from 'hono/client'
  3. import type {
  4. AuthRoutes, UserRoutes, RoleRoutes,
  5. FileRoutes, AdminActivitiesRoutes, AdminRoutesRoutes,
  6. AdminAreasRoutes, AdminLocationsRoutes, RoutesRoutes,
  7. AdminPassengersRoutes
  8. } from '@d8d/server/api';
  9. // 创建 axios 适配器
  10. const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
  11. const requestHeaders: Record<string, string> = {};
  12. if (init?.headers instanceof Headers) {
  13. init.headers.forEach((value, key) => {
  14. requestHeaders[key] = value;
  15. })
  16. }
  17. const response = await axios.request({
  18. url: url.toString(),
  19. method: init?.method || 'GET',
  20. headers: requestHeaders,
  21. data: init?.body,
  22. }).catch((error) => {
  23. console.log('axiosFetch error', error)
  24. if (isAxiosError(error)) {
  25. return {
  26. status: error.response?.status,
  27. statusText: error.response?.statusText,
  28. data: error.response?.data,
  29. headers: error.response?.headers
  30. }
  31. }
  32. throw error;
  33. })
  34. const responseHeaders = new Headers();
  35. if (response.headers) {
  36. for (const [key, value] of Object.entries(response.headers)) {
  37. responseHeaders.set(key, value);
  38. }
  39. }
  40. // 处理204 No Content响应,不设置body
  41. const body = response.status === 204
  42. ? null
  43. : responseHeaders.get('content-type')?.includes('application/json')
  44. ? JSON.stringify(response.data)
  45. : response.data;
  46. return new Response(
  47. body,
  48. {
  49. status: response.status,
  50. statusText: response.statusText,
  51. headers: responseHeaders
  52. }
  53. )
  54. }
  55. export const authClient = hc<AuthRoutes>('/', {
  56. fetch: axiosFetch,
  57. }).api.v1.auth;
  58. export const userClient = hc<UserRoutes>('/', {
  59. fetch: axiosFetch,
  60. }).api.v1.users;
  61. export const roleClient = hc<RoleRoutes>('/', {
  62. fetch: axiosFetch,
  63. }).api.v1.roles;
  64. export const fileClient = hc<FileRoutes>('/', {
  65. fetch: axiosFetch,
  66. }).api.v1.files;
  67. export const activityClient = hc<AdminActivitiesRoutes>('/', {
  68. fetch: axiosFetch,
  69. }).api.v1.admin.activities;
  70. export const routeClient = hc<AdminRoutesRoutes>('/', {
  71. fetch: axiosFetch,
  72. }).api.v1.admin.routes;
  73. export const areaClient = hc<AdminAreasRoutes>('/', {
  74. fetch: axiosFetch,
  75. }).api.v1.admin.areas;
  76. export const locationClient = hc<AdminLocationsRoutes>('/', {
  77. fetch: axiosFetch,
  78. }).api.v1.admin.locations;
  79. export const publicRouteClient = hc<RoutesRoutes>('/', {
  80. fetch: axiosFetch,
  81. }).api.v1.routes;
  82. export const passengerClient = hc<AdminPassengersRoutes>('/', {
  83. fetch: axiosFetch,
  84. }).api.v1.admin.passengers;