11import axios from 'axios' ;
2- import { getUserStore , loaderStore } from '$lib/helpers/store.js' ;
2+ import { getUserStore , globalErrorStore , loaderStore } from '$lib/helpers/store.js' ;
33
44// Add a request interceptor to attach authentication tokens or headers
55axios . interceptors . request . use (
66 ( config ) => {
77 // Add your authentication logic here
8- let user = getUserStore ( ) ;
8+ const user = getUserStore ( ) ;
99 if ( ! skipLoader ( config ) ) {
1010 loaderStore . set ( true ) ;
1111 }
@@ -25,35 +25,48 @@ axios.interceptors.response.use(
2525 ( response ) => {
2626 // If the request was successful, return the response
2727 loaderStore . set ( false ) ;
28+ const user = getUserStore ( ) ;
29+
30+ const isExpired = Date . now ( ) / 1000 > user . expires ;
31+ if ( isExpired ) {
32+ redirectToLogin ( ) ;
33+ return Promise . reject ( 'user token expired!' ) ;
34+ }
2835 return response ;
2936 } ,
3037 ( error ) => {
3138 loaderStore . set ( false ) ;
32- let user = getUserStore ( ) ;
33-
34- if ( Date . now ( ) / 1000 > user . expires ) {
35- error . response = { status : 401 } ;
36- }
37-
38- // If the error status is 401, handle it here
39- if ( error . response && error . response . status === 401 ) {
40- // Perform actions like redirecting to the login page or refreshing tokens
41- // Example: redirect to the login page
42- const curUrl = window . location . pathname + window . location . search ;
43- let loginUrl = 'login' ;
44- if ( curUrl ) {
45- loginUrl += `?redirect=${ encodeURIComponent ( curUrl ) } ` ;
46- }
47- window . location . href = loginUrl ;
39+ const user = getUserStore ( ) ;
40+ console . log ( 'error' , error ) ;
41+ const isExpired = Date . now ( ) / 1000 > user . expires ;
42+ if ( isExpired || ( error . response && error . response . status === 401 ) ) {
43+ redirectToLogin ( ) ;
44+ return Promise . reject ( error ) ;
45+ } else if ( ! skipGlobalError ( error . config ) ) {
46+ globalErrorStore . set ( true ) ;
47+ setTimeout ( ( ) => {
48+ globalErrorStore . set ( false ) ;
49+ } , 2500 ) ;
4850 }
4951
5052 // Return the error to the calling function
5153 return Promise . reject ( error ) ;
5254 }
5355) ;
5456
57+
58+ function redirectToLogin ( ) {
59+ const curUrl = window . location . pathname + window . location . search ;
60+ let loginUrl = 'login' ;
61+ if ( curUrl ) {
62+ loginUrl += `?redirect=${ encodeURIComponent ( curUrl ) } ` ;
63+ }
64+ window . location . href = loginUrl ;
65+ }
66+
5567/** @param {import('axios').InternalAxiosRequestConfig<any> } config */
5668function skipLoader ( config ) {
69+ /** @type {RegExp[] } */
5770 const postRegexes = [
5871 new RegExp ( 'http(s*)://(.*?)/conversation/(.*?)/(.*?)' , 'g' ) ,
5972 new RegExp ( 'http(s*)://(.*?)/agent' , 'g' ) ,
@@ -64,19 +77,22 @@ function skipLoader(config) {
6477 new RegExp ( 'http(s*)://(.*?)/users' , 'g' )
6578 ] ;
6679
80+ /** @type {RegExp[] } */
6781 const putRegexes = [
6882 new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/update' , 'g' ) ,
6983 new RegExp ( 'http(s*)://(.*?)/conversation/(.*?)/update-message' , 'g' ) ,
7084 new RegExp ( 'http(s*)://(.*?)/conversation/(.*?)/update-tags' , 'g' ) ,
7185 new RegExp ( 'http(s*)://(.*?)/users' , 'g' ) ,
7286 ] ;
7387
88+ /** @type {RegExp[] } */
7489 const deleteRegexes = [
7590 new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/delete-collection' , 'g' ) ,
7691 new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/data/(.*?)' , 'g' ) ,
7792 new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/data' , 'g' ) ,
7893 ] ;
7994
95+ /** @type {RegExp[] } */
8096 const getRegexes = [
8197 new RegExp ( 'http(s*)://(.*?)/setting/(.*?)' , 'g' ) ,
8298 new RegExp ( 'http(s*)://(.*?)/user/me' , 'g' ) ,
@@ -112,6 +128,56 @@ function skipLoader(config) {
112128 return false ;
113129}
114130
131+ /** @param {import('axios').InternalAxiosRequestConfig<any> } config */
132+ function skipGlobalError ( config ) {
133+ /** @type {RegExp[] } */
134+ const postRegexes = [
135+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/page' , 'g' ) ,
136+ new RegExp ( 'http(s*)://(.*?)/knowledge/(.*?)/search' , 'g' ) ,
137+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/create' , 'g' ) ,
138+ new RegExp ( 'http(s*)://(.*?)/knowledge/document/(.*?)/page' , 'g' ) ,
139+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/create-collection' , 'g' ) ,
140+ new RegExp ( 'http(s*)://(.*?)/refresh-agents' , 'g' )
141+ ] ;
142+
143+ /** @type {RegExp[] } */
144+ const putRegexes = [
145+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/update' , 'g' ) ,
146+ new RegExp ( 'http(s*)://(.*?)/role' , 'g' ) ,
147+ new RegExp ( 'http(s*)://(.*?)/user' , 'g' ) ,
148+ new RegExp ( 'http(s*)://(.*?)/conversation/(.*?)/update-message' , 'g' ) ,
149+ new RegExp ( 'http(s*)://(.*?)/conversation/(.*?)/update-tags' , 'g' )
150+ ] ;
151+
152+ /** @type {RegExp[] } */
153+ const deleteRegexes = [
154+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/delete-collection' , 'g' ) ,
155+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/data/(.*?)' , 'g' ) ,
156+ new RegExp ( 'http(s*)://(.*?)/knowledge/vector/(.*?)/data' , 'g' ) ,
157+ ] ;
158+
159+ /** @type {RegExp[] } */
160+ const getRegexes = [ ] ;
161+
162+ if ( config . method === 'post' && postRegexes . some ( regex => regex . test ( config . url || '' ) ) ) {
163+ return true ;
164+ }
165+
166+ if ( config . method === 'put' && putRegexes . some ( regex => regex . test ( config . url || '' ) ) ) {
167+ return true ;
168+ }
169+
170+ if ( config . method === 'delete' && deleteRegexes . some ( regex => regex . test ( config . url || '' ) ) ) {
171+ return true ;
172+ }
173+
174+ if ( config . method === 'get' && getRegexes . some ( regex => regex . test ( config . url || '' ) ) ) {
175+ return true ;
176+ }
177+
178+ return false ;
179+ }
180+
115181/**
116182 * @param {String } url
117183 * @param {Object } args
@@ -120,6 +186,7 @@ function skipLoader(config) {
120186export function replaceUrl ( url , args ) {
121187 const keys = Object . keys ( args ) ;
122188 keys . forEach ( key => {
189+ // @ts -ignore
123190 url = url . replace ( "{" + key + "}" , args [ key ] ) ;
124191 } ) ;
125192 return url ;
0 commit comments