Skip to content

Commit

Permalink
Merge pull request #52 from Azure/az/fixes-1-30
Browse files Browse the repository at this point in the history
Fixed several issues with configuration loading.
  • Loading branch information
azaslonov authored Jan 30, 2025
2 parents a7671b0 + 4dba41b commit 2f7d027
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
5 changes: 2 additions & 3 deletions public/config.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"dataApiHostName": "<service name>.data.<region>.azure-apicenter.ms/workspaces/default",
"dataApiHostName": "<service name>.data.<region>.azure-apicenter.ms",
"title": "API portal",
"authentication": {
"clientId": "<client ID>",
"tenantId": "<tenant ID>",
"scopes": ["https://azure-apicenter.net/user_impersonation"],
"authority": "https://login.microsoftonline.com/"
},
"scopingFilter": ""
}
}
5 changes: 5 additions & 0 deletions src/contracts/msalSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ export interface MsalSettings {
* Azure AD instance, e.g., https://login.microsoftonline.com/.
*/
authority: string;

/**
* @deprecated Use "authority" instead.
*/
azureAdInstance: string;
}
24 changes: 17 additions & 7 deletions src/routes/Main/ApisList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { Api } from "../../../contracts/api";
import { useApiService } from "../../../util/useApiService";
import { useAuthService } from "../../../util/useAuthService";
import { useConfigService } from "../../../util/useConfigService";
import { LocalStorageKey, useLocalStorage } from "../../../util/useLocalStorage";
import {
LocalStorageKey,
useLocalStorage,
} from "../../../util/useLocalStorage";
import { useLogger } from "../../../util/useLogger";
import { useSession } from "../../../util/useSession";
import useFilters, { TFilterTag } from "./Filters/useFilters";
Expand Down Expand Up @@ -51,7 +54,7 @@ const sortApis = (apis: Api[], sortBy?: string) => {
return apis;
};

const ApisList = () => {
const ApisList = () => {
const configService = useConfigService();
const layout = useLocalStorage(LocalStorageKey.apiListLayout).get();
const sortBy = useLocalStorage(LocalStorageKey.apiListSortBy).get();
Expand All @@ -65,7 +68,9 @@ const ApisList = () => {
const [filters] = useFilters();
const [apis, setApis] = useState<Api[] | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [showRestrictedModal, setShowRestrictedModal] = useState<boolean>(false);
const [showRestrictedModal, setShowRestrictedModal] = useState<boolean>(
false
);

const [searchParams] = useSearchParams();
const search = searchParams.get("search");
Expand Down Expand Up @@ -109,12 +114,13 @@ const ApisList = () => {
filterQuery += " and ";
}
});

if (filterQuery.length > 0) {
filterQuery = "$filter=" + filterQuery;
if (config.scopingFilter.length > 0) {
if (config.scopingFilter?.length > 0) {
filterQuery += " and (" + config.scopingFilter + ")";
}
} else if (config.scopingFilter.length > 0) {
} else if (config.scopingFilter?.length > 0) {
filterQuery = "$filter=(" + config.scopingFilter + ")";
}
}
Expand Down Expand Up @@ -156,11 +162,15 @@ const ApisList = () => {
{isLoading ? (
<Spinner size={"small"} />
) : !isAuthenticated ? (
<div className={css.emptyState}>Sign in or create an account to view APIs.</div>
<div className={css.emptyState}>
Sign in or create an account to view APIs.
</div>
) : apis?.length === 0 ? (
<div className={css.emptyState}>
<NoApis />
<div>Could not find APIs. Try a different search term.</div>
<div>
Could not find APIs. Try a different search term.
</div>
</div>
) : layout === TLayout.table ? (
<ApisTable apis={apis} />
Expand Down
13 changes: 11 additions & 2 deletions src/services/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ export class HttpClient implements IHttpClient {
public async fetchData(url: string, method: Method = Method.GET): Promise<any> {
const accessToken = await authService.getAccessToken();
const settings = await configService.getSettings();
const requestUrl = `https://${settings.dataApiHostName}/${url}`;

if (!settings.dataApiHostName.includes("/workspaces/default")) {
url = "workspaces/default/" + url;
}

if (!url.startsWith("/")) {
url = "/" + url;
}

const requestUrl = `https://${settings.dataApiHostName}${url}`;

const headers: HeadersInit = {
Accept: "application/json",
Expand All @@ -34,7 +43,7 @@ export class HttpClient implements IHttpClient {

case 404:
return null;

default:
break;
}
Expand Down
9 changes: 6 additions & 3 deletions src/services/msalAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export class MsalAuthService implements IAuthService {
private msalInstance?: msal.PublicClientApplication;
private scopes?: string[];

constructor(private readonly configService: IConfigService) {}
constructor(private readonly configService: IConfigService) { }

private async getMsalInstance(): Promise<msal.PublicClientApplication> {
if (this.msalInstance) {
return this.msalInstance;
}

const settings = await this.configService.getSettings();
const authorityUrl = settings.authentication.authority + settings.authentication.tenantId;
const authorityUrl = (settings.authentication.authority || settings.authentication.azureAdInstance) + settings.authentication.tenantId;

const msalConfig: msal.Configuration = {
auth: {
Expand All @@ -36,7 +36,10 @@ export class MsalAuthService implements IAuthService {
await msalInstance.initialize();

this.msalInstance = msalInstance;
this.scopes = settings.authentication.scopes;

this.scopes = typeof settings.authentication.scopes === "string"
? [settings.authentication.scopes]
: settings.authentication.scopes;

const accounts = msalInstance.getAllAccounts();

Expand Down

0 comments on commit 2f7d027

Please sign in to comment.