Skip to content

Commit 340be2d

Browse files
Standardize Base URL
Add base URL support to Data Gateway since it can be run from dev environments. Use the data gateway logic on SHIELD's client factory to standardize and extend code support.
1 parent 30f9b18 commit 340be2d

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";
22
import { FetchRequestAdapter } from "@microsoft/kiota-http-fetchlibrary";
33
import type { TokenCredential } from "@azure/core-auth";
4-
import { assert } from 'typia';
4+
import { assert, assertGuardEquals } from 'typia';
55
import { createDataGatewayClient } from "./sdk/dataGatewayClient.js";
66

77
// Export all of the SDK's types
@@ -10,22 +10,28 @@ export type * from './sdk/models/index.js';
1010
/**
1111
* Function that initializes the Data Gateway SDK.
1212
* @param credential Configured authentication session.
13+
* @param baseUrl Root of the URL that should have endpoints appended to it by the query building system.
1314
* @returns Configured API client that is able to make requests against SHI Data Gateway.
1415
*/
15-
export default function dataGatewayClientFactory(credential: TokenCredential) {
16+
export default function dataGatewayClientFactory(credential: TokenCredential, baseUrl?: URL) {
1617
// #region Input Validation
1718
assert(credential);
19+
20+
assertGuardEquals(baseUrl);
1821
// #endregion Input Validation
1922

2023
/** List of hosts that are allowed when making API calls, this is used to prevent token leaks to threat actors. */
21-
const allowedHostList = new Set(['https://api.shilab.com']);
24+
const allowedHostList = new Set([baseUrl?.host ?? 'api.shilab.com']);
2225

2326
/** Authentication system that will be used to configure the SDK client. */
2427
const authProvider = new AzureIdentityAuthenticationProvider(credential, void 0, void 0, allowedHostList);
2528

2629
/** Instance of the data gateway client initialization configuration. */
2730
const dataGatewayAdapter = new FetchRequestAdapter(authProvider);
2831

32+
// If a base URL is provided, set the root of the SDK's queries to the provided URL. Also account for trailing slashes.
33+
if (baseUrl) { dataGatewayAdapter.baseUrl = baseUrl.href.endsWith('/') ? baseUrl.href.substring(0, baseUrl.href.length - 1) : baseUrl.href; }
34+
2935
/** Instance of the API client that can be used for data gateway access. */
3036
return createDataGatewayClient(dataGatewayAdapter);
3137
}

src/shield/TypeScript/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, assertGuardEquals, tags } from 'typia';
1+
import { assert, assertGuardEquals } from 'typia';
22
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";
33
import { FetchRequestAdapter } from "@microsoft/kiota-http-fetchlibrary";
44
import type { TokenCredential } from "@azure/core-auth";
@@ -10,17 +10,18 @@ export type * from './sdk/models/index.js';
1010
/**
1111
* Function that initializes the SHIELD SDK.
1212
* @param credential Configured authentication session from Entra ID.
13+
* @param baseUrl Root of the URL that should have endpoints appended to it by the query building system.
1314
* @returns Configured API client that is able to make requests against the specified SHIELD instance.
1415
*/
15-
export default function shieldClientFactory(credential: TokenCredential, baseUrl: string & tags.Format<'hostname'>) {
16+
export default function shieldClientFactory(credential: TokenCredential, baseUrl: URL) {
1617
// #region Input Validation
1718
assert(credential);
1819

1920
assertGuardEquals(baseUrl);
2021
// #endregion Input Validation
2122

2223
/** List of hosts that are allowed when making API calls, this is used to prevent token leaks to threat actors. */
23-
const allowedHostList = new Set([baseUrl]);
24+
const allowedHostList = new Set([baseUrl.host]);
2425

2526
/** Authentication system that will be used to configure the SDK client. */
2627
const authProvider = new AzureIdentityAuthenticationProvider(credential, void 0, void 0, allowedHostList);
@@ -29,7 +30,7 @@ export default function shieldClientFactory(credential: TokenCredential, baseUrl
2930
const shieldAdapter = new FetchRequestAdapter(authProvider);
3031

3132
// Set the base URL to be what is provided, since the host name is unique every deployment
32-
shieldAdapter.baseUrl = baseUrl.startsWith('localhost') ? `http://${ baseUrl }` : `https://${ baseUrl }`;
33+
shieldAdapter.baseUrl = baseUrl.href.endsWith('/') ? baseUrl.href.substring(0, baseUrl.href.length - 1) : baseUrl.href;
3334

3435
/** Instance of the API client that can be used for SHIELD access. */
3536
return createShieldClient(shieldAdapter);

0 commit comments

Comments
 (0)