11import axios from 'axios' ;
2- import { version , window } from 'vscode' ;
3- import { app_url } from '../Constants' ;
2+ import { commands , version , window } from 'vscode' ;
3+ import { app_url , ONE_MIN_MILLIS } from '../Constants' ;
44import {
55 logIt ,
66 getPluginId ,
99 getOs ,
1010 getPluginUuid ,
1111 getItem ,
12- setItem
12+ setItem ,
13+ isPrimaryWindow
1314} from '../Util' ;
1415
1516// build the axios client
@@ -28,6 +29,8 @@ const appApi: any = axios.create({
2829 }
2930} ) ;
3031
32+ let invalidSessionNotified : boolean = false ;
33+
3134// Evaluate these headers on every request since these values can change
3235async function dynamicHeaders ( override_token ?: string ) {
3336 let headers : any = {
@@ -50,11 +53,25 @@ async function dynamicHeaders(override_token?: string) {
5053}
5154
5255export async function appGet ( api : string , queryParams : any = { } , token_override : any = '' ) {
53- return await appApi . get ( api , { params : queryParams , headers : await dynamicHeaders ( token_override ) } ) . catch ( ( err : any ) => {
56+ const headers = await dynamicHeaders ( token_override ) ;
57+ if ( ! headers [ 'Authorization' ] && isPrimaryWindow ( ) ) {
58+ const currentTime = Date . now ( ) ;
59+ const lastTimeInvalidSessionNotified = Number ( getItem ( 'lastTimeInvalidSessionNotified' ) || 0 ) ;
60+ if ( ! invalidSessionNotified && ( currentTime - lastTimeInvalidSessionNotified > ONE_MIN_MILLIS * 60 * 24 ) ) {
61+ logIt ( `No authorization token found for GET ${ api } . Please ensure you are logged in.` ) ;
62+ invalidSessionNotified = true ;
63+ setItem ( 'lastTimeInvalidSessionNotified' , currentTime ) ;
64+ await invalidSessionPrompt ( ) ;
65+ }
66+ return ;
67+ }
68+ return await appApi . get ( api , { params : queryParams , headers : headers } ) . then ( ( resp : any ) => {
69+ return resp ;
70+ } ) . catch ( ( err : any ) => {
5471 logIt ( `error for GET ${ api } , message: ${ err . message } ` ) ;
5572 if ( getResponseStatus ( err ?. response ) === 401 ) {
5673 // clear the JWT because it is invalid
57- setItem ( 'jwt' , null )
74+ setItem ( 'jwt' , null ) ;
5875 }
5976 return err ;
6077 } ) ;
@@ -103,7 +120,7 @@ export function isResponseOk(resp: any) {
103120 return false ;
104121}
105122
106- function getResponseStatus ( resp : any ) {
123+ export function getResponseStatus ( resp : any ) {
107124 let status = null ;
108125 if ( resp ?. status ) {
109126 status = resp . status ;
@@ -113,10 +130,23 @@ function getResponseStatus(resp: any) {
113130 status = 500 ;
114131 } else if ( resp ?. code === 'ECONNREFUSED' ) {
115132 status = 503 ;
133+ } else if ( resp ?. code === 'ENOTFOUND' ) {
134+ status = 404 ;
116135 }
117136 return status ;
118137}
119138
139+ async function invalidSessionPrompt ( ) {
140+ const selection = await window . showInformationMessage (
141+ "We couldn't verify your session. Please log in again to continue using Code Time features" ,
142+ { modal : true } ,
143+ ...[ 'Login' ]
144+ ) ;
145+ if ( selection === 'Login' ) {
146+ commands . executeCommand ( 'codetime.login' ) ;
147+ }
148+ }
149+
120150async function getAuthorization ( ) {
121151 const token = getItem ( 'jwt' ) ;
122152
0 commit comments