network.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { AxiosRequestConfig } from "axios"
  2. import { BehaviorSubject, Observable } from "rxjs"
  3. import AxiosStrategy, {
  4. cancelRunningAxiosRequest,
  5. } from "./strategies/AxiosStrategy"
  6. import ExtensionStrategy, {
  7. cancelRunningExtensionRequest,
  8. hasExtensionInstalled,
  9. } from "./strategies/ExtensionStrategy"
  10. import { HoppRESTResponse } from "./types/HoppRESTResponse"
  11. import { EffectiveHoppRESTRequest } from "./utils/EffectiveURL"
  12. import { settingsStore } from "~/newstore/settings"
  13. export const cancelRunningRequest = () => {
  14. if (isExtensionsAllowed() && hasExtensionInstalled()) {
  15. cancelRunningExtensionRequest()
  16. } else {
  17. cancelRunningAxiosRequest()
  18. }
  19. }
  20. const isExtensionsAllowed = () => settingsStore.value.EXTENSIONS_ENABLED
  21. const runAppropriateStrategy = (req: AxiosRequestConfig) => {
  22. if (isExtensionsAllowed() && hasExtensionInstalled()) {
  23. return ExtensionStrategy(req)
  24. }
  25. return AxiosStrategy(req)
  26. }
  27. /**
  28. * Returns an identifier for how a request will be ran
  29. * if the system is asked to fire a request
  30. *
  31. * @returns {"normal" | "extension" | "proxy"}
  32. */
  33. export function getCurrentStrategyID() {
  34. if (isExtensionsAllowed() && hasExtensionInstalled()) {
  35. return "extension"
  36. } else if (settingsStore.value.PROXY_ENABLED) {
  37. return "proxy"
  38. } else {
  39. return "normal"
  40. }
  41. }
  42. export const sendNetworkRequest = (req: any) =>
  43. runAppropriateStrategy(req).finally(() => window.$nuxt.$loading.finish())
  44. export function createRESTNetworkRequestStream(
  45. req: EffectiveHoppRESTRequest
  46. ): Observable<HoppRESTResponse> {
  47. const response = new BehaviorSubject<HoppRESTResponse>({
  48. type: "loading",
  49. req,
  50. })
  51. const headers = req.effectiveFinalHeaders.reduce((acc, { key, value }) => {
  52. return Object.assign(acc, { [key]: value })
  53. }, {})
  54. const params = req.effectiveFinalParams.reduce((acc, { key, value }) => {
  55. return Object.assign(acc, { [key]: value })
  56. }, {})
  57. const timeStart = Date.now()
  58. runAppropriateStrategy({
  59. method: req.method as any,
  60. url: req.effectiveFinalURL,
  61. headers,
  62. params,
  63. data: req.effectiveFinalBody,
  64. })
  65. .then((res: any) => {
  66. const timeEnd = Date.now()
  67. const contentLength = res.headers["content-length"]
  68. ? parseInt(res.headers["content-length"])
  69. : (res.data as ArrayBuffer).byteLength
  70. const resObj: HoppRESTResponse = {
  71. type: "success",
  72. statusCode: res.status,
  73. body: res.data,
  74. headers: Object.keys(res.headers).map((x) => ({
  75. key: x,
  76. value: res.headers[x],
  77. })),
  78. meta: {
  79. responseSize: contentLength,
  80. responseDuration: timeEnd - timeStart,
  81. },
  82. req,
  83. }
  84. response.next(resObj)
  85. response.complete()
  86. })
  87. .catch((e) => {
  88. if (e.response) {
  89. const timeEnd = Date.now()
  90. const contentLength = e.response.headers["content-length"]
  91. ? parseInt(e.response.headers["content-length"])
  92. : (e.response.data as ArrayBuffer).byteLength
  93. const resObj: HoppRESTResponse = {
  94. type: "fail",
  95. body: e.response.data,
  96. headers: Object.keys(e.response.headers).map((x) => ({
  97. key: x,
  98. value: e.response.headers[x],
  99. })),
  100. meta: {
  101. responseDuration: timeEnd - timeStart,
  102. responseSize: contentLength,
  103. },
  104. req,
  105. statusCode: e.response.status,
  106. }
  107. response.next(resObj)
  108. response.complete()
  109. } else {
  110. const resObj: HoppRESTResponse = {
  111. type: "network_fail",
  112. error: e,
  113. req,
  114. }
  115. response.next(resObj)
  116. response.complete()
  117. }
  118. })
  119. return response
  120. }