mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
alova - add interceptor
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import jwtDecode from 'jwt-decode';
|
||||
import { ACCESS_TOKEN, AXIOS } from './endpoints';
|
||||
import type { AxiosPromise } from 'axios';
|
||||
import { ACCESS_TOKEN, alovaInstance } from './endpoints';
|
||||
import type * as H from 'history';
|
||||
import type { Path } from 'react-router-dom';
|
||||
|
||||
@@ -9,14 +8,8 @@ import type { Me, SignInRequest, SignInResponse } from 'types';
|
||||
export const SIGN_IN_PATHNAME = 'loginPathname';
|
||||
export const SIGN_IN_SEARCH = 'loginSearch';
|
||||
|
||||
// TODO move verifyAuthorization to Alova
|
||||
export function verifyAuthorization(): AxiosPromise<void> {
|
||||
return AXIOS.get('/verifyAuthorization');
|
||||
}
|
||||
// TODO move signIn to Alova
|
||||
export function signIn(request: SignInRequest): AxiosPromise<SignInResponse> {
|
||||
return AXIOS.post('/signIn', request);
|
||||
}
|
||||
export const verifyAuthorization = () => alovaInstance.Get('/rest/verifyAuthorization');
|
||||
export const signIn = (request: SignInRequest) => alovaInstance.Post<SignInResponse>('/rest/signIn', request);
|
||||
|
||||
export function getStorage() {
|
||||
return localStorage || sessionStorage;
|
||||
|
||||
@@ -4,10 +4,7 @@ import ReactHook from 'alova/react';
|
||||
import axios from 'axios';
|
||||
import { unpack } from '../api/unpack';
|
||||
|
||||
// TODO axios can be removed
|
||||
import type { AxiosPromise, CancelToken, AxiosProgressEvent } from 'axios';
|
||||
export const REST_BASE_URL = '/rest/';
|
||||
export const API_BASE_URL = '/api/';
|
||||
|
||||
export const ACCESS_TOKEN = 'access_token';
|
||||
|
||||
@@ -52,14 +49,26 @@ export const alovaInstance = createAlova({
|
||||
return data;
|
||||
},
|
||||
|
||||
onError: (error) => {
|
||||
// TODO handle errors
|
||||
// Interceptor for request failure
|
||||
// This interceptor will be entered when the request is wrong.
|
||||
// The second parameter is the method instance of the current request, you can use it to synchronize the configuration information before and after the request
|
||||
onError: (error, method) => {
|
||||
console.log('error:', error); // TODO fix me
|
||||
console.log('method:', method); // TODO fix me
|
||||
alert(error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const alovaInstanceGH = createAlova({
|
||||
baseURL: 'https://api.github.com/repos/emsesp/EMS-ESP32',
|
||||
statesHook: ReactHook,
|
||||
requestAdapter: xhrRequestAdapter()
|
||||
});
|
||||
|
||||
export const AXIOS = axios.create({
|
||||
baseURL: REST_BASE_URL,
|
||||
baseURL: '/rest/',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
@@ -78,49 +87,8 @@ export const AXIOS = axios.create({
|
||||
]
|
||||
});
|
||||
|
||||
export const AXIOS_API = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
transformRequest: [
|
||||
(data, headers) => {
|
||||
if (headers) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
headers.Authorization = 'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
|
||||
}
|
||||
if (headers['Content-Type'] !== 'application/json') {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
export const AXIOS_BIN = axios.create({
|
||||
baseURL: REST_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
responseType: 'arraybuffer',
|
||||
transformRequest: [
|
||||
(data, headers) => {
|
||||
if (headers) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
headers.Authorization = 'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
|
||||
}
|
||||
if (headers['Content-Type'] !== 'application/json') {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
],
|
||||
transformResponse: [(data) => unpack(data)]
|
||||
});
|
||||
|
||||
// TODO replace fileupload with alova, see https://alova.js.org/next-step/download-upload-progress
|
||||
// TODO fileupload move to alova
|
||||
// see https://alova.js.org/next-step/download-upload-progress
|
||||
export interface FileUploadConfig {
|
||||
cancelToken?: CancelToken;
|
||||
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
import { AXIOS } from './endpoints';
|
||||
import type { AxiosPromise } from 'axios';
|
||||
import { alovaInstance } from './endpoints';
|
||||
|
||||
import type { SecuritySettings, Token } from 'types';
|
||||
|
||||
// TODO move these 3 to Alova
|
||||
export function readSecuritySettings(): AxiosPromise<SecuritySettings> {
|
||||
return AXIOS.get('/securitySettings');
|
||||
}
|
||||
export const readSecuritySettings = () => alovaInstance.Get<SecuritySettings>('/rest/securitySettings');
|
||||
|
||||
export function updateSecuritySettings(securitySettings: SecuritySettings): AxiosPromise<SecuritySettings> {
|
||||
return AXIOS.post('/securitySettings', securitySettings);
|
||||
}
|
||||
export const updateSecuritySettings = (securitySettings: SecuritySettings) =>
|
||||
alovaInstance.Post('/rest/securitySettings', securitySettings);
|
||||
|
||||
export function generateToken(username?: string): AxiosPromise<Token> {
|
||||
return AXIOS.get('/generateToken', { params: { username } });
|
||||
}
|
||||
export const generateToken = (username?: string) =>
|
||||
alovaInstance.Get<Token>('/rest/generateToken', {
|
||||
params: { username }
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { alovaInstance, startUploadFile } from './endpoints';
|
||||
import { alovaInstance, alovaInstanceGH, startUploadFile } from './endpoints';
|
||||
import type { FileUploadConfig } from './endpoints';
|
||||
import type { AxiosPromise } from 'axios';
|
||||
|
||||
import type { OTASettings, SystemStatus, LogSettings } from 'types';
|
||||
import type { OTASettings, SystemStatus, LogSettings, Version } from 'types';
|
||||
|
||||
// SystemStatus - also used to ping in Restart monitor
|
||||
export const readSystemStatus = () => alovaInstance.Get<SystemStatus>('/rest/systemStatus');
|
||||
@@ -21,6 +21,28 @@ export const readLogSettings = () => alovaInstance.Get<LogSettings>(`/rest/logSe
|
||||
export const updateLogSettings = (data: any) => alovaInstance.Post('/rest/logSettings', data);
|
||||
export const fetchLog = () => alovaInstance.Post('/rest/fetchLog');
|
||||
|
||||
// TODO fileupload move to Alova
|
||||
// Get versions from github
|
||||
export const getStableVersion = () =>
|
||||
alovaInstanceGH.Get<Version>('releases/latest', {
|
||||
transformData(reponse: any) {
|
||||
return {
|
||||
version: reponse.data.name,
|
||||
url: reponse.data.assets[1].browser_download_url,
|
||||
changelog: reponse.data.assets[0].browser_download_url
|
||||
};
|
||||
}
|
||||
});
|
||||
export const getDevVersion = () =>
|
||||
alovaInstanceGH.Get<Version>('releases/tags/latest', {
|
||||
transformData(reponse: any) {
|
||||
return {
|
||||
version: reponse.data.name.split(/\s+/).splice(-1),
|
||||
url: reponse.data.assets[1].browser_download_url,
|
||||
changelog: reponse.data.assets[0].browser_download_url
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// TODO fileupload move to alova
|
||||
export const uploadFile = (file: File, config?: FileUploadConfig): AxiosPromise<void> =>
|
||||
startUploadFile('/uploadFile', file, config);
|
||||
|
||||
Reference in New Issue
Block a user