1import NativeModulesProxy from './NativeModulesProxy';
2
3type ExpoObject = {
4  modules:
5    | undefined
6    | {
7        [key: string]: any;
8      };
9};
10
11declare global {
12  // eslint-disable-next-line no-var
13  var expo: ExpoObject | undefined;
14
15  /**
16   * @deprecated `global.ExpoModules` is deprecated, use `global.expo.modules` instead.
17   */
18  // eslint-disable-next-line no-var
19  var ExpoModules:
20    | undefined
21    | {
22        [key: string]: any;
23      };
24}
25
26/**
27 * Imports the native module registered with given name. In the first place it tries to load
28 * the module installed through the JSI host object and then falls back to the bridge proxy module.
29 * Notice that the modules loaded from the proxy may not support some features like synchronous functions.
30 *
31 * @param moduleName Name of the requested native module.
32 * @returns Object representing the native module.
33 * @throws Error when there is no native module with given name.
34 */
35export function requireNativeModule<ModuleType = any>(moduleName: string): ModuleType {
36  const nativeModule: ModuleType =
37    globalThis.expo?.modules?.[moduleName] ??
38    globalThis.ExpoModules?.[moduleName] ??
39    NativeModulesProxy[moduleName];
40
41  if (!nativeModule) {
42    throw new Error(`Cannot find native module '${moduleName}'`);
43  }
44  return nativeModule;
45}
46