From 60a5f03dab825ef070ff77d7765cb3cd6f7c24b6 Mon Sep 17 00:00:00 2001 From: bcho892 Date: Tue, 12 Mar 2024 17:11:43 +1300 Subject: [PATCH] add files --- client/src/services/OpenApiFetchClient.ts | 23 +++++++++++++++++++++++ client/src/services/Queries.ts | 7 +++++++ client/src/services/QueryClient.ts | 11 +++++++++++ 3 files changed, 41 insertions(+) create mode 100644 client/src/services/OpenApiFetchClient.ts create mode 100644 client/src/services/Queries.ts create mode 100644 client/src/services/QueryClient.ts diff --git a/client/src/services/OpenApiFetchClient.ts b/client/src/services/OpenApiFetchClient.ts new file mode 100644 index 000000000..8d8947aa9 --- /dev/null +++ b/client/src/services/OpenApiFetchClient.ts @@ -0,0 +1,23 @@ +import createClient, { Middleware } from "openapi-fetch" +import type { paths } from "models/__generated__/schema" + +let accessToken: string | undefined + +const authMiddleware: Middleware = { + async onRequest(req) { + // TODO: Handle Auth + if (!accessToken) { + accessToken = "placeholder" + } + req.headers.set("Authorization", `Bearer ${accessToken}`) + return req + } +} + +const fetchClient = createClient({ + baseUrl: import.meta.env.VITE_BACKEND_BASE_URL +}) + +fetchClient.use(authMiddleware) + +export default fetchClient diff --git a/client/src/services/Queries.ts b/client/src/services/Queries.ts new file mode 100644 index 000000000..fc6de03a3 --- /dev/null +++ b/client/src/services/Queries.ts @@ -0,0 +1,7 @@ +import { useQuery } from "@tanstack/react-query" +import UserService from "./UserService" + +export const useUsers = useQuery({ + queryKey: ["allUsers"], + queryFn: () => UserService.getUsers() +}) diff --git a/client/src/services/QueryClient.ts b/client/src/services/QueryClient.ts new file mode 100644 index 000000000..10585b54a --- /dev/null +++ b/client/src/services/QueryClient.ts @@ -0,0 +1,11 @@ +import { QueryClient } from "@tanstack/react-query" + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { + notifyOnChangeProps: ["data"] + } + } +}) + +export default queryClient