diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-12-31 19:51:21 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-12-31 19:51:21 +0900 |
| commit | c915c21d47a2b417979b20e9e9d9b6ff30a03c0d (patch) | |
| tree | 643ba4e499bae8a541d77f299837361a75890b10 /src/client/api/client.ts | |
| parent | 73ee02825f57d971f6f660fc5277d4aa268702ff (diff) | |
| download | kioku-c915c21d47a2b417979b20e9e9d9b6ff30a03c0d.tar.gz kioku-c915c21d47a2b417979b20e9e9d9b6ff30a03c0d.tar.zst kioku-c915c21d47a2b417979b20e9e9d9b6ff30a03c0d.zip | |
refactor(client): use Hono InferResponseType for API response types
Replace manually defined AuthResponse and User types with Hono's
InferResponseType to automatically derive types from server definitions.
This ensures client types stay in sync with server responses.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/client/api/client.ts')
| -rw-r--r-- | src/client/api/client.ts | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/client/api/client.ts b/src/client/api/client.ts index 7741942..7607eb6 100644 --- a/src/client/api/client.ts +++ b/src/client/api/client.ts @@ -1,6 +1,13 @@ -import { hc } from "hono/client"; +import { hc, type InferResponseType } from "hono/client"; import type { AppType } from "../../server/index.js"; -import type { ApiError, AuthResponse, Tokens } from "./types"; +import type { ApiError, Tokens } from "./types"; + +// Create a temporary client just for type inference +const _rpc = hc<AppType>(""); + +// Infer response types from server definitions +export type LoginResponse = InferResponseType<typeof _rpc.api.auth.login.$post>; +export type User = LoginResponse["user"]; export class ApiClientError extends Error { constructor( @@ -120,12 +127,12 @@ export class ApiClient { } } - async login(username: string, password: string): Promise<AuthResponse> { + async login(username: string, password: string): Promise<LoginResponse> { const res = await this.rpc.api.auth.login.$post({ json: { username, password }, }); - const data = await this.handleResponse<AuthResponse>(res); + const data = await this.handleResponse<LoginResponse>(res); this.tokenStorage.setTokens({ accessToken: data.accessToken, |
