Quellcode durchsuchen

feat: returns response or error in request notification. bump version to 0.2.2

Acathur vor 4 Jahren
Ursprung
Commit
c70e605fd5
5 geänderte Dateien mit 33 neuen und 19 gelöschten Zeilen
  1. 15 8
      dist/request/index.js
  2. 1 1
      dist/types/global.d.ts
  3. 1 1
      package.json
  4. 15 8
      src/request/index.ts
  5. 1 1
      src/types/global.ts

+ 15 - 8
dist/request/index.js

@@ -29,13 +29,20 @@ const factory = (opts) => {
                 'Content-Type': contentType
             });
         }
-        const res = yield axios({
-            url,
-            method,
-            params: query,
-            headers,
-            data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
-        });
+        let res;
+        try {
+            res = yield axios({
+                url,
+                method,
+                params: query,
+                headers,
+                data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
+            });
+        }
+        catch (e) {
+            notifier && notifier(e.message, e.status, e);
+            return e;
+        }
         if (res.data) {
             const { status, data } = res.data;
             // require login
@@ -44,7 +51,7 @@ const factory = (opts) => {
             }
             else if (status !== 1 && status !== 200) {
                 const message = data && data.message || res.data.message || res.data.info;
-                message && notifier && notifier(message, status);
+                message && notifier && notifier(message, status, res);
             }
         }
         return res;

+ 1 - 1
dist/types/global.d.ts

@@ -1 +1 @@
-export declare type Notifier = (msg: string, status?: string | number) => void;
+export declare type Notifier = (msg: string, status?: string | number, resOrErr?: any) => void;

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "proginn-lib",
-  "version": "0.2.1",
+  "version": "0.2.2",
   "description": "Proginn front-end common library.",
   "main": "dist/index.js",
   "module": "dist/index.js",

+ 15 - 8
src/request/index.ts

@@ -45,13 +45,20 @@ const factory = (opts: {
       })
     }
 
-    const res = await axios({
-      url,
-      method,
-      params: query,
-      headers,
-      data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
-    })
+    let res: AxiosResponse<any>
+
+    try {
+      res = await axios({
+        url,
+        method,
+        params: query,
+        headers,
+        data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
+      })
+    } catch (e) {
+      notifier && notifier(e.message, e.status, e)
+      return e
+    }
 
     if (res.data) {
       const { status, data } = res.data
@@ -62,7 +69,7 @@ const factory = (opts: {
       } else if (status !== 1 && status !== 200) {
         const message = data && data.message || res.data.message || res.data.info
 
-        message && notifier && notifier(message, status)
+        message && notifier && notifier(message, status, res)
       }
     }
 

+ 1 - 1
src/types/global.ts

@@ -1 +1 @@
-export type Notifier = (msg: string, status?: string | number) => void
+export type Notifier = (msg: string, status?: string | number, resOrErr?: any) => void