Skip to content

Commit 58d4d47

Browse files
feat: react-query hey-api integration (#101)
* feat: react-query hey-api integration * fix: remove `serverApi` function
1 parent 6bfa866 commit 58d4d47

File tree

6 files changed

+131
-28
lines changed

6 files changed

+131
-28
lines changed

openapi-ts.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
},
1111
plugins: [
1212
"@hey-api/sdk",
13+
"@tanstack/react-query",
1314
{
1415
enums: "typescript",
1516
name: "@hey-api/typescript",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import type { OptionsLegacyParser } from "@hey-api/client-fetch";
4+
import { queryOptions } from "@tanstack/react-query";
5+
import {
6+
client,
7+
getMessagesDashboardMessagesGet,
8+
getAlertsDashboardAlertsGet,
9+
streamSseDashboardAlertsNotificationGet,
10+
} from "../sdk.gen";
11+
12+
type QueryKey<TOptions extends OptionsLegacyParser> = [
13+
Pick<TOptions, "baseUrl" | "body" | "headers" | "path" | "query"> & {
14+
_id: string;
15+
_infinite?: boolean;
16+
},
17+
];
18+
19+
const createQueryKey = <TOptions extends OptionsLegacyParser>(
20+
id: string,
21+
options?: TOptions,
22+
infinite?: boolean,
23+
): QueryKey<TOptions>[0] => {
24+
const params: QueryKey<TOptions>[0] = {
25+
_id: id,
26+
baseUrl: (options?.client ?? client).getConfig().baseUrl,
27+
} as QueryKey<TOptions>[0];
28+
if (infinite) {
29+
params._infinite = infinite;
30+
}
31+
if (options?.body) {
32+
params.body = options.body;
33+
}
34+
if (options?.headers) {
35+
params.headers = options.headers;
36+
}
37+
if (options?.path) {
38+
params.path = options.path;
39+
}
40+
if (options?.query) {
41+
params.query = options.query;
42+
}
43+
return params;
44+
};
45+
46+
export const getMessagesDashboardMessagesGetQueryKey = (
47+
options?: OptionsLegacyParser,
48+
) => [createQueryKey("getMessagesDashboardMessagesGet", options)];
49+
50+
export const getMessagesDashboardMessagesGetOptions = (
51+
options?: OptionsLegacyParser,
52+
) => {
53+
return queryOptions({
54+
queryFn: async ({ queryKey, signal }) => {
55+
const { data } = await getMessagesDashboardMessagesGet({
56+
...options,
57+
...queryKey[0],
58+
signal,
59+
throwOnError: true,
60+
});
61+
return data;
62+
},
63+
queryKey: getMessagesDashboardMessagesGetQueryKey(options),
64+
});
65+
};
66+
67+
export const getAlertsDashboardAlertsGetQueryKey = (
68+
options?: OptionsLegacyParser,
69+
) => [createQueryKey("getAlertsDashboardAlertsGet", options)];
70+
71+
export const getAlertsDashboardAlertsGetOptions = (
72+
options?: OptionsLegacyParser,
73+
) => {
74+
return queryOptions({
75+
queryFn: async ({ queryKey, signal }) => {
76+
const { data } = await getAlertsDashboardAlertsGet({
77+
...options,
78+
...queryKey[0],
79+
signal,
80+
throwOnError: true,
81+
});
82+
return data;
83+
},
84+
queryKey: getAlertsDashboardAlertsGetQueryKey(options),
85+
});
86+
};
87+
88+
export const streamSseDashboardAlertsNotificationGetQueryKey = (
89+
options?: OptionsLegacyParser,
90+
) => [createQueryKey("streamSseDashboardAlertsNotificationGet", options)];
91+
92+
export const streamSseDashboardAlertsNotificationGetOptions = (
93+
options?: OptionsLegacyParser,
94+
) => {
95+
return queryOptions({
96+
queryFn: async ({ queryKey, signal }) => {
97+
const { data } = await streamSseDashboardAlertsNotificationGet({
98+
...options,
99+
...queryKey[0],
100+
signal,
101+
throwOnError: true,
102+
});
103+
return data;
104+
},
105+
queryKey: streamSseDashboardAlertsNotificationGetQueryKey(options),
106+
});
107+
};

src/api/service.ts

-13
This file was deleted.

src/hooks/useAlertsData.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { useQuery } from "@tanstack/react-query";
2-
import { serverApi } from "@/api/service";
3-
import { AlertConversation } from "@/api/generated";
2+
import {
3+
AlertConversation,
4+
getAlertsDashboardAlertsGet,
5+
} from "@/api/generated";
46
import { getMaliciousPackage } from "@/lib/utils";
57
import { MaliciousPkgType, TriggerType } from "@/types";
68
import { useAlertSearch } from "./useAlertSearch";
9+
import { getAlertsDashboardAlertsGetQueryKey } from "@/api/generated/@tanstack/react-query.gen";
710

811
const fetchAlerts = async (): Promise<AlertConversation[]> => {
9-
const { getAlertsDashboardAlertsGet } = await serverApi();
1012
const { data } = await getAlertsDashboardAlertsGet();
1113

1214
return (data ?? [])
@@ -25,7 +27,7 @@ const fetchAlerts = async (): Promise<AlertConversation[]> => {
2527

2628
export const useAlertsData = ({ ...args } = {}) => {
2729
return useQuery({
28-
queryKey: ["alerts"],
30+
queryKey: getAlertsDashboardAlertsGetQueryKey(),
2931
queryFn: fetchAlerts,
3032
...args,
3133
});

src/hooks/usePromptsData.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { useQuery } from "@tanstack/react-query";
2-
import { serverApi } from "@/api/service";
3-
import { Conversation } from "@/api/generated";
4-
5-
const fetchPrompts = async (): Promise<Conversation[]> => {
6-
const { getMessagesDashboardMessagesGet } = await serverApi();
7-
const { data } = await getMessagesDashboardMessagesGet();
8-
9-
if (!data) return [];
2+
import {
3+
Conversation,
4+
GetMessagesDashboardMessagesGetResponse,
5+
} from "@/api/generated";
6+
import { getMessagesDashboardMessagesGetOptions } from "@/api/generated/@tanstack/react-query.gen";
107

8+
const selectConversations = (
9+
data: GetMessagesDashboardMessagesGetResponse,
10+
): Conversation[] => {
1111
return data.filter((prompt) =>
1212
prompt.question_answers?.every((item) => item.answer && item.question),
1313
);
1414
};
1515

1616
export const usePromptsData = () => {
1717
return useQuery({
18-
queryKey: ["prompts"],
19-
queryFn: fetchPrompts,
18+
...getMessagesDashboardMessagesGetOptions(),
19+
select: selectConversations,
2020
});
2121
};

src/main.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
99
import ErrorBoundary from "./components/ErrorBoundary.tsx";
1010
import { Error } from "./components/Error.tsx";
1111
import { DarkModeProvider } from "@stacklok/ui-kit";
12+
import { client } from "./api/generated/index.ts";
13+
14+
// Initialize the API client
15+
client.setConfig({
16+
baseUrl: import.meta.env.VITE_BASE_API_URL,
17+
});
1218

1319
createRoot(document.getElementById("root")!).render(
1420
<StrictMode>
@@ -23,5 +29,5 @@ createRoot(document.getElementById("root")!).render(
2329
</SidebarProvider>
2430
</DarkModeProvider>
2531
</BrowserRouter>
26-
</StrictMode>
32+
</StrictMode>,
2733
);

0 commit comments

Comments
 (0)