File
Blob: src/client/auth/use-me.ts
| 1 | import { queryOptions } from "@tanstack/react-query"; |
| 2 | |
| 3 | import { $api, fetchClient } from "@/client/lib/api"; |
| 4 | import type { operations } from "@/client/lib/openapi-types"; |
| 5 | |
| 6 | export type MeProfile = operations["getApiV1Me"]["responses"][200]["content"]["application/json"]; |
| 7 | |
| 8 | export const meQueryKey = ["get", "/api/v1/me"] as const; |
| 9 | |
| 10 | export const meQueryOptions = queryOptions({ |
| 11 | queryKey: meQueryKey, |
| 12 | queryFn: async ({ signal }) => { |
| 13 | const { data, error, response } = await fetchClient.GET("/api/v1/me", { signal }); |
| 14 | if (response.status === 401) return null; |
| 15 | if (error) throw error; |
| 16 | return (data ?? null) as MeProfile | null; |
| 17 | }, |
| 18 | staleTime: 30_000, |
| 19 | retry: false, |
| 20 | }); |
| 21 | |
| 22 | export function useMe() { |
| 23 | return $api.useQuery( |
| 24 | "get", |
| 25 | "/api/v1/me", |
| 26 | {}, |
| 27 | { |
| 28 | staleTime: 30_000, |
| 29 | retry: false, |
| 30 | }, |
| 31 | ); |
| 32 | } |