This commit applies `strictNullChecks` to the entire codebase to improve maintainability and type safety. Key changes include: - Remove some explicit null-checks where unnecessary. - Add necessary null-checks. - Refactor static factory functions for a more functional approach. - Improve some test names and contexts for better debugging. - Add unit tests for any additional logic introduced. - Refactor `createPositionFromRegexFullMatch` to its own function as the logic is reused. - Prefer `find` prefix on functions that may return `undefined` and `get` prefix for those that always return a value.
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
|
|
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
|
|
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
|
|
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
|
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
|
import { IRuntimeEnvironment } from './IRuntimeEnvironment';
|
|
|
|
export class RuntimeEnvironment implements IRuntimeEnvironment {
|
|
public static readonly CurrentEnvironment: IRuntimeEnvironment = new RuntimeEnvironment(window);
|
|
|
|
public readonly isDesktop: boolean;
|
|
|
|
public readonly os: OperatingSystem | undefined;
|
|
|
|
public readonly isNonProduction: boolean;
|
|
|
|
protected constructor(
|
|
window: Partial<Window>,
|
|
environmentVariables: IEnvironmentVariables = EnvironmentVariablesFactory.Current.instance,
|
|
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
|
) {
|
|
if (!window) {
|
|
throw new Error('missing window');
|
|
}
|
|
this.isNonProduction = environmentVariables.isNonProduction;
|
|
this.isDesktop = isDesktop(window);
|
|
if (this.isDesktop) {
|
|
this.os = window?.os;
|
|
} else {
|
|
this.os = undefined;
|
|
const userAgent = getUserAgent(window);
|
|
if (userAgent) {
|
|
this.os = browserOsDetector.detect(userAgent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function getUserAgent(window: Partial<Window>): string | undefined {
|
|
return window?.navigator?.userAgent;
|
|
}
|
|
|
|
function isDesktop(window: Partial<WindowVariables>): boolean {
|
|
return window?.isDesktop === true;
|
|
}
|