-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de2c2f0
commit 644059a
Showing
18 changed files
with
197 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { AxiosInstance, InternalAxiosRequestConfig } from "axios"; | ||
import { AxiosInstance, InternalAxiosRequestConfig } from 'axios'; | ||
|
||
const requestFulfilledInterceptor = (config: InternalAxiosRequestConfig<FormData>) => { | ||
if (!config.data) { | ||
config.data = new FormData(); | ||
} | ||
config.data?.append('dir', window.AutoUpgradeVariables.admin_dir); | ||
return config; | ||
}; | ||
if (!config.data) { | ||
config.data = new FormData(); | ||
} | ||
config.data?.append('dir', window.AutoUpgradeVariables.admin_dir); | ||
return config; | ||
}; | ||
|
||
export const addRequestInterceptor = (axios: AxiosInstance): void => { | ||
axios.interceptors.request.use(requestFulfilledInterceptor); | ||
} | ||
axios.interceptors.request.use(requestFulfilledInterceptor); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,59 @@ | ||
import { AxiosError, AxiosInstance, AxiosResponse } from "axios"; | ||
import { APP_ERR_RESPONSE_BAD_TYPE, APP_ERR_RESPONSE_INVALID } from "../types/apiTypes"; | ||
|
||
const responseFulfilledInterceptor = (response: AxiosResponse<any, FormData>) => { | ||
console.log('Checking response', response); | ||
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios'; | ||
import { | ||
ApiResponseUnknown, | ||
ApiResponseUnknownObject, | ||
APP_ERR_RESPONSE_BAD_TYPE, | ||
APP_ERR_RESPONSE_EMPTY, | ||
APP_ERR_RESPONSE_INVALID, | ||
SilencedApiError | ||
} from '../types/apiTypes'; | ||
|
||
const responseFulfilledInterceptor = (response: AxiosResponse<ApiResponseUnknown, FormData>) => { | ||
if (!response?.data) { | ||
throw new AxiosError( | ||
'The response is empty', | ||
APP_ERR_RESPONSE_EMPTY, | ||
response.config, | ||
response.request, | ||
response | ||
); | ||
} | ||
// All responses must be a parsed JSON. If we get another type of response, | ||
// this means something went wrong, i.e Another software answered. | ||
if (Object.prototype.toString.call(response.data) !== '[object Object]') { | ||
throw new AxiosError('The response does not have a valid type', APP_ERR_RESPONSE_BAD_TYPE, response.config, response.request, response); | ||
throw new AxiosError( | ||
'The response does not have a valid type', | ||
APP_ERR_RESPONSE_BAD_TYPE, | ||
response.config, | ||
response.request, | ||
response | ||
); | ||
} | ||
|
||
// Make sure the response contains the expected data | ||
if (!response.data.kind) { | ||
throw new AxiosError('The response contents is invalid', APP_ERR_RESPONSE_INVALID, response.config, response.request, response); | ||
if (!(response.data as ApiResponseUnknownObject)?.kind) { | ||
throw new AxiosError( | ||
'The response contents is invalid', | ||
APP_ERR_RESPONSE_INVALID, | ||
response.config, | ||
response.request, | ||
response | ||
); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
const responseErroredInterceptor = (error: any) => { | ||
const responseErroredInterceptor = (error: Error) => { | ||
const errorSilenced = [AxiosError.ERR_CANCELED]; | ||
// Ignore some errors | ||
if (error instanceof AxiosError) { | ||
if (error.code && errorSilenced.includes(error.code)) { | ||
return Promise.reject(null); | ||
} | ||
if (error instanceof AxiosError && error.code && errorSilenced.includes(error.code)) { | ||
return Promise.reject(new SilencedApiError()); | ||
} | ||
|
||
return Promise.reject(error); | ||
}; | ||
}; | ||
|
||
export const addResponseInterceptor = (axios: AxiosInstance): void => { | ||
axios.interceptors.response.use(responseFulfilledInterceptor, responseErroredInterceptor); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.