Skip to content

Commit 50e40bb

Browse files
authored
detect react-native (#137)
1 parent ac9895c commit 50e40bb

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type DetectedInfoType = 'browser' | 'node' | 'bot-device' | 'bot';
1+
export type DetectedInfoType = 'browser' | 'node' | 'bot-device' | 'bot' | 'react-native';
22

33
interface DetectedInfo<
44
T extends DetectedInfoType,
@@ -51,6 +51,14 @@ export class BotInfo implements DetectedInfo<'bot', 'bot', null, null> {
5151
public readonly os: null = null;
5252
}
5353

54+
export class ReactNativeInfo
55+
implements DetectedInfo<'react-native', 'react-native', null, null> {
56+
public readonly type = 'react-native';
57+
public readonly name: 'react-native' = 'react-native';
58+
public readonly version: null = null;
59+
public readonly os: null = null;
60+
};
61+
5462
export type Browser =
5563
| 'aol'
5664
| 'edge'
@@ -180,11 +188,19 @@ const operatingSystemRules: OperatingSystemRule[] = [
180188

181189
export function detect(
182190
userAgent?: string,
183-
): BrowserInfo | SearchBotDeviceInfo | BotInfo | NodeInfo | null {
191+
): BrowserInfo | SearchBotDeviceInfo | BotInfo | NodeInfo | ReactNativeInfo | null {
184192
if (!!userAgent) {
185193
return parseUserAgent(userAgent);
186194
}
187195

196+
if (
197+
typeof document === 'undefined' &&
198+
typeof navigator !== 'undefined' &&
199+
navigator.product === 'ReactNative'
200+
) {
201+
return new ReactNativeInfo();
202+
}
203+
188204
if (typeof navigator !== 'undefined') {
189205
return parseUserAgent(navigator.userAgent);
190206
}

0 commit comments

Comments
 (0)