Skip to content

Commit 0ab4ccd

Browse files
committed
feat: Add custom request header zone
1 parent 0115723 commit 0ab4ccd

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

webapp/packages/core-sdk/src/GraphQLService.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { uploadDriverLibraryExtension } from './Extensions/uploadDriverLibraryEx
1414
import { uploadResultDataExtension } from './Extensions/uploadResultDataExtension.js';
1515
import type { IResponseInterceptor } from './IResponseInterceptor.js';
1616
import { getSdk, type Sdk } from './sdk.js';
17+
import { getRecentlySelectedZone } from './LocalStorage.js';
1718

1819
function extendedSDK(
1920
client: CustomGraphQLClient,
@@ -39,7 +40,11 @@ export class GraphQLService {
3940

4041
constructor(private readonly environmentService: EnvironmentService) {
4142
const gqlEndpoint = this.environmentService.gqlEndpoint;
42-
this.client = new CustomGraphQLClient(gqlEndpoint);
43+
this.client = new CustomGraphQLClient(gqlEndpoint, {
44+
headers: {
45+
zone: getRecentlySelectedZone(),
46+
},
47+
});
4348
this.sdk = extendedSDK(this.client);
4449
}
4550

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export enum StorageKey {
2+
DMS_AVAILABILITY_ZONE = 'DMS_AVAILABILITY_ZONE',
3+
}
4+
5+
class LocalStorageWrapper {
6+
public set<T extends string = string>(key: string, value: T) {
7+
return localStorage.setItem(key, value);
8+
}
9+
10+
public get(key: string) {
11+
return localStorage.getItem(key);
12+
}
13+
14+
public getOrDefault(key: string, defaultValue: string): string {
15+
if (localStorage.getItem(key) === null) {
16+
return defaultValue;
17+
}
18+
return localStorage.getItem(key) as string;
19+
}
20+
}
21+
22+
export const localStorageWrapper = new LocalStorageWrapper();
23+
24+
export const getRecentlySelectedZone = (): string => {
25+
const data = localStorageWrapper.get(StorageKey.DMS_AVAILABILITY_ZONE);
26+
try {
27+
const parsedData = JSON.parse(data || '[]');
28+
return parsedData?.[0]?.uid ?? '';
29+
} catch (error) {
30+
console.error(error);
31+
return '';
32+
}
33+
};

webapp/packages/core-sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export * from './ServiceError.js';
2626
export * from './SessionError.js';
2727
export * from './manifest.js';
2828
export * from './getObjectPropertyValue.js';
29+
export * from './LocalStorage.js';

0 commit comments

Comments
 (0)