Skip to content

Commit b97d736

Browse files
Chore API restructure to use request util (#10)
* chore (api): restructure to use request util * docs: add changelog entry for API refactor * style: fix prettier formatting issues --------- Co-authored-by: RicardoASJunior <[email protected]>
1 parent 83785c7 commit b97d736

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add ObjectScript enter rules for semicolon (`;`) continuation on line break. (#5)
88
- Auto-indent dot syntax on Enter for `objectscript`/`objectscript-int` (replicates leading dots). (#6)
99
- Added `resolveContextExpression` command: posts current line/routine to API, inserts returned code on success, shows error otherwise. (#7)
10+
- Refactor API: extracted request util and updated endpoints (#8)
1011

1112
## [3.0.6] 09-Sep-2025
1213

src/api/ccs/sourceControl.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
2+
import * as https from "https";
3+
import * as vscode from "vscode";
4+
5+
import { AtelierAPI } from "../";
6+
7+
export class SourceControlApi {
8+
private readonly client: AxiosInstance;
9+
10+
private constructor(client: AxiosInstance) {
11+
this.client = client;
12+
}
13+
14+
public static fromAtelierApi(api: AtelierAPI): SourceControlApi {
15+
const { host, port, username, password, https: useHttps, pathPrefix } = api.config;
16+
17+
if (!host || !port) {
18+
throw new Error("No active InterSystems server connection for this file.");
19+
}
20+
21+
const normalizedPrefix = pathPrefix ? (pathPrefix.startsWith("/") ? pathPrefix : `/${pathPrefix}`) : "";
22+
const baseUrl = `${useHttps ? "https" : "http"}://${host}:${port}${encodeURI(normalizedPrefix)}`;
23+
24+
const httpsAgent = new https.Agent({
25+
rejectUnauthorized: vscode.workspace.getConfiguration("http").get("proxyStrictSSL"),
26+
});
27+
28+
const client = axios.create({
29+
baseURL: `${baseUrl}/api/sourcecontrol/vscode`,
30+
headers: {
31+
"Content-Type": "application/json",
32+
},
33+
httpsAgent,
34+
auth:
35+
typeof username === "string" && typeof password === "string"
36+
? {
37+
username,
38+
password,
39+
}
40+
: undefined,
41+
});
42+
43+
return new SourceControlApi(client);
44+
}
45+
46+
public post<T = unknown, R = AxiosResponse<T>>(
47+
endpoint: string,
48+
data?: unknown,
49+
config?: AxiosRequestConfig<unknown>
50+
): Promise<R> {
51+
return this.client.post<T, R>(endpoint, data, config);
52+
}
53+
}

src/commands/contextHelp.ts renamed to src/commands/ccs/contextHelp.ts

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import axios from "axios";
2-
import * as https from "https";
31
import * as path from "path";
42
import * as vscode from "vscode";
53

6-
import { AtelierAPI } from "../api";
7-
import { handleError } from "../utils";
4+
import { AtelierAPI } from "../../api";
5+
import { SourceControlApi } from "../../api/ccs/sourceControl";
6+
import { handleError } from "../../utils";
87

98
interface ResolveContextExpressionResponse {
109
status?: string;
@@ -30,43 +29,20 @@ export async function resolveContextExpression(): Promise<void> {
3029

3130
const routine = path.basename(document.fileName);
3231
const api = new AtelierAPI(document.uri);
33-
const { host, port, username, password, https: useHttps, pathPrefix } = api.config;
3432

35-
if (!host || !port) {
36-
void vscode.window.showErrorMessage("No active InterSystems server connection for this file.");
33+
let sourceControlApi: SourceControlApi;
34+
try {
35+
sourceControlApi = SourceControlApi.fromAtelierApi(api);
36+
} catch (error) {
37+
void vscode.window.showErrorMessage(error instanceof Error ? error.message : String(error));
3738
return;
3839
}
3940

40-
const normalizedPrefix = pathPrefix ? (pathPrefix.startsWith("/") ? pathPrefix : `/${pathPrefix}`) : "";
41-
42-
const baseUrl = `${useHttps ? "https" : "http"}://${host}:${port}${encodeURI(normalizedPrefix)}`;
43-
const url = `${baseUrl}/api/sourcecontrol/vscode/resolveContextExpression`;
44-
45-
const httpsAgent = new https.Agent({
46-
rejectUnauthorized: vscode.workspace.getConfiguration("http").get("proxyStrictSSL"),
47-
});
48-
4941
try {
50-
const response = await axios.post<ResolveContextExpressionResponse>(
51-
url,
52-
{
53-
routine,
54-
contextExpression,
55-
},
56-
{
57-
headers: {
58-
"Content-Type": "application/json",
59-
},
60-
auth:
61-
typeof username === "string" && typeof password === "string"
62-
? {
63-
username,
64-
password,
65-
}
66-
: undefined,
67-
httpsAgent,
68-
}
69-
);
42+
const response = await sourceControlApi.post<ResolveContextExpressionResponse>("/resolveContextExpression", {
43+
routine,
44+
contextExpression,
45+
});
7046

7147
const data = response.data ?? {};
7248
if (typeof data.status === "string" && data.status.toLowerCase() === "success" && data.textExpression) {

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ import {
162162
import { WorkspaceNode, NodeBase } from "./explorer/nodes";
163163
import { showPlanWebview } from "./commands/showPlanPanel";
164164
import { isfsConfig } from "./utils/FileProviderUtil";
165-
import { resolveContextExpression } from "./commands/contextHelp";
165+
import { resolveContextExpression } from "./commands/ccs/contextHelp";
166166

167167
const packageJson = vscode.extensions.getExtension(extensionId).packageJSON;
168168
const extensionVersion = packageJson.version;

0 commit comments

Comments
 (0)