angular/devtools/projects/ng-devtools-backend/src/lib/ng-debug-api/ng-debug-api.ts
Doug Parker 2866355872 refactor(devtools): make ngDebugClient return a Partial (#60209)
In general, we can't assume all applications implement the full `ng` contract as many are older Angular application which pre-date the current interface. As a result, it is safer to type this as a `Partial`.

For now, I just added non-null assertions at all current usage locations, as we do generally feature detect before using these fields. However, hopefully this `Partial` type will make it harder to accidentally call a function which might not be supported.

PR Close #60209
2025-03-05 13:42:40 -08:00

51 lines
1.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type {ɵGlobalDevModeUtils as GlobalDevModeUtils} from '@angular/core';
/**
* Returns a handle to window.ng APIs (global angular debugging).
*
* @returns window.ng
*/
export const ngDebugClient = () => (window as any).ng as Partial<GlobalDevModeUtils['ng']>;
/**
* Checks whether a given debug API is supported within window.ng
*
* @returns boolean
*/
export function ngDebugApiIsSupported(api: keyof GlobalDevModeUtils['ng']): boolean {
const ng = ngDebugClient();
return typeof ng[api] === 'function';
}
/**
* Checks whether Dependency Injection debug API is supported within window.ng
*
* @returns boolean
*/
export function ngDebugDependencyInjectionApiIsSupported(): boolean {
if (!ngDebugApiIsSupported('getInjector')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetInjectorResolutionPath')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetDependenciesFromInjectable')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetInjectorProviders')) {
return false;
}
if (!ngDebugApiIsSupported('ɵgetInjectorMetadata')) {
return false;
}
return true;
}