Skip to content
File

Blob: src/client/auth/use-me.ts

typescript33 lines
1import { queryOptions } from "@tanstack/react-query";
2 
3import { $api, fetchClient } from "@/client/lib/api";
4import type { operations } from "@/client/lib/openapi-types";
5 
6export type MeProfile = operations["getApiV1Me"]["responses"][200]["content"]["application/json"];
7 
8export const meQueryKey = ["get", "/api/v1/me"] as const;
9 
10export 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 
22export function useMe() {
23 return $api.useQuery(
24 "get",
25 "/api/v1/me",
26 {},
27 {
28 staleTime: 30_000,
29 retry: false,
30 },
31 );
32}