@gip-recia/extended-uportal-header
Version:
a component to deport the uPortal menu
64 lines (57 loc) • 1.72 kB
text/typescript
import openIdConnect, {
type Response as OIDCResponse,
} from '@uportal/open-id-connect';
export interface orgInfo {
displayName: string;
logo?: string;
}
export default class orgInfoService {
static async get(
userInfoApiUrl: string,
orgApiUrl: string,
orgId: string,
userInfo: OIDCResponse | null = null,
debug = false
): Promise<orgInfo | null> {
try {
const requestHeaders: HeadersInit = new Headers();
let userInfoFetch;
if (!debug) {
if (userInfo === null || !userInfo?.encoded) {
userInfoFetch = await openIdConnect({ userInfoApiUrl });
} else {
userInfoFetch = userInfo;
}
const jwt = userInfoFetch.encoded;
requestHeaders.set('Authorization', `Bearer ${jwt}`);
}
const options = {
method: 'GET',
credentials: 'include' as RequestCredentials,
headers: requestHeaders,
};
const getParams = new URLSearchParams({
ids: orgId,
});
const response = await fetch(`${orgApiUrl}?${getParams}`, options);
if (!response.ok) {
throw new Error(response.statusText);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const org: any = await response.json();
if (org[orgId]) {
const currentOrg = org[orgId];
const orgInfos = {
displayName: currentOrg?.displayName,
logo: currentOrg?.otherAttributes?.ESCOStructureLogo,
};
if (orgInfos.displayName) return orgInfos;
}
} catch (err) {
// eslint-disable-next-line
console.error(err, userInfoApiUrl, orgApiUrl, debug);
return null;
}
return null;
}
}