fix: wire response + init error handling
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
export const RESTReqSchemaVersion = "1"
|
||||
|
||||
export type HoppRESTParam = {
|
||||
key: string
|
||||
value: string
|
||||
@@ -11,8 +13,44 @@ export type HoppRESTHeader = {
|
||||
}
|
||||
|
||||
export interface HoppRESTRequest {
|
||||
v: string
|
||||
|
||||
method: string
|
||||
endpoint: string
|
||||
params: HoppRESTParam[]
|
||||
headers: HoppRESTHeader[]
|
||||
}
|
||||
|
||||
export function isHoppRESTRequest(x: any): x is HoppRESTRequest {
|
||||
return x && typeof x === "object" && "v" in x
|
||||
}
|
||||
|
||||
export function translateToNewRequest(x: any): HoppRESTRequest {
|
||||
if (isHoppRESTRequest(x)) {
|
||||
return x
|
||||
} else {
|
||||
// Old format
|
||||
const endpoint: string = `${x.url}${x.path}`
|
||||
|
||||
const headers: HoppRESTHeader[] = x.headers
|
||||
|
||||
// Remove old keys from params
|
||||
const params: HoppRESTParam[] = (x.params as any[]).map(
|
||||
({ key, value, active }) => ({
|
||||
key,
|
||||
value,
|
||||
active,
|
||||
})
|
||||
)
|
||||
|
||||
const method = x.method
|
||||
|
||||
return {
|
||||
endpoint,
|
||||
headers,
|
||||
params,
|
||||
method,
|
||||
v: RESTReqSchemaVersion,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
import { HoppRESTRequest } from "./HoppRESTRequest"
|
||||
|
||||
export type HoppRESTResponse =
|
||||
| { type: "loading" }
|
||||
| { type: "loading"; req: HoppRESTRequest }
|
||||
| {
|
||||
type: "fail"
|
||||
headers: { key: string; value: string }[]
|
||||
body: ArrayBuffer
|
||||
statusCode: number
|
||||
|
||||
meta: {
|
||||
responseSize: number // in bytes
|
||||
responseDuration: number // in millis
|
||||
}
|
||||
|
||||
req: HoppRESTRequest
|
||||
}
|
||||
| {
|
||||
type: "network_fail"
|
||||
error: Error
|
||||
|
||||
req: HoppRESTRequest
|
||||
}
|
||||
| {
|
||||
type: "success"
|
||||
@@ -19,4 +30,6 @@ export type HoppRESTResponse =
|
||||
responseSize: number // in bytes
|
||||
responseDuration: number // in millis
|
||||
}
|
||||
|
||||
req: HoppRESTRequest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user