类型安全:
一致性:
api版本:
版本号必须与OpenAPI版本号一致 目前仅支持v1版本 示例:
import { authClient } from '@/client/api';
import { hc } from 'hono/client'
import { AuthRoutes } from '@/server/api';
export const authClient = hc<AuthRoutes>('/', {
fetch: axiosFetch,
}).api.v1.auth;
示例:
const res = await authClient.templates.blank[':templateType'].$get({
param: {
templateType
}
});
if (res.status !== 200) {
throw new Error(res.message);
}
const templateInfo = await res.json();
响应类型提取:
InferResponseType 从客户端方法提取响应类型示例:
type ResponseType = InferResponseType<typeof client.method.$get, 200>['data'];
请求类型提取:
InferRequestType 从客户端方法提取请求类型示例:
type RequestType = InferRequestType<typeof client.method.$post>['json'];
类型命名规范:
[ResourceName][ResourceName]Post 或 [ResourceName]Put示例:
import type { InferRequestType, InferResponseType } from 'hono/client'
type ZichanInfo = InferResponseType<typeof zichanClient.$get, 200>['data'][0];
type ZichanListResponse = InferResponseType<typeof zichanClient.$get, 200>;
type ZichanDetailResponse = InferResponseType<typeof zichanClient[':id']['$get'], 200>;
type CreateZichanRequest = InferRequestType<typeof zichanClient.$post>['json'];
type UpdateZichanRequest = InferRequestType<typeof zichanClient[':id']['$put']>['json'];
type DeleteZichanResponse = InferResponseType<typeof zichanClient[':id']['$delete'], 200>;