api-client/helpers/types/HoppRESTRequest.ts

150 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-07-22 18:37:39 +00:00
import { ValidContentTypes } from "../utils/contenttypes"
import { HoppRESTAuth } from "./HoppRESTAuth"
2021-07-22 18:37:39 +00:00
export const RESTReqSchemaVersion = "1"
2021-07-12 01:33:11 +00:00
export type HoppRESTParam = {
key: string
value: string
active: boolean
}
export type HoppRESTHeader = {
key: string
value: string
active: boolean
}
export type FormDataKeyValue = {
key: string
active: boolean
} & ({ isFile: true; value: Blob[] } | { isFile: false; value: string })
export type HoppRESTReqBodyFormData = {
contentType: "multipart/form-data"
body: FormDataKeyValue[]
2021-07-22 18:37:39 +00:00
}
export type HoppRESTReqBody =
| {
contentType: Exclude<ValidContentTypes, "multipart/form-data">
body: string
}
| HoppRESTReqBodyFormData
2021-07-12 01:33:11 +00:00
export interface HoppRESTRequest {
v: string
name: string
2021-07-12 03:41:55 +00:00
method: string
2021-07-12 01:33:11 +00:00
endpoint: string
params: HoppRESTParam[]
headers: HoppRESTHeader[]
preRequestScript: string
testScript: string
2021-07-22 18:37:39 +00:00
auth: HoppRESTAuth
2021-07-22 18:37:39 +00:00
body: HoppRESTReqBody
2021-07-12 01:33:11 +00:00
}
2021-07-18 04:33:52 +00:00
export function makeRESTRequest(
x: Omit<HoppRESTRequest, "v">
): HoppRESTRequest {
return {
...x,
v: RESTReqSchemaVersion,
}
}
export function isHoppRESTRequest(x: any): x is HoppRESTRequest {
return x && typeof x === "object" && "v" in x
}
2021-07-22 18:37:39 +00:00
function parseRequestBody(x: any): HoppRESTReqBody {
if (x.contentType === "application/json") {
return {
contentType: "application/json",
body: x.rawParams,
}
}
return {
contentType: "application/json",
body: "",
}
}
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 name = x.name
const method = x.method
const preRequestScript = x.preRequestScript
const testScript = x.testScript
2021-07-22 18:37:39 +00:00
const body = parseRequestBody(x)
const auth = parseOldAuth(x)
const result: HoppRESTRequest = {
name,
endpoint,
headers,
params,
method,
preRequestScript,
testScript,
2021-07-22 18:37:39 +00:00
body,
auth,
v: RESTReqSchemaVersion,
}
return result
}
}
export function parseOldAuth(x: any): HoppRESTAuth {
if (!x.auth || x.auth === "None")
return {
authType: "none",
authName: "None",
2021-08-12 08:26:11 +00:00
authActive: true,
}
if (x.auth === "Basic Auth")
return {
authType: "basic",
authName: "Basic Auth",
2021-08-12 08:26:11 +00:00
authActive: true,
username: x.httpUser,
password: x.httpPassword,
}
if (x.auth === "Bearer Token")
return {
authType: "bearer",
authName: "Bearer Token",
2021-08-12 08:26:11 +00:00
authActive: true,
token: x.bearerToken,
}
return { authType: "none", authName: "None", authActive: true }
}