// Custom path join code to work both on node & browser import { ManagedServiceId } from './types'; // Ref: https://stackoverflow.com/questions/29855098/is-there-a-built-in-javascript-function-similar-to-os-path-join const joinPath = (...paths: string[]): string => { return paths .map((part, index) => { if (index === 0) { return part.trim().replace(/[\\/]*$/g, ''); } else { return part.trim().replace(/(^[\\/]*|[\\/]*$)/g, ''); } }) .filter((x) => x.length) .join('/'); }; /** * Concatenates a list of paths to a base URL * @param base Base URL string * @param paths List of paths to concatenate * @returns Concatenated URL object */ export const concatUrl = (base: string, ...paths: string[]): URL => { const url = new URL(base); const newPath = joinPath(...[url.pathname, ...paths]); return new URL(newPath, url.origin); }; const getNaiveServiceNameFromServiceId = (serviceId: string): string => { return serviceId .split('-') .map((word) => { return word.toUpperCase(); }) .join(' '); }; export const getServiceNameFromServiceId = ( serviceId: ManagedServiceId | string, isManaged: boolean | undefined, ): string => { if (isManaged) { const managedServiceId = serviceId as ManagedServiceId; switch (managedServiceId) { case ManagedServiceId.ID_SERVICE: return 'Identity Service'; case ManagedServiceId.MICRO_FRONTEND_SERVICE: return 'Microfrontend Service'; case ManagedServiceId.PORTAL_SERVICE: return 'Management System Service'; case ManagedServiceId.AXAUTH_SERVICE: return 'AxAuth Service'; case ManagedServiceId.VIDEO_SERVICE: return 'Video Service'; case ManagedServiceId.IMAGE_SERVICE: return 'Image Service'; case ManagedServiceId.USER_SERVICE: return 'User Service'; case ManagedServiceId.PERSONALIZATION_SERVICE: return 'Personalization Service'; case ManagedServiceId.HOSTING_SERVICE: return 'Hosting Service'; case ManagedServiceId.LOCALIZATION_SERVICE: return 'Localization Service'; case ManagedServiceId.KEY_SERVICE: return 'Key Service'; case ManagedServiceId.ENTITLEMENT_SERVICE: return 'Entitlement Service'; case ManagedServiceId.DRM_SERVICE: return 'DRM Service'; default: { // This block would never execute in runtime, and used as a build-time exhaustive switch-case check for the `ManagedServiceId` Type. // eslint-disable-next-line @typescript-eslint/no-unused-vars const unhandledManagedServiceId: never = managedServiceId; // In case of an unhandled ManagedServiceId, we return a fallback value instead of throwing an error. // eslint-disable-next-line no-console console.warn( `Warning: Unhandled ManagedServiceId "${managedServiceId}". Returning a fallback service name.`, ); return getNaiveServiceNameFromServiceId(serviceId); } } } else { return getNaiveServiceNameFromServiceId(serviceId); } };