refactor: centralize frontend auth query gating

This commit is contained in:
2026-08-25 14:29:41 +03:00
parent fff36f252a
commit 268acdf2e1
24 changed files with 352 additions and 82 deletions
@@ -2,14 +2,22 @@ import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import {
brokerageOperationsQueryParams,
useBrokerageOperations,
} from "./useBrokerageOperations";
describe("useBrokerageOperations", () => {
afterEach(() => configureAuthClient(null));
it("normalizes query parameters and loads a page without retries", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
expect(
brokerageOperationsQueryParams({
categories: ["tax", "income"],
@@ -24,7 +32,7 @@ describe("useBrokerageOperations", () => {
);
const { result } = renderHook(
() => useBrokerageOperations("account-1", "token", { limit: 5 }),
() => useBrokerageOperations("account-1", { limit: 5 }),
{ wrapper },
);
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
type OperationsEndpoint =
@@ -37,17 +36,16 @@ export function brokerageOperationsQueryParams(
export function useBrokerageOperations(
accountId: string | undefined,
accessToken: string | null,
query: BrokerageOperationsQuery = {},
) {
const params = brokerageOperationsQueryParams(query);
return useQuery({
return useAuthenticatedQuery({
...apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts/{accountId}/operations",
{ params: { path: { accountId: accountId ?? "" }, query: params } },
),
enabled: Boolean(accountId && accessToken),
enabled: Boolean(accountId),
staleTime: 30_000,
retry: false,
});
@@ -0,0 +1,56 @@
import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { HttpResponse, http } from "msw";
import type { ReactNode } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { server } from "@/test/server";
import { useBrokerageAccounts } from "./useBrokerageAccounts";
function createWrapper() {
const client = new QueryClient();
return ({ children }: { children: ReactNode }) => (
<MantineProvider>
<QueryClientProvider client={client}>{children}</QueryClientProvider>
</MantineProvider>
);
}
describe("useBrokerageAccounts", () => {
afterEach(() => configureAuthClient(null));
it("loads accounts through the authenticated query seam", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
server.use(
http.get("*/api/v1/brokerage-accounts", () =>
HttpResponse.json([
{
id: "account-1",
name: "Основной",
type: "BROKERAGE",
openedOn: "2024-08-13",
},
]),
),
);
const { result } = renderHook(() => useBrokerageAccounts(), {
wrapper: createWrapper(),
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual([
{
id: "account-1",
name: "Основной",
type: "BROKERAGE",
openedOn: "2024-08-13",
},
]);
});
});
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
const brokerageAccountsQueryOptions = () =>
@@ -11,10 +10,9 @@ export const brokerageAccountsQueryKey =
export type BrokerageAccount =
paths["/api/v1/brokerage-accounts"]["get"]["responses"][200]["content"]["application/json"][number];
export function useBrokerageAccounts(accessToken: string | null) {
return useQuery({
export function useBrokerageAccounts() {
return useAuthenticatedQuery({
...brokerageAccountsQueryOptions(),
enabled: Boolean(accessToken),
staleTime: 60_000,
retry: false,
});
@@ -0,0 +1,68 @@
import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { HttpResponse, http } from "msw";
import type { ReactNode } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { server } from "@/test/server";
import { useBrokerageEvents } from "./useBrokerageEvents";
function createWrapper() {
const client = new QueryClient();
return ({ children }: { children: ReactNode }) => (
<MantineProvider>
<QueryClientProvider client={client}>{children}</QueryClientProvider>
</MantineProvider>
);
}
describe("useBrokerageEvents", () => {
afterEach(() => configureAuthClient(null));
it("loads future cash events through the authenticated query seam", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
server.use(
http.get("*/api/v1/brokerage-accounts/account-1/events", () =>
HttpResponse.json({
accountId: "account-1",
calculatedAt: "2026-08-14T00:00:00Z",
events: [
{
type: "dividend",
date: "2026-09-01",
dateKind: "declared",
instrumentId: "figi-1",
ticker: "SBER",
displayName: "Сбер",
amount: "12.34",
currency: "RUB",
},
],
}),
),
);
const { result } = renderHook(() => useBrokerageEvents("account-1"), {
wrapper: createWrapper(),
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data?.events).toEqual([
{
type: "dividend",
date: "2026-09-01",
dateKind: "declared",
instrumentId: "figi-1",
ticker: "SBER",
displayName: "Сбер",
amount: "12.34",
currency: "RUB",
},
]);
});
});
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
type EventsEndpoint =
@@ -7,17 +6,14 @@ type EventsEndpoint =
export type BrokerageEvent =
EventsEndpoint["responses"][200]["content"]["application/json"]["events"][number];
export function useBrokerageEvents(
accountId: string | undefined,
accessToken: string | null,
) {
return useQuery({
export function useBrokerageEvents(accountId: string | undefined) {
return useAuthenticatedQuery({
...apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts/{accountId}/events",
{ params: { path: { accountId: accountId ?? "" } } },
),
enabled: Boolean(accountId && accessToken),
enabled: Boolean(accountId),
staleTime: 30_000,
retry: false,
});
@@ -2,12 +2,20 @@ import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { usePortfolio } from "./usePortfolio";
describe("usePortfolio", () => {
afterEach(() => configureAuthClient(null));
it("uses the account endpoint with a 30 second cache and no retry", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
const client = new QueryClient();
const wrapper = ({ children }: { children: ReactNode }) => (
<MantineProvider>
@@ -15,7 +23,7 @@ describe("usePortfolio", () => {
</MantineProvider>
);
const { result } = renderHook(() => usePortfolio("account-1", "token"), {
const { result } = renderHook(() => usePortfolio("account-1"), {
wrapper,
});
+4 -8
View File
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
const portfolioQueryPolicy = {
@@ -10,17 +9,14 @@ const portfolioQueryPolicy = {
export type Portfolio =
paths["/api/v1/brokerage-accounts/{accountId}/portfolio"]["get"]["responses"][200]["content"]["application/json"];
export function usePortfolio(
accountId: string | undefined,
accessToken: string | null,
) {
return useQuery({
export function usePortfolio(accountId: string | undefined) {
return useAuthenticatedQuery({
...apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts/{accountId}/portfolio",
{ params: { path: { accountId: accountId ?? "" } } },
),
enabled: Boolean(accountId && accessToken),
enabled: Boolean(accountId),
...portfolioQueryPolicy,
});
}