ソースを参照

bump version to 0.3.2

Acathur 4 年 前
コミット
d318ee9a6a
4 ファイル変更23 行追加9 行削除
  1. 1 0
      dist/request/index.d.ts
  2. 7 4
      dist/request/index.js
  3. 1 1
      package.json
  4. 14 4
      src/request/index.ts

+ 1 - 0
dist/request/index.d.ts

@@ -7,6 +7,7 @@ declare const ProginnRequest: {
         bridge?: ProginnBridge;
         notifier?: Notifier;
         withCredentials?: boolean;
+        defaultDataType?: 'json' | 'form' | false;
     }) => (opts: {
         method: 'GET' | 'POST';
         url: string;

+ 7 - 4
dist/request/index.js

@@ -10,21 +10,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
 import Axios from 'axios';
 import qs from 'querystring';
 const factory = (opts) => {
-    const { baseURL, notifier, bridge, withCredentials = true } = opts || {};
+    const { baseURL, notifier, bridge, withCredentials = true, defaultDataType = 'form' } = opts || {};
     const axios = Axios.create({
         baseURL,
         withCredentials
     });
     return (opts) => __awaiter(void 0, void 0, void 0, function* () {
-        const { url, method, query, data, dataType = 'form', headers = {} } = opts;
+        const { url, method, query, data, dataType = defaultDataType, headers = {} } = opts;
         let contentType;
+        let formattedData;
         if (dataType === 'form') {
             contentType = 'application/x-www-form-urlencoded';
+            formattedData = data && qs.stringify(data);
         }
         else if (dataType === 'json') {
             contentType = 'application/json';
+            formattedData = data && JSON.stringify(data);
         }
-        if (method === 'POST') {
+        if (method === 'POST' && contentType) {
             Object.assign(headers, {
                 'Content-Type': contentType
             });
@@ -36,7 +39,7 @@ const factory = (opts) => {
                 method,
                 params: query,
                 headers,
-                data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
+                data: formattedData
             });
         }
         catch (e) {

+ 1 - 1
package.json

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

+ 14 - 4
src/request/index.ts

@@ -8,8 +8,15 @@ const factory = (opts: {
   bridge?: ProginnBridge
   notifier?: Notifier
   withCredentials?: boolean
+  defaultDataType?: 'json' | 'form' | false
 }) => {
-  const { baseURL, notifier, bridge, withCredentials = true } = opts || {}
+  const {
+    baseURL,
+    notifier,
+    bridge,
+    withCredentials = true,
+    defaultDataType = 'form'
+  } = opts || {}
   const axios = Axios.create({
     baseURL,
     withCredentials
@@ -28,19 +35,22 @@ const factory = (opts: {
       method,
       query,
       data,
-      dataType = 'form',
+      dataType = defaultDataType,
       headers = {}
     } = opts
 
     let contentType!: string
+    let formattedData!: any
 
     if (dataType === 'form') {
       contentType = 'application/x-www-form-urlencoded'
+      formattedData = data && qs.stringify(data)
     } else if (dataType === 'json') {
       contentType = 'application/json'
+      formattedData = data && JSON.stringify(data)
     }
 
-    if (method === 'POST') {
+    if (method === 'POST' && contentType) {
       Object.assign(headers, {
         'Content-Type': contentType
       })
@@ -54,7 +64,7 @@ const factory = (opts: {
         method,
         params: query,
         headers,
-        data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
+        data: formattedData
       })
     } catch (e) {
       notifier && notifier(e.message, e.status, e)