Consolidate format helpers under lib/format, split React Query hooks by domain, extract AuthPicker/ConfigRefHint and HTTP trigger routing, demote file-private exports, and move KV/secrets/variables/profiles/http-auths stores under src/stores with root re-exports. Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { api } from "../client.js";
|
|
|
|
export function useBootstrap() {
|
|
return useQuery({
|
|
queryKey: ["bootstrap"],
|
|
queryFn: async () => (await api.get("/auth/bootstrap")).data,
|
|
});
|
|
}
|
|
|
|
export function useMe() {
|
|
return useQuery({
|
|
queryKey: ["me"],
|
|
queryFn: async () => {
|
|
try {
|
|
return (await api.get("/auth/me")).data;
|
|
} catch (err) {
|
|
if (err.response?.status === 401) return { user: null };
|
|
throw err;
|
|
}
|
|
},
|
|
retry: false,
|
|
});
|
|
}
|
|
|
|
export function useLogin() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body) => (await api.post("/auth/login", body)).data,
|
|
onSuccess: (data) => {
|
|
qc.setQueryData(["me"], data);
|
|
qc.invalidateQueries({ queryKey: ["bootstrap"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRegister() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body) => (await api.post("/auth/register", body)).data,
|
|
onSuccess: (data) => {
|
|
qc.setQueryData(["me"], data);
|
|
qc.invalidateQueries({ queryKey: ["bootstrap"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useLogout() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async () => (await api.post("/auth/logout")).data,
|
|
onSuccess: () => {
|
|
qc.setQueryData(["me"], null);
|
|
qc.clear();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUsers() {
|
|
return useQuery({
|
|
queryKey: ["users"],
|
|
queryFn: async () => (await api.get("/users")).data.users,
|
|
});
|
|
}
|
|
|
|
export function useCreateUser() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body) => (await api.post("/users", body)).data,
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateUser() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ id, ...body }) =>
|
|
(await api.patch(`/users/${encodeURIComponent(id)}`, body)).data,
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteUser() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (id) =>
|
|
(await api.delete(`/users/${encodeURIComponent(id)}`)).data,
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["users"] }),
|
|
});
|
|
}
|
|
|