fix: wire response + init error handling

This commit is contained in:
liyasthomas
2021-07-15 09:40:45 +05:30
parent 86c9e09782
commit 3ef8e677c7
16 changed files with 283 additions and 152 deletions

View File

@@ -1,9 +1,10 @@
import { pluck, distinctUntilChanged, map } from "rxjs/operators"
import { pluck, distinctUntilChanged, map, filter } from "rxjs/operators"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
import {
HoppRESTHeader,
HoppRESTParam,
HoppRESTRequest,
RESTReqSchemaVersion,
} from "~/helpers/types/HoppRESTRequest"
import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
@@ -115,6 +116,7 @@ type RESTSession = {
const defaultRESTSession: RESTSession = {
request: {
v: RESTReqSchemaVersion,
endpoint: "https://httpbin.org/get",
params: [],
headers: [],
@@ -124,6 +126,11 @@ const defaultRESTSession: RESTSession = {
}
const dispatchers = defineDispatchers({
setRequest(_: RESTSession, { req }: { req: HoppRESTRequest }) {
return {
request: req,
}
},
setEndpoint(curr: RESTSession, { newEndpoint }: { newEndpoint: string }) {
const paramsInNewURL = getParamsInURL(newEndpoint)
const updatedParams = recalculateParams(
@@ -297,6 +304,15 @@ const dispatchers = defineDispatchers({
const restSessionStore = new DispatchingStore(defaultRESTSession, dispatchers)
export function setRESTRequest(req: HoppRESTRequest) {
restSessionStore.dispatch({
dispatcher: "setRequest",
payload: {
req,
},
})
}
export function setRESTEndpoint(newEndpoint: string) {
restSessionStore.dispatch({
dispatcher: "setEndpoint",
@@ -438,3 +454,10 @@ export const restResponse$ = restSessionStore.subject$.pipe(
pluck("response"),
distinctUntilChanged()
)
export const completedRESTResponse$ = restResponse$.pipe(
filter(
(res) =>
res !== null && res.type !== "loading" && res.type !== "network_fail"
)
)

View File

@@ -1,6 +1,7 @@
import eq from "lodash/eq"
import { pluck } from "rxjs/operators"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
import { completedRESTResponse$ } from "./RESTSession"
export const defaultRESTHistoryState = {
state: [] as any[],
@@ -136,3 +137,18 @@ export function toggleGraphqlHistoryEntryStar(entry: any) {
payload: { entry },
})
}
// Listen to completed responses to add to history
completedRESTResponse$.subscribe((res) => {
if (res !== null) {
if (res.type === "loading" || res.type === "network_fail") return
addRESTHistoryEntry({
...res.req,
type: res.type,
meta: res.meta,
statusCode: res.statusCode,
star: false,
})
}
})