Browse Source

Improve connection manager to match linter

Arnaud Vergnet 1 year ago
parent
commit
3629c5730a
1 changed files with 152 additions and 145 deletions
  1. 152
    145
      src/managers/ConnectionManager.js

+ 152
- 145
src/managers/ConnectionManager.js View File

@@ -1,7 +1,8 @@
1 1
 // @flow
2 2
 
3 3
 import * as Keychain from 'react-native-keychain';
4
-import {apiRequest, ERROR_TYPE} from "../utils/WebData";
4
+import type {ApiDataLoginType, ApiGenericDataType} from '../utils/WebData';
5
+import {apiRequest, ERROR_TYPE} from '../utils/WebData';
5 6
 
6 7
 /**
7 8
  * champ: error
@@ -14,161 +15,167 @@ import {apiRequest, ERROR_TYPE} from "../utils/WebData";
14 15
  * 500 : SERVER_ERROR -> pb coté serveur
15 16
  */
16 17
 
17
-const SERVER_NAME = "amicale-insat.fr";
18
-const AUTH_PATH = "password";
18
+const SERVER_NAME = 'amicale-insat.fr';
19
+const AUTH_PATH = 'password';
19 20
 
20 21
 export default class ConnectionManager {
21
-    static instance: ConnectionManager | null = null;
22
+  static instance: ConnectionManager | null = null;
22 23
 
23
-    #email: string;
24
-    #token: string | null;
24
+  #email: string;
25 25
 
26
-    constructor() {
27
-        this.#token = null;
28
-    }
26
+  #token: string | null;
29 27
 
30
-    /**
31
-     * Gets this class instance or create one if none is found
32
-     *
33
-     * @returns {ConnectionManager}
34
-     */
35
-    static getInstance(): ConnectionManager {
36
-        return ConnectionManager.instance === null ?
37
-            ConnectionManager.instance = new ConnectionManager() :
38
-            ConnectionManager.instance;
39
-    }
28
+  constructor() {
29
+    this.#token = null;
30
+  }
40 31
 
41
-    /**
42
-     * Gets the current token
43
-     *
44
-     * @returns {string | null}
45
-     */
46
-    getToken(): string | null {
47
-        return this.#token;
48
-    }
32
+  /**
33
+   * Gets this class instance or create one if none is found
34
+   *
35
+   * @returns {ConnectionManager}
36
+   */
37
+  static getInstance(): ConnectionManager {
38
+    if (ConnectionManager.instance == null)
39
+      ConnectionManager.instance = new ConnectionManager();
40
+    return ConnectionManager.instance;
41
+  }
49 42
 
50
-    /**
51
-     * Tries to recover login token from the secure keychain
52
-     *
53
-     * @returns {Promise<R>}
54
-     */
55
-    async recoverLogin() {
56
-        return new Promise((resolve, reject) => {
57
-            if (this.getToken() !== null)
58
-                resolve(this.getToken());
59
-            else {
60
-                Keychain.getInternetCredentials(SERVER_NAME)
61
-                    .then((data) => {
62
-                        if (data) {
63
-                            this.#token = data.password;
64
-                            resolve(this.#token);
65
-                        } else
66
-                            reject(false);
67
-                    })
68
-                    .catch(() => {
69
-                        reject(false);
70
-                    });
71
-            }
72
-        });
73
-    }
43
+  /**
44
+   * Gets the current token
45
+   *
46
+   * @returns {string | null}
47
+   */
48
+  getToken(): string | null {
49
+    return this.#token;
50
+  }
74 51
 
75
-    /**
76
-     * Check if the user has a valid token
77
-     *
78
-     * @returns {boolean}
79
-     */
80
-    isLoggedIn() {
81
-        return this.getToken() !== null;
82
-    }
52
+  /**
53
+   * Tries to recover login token from the secure keychain
54
+   *
55
+   * @returns Promise<string>
56
+   */
57
+  async recoverLogin(): Promise<string> {
58
+    return new Promise(
59
+      (resolve: (token: string) => void, reject: () => void) => {
60
+        const token = this.getToken();
61
+        if (token != null) resolve(token);
62
+        else {
63
+          Keychain.getInternetCredentials(SERVER_NAME)
64
+            .then((data: Keychain.UserCredentials | false) => {
65
+              if (
66
+                data != null &&
67
+                data.password != null &&
68
+                typeof data.password === 'string'
69
+              ) {
70
+                this.#token = data.password;
71
+                resolve(this.#token);
72
+              } else reject();
73
+            })
74
+            .catch((): void => reject());
75
+        }
76
+      },
77
+    );
78
+  }
83 79
 
84
-    /**
85
-     * Saves the login token in the secure keychain
86
-     *
87
-     * @param email
88
-     * @param token
89
-     * @returns {Promise<R>}
90
-     */
91
-    async saveLogin(email: string, token: string) {
92
-        return new Promise((resolve, reject) => {
93
-            Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
94
-                .then(() => {
95
-                    this.#token = token;
96
-                    this.#email = email;
97
-                    resolve(true);
98
-                })
99
-                .catch(() => {
100
-                    reject(false);
101
-                });
102
-        });
103
-    }
80
+  /**
81
+   * Check if the user has a valid token
82
+   *
83
+   * @returns {boolean}
84
+   */
85
+  isLoggedIn(): boolean {
86
+    return this.getToken() !== null;
87
+  }
104 88
 
105
-    /**
106
-     * Deletes the login token from the keychain
107
-     *
108
-     * @returns {Promise<R>}
109
-     */
110
-    async disconnect() {
111
-        return new Promise((resolve, reject) => {
112
-            Keychain.resetInternetCredentials(SERVER_NAME)
113
-                .then(() => {
114
-                    this.#token = null;
115
-                    resolve(true);
116
-                })
117
-                .catch(() => {
118
-                    reject(false);
119
-                });
120
-        });
121
-    }
89
+  /**
90
+   * Saves the login token in the secure keychain
91
+   *
92
+   * @param email
93
+   * @param token
94
+   * @returns Promise<void>
95
+   */
96
+  async saveLogin(email: string, token: string): Promise<void> {
97
+    return new Promise((resolve: () => void, reject: () => void) => {
98
+      Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
99
+        .then(() => {
100
+          this.#token = token;
101
+          this.#email = email;
102
+          resolve();
103
+        })
104
+        .catch((): void => reject());
105
+    });
106
+  }
122 107
 
108
+  /**
109
+   * Deletes the login token from the keychain
110
+   *
111
+   * @returns Promise<void>
112
+   */
113
+  async disconnect(): Promise<void> {
114
+    return new Promise((resolve: () => void, reject: () => void) => {
115
+      Keychain.resetInternetCredentials(SERVER_NAME)
116
+        .then(() => {
117
+          this.#token = null;
118
+          resolve();
119
+        })
120
+        .catch((): void => reject());
121
+    });
122
+  }
123 123
 
124
-    /**
125
-     * Sends the given login and password to the api.
126
-     * If the combination is valid, the login token is received and saved in the secure keychain.
127
-     * If not, the promise is rejected with the corresponding error code.
128
-     *
129
-     * @param email
130
-     * @param password
131
-     * @returns {Promise<R>}
132
-     */
133
-    async connect(email: string, password: string) {
134
-        return new Promise((resolve, reject) => {
135
-            const data = {
136
-                email: email,
137
-                password: password,
138
-            };
139
-            apiRequest(AUTH_PATH, 'POST', data)
140
-                .then((response) => {
141
-                    this.saveLogin(email, response.token)
142
-                        .then(() => {
143
-                            resolve(true);
144
-                        })
145
-                        .catch(() => {
146
-                            reject(ERROR_TYPE.TOKEN_SAVE);
147
-                        });
148
-                })
149
-                .catch((error) => reject(error));
150
-        });
151
-    }
124
+  /**
125
+   * Sends the given login and password to the api.
126
+   * If the combination is valid, the login token is received and saved in the secure keychain.
127
+   * If not, the promise is rejected with the corresponding error code.
128
+   *
129
+   * @param email
130
+   * @param password
131
+   * @returns Promise<void>
132
+   */
133
+  async connect(email: string, password: string): Promise<void> {
134
+    return new Promise(
135
+      (resolve: () => void, reject: (error: number) => void) => {
136
+        const data = {
137
+          email,
138
+          password,
139
+        };
140
+        apiRequest(AUTH_PATH, 'POST', data)
141
+          .then((response: ApiDataLoginType) => {
142
+            if (response.token != null) {
143
+              this.saveLogin(email, response.token)
144
+                .then((): void => resolve())
145
+                .catch((): void => reject(ERROR_TYPE.TOKEN_SAVE));
146
+            } else reject(ERROR_TYPE.SERVER_ERROR);
147
+          })
148
+          .catch((error: number): void => reject(error));
149
+      },
150
+    );
151
+  }
152 152
 
153
-    /**
154
-     * Sends an authenticated request with the login token to the API
155
-     *
156
-     * @param path
157
-     * @param params
158
-     * @returns {Promise<R>}
159
-     */
160
-    async authenticatedRequest(path: string, params: Object) {
161
-        return new Promise((resolve, reject) => {
162
-            if (this.getToken() !== null) {
163
-                let data = {
164
-                    token: this.getToken(),
165
-                    ...params
166
-                };
167
-                apiRequest(path, 'POST', data)
168
-                    .then((response) => resolve(response))
169
-                    .catch((error) => reject(error));
170
-            } else
171
-                reject(ERROR_TYPE.TOKEN_RETRIEVE);
172
-        });
173
-    }
153
+  /**
154
+   * Sends an authenticated request with the login token to the API
155
+   *
156
+   * @param path
157
+   * @param params
158
+   * @returns Promise<ApiGenericDataType>
159
+   */
160
+  async authenticatedRequest(
161
+    path: string,
162
+    params: {...},
163
+  ): Promise<ApiGenericDataType> {
164
+    return new Promise(
165
+      (
166
+        resolve: (response: ApiGenericDataType) => void,
167
+        reject: (error: number) => void,
168
+      ) => {
169
+        if (this.getToken() !== null) {
170
+          const data = {
171
+            ...params,
172
+            token: this.getToken(),
173
+          };
174
+          apiRequest(path, 'POST', data)
175
+            .then((response: ApiGenericDataType): void => resolve(response))
176
+            .catch((error: number): void => reject(error));
177
+        } else reject(ERROR_TYPE.TOKEN_RETRIEVE);
178
+      },
179
+    );
180
+  }
174 181
 }

Loading…
Cancel
Save