|
|
@@ -1,71 +1,100 @@
|
|
|
-// 在现有基础上添加银龄人才API
|
|
|
-// 读取现有文件内容
|
|
|
-import { hc } from 'hono/client'
|
|
|
-import axios from 'axios'
|
|
|
-import type { AuthRoutes } from '@/server/api'
|
|
|
-import type { UserRoutes } from '@/server/api'
|
|
|
-import type { SilverJobsRoutes } from '@/server/api'
|
|
|
-import type { SilverTalentsRoutes } from '@/server/api'
|
|
|
-import type { FileRoutes } from '@/server/api'
|
|
|
-import type { ElderlyUniversityRoutes } from '@/server/api'
|
|
|
-import type { PolicyNewsRoutes } from '@/server/api'
|
|
|
-import type { HomeRoutes } from '@/server/api'
|
|
|
-import type { UserPreferenceRoutes } from '@/server/api'
|
|
|
+import { hc } from 'hono/client';
|
|
|
+import type {
|
|
|
+ AuthRoutes,
|
|
|
+ UserRoutes,
|
|
|
+ RoleRoutes,
|
|
|
+ FileRoutes,
|
|
|
+ SilverJobsRoutes,
|
|
|
+ SilverUsersRoutes,
|
|
|
+ ElderlyUniversityRoutes,
|
|
|
+ SilverTalentsRoutes,
|
|
|
+ PolicyNewsRoutes,
|
|
|
+ UserPreferenceRoutes,
|
|
|
+ HomeRoutes
|
|
|
+} from '@/server/api';
|
|
|
+import axios from 'axios';
|
|
|
|
|
|
-// 创建自定义fetch函数,使用axios
|
|
|
+// 创建axios fetch适配器
|
|
|
const axiosFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
- const url = typeof input === 'string' ? input : input.toString()
|
|
|
- const method = init?.method || 'GET'
|
|
|
- const headers = init?.headers ? Object.fromEntries(Object.entries(init.headers)) : {}
|
|
|
- const body = init?.body
|
|
|
+
|
|
|
+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
|
|
+ const method = init?.method || 'GET';
|
|
|
+ const headers = init?.headers ? Object.fromEntries(new Map(init.headers as any)) : {};
|
|
|
+ const data = init?.body;
|
|
|
|
|
|
- const response = await axios({
|
|
|
- url,
|
|
|
- method,
|
|
|
- headers,
|
|
|
- data: body,
|
|
|
- })
|
|
|
+ try {
|
|
|
+ const response = await axios({
|
|
|
+ url,
|
|
|
+ method,
|
|
|
+ headers,
|
|
|
+ data,
|
|
|
+ responseType: 'json',
|
|
|
+ validateStatus: () => true,
|
|
|
+ });
|
|
|
|
|
|
- return new Response(response.data, {
|
|
|
- status: response.status,
|
|
|
- statusText: response.statusText,
|
|
|
- headers: response.headers as any,
|
|
|
- })
|
|
|
-}
|
|
|
+ return new Response(JSON.stringify(response.data), {
|
|
|
+ status: response.status,
|
|
|
+ statusText: response.statusText,
|
|
|
+ headers: response.headers as any,
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
-// 客户端实例
|
|
|
+// 客户端实例 - 严格按照RPC规范命名
|
|
|
export const authClient = hc<AuthRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1.auth
|
|
|
+}).api.v1.auth;
|
|
|
|
|
|
export const userClient = hc<UserRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1.users
|
|
|
+}).api.v1.users;
|
|
|
|
|
|
-export const silverJobsClient = hc<SilverJobsRoutes>('/', {
|
|
|
+export const roleClient = hc<RoleRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1['silver-jobs']
|
|
|
+}).api.v1.roles;
|
|
|
|
|
|
-export const silverTalentsClient = hc<SilverTalentsRoutes>('/', {
|
|
|
+export const fileClient = hc<FileRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1['silver-talents']
|
|
|
+}).api.v1.files;
|
|
|
|
|
|
-export const fileClient = hc<FileRoutes>('/', {
|
|
|
+// 银龄岗API客户端(银龄岗资源集合)
|
|
|
+const silverJobsClient = hc<SilverJobsRoutes>('/', {
|
|
|
+ fetch: axiosFetch,
|
|
|
+}).api.v1['silver-jobs'];
|
|
|
+
|
|
|
+// 银龄岗具体资源客户端
|
|
|
+export const companyClient = silverJobsClient.companies;
|
|
|
+export const jobClient = silverJobsClient.jobs;
|
|
|
+export const applicationClient = silverJobsClient.applications;
|
|
|
+export const favoriteClient = silverJobsClient.favorites;
|
|
|
+export const viewClient = silverJobsClient.views;
|
|
|
+export const companyImageClient = silverJobsClient['company-images'];
|
|
|
+
|
|
|
+// 银龄用户资源客户端
|
|
|
+export const silverUsersClient = hc<SilverUsersRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1.files
|
|
|
+}).api.v1['silver-users'];
|
|
|
|
|
|
+// 其他资源客户端
|
|
|
export const elderlyUniversityClient = hc<ElderlyUniversityRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1['elderly-universities']
|
|
|
+}).api.v1['elderly-universities'];
|
|
|
|
|
|
export const policyNewsClient = hc<PolicyNewsRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1['policy-news']
|
|
|
+}).api.v1['policy-news'];
|
|
|
|
|
|
+export const userPreferenceClient = hc<UserPreferenceRoutes>('/', {
|
|
|
+ fetch: axiosFetch,
|
|
|
+}).api.v1['user-preferences'];
|
|
|
+
|
|
|
+// 首页API客户端
|
|
|
export const homeClient = hc<HomeRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1.home
|
|
|
+}).api.v1.home;
|
|
|
|
|
|
-export const userPreferenceClient = hc<UserPreferenceRoutes>('/', {
|
|
|
+export const silverTalentsClient = hc<SilverTalentsRoutes>('/', {
|
|
|
fetch: axiosFetch,
|
|
|
-}).api.v1['user-preferences']
|
|
|
+}).api.v1['silver-talents']
|