corretto altro ts...
ora manca ancora il vuelidate
This commit is contained in:
@@ -3,60 +3,64 @@ import {
|
||||
AxiosError,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse
|
||||
} from "axios";
|
||||
import { default as VueRouter } from "vue-router";
|
||||
import { TokenHelper } from "./token-helper";
|
||||
} from 'axios'
|
||||
import { default as VueRouter } from 'vue-router'
|
||||
// import { TokenHelper } from "./token-helper";
|
||||
|
||||
let initialized: boolean = false;
|
||||
let initialized: boolean = false
|
||||
|
||||
interface IRequestConfig extends AxiosRequestConfig {
|
||||
ignore: number[];
|
||||
ignore: number[]
|
||||
}
|
||||
|
||||
function handle(status: number, exclude: number[]) {
|
||||
if (exclude.length === 0) return true;
|
||||
else return exclude.find(o => o === status) === undefined;
|
||||
if (exclude.length === 0) return true
|
||||
else return exclude.find(o => o === status) === undefined
|
||||
}
|
||||
|
||||
export function UseAxios(router: VueRouter) {
|
||||
if (!initialized) {
|
||||
// @ts-ignore
|
||||
Axios.interceptors.request.use((config: IRequestConfig) => {
|
||||
if (!config.headers["Authorization"]) {
|
||||
if (!config.headers['Authorization']) {
|
||||
// append authorization header
|
||||
let bearerToken = TokenHelper.getBearerToken();
|
||||
/* ++Todo: disattivato per ora...
|
||||
let bearerToken = TokenHelper.getBearerToken()
|
||||
|
||||
if (bearerToken.Authorization)
|
||||
Object.assign(config.headers, bearerToken);
|
||||
Object.assign(config.headers, bearerToken)
|
||||
*/
|
||||
}
|
||||
|
||||
if (!config.maxRedirects || config.maxRedirects === 5)
|
||||
// ensure axios does not follow redirects, so custom response interceptor below can push to app login page
|
||||
config.maxRedirects = 0;
|
||||
config.maxRedirects = 0
|
||||
|
||||
return config;
|
||||
});
|
||||
return config
|
||||
})
|
||||
|
||||
Axios.interceptors.response.use(undefined, (config: AxiosError) => {
|
||||
let response: AxiosResponse = config.response;
|
||||
let exclude = (<IRequestConfig>config.config).ignore || [];
|
||||
// @ts-ignore
|
||||
let response: AxiosResponse = config.response
|
||||
let exclude = (<IRequestConfig>config.config).ignore || []
|
||||
|
||||
if (response.status === 401 && handle(response.status, exclude)) {
|
||||
let location: string =
|
||||
response.headers["location"] || response.headers["Location"];
|
||||
response.headers['location'] || response.headers['Location']
|
||||
|
||||
if (location) {
|
||||
let redirectTo = "/" + location;
|
||||
window.setTimeout(() => router.replace(redirectTo), 200);
|
||||
let redirectTo = '/' + location
|
||||
window.setTimeout(() => router.replace(redirectTo), 200)
|
||||
}
|
||||
}
|
||||
|
||||
if (response.status === 403 && handle(response.status, exclude)) {
|
||||
window.setTimeout(() => router.replace("/forbidden"), 200);
|
||||
window.setTimeout(() => router.replace('/forbidden'), 200)
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
return config
|
||||
})
|
||||
|
||||
initialized = true;
|
||||
initialized = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
* Returns a function, that, as long as it continues to be invoked, will not
|
||||
* be triggered. The function will be called after it stops being called for
|
||||
* N milliseconds. If `immediate` is passed, trigger the function on the
|
||||
* leading edge, instead of the trailing. The function also has a property 'clear'
|
||||
* that is a function which will clear the timer to prevent previously scheduled executions.
|
||||
* leading edge, instead of the trailing. The function also has a property 'clear'
|
||||
* that is a function which will clear the timer to prevent previously scheduled executions.
|
||||
*
|
||||
* @source underscore.js
|
||||
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
|
||||
@@ -14,13 +14,17 @@
|
||||
*/
|
||||
|
||||
export function Debounce(func: Function, wait?: number, immediate?: boolean) {
|
||||
let timeout, args, context, timestamp, result
|
||||
if (null == wait) wait = 100
|
||||
// @ts-ignore
|
||||
let timeout: any, args: any, context: any, timestamp: any, result: any
|
||||
if (null == wait)
|
||||
wait = 100
|
||||
|
||||
function later() {
|
||||
let last = Date.now() - timestamp
|
||||
|
||||
// @ts-ignore
|
||||
if (last < wait && last > 0) {
|
||||
// @ts-ignore
|
||||
timeout = setTimeout(later, wait - last)
|
||||
} else {
|
||||
timeout = null
|
||||
@@ -33,6 +37,7 @@ export function Debounce(func: Function, wait?: number, immediate?: boolean) {
|
||||
|
||||
let debounced = function () {
|
||||
|
||||
// @ts-ignore
|
||||
context = this
|
||||
args = arguments
|
||||
timestamp = Date.now()
|
||||
@@ -47,4 +52,4 @@ export function Debounce(func: Function, wait?: number, immediate?: boolean) {
|
||||
}
|
||||
|
||||
return debounced
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from './pattern'
|
||||
export * from './axios'
|
||||
export * from './debounce'
|
||||
export * from './message'
|
||||
export { default as GlobalConfig } from '../config'
|
||||
|
||||
7
src/common/message.ts
Normal file
7
src/common/message.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const PayloadMessageTypes = {
|
||||
error: 'Error',
|
||||
info: 'Info',
|
||||
failure: 'Failure',
|
||||
success: 'Success',
|
||||
warning: 'Warning'
|
||||
}
|
||||
Reference in New Issue
Block a user