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

@@ -10,8 +10,8 @@ import {
GraphQLInterfaceType,
} from "graphql"
import { distinctUntilChanged, map } from "rxjs/operators"
import { GQLHeader } from "@hoppscotch/data"
import { sendNetworkRequest } from "./network"
import { GQLHeader } from "./types/HoppGQLRequest"
const GQL_SCHEMA_POLL_INTERVAL = 7000

View File

@@ -1,5 +1,5 @@
import clone from "lodash/clone"
import { FormDataKeyValue, HoppRESTRequest } from "./types/HoppRESTRequest"
import { FormDataKeyValue, HoppRESTRequest } from "@hoppscotch/data"
import { isJSONContentType } from "./utils/contenttypes"
import { defaultRESTRequest } from "~/newstore/RESTSession"

View File

@@ -5,6 +5,7 @@ import { pipe } from "fp-ts/function"
import * as O from "fp-ts/Option"
import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
import { isRight } from "fp-ts/Either"
import { isJSONContentType } from "@hoppscotch/data"
import {
getCombinedEnvVariables,
getFinalEnvsFromPreRequest,
@@ -13,7 +14,6 @@ import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
import { HoppRESTResponse } from "./types/HoppRESTResponse"
import { createRESTNetworkRequestStream } from "./network"
import { HoppTestData, HoppTestResult } from "./types/HoppTestResult"
import { isJSONContentType } from "./utils/contenttypes"
import { getRESTRequest, setRESTTestResults } from "~/newstore/RESTSession"
const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {

View File

@@ -1,10 +1,10 @@
import { HoppRESTRequest } from "@hoppscotch/data"
import { runMutation } from "../GQLClient"
import {
CreateShortcodeDocument,
CreateShortcodeMutation,
CreateShortcodeMutationVariables,
} from "../graphql"
import { HoppRESTRequest } from "~/helpers/types/HoppRESTRequest"
export const createShortcode = (request: HoppRESTRequest) =>
runMutation<CreateShortcodeMutation, CreateShortcodeMutationVariables, "">(

View File

@@ -2,7 +2,7 @@ import {
FormDataKeyValue,
HoppRESTHeader,
HoppRESTParam,
} from "../types/HoppRESTRequest"
} from "@hoppscotch/data"
import { EffectiveHoppRESTRequest } from "../utils/EffectiveURL"
import { CLibcurlCodegen } from "./generators/c-libcurl"
import { CsRestsharpCodegen } from "./generators/cs-restsharp"

View File

@@ -11,7 +11,7 @@ import {
query,
updateDoc,
} from "firebase/firestore"
import { FormDataKeyValue } from "../types/HoppRESTRequest"
import { FormDataKeyValue } from "@hoppscotch/data"
import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings"
import {

View File

@@ -9,10 +9,7 @@ import {
} from "rxjs"
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore"
import cloneDeep from "lodash/cloneDeep"
import {
HoppRESTRequest,
translateToNewRequest,
} from "../types/HoppRESTRequest"
import { HoppRESTRequest, translateToNewRequest } from "@hoppscotch/data"
import { currentUser$, HoppUser } from "./auth"
import { restRequest$ } from "~/newstore/RESTSession"

View File

@@ -2,7 +2,7 @@ import { BehaviorSubject } from "rxjs"
import { gql } from "graphql-tag"
import pull from "lodash/pull"
import remove from "lodash/remove"
import { translateToNewRequest } from "../types/HoppRESTRequest"
import { translateToNewRequest } from "@hoppscotch/data"
import { TeamCollection } from "./TeamCollection"
import { TeamRequest } from "./TeamRequest"
import {

View File

@@ -1,4 +1,4 @@
import { HoppRESTRequest } from "../types/HoppRESTRequest"
import { HoppRESTRequest } from "@hoppscotch/data"
/**
* Defines how a Teams request is represented in TeamCollectionAdapter

View File

@@ -1,41 +0,0 @@
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

@@ -1,34 +0,0 @@
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

@@ -1,161 +0,0 @@
import { ValidContentTypes } from "../utils/contenttypes"
import { HoppRESTAuth } 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 }
}

View File

@@ -1,4 +1,4 @@
import { HoppRESTRequest } from "./HoppRESTRequest"
import { HoppRESTRequest } from "@hoppscotch/data"
export type HoppRESTResponse =
| { type: "loading"; req: HoppRESTRequest }

View File

@@ -1,6 +1,6 @@
import { combineLatest, Observable } from "rxjs"
import { map } from "rxjs/operators"
import { FormDataKeyValue, HoppRESTRequest } from "../types/HoppRESTRequest"
import { FormDataKeyValue, HoppRESTRequest } from "@hoppscotch/data"
import { parseTemplateString, parseBodyEnvVariables } from "../templating"
import { Environment, getGlobalVariables } from "~/newstore/environments"

View File

@@ -1,4 +1,8 @@
export const knownContentTypes = {
import { ValidContentTypes } from "@hoppscotch/data"
export type Content = "json" | "xml" | "multipart" | "html" | "plain"
export const knownContentTypes: Record<ValidContentTypes, Content> = {
"application/json": "json",
"application/ld+json": "json",
"application/hal+json": "json",
@@ -10,8 +14,6 @@ export const knownContentTypes = {
"text/plain": "plain",
}
export type ValidContentTypes = keyof typeof knownContentTypes
export function isJSONContentType(contentType: string) {
return /\bjson\b/i.test(contentType)
}