mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
optimizations
This commit is contained in:
@@ -4,55 +4,57 @@ import ReactHook from 'alova/react';
|
||||
|
||||
import { unpack } from './unpack';
|
||||
|
||||
export const ACCESS_TOKEN = 'access_token';
|
||||
export const ACCESS_TOKEN = 'access_token' as const;
|
||||
|
||||
// Cached token to avoid repeated localStorage access
|
||||
let cachedToken: string | null = null;
|
||||
|
||||
const getAccessToken = (): string | null => {
|
||||
if (cachedToken === null) {
|
||||
cachedToken = localStorage.getItem(ACCESS_TOKEN);
|
||||
}
|
||||
return cachedToken;
|
||||
};
|
||||
|
||||
// Clear token cache when needed (e.g., on logout)
|
||||
export const clearTokenCache = (): void => {
|
||||
cachedToken = null;
|
||||
};
|
||||
|
||||
const handleResponse = async (response: AlovaXHRResponse) => {
|
||||
// Handle various HTTP status codes
|
||||
if (response.status === 205) {
|
||||
throw new Error('Reboot required');
|
||||
}
|
||||
if (response.status === 400) {
|
||||
throw new Error('Request Failed');
|
||||
}
|
||||
if (response.status >= 400) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
|
||||
const data = (await response.data) as ArrayBuffer;
|
||||
|
||||
// Unpack MessagePack data if ArrayBuffer
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return unpack(data) as ArrayBuffer;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const alovaInstance = createAlova({
|
||||
statesHook: ReactHook,
|
||||
// timeout: 3000, // 3 seconds before throwing a timeout error, default is 0 = none
|
||||
cacheFor: null, // disable cache
|
||||
// cacheFor: {
|
||||
// GET: {
|
||||
// mode: 'memory',
|
||||
// expire: 60 * 10 * 1000 // 60 seconds in cache
|
||||
// }
|
||||
// },
|
||||
requestAdapter: xhrRequestAdapter(),
|
||||
beforeRequest(method) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
method.config.headers.Authorization =
|
||||
'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
|
||||
const token = getAccessToken();
|
||||
if (token) {
|
||||
method.config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
// for simulating very slow networks
|
||||
// return new Promise((resolve) => {
|
||||
// const random = 3000 + Math.random() * 2000;
|
||||
// setTimeout(resolve, Math.floor(random));
|
||||
// });
|
||||
},
|
||||
|
||||
responded: {
|
||||
onSuccess: async (response: AlovaXHRResponse) => {
|
||||
// if (response.status === 202) {
|
||||
// throw new Error('Wait'); // wifi scan in progress
|
||||
// } else
|
||||
if (response.status === 205) {
|
||||
throw new Error('Reboot required');
|
||||
} else if (response.status === 400) {
|
||||
throw new Error('Request Failed');
|
||||
} else if (response.status >= 400) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const data: ArrayBuffer = (await response.data) as ArrayBuffer;
|
||||
if (response.data instanceof ArrayBuffer) {
|
||||
return unpack(data) as ArrayBuffer;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// Interceptor for request failure. This interceptor will be entered when the request is wrong.
|
||||
// http errors like 401 (unauthorized) are handled either in the methods or AuthenticatedRouting()
|
||||
// onError: (error, method) => {
|
||||
// alert(error.message);
|
||||
// }
|
||||
onSuccess: handleResponse
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user