2021-07-14 19:36:15 +00:00
|
|
|
import { AxiosRequestConfig } from "axios"
|
2021-07-13 05:37:29 +00:00
|
|
|
import { BehaviorSubject, Observable } from "rxjs"
|
2021-12-04 14:17:57 +00:00
|
|
|
import cloneDeep from "lodash/cloneDeep"
|
2021-05-18 09:27:29 +00:00
|
|
|
import AxiosStrategy, {
|
|
|
|
|
cancelRunningAxiosRequest,
|
|
|
|
|
} from "./strategies/AxiosStrategy"
|
2020-05-30 22:28:13 +00:00
|
|
|
import ExtensionStrategy, {
|
|
|
|
|
cancelRunningExtensionRequest,
|
|
|
|
|
hasExtensionInstalled,
|
|
|
|
|
} from "./strategies/ExtensionStrategy"
|
2021-07-13 05:37:29 +00:00
|
|
|
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
|
|
|
|
import { EffectiveHoppRESTRequest } from "./utils/EffectiveURL"
|
2021-03-23 15:18:14 +00:00
|
|
|
import { settingsStore } from "~/newstore/settings"
|
2020-05-30 22:28:13 +00:00
|
|
|
|
2021-03-23 15:18:14 +00:00
|
|
|
export const cancelRunningRequest = () => {
|
|
|
|
|
if (isExtensionsAllowed() && hasExtensionInstalled()) {
|
2020-05-30 22:28:13 +00:00
|
|
|
cancelRunningExtensionRequest()
|
|
|
|
|
} else {
|
|
|
|
|
cancelRunningAxiosRequest()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-16 06:25:43 +00:00
|
|
|
|
2021-03-23 15:18:14 +00:00
|
|
|
const isExtensionsAllowed = () => settingsStore.value.EXTENSIONS_ENABLED
|
2020-02-04 18:13:20 +00:00
|
|
|
|
2021-07-14 19:36:15 +00:00
|
|
|
const runAppropriateStrategy = (req: AxiosRequestConfig) => {
|
2021-03-23 15:18:14 +00:00
|
|
|
if (isExtensionsAllowed() && hasExtensionInstalled()) {
|
|
|
|
|
return ExtensionStrategy(req)
|
2020-01-16 06:26:25 +00:00
|
|
|
}
|
2020-01-14 04:47:28 +00:00
|
|
|
|
2021-03-23 15:18:14 +00:00
|
|
|
return AxiosStrategy(req)
|
2020-02-24 18:44:50 +00:00
|
|
|
}
|
2020-01-16 06:25:43 +00:00
|
|
|
|
2021-07-06 00:02:29 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an identifier for how a request will be ran
|
|
|
|
|
* if the system is asked to fire a request
|
|
|
|
|
*
|
|
|
|
|
* @returns {"normal" | "extension" | "proxy"}
|
|
|
|
|
*/
|
|
|
|
|
export function getCurrentStrategyID() {
|
|
|
|
|
if (isExtensionsAllowed() && hasExtensionInstalled()) {
|
|
|
|
|
return "extension"
|
|
|
|
|
} else if (settingsStore.value.PROXY_ENABLED) {
|
|
|
|
|
return "proxy"
|
|
|
|
|
} else {
|
|
|
|
|
return "normal"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 05:37:29 +00:00
|
|
|
export const sendNetworkRequest = (req: any) =>
|
2021-03-23 15:18:14 +00:00
|
|
|
runAppropriateStrategy(req).finally(() => window.$nuxt.$loading.finish())
|
2021-07-13 05:37:29 +00:00
|
|
|
|
|
|
|
|
export function createRESTNetworkRequestStream(
|
2021-12-04 14:17:57 +00:00
|
|
|
request: EffectiveHoppRESTRequest
|
2021-07-13 05:37:29 +00:00
|
|
|
): Observable<HoppRESTResponse> {
|
2021-12-04 14:17:57 +00:00
|
|
|
const req = cloneDeep(request)
|
2021-07-15 04:10:45 +00:00
|
|
|
const response = new BehaviorSubject<HoppRESTResponse>({
|
|
|
|
|
type: "loading",
|
|
|
|
|
req,
|
|
|
|
|
})
|
2021-07-13 05:37:29 +00:00
|
|
|
|
2021-07-14 19:36:15 +00:00
|
|
|
const headers = req.effectiveFinalHeaders.reduce((acc, { key, value }) => {
|
|
|
|
|
return Object.assign(acc, { [key]: value })
|
|
|
|
|
}, {})
|
|
|
|
|
|
2021-07-20 22:53:24 +00:00
|
|
|
const params = req.effectiveFinalParams.reduce((acc, { key, value }) => {
|
|
|
|
|
return Object.assign(acc, { [key]: value })
|
|
|
|
|
}, {})
|
|
|
|
|
|
2021-07-14 18:11:52 +00:00
|
|
|
const timeStart = Date.now()
|
|
|
|
|
|
2021-07-13 05:37:29 +00:00
|
|
|
runAppropriateStrategy({
|
2021-07-15 04:10:45 +00:00
|
|
|
method: req.method as any,
|
2021-07-13 05:37:29 +00:00
|
|
|
url: req.effectiveFinalURL,
|
2021-07-14 19:36:15 +00:00
|
|
|
headers,
|
2021-07-20 22:53:24 +00:00
|
|
|
params,
|
2021-08-16 01:42:22 +00:00
|
|
|
data: req.effectiveFinalBody,
|
2021-07-13 05:37:29 +00:00
|
|
|
})
|
2021-07-15 04:10:45 +00:00
|
|
|
.then((res: any) => {
|
|
|
|
|
const timeEnd = Date.now()
|
|
|
|
|
|
|
|
|
|
const contentLength = res.headers["content-length"]
|
|
|
|
|
? parseInt(res.headers["content-length"])
|
|
|
|
|
: (res.data as ArrayBuffer).byteLength
|
|
|
|
|
|
|
|
|
|
const resObj: HoppRESTResponse = {
|
|
|
|
|
type: "success",
|
|
|
|
|
statusCode: res.status,
|
|
|
|
|
body: res.data,
|
|
|
|
|
headers: Object.keys(res.headers).map((x) => ({
|
|
|
|
|
key: x,
|
|
|
|
|
value: res.headers[x],
|
|
|
|
|
})),
|
|
|
|
|
meta: {
|
|
|
|
|
responseSize: contentLength,
|
|
|
|
|
responseDuration: timeEnd - timeStart,
|
|
|
|
|
},
|
|
|
|
|
req,
|
|
|
|
|
}
|
|
|
|
|
response.next(resObj)
|
|
|
|
|
|
|
|
|
|
response.complete()
|
|
|
|
|
})
|
2021-08-11 11:27:40 +00:00
|
|
|
.catch((e) => {
|
|
|
|
|
if (e.response) {
|
2021-07-15 04:10:45 +00:00
|
|
|
const timeEnd = Date.now()
|
|
|
|
|
|
2021-08-11 11:27:40 +00:00
|
|
|
const contentLength = e.response.headers["content-length"]
|
|
|
|
|
? parseInt(e.response.headers["content-length"])
|
|
|
|
|
: (e.response.data as ArrayBuffer).byteLength
|
2021-07-15 04:10:45 +00:00
|
|
|
|
|
|
|
|
const resObj: HoppRESTResponse = {
|
|
|
|
|
type: "fail",
|
2021-08-11 11:27:40 +00:00
|
|
|
body: e.response.data,
|
|
|
|
|
headers: Object.keys(e.response.headers).map((x) => ({
|
2021-07-15 04:10:45 +00:00
|
|
|
key: x,
|
2021-08-11 11:27:40 +00:00
|
|
|
value: e.response.headers[x],
|
2021-07-15 04:10:45 +00:00
|
|
|
})),
|
|
|
|
|
meta: {
|
|
|
|
|
responseDuration: timeEnd - timeStart,
|
|
|
|
|
responseSize: contentLength,
|
|
|
|
|
},
|
|
|
|
|
req,
|
2021-08-11 11:27:40 +00:00
|
|
|
statusCode: e.response.status,
|
2021-07-15 04:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.next(resObj)
|
|
|
|
|
|
|
|
|
|
response.complete()
|
|
|
|
|
} else {
|
|
|
|
|
const resObj: HoppRESTResponse = {
|
|
|
|
|
type: "network_fail",
|
2021-08-11 11:27:40 +00:00
|
|
|
error: e,
|
2021-07-15 04:10:45 +00:00
|
|
|
req,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.next(resObj)
|
|
|
|
|
|
|
|
|
|
response.complete()
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-07-13 05:37:29 +00:00
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
}
|