fix: migration of request url from old to new format
This commit is contained in:
113
helpers/RESTExtURLParams.ts
Normal file
113
helpers/RESTExtURLParams.ts
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
import clone from "lodash/clone"
|
||||||
|
import { FormDataKeyValue, HoppRESTRequest } from "./types/HoppRESTRequest"
|
||||||
|
import { isJSONContentType } from "./utils/contenttypes"
|
||||||
|
import { defaultRESTRequest } from "~/newstore/RESTSession"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles translations for all the hopp.io REST Shareable URL params
|
||||||
|
*/
|
||||||
|
export function translateExtURLParams(
|
||||||
|
urlParams: Record<string, any>
|
||||||
|
): HoppRESTRequest {
|
||||||
|
if (urlParams.v) return parseV1ExtURL(urlParams)
|
||||||
|
else return parseV0ExtURL(urlParams)
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseV0ExtURL(urlParams: Record<string, any>): HoppRESTRequest {
|
||||||
|
const resolvedReq = clone(defaultRESTRequest)
|
||||||
|
|
||||||
|
if (urlParams.method && typeof urlParams.method === "string") {
|
||||||
|
resolvedReq.method = urlParams.method
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.url && typeof urlParams.url === "string") {
|
||||||
|
if (urlParams.path && typeof urlParams.path === "string") {
|
||||||
|
resolvedReq.endpoint = `${urlParams.url}/${urlParams.path}`
|
||||||
|
} else {
|
||||||
|
resolvedReq.endpoint = urlParams.url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.headers && typeof urlParams.headers === "string") {
|
||||||
|
resolvedReq.headers = JSON.parse(urlParams.headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.params && typeof urlParams.params === "string") {
|
||||||
|
resolvedReq.params = JSON.parse(urlParams.params)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.httpUser && typeof urlParams.httpUser === "string") {
|
||||||
|
resolvedReq.auth = {
|
||||||
|
authType: "basic",
|
||||||
|
authActive: true,
|
||||||
|
username: urlParams.httpUser,
|
||||||
|
password: urlParams.httpPassword ?? "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.bearerToken && typeof urlParams.bearerToken === "string") {
|
||||||
|
resolvedReq.auth = {
|
||||||
|
authType: "bearer",
|
||||||
|
authActive: true,
|
||||||
|
token: urlParams.bearerToken,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.contentType) {
|
||||||
|
if (urlParams.contentType === "multipart/formdata") {
|
||||||
|
resolvedReq.body = {
|
||||||
|
contentType: "multipart/form-data",
|
||||||
|
body: JSON.parse(urlParams.bodyParams || "[]").map(
|
||||||
|
(x: any) =>
|
||||||
|
<FormDataKeyValue>{
|
||||||
|
active: x.active,
|
||||||
|
key: x.key,
|
||||||
|
value: x.value,
|
||||||
|
isFile: false,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else if (isJSONContentType(urlParams.contentType)) {
|
||||||
|
if (urlParams.rawInput) {
|
||||||
|
resolvedReq.body = {
|
||||||
|
contentType: urlParams.contentType,
|
||||||
|
body: urlParams.rawInput,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolvedReq.body = {
|
||||||
|
contentType: urlParams.contentType,
|
||||||
|
body: urlParams.bodyParams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolvedReq.body = {
|
||||||
|
contentType: urlParams.contentType,
|
||||||
|
body: urlParams.rawInput,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolvedReq
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseV1ExtURL(urlParams: Record<string, any>): HoppRESTRequest {
|
||||||
|
const resolvedReq = clone(defaultRESTRequest)
|
||||||
|
|
||||||
|
if (urlParams.headers && typeof urlParams.headers === "string") {
|
||||||
|
resolvedReq.headers = JSON.parse(urlParams.headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.params && typeof urlParams.params === "string") {
|
||||||
|
resolvedReq.params = JSON.parse(urlParams.params)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.method && typeof urlParams.method === "string") {
|
||||||
|
resolvedReq.method = urlParams.method
|
||||||
|
}
|
||||||
|
|
||||||
|
if (urlParams.endpoint && typeof urlParams.endpoint === "string") {
|
||||||
|
resolvedReq.endpoint = urlParams.endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolvedReq
|
||||||
|
}
|
||||||
@@ -15,8 +15,7 @@ export type HoppRESTAuthBearer = {
|
|||||||
token: string
|
token: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type HoppRESTAuth =
|
export type HoppRESTAuth = { authActive: boolean } & (
|
||||||
{ authActive: boolean } & (
|
|
||||||
| HoppRESTAuthNone
|
| HoppRESTAuthNone
|
||||||
| HoppRESTAuthBasic
|
| HoppRESTAuthBasic
|
||||||
| HoppRESTAuthBearer
|
| HoppRESTAuthBearer
|
||||||
|
|||||||
@@ -101,11 +101,8 @@ import {
|
|||||||
restActiveHeadersCount$,
|
restActiveHeadersCount$,
|
||||||
getRESTRequest,
|
getRESTRequest,
|
||||||
setRESTRequest,
|
setRESTRequest,
|
||||||
setRESTHeaders,
|
|
||||||
setRESTParams,
|
|
||||||
updateRESTMethod,
|
|
||||||
setRESTEndpoint,
|
|
||||||
} from "~/newstore/RESTSession"
|
} from "~/newstore/RESTSession"
|
||||||
|
import { translateExtURLParams } from "~/helpers/RESTExtURLParams"
|
||||||
import {
|
import {
|
||||||
useReadonlyStream,
|
useReadonlyStream,
|
||||||
useStream,
|
useStream,
|
||||||
@@ -161,14 +158,9 @@ function bindRequestToURLParams() {
|
|||||||
// Now, we have to see the initial URL param and set that as the request
|
// Now, we have to see the initial URL param and set that as the request
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const query = route.value.query
|
const query = route.value.query
|
||||||
if (query.headers && typeof query.headers === "string")
|
|
||||||
setRESTHeaders(JSON.parse(query.headers))
|
if (Object.keys(query).length === 0) return
|
||||||
if (query.params && typeof query.params === "string")
|
setRESTRequest(translateExtURLParams(query))
|
||||||
setRESTParams(JSON.parse(query.params))
|
|
||||||
if (query.method && typeof query.method === "string")
|
|
||||||
updateRESTMethod(query.method)
|
|
||||||
if (query.endpoint && typeof query.endpoint === "string")
|
|
||||||
setRESTEndpoint(query.endpoint)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user