diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/configs.js | 24 | ||||
| -rw-r--r-- | src/api/logs.js | 18 | ||||
| -rw-r--r-- | src/api/proxies.js | 30 | ||||
| -rw-r--r-- | src/api/traffic.js | 18 |
4 files changed, 25 insertions, 65 deletions
diff --git a/src/api/configs.js b/src/api/configs.js index 0690221..8889ad0 100644 --- a/src/api/configs.js +++ b/src/api/configs.js @@ -1,29 +1,19 @@ import { - getAPIConfig, + getURLAndInit, genCommonHeaders, getAPIBaseURL } from 'm/request-helper'; 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(apiConfig) { + const { url, init } = getURLAndInit(apiConfig); + return await fetch(url + endpoint, init); } -export async function fetchConfigs() { - const { url, init } = getURLAndInit(); - return await fetch(url, init); -} - -export async function updateConfigs(o) { - const { url, init } = getURLAndInit(); - return await fetch(url, { +export async function updateConfigs(apiConfig, o) { + const { url, init } = getURLAndInit(apiConfig); + return await fetch(url + endpoint, { ...init, method: 'PUT', // mode: 'cors', diff --git a/src/api/logs.js b/src/api/logs.js index 59d064a..948a23f 100644 --- a/src/api/logs.js +++ b/src/api/logs.js @@ -1,5 +1,5 @@ import { - getAPIConfig, + getURLAndInit, genCommonHeaders, getAPIBaseURL } from 'm/request-helper'; @@ -10,16 +10,6 @@ const getRandomStr = () => { return Math.floor((1 + Math.random()) * 0x10000).toString(16); }; -function getURLAndInit() { - const c = getAPIConfig(); - const baseURL = getAPIBaseURL(c); - const headers = genCommonHeaders(c); - return { - url: baseURL + endpoint, - init: { headers } - }; -} - const Size = 300; let even = false; @@ -71,11 +61,11 @@ function pump(reader) { }); } -function fetchLogs() { +function fetchLogs(apiConfig) { if (store.fetched) return store; store.fetched = true; - const { url, init } = getURLAndInit(); - fetch(url, init) + const { url, init } = getURLAndInit(apiConfig); + fetch(url + endpoint, init) .then(response => { const reader = response.body.getReader(); pump(reader); diff --git a/src/api/proxies.js b/src/api/proxies.js index 592a38b..a38d92d 100644 --- a/src/api/proxies.js +++ b/src/api/proxies.js @@ -1,20 +1,10 @@ import { - getAPIConfig, + getURLAndInit, genCommonHeaders, getAPIBaseURL } from 'm/request-helper'; const endpoint = '/proxies'; -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 HTTP/1.1 400 Bad Request @@ -32,16 +22,16 @@ Vary: Origin Date: Tue, 16 Oct 2018 16:38:33 GMT */ -async function fetchProxies() { - const { url, init } = getURLAndInit(); - const res = await fetch(url, init); +async function fetchProxies(config) { + const { url, init } = getURLAndInit(config); + const res = await fetch(url + endpoint, init); return await res.json(); } -async function requestToSwitchProxy(name1, name2) { +async function requestToSwitchProxy(apiConfig, name1, name2) { const body = { name: name2 }; - const { url, init } = getURLAndInit(); - const fullURL = `${url}/${name1}`; + const { url, init } = getURLAndInit(apiConfig); + const fullURL = `${url}${endpoint}/${name1}`; return await fetch(fullURL, { ...init, method: 'PUT', @@ -49,10 +39,10 @@ async function requestToSwitchProxy(name1, name2) { }); } -async function requestDelayForProxy(name) { - const { url, init } = getURLAndInit(); +async function requestDelayForProxy(apiConfig, name) { + const { url, init } = getURLAndInit(apiConfig); const qs = 'timeout=5000&url=http://www.google.com/generate_204'; - const fullURL = `${url}/${name}/delay?${qs}`; + const fullURL = `${url}${endpoint}/${name}/delay?${qs}`; return await fetch(fullURL, init); } diff --git a/src/api/traffic.js b/src/api/traffic.js index 12c88dc..8d1e3b7 100644 --- a/src/api/traffic.js +++ b/src/api/traffic.js @@ -1,21 +1,11 @@ import { - getAPIConfig, + getURLAndInit, genCommonHeaders, getAPIBaseURL } from 'm/request-helper'; const endpoint = '/traffic'; const textDecoder = new TextDecoder('utf-8', { stream: true }); -function getURLAndInit() { - const c = getAPIConfig(); - const baseURL = getAPIBaseURL(c); - const headers = genCommonHeaders(c); - return { - url: baseURL + endpoint, - init: { headers } - }; -} - const Size = 150; const traffic = { @@ -69,10 +59,10 @@ function pump(reader) { }); } -function fetchData() { +function fetchData(apiConfig) { if (fetched) return traffic; - const { url, init } = getURLAndInit(); - fetch(url, init).then(response => { + const { url, init } = getURLAndInit(apiConfig); + fetch(url + endpoint, init).then(response => { if (response.ok) { fetched = true; const reader = response.body.getReader(); |
