From c93d3bcea3c9b5a1d4feda3807c603292af02021 Mon Sep 17 00:00:00 2001 From: Shikhar Bhargava Date: Thu, 12 Jun 2025 18:37:51 -0700 Subject: [PATCH 1/5] Add locale header for every request with ThoughtSpot Rest client --- package-lock.json | 14 +++++++++++--- package.json | 1 + src/thoughtspot/thoughtspot-client.ts | 20 +++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index cc7048f..45b1500 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@thoughtspot/rest-api-sdk": "^2.13.1", "agents": "^0.0.95", "hono": "^4.7.8", + "rxjs": "^7.8.2", "yaml": "^2.7.1", "zod": "^3.24.3", "zod-to-json-schema": "^3.24.5" @@ -4852,6 +4853,15 @@ "queue-microtask": "^1.2.2" } }, + "node_modules/rxjs": { + "version": "7.8.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz", + "integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.1.0" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -5493,9 +5503,7 @@ "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", - "dev": true, - "license": "0BSD", - "optional": true + "license": "0BSD" }, "node_modules/tsx": { "version": "4.19.3", diff --git a/package.json b/package.json index 3d17fc3..4edf55c 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@thoughtspot/rest-api-sdk": "^2.13.1", "agents": "^0.0.95", "hono": "^4.7.8", + "rxjs": "^7.8.2", "yaml": "^2.7.1", "zod": "^3.24.3", "zod-to-json-schema": "^3.24.5" diff --git a/src/thoughtspot/thoughtspot-client.ts b/src/thoughtspot/thoughtspot-client.ts index 8c5908a..33defca 100644 --- a/src/thoughtspot/thoughtspot-client.ts +++ b/src/thoughtspot/thoughtspot-client.ts @@ -1,11 +1,25 @@ -import { createBearerAuthenticationConfig, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk" +import { createBearerAuthenticationConfig, RequestContext, ResponseContext, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk" import YAML from "yaml"; +import { Observable, of } from "rxjs"; export const getThoughtSpotClient = (instanceUrl: string, bearerToken: string) => { - const client = new ThoughtSpotRestApi(createBearerAuthenticationConfig( + const config = createBearerAuthenticationConfig( instanceUrl, () => Promise.resolve(bearerToken), - )); + ); + + config.middleware.push({ + pre: (context: RequestContext): Observable => { + context.setHeaderParam('Accept-Language', 'en-US'); + console.log('[Middleware][Pre] Sending request to:', context.getUrl()); + console.log('[Middleware][Pre] Headers:', context.getHeaders()); + return of(context); + }, + post: (context: ResponseContext): Observable => { + return of(context); + } + }); + const client = new ThoughtSpotRestApi(config); (client as any).instanceUrl = instanceUrl; addExportUnsavedAnswerTML(client, instanceUrl, bearerToken); addGetSessionInfo(client, instanceUrl, bearerToken); From 39b0c8a13754118a427186cde860f5c647da6b51 Mon Sep 17 00:00:00 2001 From: Shikhar Bhargava Date: Thu, 12 Jun 2025 18:39:42 -0700 Subject: [PATCH 2/5] clear logs --- src/thoughtspot/thoughtspot-client.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/thoughtspot/thoughtspot-client.ts b/src/thoughtspot/thoughtspot-client.ts index 33defca..ac510b0 100644 --- a/src/thoughtspot/thoughtspot-client.ts +++ b/src/thoughtspot/thoughtspot-client.ts @@ -11,8 +11,6 @@ export const getThoughtSpotClient = (instanceUrl: string, bearerToken: string) = config.middleware.push({ pre: (context: RequestContext): Observable => { context.setHeaderParam('Accept-Language', 'en-US'); - console.log('[Middleware][Pre] Sending request to:', context.getUrl()); - console.log('[Middleware][Pre] Headers:', context.getHeaders()); return of(context); }, post: (context: ResponseContext): Observable => { From 266fcd935b66a22635f27cd65ba63891dbb501fc Mon Sep 17 00:00:00 2001 From: Shikhar Bhargava Date: Mon, 16 Jun 2025 11:49:17 -0700 Subject: [PATCH 3/5] do not add Accept-Language header if already present in context --- src/thoughtspot/thoughtspot-client.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/thoughtspot/thoughtspot-client.ts b/src/thoughtspot/thoughtspot-client.ts index ac510b0..8f40670 100644 --- a/src/thoughtspot/thoughtspot-client.ts +++ b/src/thoughtspot/thoughtspot-client.ts @@ -10,7 +10,10 @@ export const getThoughtSpotClient = (instanceUrl: string, bearerToken: string) = config.middleware.push({ pre: (context: RequestContext): Observable => { - context.setHeaderParam('Accept-Language', 'en-US'); + const headers = context.getHeaders(); + if (!headers || !headers["Accept-Language"]) { + context.setHeaderParam('Accept-Language', 'en-US'); + } return of(context); }, post: (context: ResponseContext): Observable => { From b1922a4a49f0e0b2171d7b7b8a8417f5ebc716f0 Mon Sep 17 00:00:00 2001 From: Shikhar Bhargava Date: Mon, 16 Jun 2025 13:21:53 -0700 Subject: [PATCH 4/5] fix lint errors --- src/thoughtspot/thoughtspot-client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/thoughtspot/thoughtspot-client.ts b/src/thoughtspot/thoughtspot-client.ts index 8f40670..bf60afd 100644 --- a/src/thoughtspot/thoughtspot-client.ts +++ b/src/thoughtspot/thoughtspot-client.ts @@ -1,6 +1,7 @@ import { createBearerAuthenticationConfig, RequestContext, ResponseContext, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk" import YAML from "yaml"; -import { Observable, of } from "rxjs"; +import type { Observable } from "rxjs"; +import { of } from "rxjs"; export const getThoughtSpotClient = (instanceUrl: string, bearerToken: string) => { const config = createBearerAuthenticationConfig( From f989fca4946d99a52ec3663618e3624648dddc17 Mon Sep 17 00:00:00 2001 From: Shikhar Bhargava Date: Mon, 16 Jun 2025 13:23:44 -0700 Subject: [PATCH 5/5] fix lint error 2 --- src/thoughtspot/thoughtspot-client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/thoughtspot/thoughtspot-client.ts b/src/thoughtspot/thoughtspot-client.ts index bf60afd..abe82d6 100644 --- a/src/thoughtspot/thoughtspot-client.ts +++ b/src/thoughtspot/thoughtspot-client.ts @@ -1,4 +1,5 @@ -import { createBearerAuthenticationConfig, RequestContext, ResponseContext, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk" +import { createBearerAuthenticationConfig, ThoughtSpotRestApi } from "@thoughtspot/rest-api-sdk" +import type { RequestContext, ResponseContext } from "@thoughtspot/rest-api-sdk" import YAML from "yaml"; import type { Observable } from "rxjs"; import { of } from "rxjs";