Skip to content

Commit

Permalink
rm deprecated request
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaJ2305 committed Feb 1, 2025
1 parent 841d281 commit a23757d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
36 changes: 19 additions & 17 deletions src/Providers/AuthUserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { AuthUserContext } from "@/hooks/useAuthUser";
import { LocalStorageKeys } from "@/common/constants";

import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import request from "@/Utils/request/request";
import query, { callApi } from "@/Utils/request/query";
import { TokenData } from "@/types/auth/otpToken";

interface Props {
Expand Down Expand Up @@ -56,12 +55,12 @@ export default function AuthUserProvider({

const signIn = useCallback(
async (creds: { username: string; password: string }) => {
const query = await request(routes.login, { body: creds });
const data = await callApi(routes.login, { body: creds });

if (query.res?.ok && query.data) {
setAccessToken(query.data.access);
localStorage.setItem(LocalStorageKeys.accessToken, query.data.access);
localStorage.setItem(LocalStorageKeys.refreshToken, query.data.refresh);
if (data?.access && data?.refresh) {
setAccessToken(data.access);
localStorage.setItem(LocalStorageKeys.accessToken, data.access);
localStorage.setItem(LocalStorageKeys.refreshToken, data.refresh);

await queryClient.invalidateQueries({ queryKey: ["currentUser"] });

Expand All @@ -70,7 +69,7 @@ export default function AuthUserProvider({
}
}

return query;
return data;
},
[queryClient],
);
Expand Down Expand Up @@ -155,19 +154,22 @@ const updateRefreshToken = async (silent = false) => {
return;
}

const { res, data } = await request(routes.token_refresh, {
body: { refresh },
silent,
});
try {
const data = await callApi(routes.token_refresh, {
body: { refresh },
silent,
});

if (!data) {
throw new Error("Token refresh failed");
}

if (res?.status !== 200 || !data) {
localStorage.setItem(LocalStorageKeys.accessToken, data.access);
localStorage.setItem(LocalStorageKeys.refreshToken, data.refresh);
} catch {
localStorage.removeItem(LocalStorageKeys.accessToken);
localStorage.removeItem(LocalStorageKeys.refreshToken);
return;
}

localStorage.setItem(LocalStorageKeys.accessToken, data.access);
localStorage.setItem(LocalStorageKeys.refreshToken, data.refresh);
};

const getRedirectURL = () => {
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/request/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ApiCallOptions, ApiRoute, HTTPError } from "@/Utils/request/types";
import { makeHeaders, makeUrl } from "@/Utils/request/utils";
import { sleep } from "@/Utils/utils";

export type ApiResponse<TData> = TData;

export async function callApi<Route extends ApiRoute<unknown, unknown>>(
{ path, method, noAuth }: Route,
options?: ApiCallOptions<Route>,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ const Login = (props: LoginProps) => {
FiltersCache.invaldiateAll();
return await signIn(data);
},
onSuccess: ({ res }) => {
setCaptcha(res?.status === 429);
onSuccess: (res) => {
setCaptcha(!!res?.access);
},
});

Expand Down
12 changes: 6 additions & 6 deletions src/components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { FACILITY_FEATURE_TYPES } from "@/common/constants";
import { PLUGIN_Component } from "@/PluginEngine";
import routes from "@/Utils/request/api";
import query, { callApi } from "@/Utils/request/query";
import request from "@/Utils/request/request";
import uploadFile from "@/Utils/request/uploadFile";
import { getAuthorizationHeader } from "@/Utils/request/utils";
import { sleep } from "@/Utils/utils";
Expand Down Expand Up @@ -150,16 +149,17 @@ export const FacilityHome = ({ facilityId }: Props) => {
};

const handleCoverImageDelete = async (onError: () => void) => {
const { res } = await request(routes.deleteFacilityCoverImage, {
pathParams: { id: facilityId },
});
if (res?.ok) {
try {
await callApi(routes.deleteFacilityCoverImage, {
pathParams: { id: facilityId },
});

toast.success(t("cover_image_deleted"));
queryClient.invalidateQueries({
queryKey: ["facility", facilityId],
});
setEditCoverImage(false);
} else {
} catch {
onError();
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/Users/UserSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
showUserPasswordReset,
} from "@/Utils/permissions";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import { callApi } from "@/Utils/request/query";
import EditUserSheet from "@/pages/Organization/components/EditUserSheet";
import { UserBase } from "@/types/user/user";

Expand All @@ -40,7 +40,7 @@ export default function UserSummaryTab({ userData }: { userData?: UserBase }) {

const { mutate: deleteUser, isPending: isDeleting } = useMutation({
mutationFn: async () => {
return await request(routes.deleteUser, {
return await callApi(routes.deleteUser, {
pathParams: { username: userData?.username || "" },
});
},
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useAuthUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { createContext, useContext } from "react";
import { UserModel } from "@/components/Users/models";

import { JwtTokenObtainPair, LoginCredentials } from "@/Utils/request/api";
import { RequestResult } from "@/Utils/request/types";
import { ApiResponse } from "@/Utils/request/query";
import { TokenData } from "@/types/auth/otpToken";

type SignInReturnType = RequestResult<JwtTokenObtainPair>;

type SignInReturnType = ApiResponse<JwtTokenObtainPair>;
interface AuthContextType {
user: UserModel | undefined;
signIn: (creds: LoginCredentials) => Promise<SignInReturnType>;
Expand Down

0 comments on commit a23757d

Please sign in to comment.