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