feat: init new response state system

This commit is contained in:
liyasthomas
2021-07-13 11:07:29 +05:30
parent ffc891f38e
commit a4032836c3
20 changed files with 620 additions and 387 deletions

View File

@@ -7,13 +7,14 @@ import xmlLens from "./xmlLens"
export const lenses = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
export function getSuitableLenses(response) {
if (!response || !response.headers || !response.headers["content-type"])
return [rawLens]
const contentType = response.headers.find((h) => h.key === "content-type")
console.log(contentType)
if (!contentType) return [rawLens]
const result = []
for (const lens of lenses) {
if (lens.isSupportedContentType(response.headers["content-type"]))
result.push(lens)
if (lens.isSupportedContentType(contentType.value)) result.push(lens)
}
return result

View File

@@ -1,3 +1,4 @@
import { BehaviorSubject, Observable } from "rxjs"
import AxiosStrategy, {
cancelRunningAxiosRequest,
} from "./strategies/AxiosStrategy"
@@ -5,6 +6,8 @@ import ExtensionStrategy, {
cancelRunningExtensionRequest,
hasExtensionInstalled,
} from "./strategies/ExtensionStrategy"
import { HoppRESTResponse } from "./types/HoppRESTResponse"
import { EffectiveHoppRESTRequest } from "./utils/EffectiveURL"
import { settingsStore } from "~/newstore/settings"
export const cancelRunningRequest = () => {
@@ -17,7 +20,7 @@ export const cancelRunningRequest = () => {
const isExtensionsAllowed = () => settingsStore.value.EXTENSIONS_ENABLED
const runAppropriateStrategy = (req) => {
const runAppropriateStrategy = (req: any) => {
if (isExtensionsAllowed() && hasExtensionInstalled()) {
return ExtensionStrategy(req)
}
@@ -41,5 +44,37 @@ export function getCurrentStrategyID() {
}
}
export const sendNetworkRequest = (req) =>
export const sendNetworkRequest = (req: any) =>
runAppropriateStrategy(req).finally(() => window.$nuxt.$loading.finish())
export function createRESTNetworkRequestStream(
req: EffectiveHoppRESTRequest
): Observable<HoppRESTResponse> {
const response = new BehaviorSubject<HoppRESTResponse>({ type: "loading" })
runAppropriateStrategy({
url: req.effectiveFinalURL,
}).then((res: any) => {
console.log(res)
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: {
// TODO: Implement
responseSize: 0,
responseDuration: 0,
},
}
response.next(resObj)
response.complete()
})
return response
}

View File

@@ -0,0 +1,22 @@
export type HoppRESTResponse =
| { type: "loading" }
| {
type: "fail"
headers: { key: string; value: string }[]
body: ArrayBuffer
statusCode: number
}
| {
type: "network_fail"
error: Error
}
| {
type: "success"
headers: { key: string; value: string }[]
body: ArrayBuffer
statusCode: number
meta: {
responseSize: number // in bytes
responseDuration: number // in millis
}
}

View File

@@ -3,7 +3,7 @@ import { map } from "rxjs/operators"
import { HoppRESTRequest } from "../types/HoppRESTRequest"
import { Environment } from "~/newstore/environments"
interface EffectiveHoppRESTRequest extends HoppRESTRequest {
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
/**
* The effective final URL.
*