Browse Source

Check can now accept a string or an object

Mathieu Acthernoene 7 years ago
parent
commit
e3694e73a3
5 changed files with 28 additions and 16 deletions
  1. 2
    0
      .prettierignore
  2. 1
    1
      README.md
  3. 4
    3
      lib/permissions.android.js
  4. 18
    9
      lib/permissions.ios.js
  5. 3
    3
      package.json

+ 2
- 0
.prettierignore View File

@@ -1 +1,3 @@
1
+android/
2
+ios/
1 3
 example/node_modules/

+ 1
- 1
README.md View File

@@ -10,7 +10,7 @@ Request user permissions from React Native, iOS + Android
10 10
 
11 11
 | Version | React Native Support |
12 12
 | ------- | -------------------- |
13
-| 1.0.4   | 0.40 - 0.50          |
13
+| 1.0.5   | 0.40 - 0.51          |
14 14
 | 0.2.5   | 0.33 - 0.39          |
15 15
 
16 16
 _Complies with

+ 4
- 3
lib/permissions.android.js View File

@@ -4,7 +4,8 @@ import { AsyncStorage, NativeModules, PermissionsAndroid } from 'react-native'
4 4
 
5 5
 type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
6 6
 type Rationale = { title: string, message: string }
7
-type Options = string | { type: string, rationale?: Rationale }
7
+type CheckOptions = string | { type: string }
8
+type RequestOptions = string | { type: string, rationale?: Rationale }
8 9
 
9 10
 const permissionTypes = {
10 11
   location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
@@ -41,7 +42,7 @@ class ReactNativePermissions {
41 42
 
42 43
   getTypes: () => Array<string> = () => Object.keys(permissionTypes)
43 44
 
44
-  check = (permission: string, type?: string): Promise<Status> => {
45
+  check = (permission: string, options?: CheckOptions): Promise<Status> => {
45 46
     if (!permissionTypes[permission]) {
46 47
       const error = new Error(
47 48
         `ReactNativePermissions: ${
@@ -71,7 +72,7 @@ class ReactNativePermissions {
71 72
     )
72 73
   }
73 74
 
74
-  request = (permission: string, options?: Options): Promise<Status> => {
75
+  request = (permission: string, options?: RequestOptions): Promise<Status> => {
75 76
     if (!permissionTypes[permission]) {
76 77
       const error = new Error(
77 78
         `ReactNativePermissions: ${

+ 18
- 9
lib/permissions.ios.js View File

@@ -5,7 +5,8 @@ const PermissionsIOS = NativeModules.ReactNativePermissions
5 5
 
6 6
 type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
7 7
 type Rationale = { title: string, message: string }
8
-type Options = string | { type: string, rationale?: Rationale }
8
+type CheckOptions = string | { type: string }
9
+type RequestOptions = string | { type: string, rationale?: Rationale }
9 10
 
10 11
 const permissionTypes = [
11 12
   'location',
@@ -34,7 +35,7 @@ class ReactNativePermissions {
34 35
 
35 36
   getTypes: () => Array<string> = () => permissionTypes
36 37
 
37
-  check = (permission: string, type?: string): Promise<Status> => {
38
+  check = (permission: string, options?: CheckOptions): Promise<Status> => {
38 39
     if (!permissionTypes.includes(permission)) {
39 40
       const error = new Error(
40 41
         `ReactNativePermissions: ${
@@ -45,13 +46,6 @@ class ReactNativePermissions {
45 46
       return Promise.reject(error)
46 47
     }
47 48
 
48
-    return PermissionsIOS.getPermissionStatus(
49
-      permission,
50
-      type || DEFAULTS[permission],
51
-    )
52
-  }
53
-
54
-  request = (permission: string, options?: Options): Promise<Status> => {
55 49
     let type
56 50
 
57 51
     if (typeof options === 'string') {
@@ -60,6 +54,13 @@ class ReactNativePermissions {
60 54
       type = options.type
61 55
     }
62 56
 
57
+    return PermissionsIOS.getPermissionStatus(
58
+      permission,
59
+      type || DEFAULTS[permission],
60
+    )
61
+  }
62
+
63
+  request = (permission: string, options?: RequestOptions): Promise<Status> => {
63 64
     if (!permissionTypes.includes(permission)) {
64 65
       const error = new Error(
65 66
         `ReactNativePermissions: ${
@@ -78,6 +79,14 @@ class ReactNativePermissions {
78 79
       return Promise.reject(error)
79 80
     }
80 81
 
82
+    let type
83
+
84
+    if (typeof options === 'string') {
85
+      type = options
86
+    } else if (options && options.type) {
87
+      type = options.type
88
+    }
89
+
81 90
     return PermissionsIOS.requestPermission(
82 91
       permission,
83 92
       type || DEFAULTS[permission],

+ 3
- 3
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "react-native-permissions",
3
-  "version": "1.0.4",
3
+  "version": "1.0.5",
4 4
   "description": "Check user permissions in React Native",
5 5
   "author": "Yonah Forst <yonaforst@hotmail.com>",
6 6
   "homepage": "https://github.com/yonahforst/react-native-permissions",
@@ -21,7 +21,7 @@
21 21
   "devDependencies": {
22 22
     "flow-bin": "^0.57.3",
23 23
     "husky": "^0.14.3",
24
-    "lint-staged": "^5.0.0",
25
-    "prettier": "^1.8.2"
24
+    "lint-staged": "^6.0.0",
25
+    "prettier": "^1.9.2"
26 26
   }
27 27
 }