Browse Source

Check can now accept a string or an object

Mathieu Acthernoene 8 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

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

+ 1 - 1
README.md

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

+ 4 - 3
lib/permissions.android.js

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

+ 18 - 9
lib/permissions.ios.js

@@ -5,7 +5,8 @@ const PermissionsIOS = NativeModules.ReactNativePermissions
 
 type Status = 'authorized' | 'denied' | 'restricted' | 'undetermined'
 type Rationale = { title: string, message: string }
-type Options = string | { type: string, rationale?: Rationale }
+type CheckOptions = string | { type: string }
+type RequestOptions = string | { type: string, rationale?: Rationale }
 
 const permissionTypes = [
   'location',
@@ -34,7 +35,7 @@ class ReactNativePermissions {
 
   getTypes: () => Array<string> = () => permissionTypes
 
-  check = (permission: string, type?: string): Promise<Status> => {
+  check = (permission: string, options?: CheckOptions): Promise<Status> => {
     if (!permissionTypes.includes(permission)) {
       const error = new Error(
         `ReactNativePermissions: ${
@@ -45,13 +46,6 @@ class ReactNativePermissions {
       return Promise.reject(error)
     }
 
-    return PermissionsIOS.getPermissionStatus(
-      permission,
-      type || DEFAULTS[permission],
-    )
-  }
-
-  request = (permission: string, options?: Options): Promise<Status> => {
     let type
 
     if (typeof options === 'string') {
@@ -60,6 +54,13 @@ class ReactNativePermissions {
       type = options.type
     }
 
+    return PermissionsIOS.getPermissionStatus(
+      permission,
+      type || DEFAULTS[permission],
+    )
+  }
+
+  request = (permission: string, options?: RequestOptions): Promise<Status> => {
     if (!permissionTypes.includes(permission)) {
       const error = new Error(
         `ReactNativePermissions: ${
@@ -78,6 +79,14 @@ class ReactNativePermissions {
       return Promise.reject(error)
     }
 
+    let type
+
+    if (typeof options === 'string') {
+      type = options
+    } else if (options && options.type) {
+      type = options.type
+    }
+
     return PermissionsIOS.requestPermission(
       permission,
       type || DEFAULTS[permission],

+ 3 - 3
package.json

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