Skip to content

Commit 25417b7

Browse files
committed
fix: code smells
1 parent 72f4877 commit 25417b7

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

resources/js/service/api.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2-
31
import { useAuthStore } from '@/stores/AuthStore';
4-
import { AxiosError } from 'axios';
52
import { getCSRF } from '@/service/authAPI';
63
import { toast } from '@aminnausin/cedar-ui';
74

5+
import axios, { AxiosError, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios';
86
import nProgress from 'nprogress';
9-
import axios from 'axios';
107

118
// For progress bar
129
let progressTimeout: NodeJS.Timeout;
@@ -17,11 +14,9 @@ let isRefreshing = false;
1714
let queue: Array<(tokenReady: boolean) => void> = []; // A queue of promises that are conditionally called after attempt at refreshing csrf
1815

1916
function refreshCsrf() {
20-
if (!csrfRefreshPromise) {
21-
csrfRefreshPromise = getCSRF().finally(() => {
22-
csrfRefreshPromise = null;
23-
});
24-
}
17+
csrfRefreshPromise ??= getCSRF().finally(() => {
18+
csrfRefreshPromise = null;
19+
});
2520

2621
return csrfRefreshPromise;
2722
}
@@ -47,7 +42,7 @@ const handleError = async (error: AxiosError<{ message?: string }>) => {
4742

4843
const auth = useAuthStore();
4944
const status = error.response?.status ?? 0;
50-
const config = error.config as InternalAxiosRequestConfig & { _retried?: boolean };
45+
const config = error.config;
5146
const message = error.response?.data?.message ?? error.message;
5247
const showToast = !config?.headers?.['X-Skip-Toast'];
5348

resources/js/service/authAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const register = async (credentials: any) => {
2929

3030
export const logout = async () => {
3131
const response = await API.delete('/logout');
32-
return Promise.resolve({ response: response.data });
32+
return { response: response.data };
3333
};
3434

3535
export const authenticate = async (): Promise<AxiosResponse<{ user: UserResource | null; isAuthenticated: boolean }>> => {

resources/js/stores/AuthStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const useAuthStore = defineStore('Auth', () => {
6060
localStorage.removeItem('auth-token'); // Legacy: Clears existing auth-tokens. No auth-tokens are created ever again.
6161

6262
if (!showMessage) return;
63-
const message = `Login Expired${status ? ` (${status})` : ''}`;
63+
const message = 'Login Expired' + status ? ` ${status}` : '';
6464

6565
// Can remove?
6666
toast.warning(message, {

resources/js/types/axios.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// axios.d.ts
2+
import 'axios';
3+
4+
declare module 'axios' {
5+
export interface InternalAxiosRequestConfig {
6+
_retried?: boolean;
7+
}
8+
}

0 commit comments

Comments
 (0)