OSDN Git Service

add Connection class
authorYongfeng LI <wliyongfeng@gmail.com>
Fri, 11 May 2018 03:11:14 +0000 (11:11 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Fri, 11 May 2018 03:11:14 +0000 (11:11 +0800)
package.json
src/client.js
src/connection.js [new file with mode: 0644]
yarn.lock

index 2203d7b..a22f53e 100644 (file)
@@ -10,5 +10,9 @@
   "devDependencies": {
     "babel-cli": "^6.26.0",
     "babel-preset-es2015": "^6.24.1"
+  },
+  "dependencies": {
+    "axios": "^0.18.0",
+    "btoa": "^1.2.1"
   }
 }
index ced8b03..3bfb79a 100644 (file)
@@ -1,5 +1,9 @@
+import Connection from 'connection'
+
 class Client {
-  // TODO: to be implementd
+  constructor(baseUrl, token) {
+    this.connection = new Connection(baseUrl, token)
+  }
 }
 
 export default Client
diff --git a/src/connection.js b/src/connection.js
new file mode 100644 (file)
index 0000000..86589ba
--- /dev/null
@@ -0,0 +1,29 @@
+import axios from 'axios'
+import btoa from 'btoa'
+
+class Connection {
+  constructor(baseUrl, token = '') {
+    this.baseUrl = baseUrl
+    this.token = token
+  }
+
+  request(path, body = {}) {
+    let config = {
+      url: `${this.baseUrl}${path}`,
+      method: 'post',
+      headers: {
+        Accept: 'application/json',
+      },
+      data: body,
+      timeout: 1000
+    }
+
+    if (this.token) {
+      config.headers['Authorization'] = `Basic ${btoa(this.token)}`
+    }
+
+    return axios.request(config)
+  }
+}
+
+export default Connection
index d660b31..c41511f 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -50,6 +50,13 @@ async-each@^1.0.0:
   version "1.0.1"
   resolved "http://registry.npm.taobao.org/async-each/download/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"
 
+axios@^0.18.0:
+  version "0.18.0"
+  resolved "http://registry.npm.taobao.org/axios/download/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
+  dependencies:
+    follow-redirects "^1.3.0"
+    is-buffer "^1.1.5"
+
 babel-cli@^6.26.0:
   version "6.26.0"
   resolved "http://registry.npm.taobao.org/babel-cli/download/babel-cli-6.26.0.tgz#502ab54874d7db88ad00b887a06383ce03d002f1"
@@ -500,6 +507,10 @@ braces@^1.8.2:
     preserve "^0.2.0"
     repeat-element "^1.1.2"
 
+btoa@^1.2.1:
+  version "1.2.1"
+  resolved "http://registry.npm.taobao.org/btoa/download/btoa-1.2.1.tgz#01a9909f8b2c93f6bf680ba26131eb30f7fa3d73"
+
 chalk@^1.1.3:
   version "1.1.3"
   resolved "http://registry.npm.taobao.org/chalk/download/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@@ -563,6 +574,12 @@ debug@^2.1.2, debug@^2.6.8, debug@^2.6.9:
   dependencies:
     ms "2.0.0"
 
+debug@^3.1.0:
+  version "3.1.0"
+  resolved "http://registry.npm.taobao.org/debug/download/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
+  dependencies:
+    ms "2.0.0"
+
 deep-extend@^0.5.1:
   version "0.5.1"
   resolved "http://registry.npm.taobao.org/deep-extend/download/deep-extend-0.5.1.tgz#b894a9dd90d3023fbf1c55a394fb858eb2066f1f"
@@ -621,6 +638,12 @@ fill-range@^2.1.0:
     repeat-element "^1.1.2"
     repeat-string "^1.5.2"
 
+follow-redirects@^1.3.0:
+  version "1.4.1"
+  resolved "http://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.4.1.tgz#d8120f4518190f55aac65bb6fc7b85fcd666d6aa"
+  dependencies:
+    debug "^3.1.0"
+
 for-in@^1.0.1:
   version "1.0.2"
   resolved "http://registry.npm.taobao.org/for-in/download/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"