Skip to content

Commit 90f15e5

Browse files
authored
Merge pull request #37 from MetabobProject/reset-analyze-on-save-config
feat: analyze on save config set to false by default
2 parents 580d9ab + e896ed1 commit 90f15e5

File tree

3 files changed

+90
-37
lines changed

3 files changed

+90
-37
lines changed

ext-src/helpers/CreateOrUpdateUserSession.ts

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { GetAPIConfig, GetRequestParamId } from '../config'
33
import { sessionService, CreateSessionRequest } from '../services'
44
import { Session } from '../state'
5+
import debugChannel from '../debug'
56

67
export async function createOrUpdateUserSession(context: vscode.ExtensionContext): Promise<undefined> {
78
const sessionState = new Session(context)
@@ -15,6 +16,39 @@ export async function createOrUpdateUserSession(context: vscode.ExtensionContext
1516
}
1617

1718
const sessionToken = sessionState.get()?.value
19+
20+
const one_minute = 60_000;
21+
const thirty_minutes = one_minute * 30;
22+
23+
// Periodically checking the session
24+
setInterval(() => {
25+
sessionService.getUserSession(sessionToken || '')
26+
.then((response) => {
27+
if (response.isOk()) {
28+
// @ts-ignore
29+
const status = response.value?.httpConfig?.status;
30+
if (status === 200) {
31+
debugChannel.appendLine("Metabob: Successfully checked the session of the user \n");
32+
} else if (status === 404) {
33+
debugChannel.appendLine("Metabob: Session of User does not exist. Recreating it again.. \n");
34+
sessionService.createUserSession(payload)
35+
.then((response) => {
36+
if (response.isOk()) {
37+
if (response.value?.session) {
38+
sessionState.set(response.value?.session)
39+
}
40+
}
41+
})
42+
}
43+
}
44+
})
45+
.catch((error) => {
46+
debugChannel.appendLine("Metabob: Error while activating sesstion \n" + JSON.stringify(error));
47+
})
48+
49+
}, thirty_minutes);
50+
51+
1852
if (sessionToken) {
1953
payload['sessionToken'] = sessionToken
2054
}

ext-src/services/base.service.ts

+55-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
import axios, { AxiosRequestConfig } from 'axios'
2-
import { err, ok, Result } from 'rusty-result-ts'
3-
import { GetAPIBaseURLConfig } from '../config'
4-
import { ApiErrorBase } from './base.error'
5-
import FormData from 'form-data'
1+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2+
import { err, ok, Result } from 'rusty-result-ts';
3+
import { GetAPIBaseURLConfig } from '../config';
4+
import { ApiErrorBase } from './base.error';
5+
import FormData from 'form-data';
66

7-
const apiBase = GetAPIBaseURLConfig()
7+
const apiBase = GetAPIBaseURLConfig();
88

99
export class ApiServiceBase {
10-
protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
10+
protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase;
1111

1212
/**
1313
* Creates a new service instance.
1414
* @param path A base path for all requests this service will make. Defaults to `/api`.
1515
*/
1616
public constructor() {
17-
this.urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
17+
this.urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase;
1818
}
1919

2020
/**
2121
* Returns a new instance of the base config for all requests this service makes.
2222
* @protected
2323
*/
24-
protected getConfig(sessionToken?: string, formDataHeaders?: FormData.Headers): AxiosRequestConfig {
24+
protected getConfig(
25+
sessionToken?: string,
26+
formDataHeaders?: FormData.Headers,
27+
): AxiosRequestConfig {
2528
const config: AxiosRequestConfig = {
2629
headers: {
2730
Accept: 'application/json',
2831
'Content-Type': 'application/json',
29-
'Access-Control-Allow-Origin': '*'
30-
}
31-
}
32+
'Access-Control-Allow-Origin': '*',
33+
},
34+
};
3235

3336
if (sessionToken && config.headers) {
34-
config.headers.Authorization = `Bearer ${sessionToken}`
37+
config.headers.Authorization = `Bearer ${sessionToken}`;
3538
}
3639

3740
if (formDataHeaders && config.headers) {
3841
config.headers = {
3942
...config.headers,
40-
...formDataHeaders
41-
}
43+
...formDataHeaders,
44+
};
4245
}
4346

44-
return config
47+
return config;
4548
}
4649

4750
/**
@@ -50,10 +53,13 @@ export class ApiServiceBase {
5053
* @param configOverrides A config object to merge onto the base config.
5154
* @protected
5255
*/
53-
protected async get<T>(path = '', configOverrides: AxiosRequestConfig): Promise<Result<T | null, ApiErrorBase>> {
56+
protected async get<T>(
57+
path = '',
58+
configOverrides: AxiosRequestConfig,
59+
): Promise<Result<T | null, ApiErrorBase>> {
5460
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
55-
return axios.get(fullPath, config)
56-
})
61+
return axios.get(fullPath, config);
62+
});
5763
}
5864

5965
/**
@@ -66,11 +72,11 @@ export class ApiServiceBase {
6672
protected async post<T>(
6773
path = '',
6874
data: unknown = undefined,
69-
configOverrides: AxiosRequestConfig
75+
configOverrides: AxiosRequestConfig,
7076
): Promise<Result<T | null, ApiErrorBase>> {
7177
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
72-
return axios.post(fullPath, data, config)
73-
})
78+
return axios.post(fullPath, data, config);
79+
});
7480
}
7581

7682
/**
@@ -83,11 +89,11 @@ export class ApiServiceBase {
8389
protected async put<T>(
8490
path = '',
8591
data: unknown = undefined,
86-
configOverrides: AxiosRequestConfig
92+
configOverrides: AxiosRequestConfig,
8793
): Promise<Result<T | null, ApiErrorBase>> {
8894
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
89-
return axios.put(fullPath, data, config)
90-
})
95+
return axios.put(fullPath, data, config);
96+
});
9197
}
9298

9399
/**
@@ -100,11 +106,11 @@ export class ApiServiceBase {
100106
protected async patch<T>(
101107
path = '',
102108
data: unknown = undefined,
103-
configOverrides: AxiosRequestConfig
109+
configOverrides: AxiosRequestConfig,
104110
): Promise<Result<T | null, ApiErrorBase>> {
105111
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
106-
return axios.patch(fullPath, data, config)
107-
})
112+
return axios.patch(fullPath, data, config);
113+
});
108114
}
109115

110116
/**
@@ -113,25 +119,38 @@ export class ApiServiceBase {
113119
* @param configOverrides A config object to merge onto the base config.
114120
* @protected
115121
*/
116-
protected async delete<T>(path = '', configOverrides: AxiosRequestConfig): Promise<Result<T | null, ApiErrorBase>> {
122+
protected async delete<T>(
123+
path = '',
124+
configOverrides: AxiosRequestConfig,
125+
): Promise<Result<T | null, ApiErrorBase>> {
117126
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
118-
return axios.delete(fullPath, config)
119-
})
127+
return axios.delete(fullPath, config);
128+
});
120129
}
121130

122131
private async requestResultWrapper<T>(
123132
subPath: string,
124133
config: AxiosRequestConfig,
125-
request: (fullPath: string, config: AxiosRequestConfig | undefined) => Promise<{ data: unknown } | null>
134+
request: (
135+
fullPath: string,
136+
config: AxiosRequestConfig | undefined,
137+
) => Promise<AxiosResponse<T>>,
126138
): Promise<Result<T | null, ApiErrorBase>> {
127139
if (subPath.length > 0 && subPath[0] !== '/') {
128-
subPath = `/${subPath}`
140+
subPath = `/${subPath}`;
129141
}
130142
try {
131-
const responseData: T | null = ((await request(`${this.urlBase}${subPath}`, config))?.data as T) ?? null
132-
return ok(responseData)
143+
const response = (await request(`${this.urlBase}${subPath}`, config)) ?? null;
144+
const status = response?.status;
145+
const responseData = response.data as T;
146+
return ok({
147+
...responseData,
148+
httpConfig: {
149+
status,
150+
},
151+
});
133152
} catch (e: unknown) {
134-
return err(new ApiErrorBase(e))
153+
return err(new ApiErrorBase(e));
135154
}
136155
}
137156
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"metabob.analyzeDocumentOnSave": {
128128
"description": "Analyze Document On Save",
129129
"type": "boolean",
130-
"default": true,
130+
"default": false,
131131
"category": "Metabob"
132132
},
133133
"metabob.backendSelection": {

0 commit comments

Comments
 (0)