refactor: separate out data structures into hoppscotch-data

This commit is contained in:
Andrew Bastin
2021-11-24 01:20:07 +05:30
parent 2884854aab
commit 38755bf3e3
47 changed files with 820 additions and 74 deletions

View File

@@ -0,0 +1,41 @@
export type GQLHeader = {
key: string
value: string
active: boolean
}
export type HoppGQLRequest = {
v: number
name: string
url: string
headers: GQLHeader[]
query: string
variables: string
}
export function translateToGQLRequest(x: any): HoppGQLRequest {
if (x.v && x.v === 1) return x
// Old request
const name = x.name ?? "Untitled"
const url = x.url ?? ""
const headers = x.headers ?? []
const query = x.query ?? ""
const variables = x.variables ?? []
return {
v: 1,
name,
url,
headers,
query,
variables,
}
}
export function makeGQLRequest(x: Omit<HoppGQLRequest, "v">) {
return {
v: 1,
...x,
}
}

View File

@@ -0,0 +1,2 @@
export * from "./rest"
export * from "./graphql"

View File

@@ -0,0 +1,34 @@
export type HoppRESTAuthNone = {
authType: "none"
}
export type HoppRESTAuthBasic = {
authType: "basic"
username: string
password: string
}
export type HoppRESTAuthBearer = {
authType: "bearer"
token: string
}
export type HoppRESTAuthOAuth2 = {
authType: "oauth-2"
token: string
oidcDiscoveryURL: string
authURL: string
accessTokenURL: string
clientID: string
scope: string
}
export type HoppRESTAuth = { authActive: boolean } & (
| HoppRESTAuthNone
| HoppRESTAuthBasic
| HoppRESTAuthBearer
| HoppRESTAuthOAuth2
)

View File

@@ -0,0 +1,13 @@
export const knownContentTypes = {
"application/json": "json",
"application/ld+json": "json",
"application/hal+json": "json",
"application/vnd.api+json": "json",
"application/xml": "xml",
"application/x-www-form-urlencoded": "multipart",
"multipart/form-data": "multipart",
"text/html": "html",
"text/plain": "plain",
}
export type ValidContentTypes = keyof typeof knownContentTypes

View File

@@ -0,0 +1,164 @@
import { ValidContentTypes } from "./content-types"
import { HoppRESTAuth } from "./HoppRESTAuth"
export * from "./content-types"
export * from "./HoppRESTAuth"
export const RESTReqSchemaVersion = "1"
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[]
}
export type HoppRESTReqBody =
| {
contentType: Exclude<ValidContentTypes, "multipart/form-data">
body: string
}
| HoppRESTReqBodyFormData
| {
contentType: null
body: null
}
export interface HoppRESTRequest {
v: string
id?: string // Firebase Firestore ID
name: string
method: string
endpoint: string
params: HoppRESTParam[]
headers: HoppRESTHeader[]
preRequestScript: string
testScript: string
auth: HoppRESTAuth
body: HoppRESTReqBody
}
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
}
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 ?? []).map(
({
key,
value,
active,
}: {
key: string
value: string
active: boolean
}) => ({
key,
value,
active,
})
)
const name = x.name
const method = x.method
const preRequestScript = x.preRequestScript
const testScript = x.testScript
const body = parseRequestBody(x)
const auth = parseOldAuth(x)
const result: HoppRESTRequest = {
name,
endpoint,
headers,
params,
method,
preRequestScript,
testScript,
body,
auth,
v: RESTReqSchemaVersion,
}
if (x.id) result.id = x.id
return result
}
}
export function parseOldAuth(x: any): HoppRESTAuth {
if (!x.auth || x.auth === "None")
return {
authType: "none",
authActive: true,
}
if (x.auth === "Basic Auth")
return {
authType: "basic",
authActive: true,
username: x.httpUser,
password: x.httpPassword,
}
if (x.auth === "Bearer Token")
return {
authType: "bearer",
authActive: true,
token: x.bearerToken,
}
return { authType: "none", authActive: true }
}