diff options
| author | Haishan <[email protected]> | 2018-10-21 00:38:13 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2018-10-21 00:38:13 +0800 |
| commit | 380f5ad582325f1e736f897ac9c779bfd36f8035 (patch) | |
| tree | 73db0308056ff1d4e331ed0c30b62561aae68f8c /src/api | |
| parent | 0d5cc1e0ae844bf52e7164b4b535a3bd93102381 (diff) | |
feat: support authorization secret
* also make redux store the single truth store for API information
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/configs.js | 30 | ||||
| -rw-r--r-- | src/api/logs.js | 30 | ||||
| -rw-r--r-- | src/api/proxies.js | 37 | ||||
| -rw-r--r-- | src/api/traffic.js | 21 |
4 files changed, 82 insertions, 36 deletions
diff --git a/src/api/configs.js b/src/api/configs.js index a9ae0e3..e3cbebc 100644 --- a/src/api/configs.js +++ b/src/api/configs.js @@ -1,22 +1,34 @@ 'use strict'; -const { getAPIURL } = require('../config'); +import { + getAPIConfig, + genCommonHeaders, + getAPIBaseURL +} from 'm/request-helper'; -const headers = { - 'Content-Type': 'application/json' -}; +const endpoint = '/configs'; + +function getURLAndInit() { + const c = getAPIConfig(); + const baseURL = getAPIBaseURL(c); + const headers = genCommonHeaders(c); + return { + url: baseURL + endpoint, + init: { headers } + }; +} export async function fetchConfigs() { - const apiURL = getAPIURL(); - return await fetch(apiURL.configs); + const { url, init } = getURLAndInit(); + return await fetch(url, init); } export async function updateConfigs(o) { - const apiURL = getAPIURL(); - return await fetch(apiURL.configs, { + const { url, init } = getURLAndInit(); + return await fetch(url, { + ...init, method: 'PUT', // mode: 'cors', - headers, body: JSON.stringify(o) }); } diff --git a/src/api/logs.js b/src/api/logs.js index 9bcab58..f158830 100644 --- a/src/api/logs.js +++ b/src/api/logs.js @@ -1,7 +1,22 @@ 'use strict'; -const { getAPIURL } = require('../config'); const textDecoder = new TextDecoder('utf-8'); +import { + getAPIConfig, + genCommonHeaders, + getAPIBaseURL +} from 'm/request-helper'; +const endpoint = '/logs'; + +function getURLAndInit() { + const c = getAPIConfig(); + const baseURL = getAPIBaseURL(c); + const headers = genCommonHeaders(c); + return { + url: baseURL + endpoint, + init: { headers } + }; +} const Size = 300; @@ -35,14 +50,7 @@ function pump(reader) { try { o = JSON.parse(t); } catch (err) { - console.log( - 'lastchar', - t.length, - ' is r', - l === '\r', - ' is n', - l === '\n' - ); + console.log(err); } store.appendData(o); return pump(reader); @@ -52,8 +60,8 @@ function pump(reader) { let fetched = false; function fetchLogs() { if (fetched) return store; - const apiURL = getAPIURL(); - fetch(apiURL.logs).then(response => { + const { url, init } = getURLAndInit(); + fetch(url, init).then(response => { fetched = true; const reader = response.body.getReader(); pump(reader); diff --git a/src/api/proxies.js b/src/api/proxies.js index 1789f67..60a8f7f 100644 --- a/src/api/proxies.js +++ b/src/api/proxies.js @@ -1,10 +1,21 @@ 'use strict'; -const { getAPIURL } = require('../config'); +import { + getAPIConfig, + genCommonHeaders, + getAPIBaseURL +} from 'm/request-helper'; +const endpoint = '/proxies'; -const headers = { - 'Content-Type': 'application/json' -}; +function getURLAndInit() { + const c = getAPIConfig(); + const baseURL = getAPIBaseURL(c); + const headers = genCommonHeaders(c); + return { + url: baseURL + endpoint, + init: { headers } + }; +} /* $ curl "http://127.0.0.1:8080/proxies/Proxy" -XPUT -d '{ "name": "ss3" }' -i @@ -24,27 +35,27 @@ Date: Tue, 16 Oct 2018 16:38:33 GMT */ async function fetchProxies() { - const apiURL = getAPIURL(); - const res = await fetch(apiURL.proxies); + const { url, init } = getURLAndInit(); + const res = await fetch(url, init); return await res.json(); } async function requestToSwitchProxy(name1, name2) { const body = { name: name2 }; - const apiURL = getAPIURL(); - const url = `${apiURL.proxies}/${name1}`; - return await fetch(url, { + const { url, init } = getURLAndInit(); + const fullURL = `${url}/${name1}`; + return await fetch(fullURL, { + ...init, method: 'PUT', - headers, body: JSON.stringify(body) }); } async function requestDelayForProxy(name) { - const apiURL = getAPIURL(); + const { url, init } = getURLAndInit(); const qs = `timeout=5000&url=http://www.google.com/generate_204`; - const url = `${apiURL.proxies}/${name}/delay?${qs}`; - return await fetch(url); + const fullURL = `${url}/${name}/delay?${qs}`; + return await fetch(fullURL, init); } export { fetchProxies, requestToSwitchProxy, requestDelayForProxy }; diff --git a/src/api/traffic.js b/src/api/traffic.js index 53330a5..434384a 100644 --- a/src/api/traffic.js +++ b/src/api/traffic.js @@ -1,7 +1,22 @@ 'use strict'; -const { getAPIURL } = require('../config'); const textDecoder = new TextDecoder('utf-8'); +import { + getAPIConfig, + genCommonHeaders, + getAPIBaseURL +} from 'm/request-helper'; +const endpoint = '/traffic'; + +function getURLAndInit() { + const c = getAPIConfig(); + const baseURL = getAPIBaseURL(c); + const headers = genCommonHeaders(c); + return { + url: baseURL + endpoint, + init: { headers } + }; +} const Size = 150; @@ -55,8 +70,8 @@ function pump(reader) { let fetched = false; function fetchData() { if (fetched) return traffic; - const apiURL = getAPIURL(); - fetch(apiURL.traffic).then(response => { + const { url, init } = getURLAndInit(); + fetch(url, init).then(response => { fetched = true; const reader = response.body.getReader(); pump(reader); |
