refactor: versioning and migration mechanism for public data structures (#3457)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
"main": "dist/hoppscotch-data.cjs",
|
||||
"module": "dist/hoppscotch-data.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [ "dist/*" ],
|
||||
"files": [
|
||||
"dist/*"
|
||||
],
|
||||
"scripts": {
|
||||
"build:code": "vite build",
|
||||
"build:decl": "tsc --project tsconfig.decl.json",
|
||||
@@ -33,13 +35,15 @@
|
||||
"homepage": "https://github.com/hoppscotch/hoppscotch#readme",
|
||||
"devDependencies": {
|
||||
"@types/lodash": "^4.14.181",
|
||||
"typescript": "^4.6.3",
|
||||
"typescript": "^5.2.2",
|
||||
"vite": "^3.2.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"fp-ts": "^2.11.10",
|
||||
"io-ts": "^2.2.16",
|
||||
"lodash": "^4.17.21",
|
||||
"parser-ts": "^0.6.16"
|
||||
"parser-ts": "^0.6.16",
|
||||
"verzod": "^0.1.1",
|
||||
"zod": "^3.22.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as E from "fp-ts/Either"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { InferredEntity, createVersionedEntity } from "verzod"
|
||||
|
||||
export type Environment = {
|
||||
id?: string
|
||||
name: string
|
||||
variables: {
|
||||
key: string
|
||||
value: string
|
||||
}[]
|
||||
}
|
||||
import V0_VERSION from "./v/0"
|
||||
|
||||
export const Environment = createVersionedEntity({
|
||||
latestVersion: 0,
|
||||
versionMap: {
|
||||
0: V0_VERSION
|
||||
},
|
||||
getVersion(x) {
|
||||
return V0_VERSION.schema.safeParse(x).success
|
||||
? 0
|
||||
: null
|
||||
}
|
||||
})
|
||||
|
||||
export type Environment = InferredEntity<typeof Environment>
|
||||
|
||||
const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<<myVariable>>"
|
||||
|
||||
18
packages/hoppscotch-data/src/environment/v/0.ts
Normal file
18
packages/hoppscotch-data/src/environment/v/0.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from "zod"
|
||||
import { defineVersion } from "verzod"
|
||||
|
||||
export const V0_SCHEMA = z.object({
|
||||
id: z.optional(z.string()),
|
||||
name: z.string(),
|
||||
variables: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
export default defineVersion({
|
||||
initial: true,
|
||||
schema: V0_SCHEMA
|
||||
})
|
||||
@@ -1,43 +0,0 @@
|
||||
export type HoppGQLAuthNone = {
|
||||
authType: "none"
|
||||
}
|
||||
|
||||
export type HoppGQLAuthBasic = {
|
||||
authType: "basic"
|
||||
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export type HoppGQLAuthBearer = {
|
||||
authType: "bearer"
|
||||
|
||||
token: string
|
||||
}
|
||||
|
||||
export type HoppGQLAuthOAuth2 = {
|
||||
authType: "oauth-2"
|
||||
|
||||
token: string
|
||||
oidcDiscoveryURL: string
|
||||
authURL: string
|
||||
accessTokenURL: string
|
||||
clientID: string
|
||||
scope: string
|
||||
}
|
||||
|
||||
export type HoppGQLAuthAPIKey = {
|
||||
authType: "api-key"
|
||||
|
||||
key: string
|
||||
value: string
|
||||
addTo: string
|
||||
}
|
||||
|
||||
export type HoppGQLAuth = { authActive: boolean } & (
|
||||
| HoppGQLAuthNone
|
||||
| HoppGQLAuthBasic
|
||||
| HoppGQLAuthBearer
|
||||
| HoppGQLAuthOAuth2
|
||||
| HoppGQLAuthAPIKey
|
||||
)
|
||||
@@ -1,51 +1,75 @@
|
||||
import { HoppGQLAuth } from "./HoppGQLAuth"
|
||||
import { InferredEntity, createVersionedEntity } from "verzod"
|
||||
import { z } from "zod"
|
||||
import V1_VERSION from "./v/1"
|
||||
import V2_VERSION from "./v/2"
|
||||
|
||||
export * from "./HoppGQLAuth"
|
||||
export { GQLHeader } from "./v/1"
|
||||
export {
|
||||
HoppGQLAuth,
|
||||
HoppGQLAuthAPIKey,
|
||||
HoppGQLAuthBasic,
|
||||
HoppGQLAuthBearer,
|
||||
HoppGQLAuthNone,
|
||||
HoppGQLAuthOAuth2,
|
||||
} from "./v/2"
|
||||
|
||||
export const GQL_REQ_SCHEMA_VERSION = 2
|
||||
|
||||
export type GQLHeader = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
const versionedObject = z.object({
|
||||
v: z.number(),
|
||||
})
|
||||
|
||||
export type HoppGQLRequest = {
|
||||
id?: string
|
||||
v: number
|
||||
name: string
|
||||
url: string
|
||||
headers: GQLHeader[]
|
||||
query: string
|
||||
variables: string
|
||||
auth: HoppGQLAuth
|
||||
}
|
||||
export const HoppGQLRequest = createVersionedEntity({
|
||||
latestVersion: 2,
|
||||
versionMap: {
|
||||
1: V1_VERSION,
|
||||
2: V2_VERSION,
|
||||
},
|
||||
getVersion(x) {
|
||||
const result = versionedObject.safeParse(x)
|
||||
|
||||
export function translateToGQLRequest(x: any): HoppGQLRequest {
|
||||
if (x.v && x.v === GQL_REQ_SCHEMA_VERSION) return x
|
||||
return result.success ? result.data.v : null
|
||||
},
|
||||
})
|
||||
|
||||
// Old request
|
||||
const name = x.name ?? "Untitled"
|
||||
const url = x.url ?? ""
|
||||
const headers = x.headers ?? []
|
||||
const query = x.query ?? ""
|
||||
const variables = x.variables ?? []
|
||||
const auth = x.auth ?? {
|
||||
authType: "none",
|
||||
authActive: true,
|
||||
export type HoppGQLRequest = InferredEntity<typeof HoppGQLRequest>
|
||||
|
||||
const DEFAULT_QUERY = `
|
||||
query Request {
|
||||
method
|
||||
url
|
||||
headers {
|
||||
key
|
||||
value
|
||||
}
|
||||
}`.trim()
|
||||
|
||||
export function getDefaultGQLRequest(): HoppGQLRequest {
|
||||
return {
|
||||
v: GQL_REQ_SCHEMA_VERSION,
|
||||
name,
|
||||
url,
|
||||
headers,
|
||||
query,
|
||||
variables,
|
||||
auth
|
||||
name: "Untitled",
|
||||
url: "https://echo.hoppscotch.io/graphql",
|
||||
headers: [],
|
||||
variables: `
|
||||
{
|
||||
"id": "1"
|
||||
}`.trim(),
|
||||
query: DEFAULT_QUERY,
|
||||
auth: {
|
||||
authType: "none",
|
||||
authActive: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated This function is deprecated. Use `HoppGQLRequest` instead.
|
||||
*/
|
||||
export function translateToGQLRequest(x: unknown): HoppGQLRequest {
|
||||
const result = HoppGQLRequest.safeParse(x)
|
||||
return result.type === "ok" ? result.value : getDefaultGQLRequest()
|
||||
}
|
||||
|
||||
export function makeGQLRequest(x: Omit<HoppGQLRequest, "v">): HoppGQLRequest {
|
||||
return {
|
||||
v: GQL_REQ_SCHEMA_VERSION,
|
||||
|
||||
24
packages/hoppscotch-data/src/graphql/v/1.ts
Normal file
24
packages/hoppscotch-data/src/graphql/v/1.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { z } from "zod"
|
||||
import { defineVersion } from "verzod"
|
||||
|
||||
export const GQLHeader = z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
active: z.boolean()
|
||||
})
|
||||
|
||||
export type GQLHeader = z.infer<typeof GQLHeader>
|
||||
|
||||
export const V1_SCHEMA = z.object({
|
||||
v: z.literal(1),
|
||||
name: z.string(),
|
||||
url: z.string(),
|
||||
headers: z.array(GQLHeader),
|
||||
query: z.string(),
|
||||
variables: z.string(),
|
||||
})
|
||||
|
||||
export default defineVersion({
|
||||
initial: true,
|
||||
schema: V1_SCHEMA
|
||||
})
|
||||
91
packages/hoppscotch-data/src/graphql/v/2.ts
Normal file
91
packages/hoppscotch-data/src/graphql/v/2.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { z } from "zod"
|
||||
import { defineVersion } from "verzod"
|
||||
import { GQLHeader, V1_SCHEMA } from "./1"
|
||||
|
||||
export const HoppGQLAuthNone = z.object({
|
||||
authType: z.literal("none")
|
||||
})
|
||||
|
||||
export type HoppGQLAuthNone = z.infer<typeof HoppGQLAuthNone>
|
||||
|
||||
export const HoppGQLAuthBasic = z.object({
|
||||
authType: z.literal("basic"),
|
||||
|
||||
username: z.string(),
|
||||
password: z.string()
|
||||
})
|
||||
|
||||
export type HoppGQLAuthBasic = z.infer<typeof HoppGQLAuthBasic>
|
||||
|
||||
export const HoppGQLAuthBearer = z.object({
|
||||
authType: z.literal("bearer"),
|
||||
|
||||
token: z.string()
|
||||
})
|
||||
|
||||
export type HoppGQLAuthBearer = z.infer<typeof HoppGQLAuthBearer>
|
||||
|
||||
export const HoppGQLAuthOAuth2 = z.object({
|
||||
authType: z.literal("oauth-2"),
|
||||
|
||||
token: z.string(),
|
||||
oidcDiscoveryURL: z.string(),
|
||||
authURL: z.string(),
|
||||
accessTokenURL: z.string(),
|
||||
clientID: z.string(),
|
||||
scope: z.string()
|
||||
})
|
||||
|
||||
export type HoppGQLAuthOAuth2 = z.infer<typeof HoppGQLAuthOAuth2>
|
||||
|
||||
export const HoppGQLAuthAPIKey = z.object({
|
||||
authType: z.literal("api-key"),
|
||||
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
addTo: z.string()
|
||||
})
|
||||
|
||||
export type HoppGQLAuthAPIKey = z.infer<typeof HoppGQLAuthAPIKey>
|
||||
|
||||
export const HoppGQLAuth = z.discriminatedUnion("authType", [
|
||||
HoppGQLAuthNone,
|
||||
HoppGQLAuthBasic,
|
||||
HoppGQLAuthBearer,
|
||||
HoppGQLAuthOAuth2,
|
||||
HoppGQLAuthAPIKey
|
||||
]).and(
|
||||
z.object({
|
||||
authActive: z.boolean()
|
||||
})
|
||||
)
|
||||
|
||||
export type HoppGQLAuth = z.infer<typeof HoppGQLAuth>
|
||||
|
||||
const V2_SCHEMA = z.object({
|
||||
id: z.optional(z.string()),
|
||||
v: z.literal(2),
|
||||
|
||||
name: z.string(),
|
||||
url: z.string(),
|
||||
headers: z.array(GQLHeader),
|
||||
query: z.string(),
|
||||
variables: z.string(),
|
||||
|
||||
auth: HoppGQLAuth
|
||||
})
|
||||
|
||||
export default defineVersion({
|
||||
initial: false,
|
||||
schema: V2_SCHEMA,
|
||||
up(old: z.infer<typeof V1_SCHEMA>) {
|
||||
return <z.infer<typeof V2_SCHEMA>>{
|
||||
...old,
|
||||
v: 2,
|
||||
auth: {
|
||||
authActive: true,
|
||||
authType: "none",
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -1,43 +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 HoppRESTAuthAPIKey = {
|
||||
authType: "api-key"
|
||||
|
||||
key: string
|
||||
value: string
|
||||
addTo: string
|
||||
}
|
||||
|
||||
export type HoppRESTAuth = { authActive: boolean } & (
|
||||
| HoppRESTAuthNone
|
||||
| HoppRESTAuthBasic
|
||||
| HoppRESTAuthBearer
|
||||
| HoppRESTAuthOAuth2
|
||||
| HoppRESTAuthAPIKey
|
||||
)
|
||||
@@ -11,3 +11,5 @@ export const knownContentTypes = {
|
||||
}
|
||||
|
||||
export type ValidContentTypes = keyof typeof knownContentTypes
|
||||
|
||||
export const ValidContentTypesList = Object.keys(knownContentTypes) as ValidContentTypes[]
|
||||
|
||||
@@ -1,66 +1,58 @@
|
||||
import cloneDeep from "lodash/cloneDeep"
|
||||
import * as Eq from "fp-ts/Eq"
|
||||
import * as S from "fp-ts/string"
|
||||
import { ValidContentTypes } from "./content-types"
|
||||
import { HoppRESTAuth } from "./HoppRESTAuth"
|
||||
import cloneDeep from "lodash/cloneDeep"
|
||||
import V0_VERSION from "./v/0"
|
||||
import V1_VERSION from "./v/1"
|
||||
import { createVersionedEntity, InferredEntity } from "verzod"
|
||||
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
|
||||
import {
|
||||
HoppRESTAuth,
|
||||
HoppRESTReqBody,
|
||||
HoppRESTHeaders,
|
||||
HoppRESTParams,
|
||||
} from "./v/1"
|
||||
import { z } from "zod"
|
||||
|
||||
export * from "./content-types"
|
||||
export * from "./HoppRESTAuth"
|
||||
export {
|
||||
FormDataKeyValue,
|
||||
HoppRESTReqBodyFormData,
|
||||
HoppRESTAuth,
|
||||
HoppRESTAuthAPIKey,
|
||||
HoppRESTAuthBasic,
|
||||
HoppRESTAuthBearer,
|
||||
HoppRESTAuthNone,
|
||||
HoppRESTAuthOAuth2,
|
||||
HoppRESTReqBody,
|
||||
} from "./v/1"
|
||||
|
||||
export const RESTReqSchemaVersion = "1"
|
||||
const versionedObject = z.object({
|
||||
// v is a stringified number
|
||||
v: z.string().regex(/^\d+$/).transform(Number),
|
||||
})
|
||||
|
||||
export type HoppRESTParam = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
export const HoppRESTRequest = createVersionedEntity({
|
||||
latestVersion: 1,
|
||||
versionMap: {
|
||||
0: V0_VERSION,
|
||||
1: V1_VERSION,
|
||||
},
|
||||
getVersion(data) {
|
||||
// For V1 onwards we have the v string storing the number
|
||||
const versionCheck = versionedObject.safeParse(data)
|
||||
|
||||
export type HoppRESTHeader = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
if (versionCheck.success) return versionCheck.data.v
|
||||
|
||||
export type FormDataKeyValue = {
|
||||
key: string
|
||||
active: boolean
|
||||
} & ({ isFile: true; value: Blob[] } | { isFile: false; value: string })
|
||||
// For V0 we have to check the schema
|
||||
const result = V0_VERSION.schema.safeParse(data)
|
||||
|
||||
export type HoppRESTReqBodyFormData = {
|
||||
contentType: "multipart/form-data"
|
||||
body: FormDataKeyValue[]
|
||||
}
|
||||
return result.success ? 0 : null
|
||||
},
|
||||
})
|
||||
|
||||
export type HoppRESTReqBody =
|
||||
| {
|
||||
contentType: Exclude<ValidContentTypes, "multipart/form-data">
|
||||
body: string
|
||||
}
|
||||
| HoppRESTReqBodyFormData
|
||||
| {
|
||||
contentType: null
|
||||
body: null
|
||||
}
|
||||
export type HoppRESTRequest = InferredEntity<typeof HoppRESTRequest>
|
||||
|
||||
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 const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
||||
const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
||||
id: undefinedEq(S.Eq),
|
||||
v: S.Eq,
|
||||
auth: lodashIsEqualEq,
|
||||
@@ -80,6 +72,11 @@ export const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
||||
testScript: S.Eq,
|
||||
})
|
||||
|
||||
export const RESTReqSchemaVersion = "1"
|
||||
|
||||
export type HoppRESTParam = HoppRESTRequest["params"][number]
|
||||
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
|
||||
|
||||
export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
|
||||
|
||||
/**
|
||||
@@ -87,6 +84,9 @@ export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
|
||||
* If we fail to detect certain bits, we just resolve it to the default value
|
||||
* @param x The value to extract REST Request data from
|
||||
* @param defaultReq The default REST Request to source from
|
||||
*
|
||||
* @deprecated Usage of this function is no longer recommended and is only here
|
||||
* for legacy reasons and will be removed
|
||||
*/
|
||||
export function safelyExtractRESTRequest(
|
||||
x: unknown,
|
||||
@@ -94,40 +94,53 @@ export function safelyExtractRESTRequest(
|
||||
): HoppRESTRequest {
|
||||
const req = cloneDeep(defaultReq)
|
||||
|
||||
// TODO: A cleaner way to do this ?
|
||||
if (!!x && typeof x === "object") {
|
||||
if (x.hasOwnProperty("v") && typeof x.v === "string")
|
||||
req.v = x.v
|
||||
if ("id" in x && typeof x.id === "string") req.id = x.id
|
||||
|
||||
if (x.hasOwnProperty("id") && typeof x.id === "string")
|
||||
req.id = x.id
|
||||
if ("name" in x && typeof x.name === "string") req.name = x.name
|
||||
|
||||
if (x.hasOwnProperty("name") && typeof x.name === "string")
|
||||
req.name = x.name
|
||||
if ("method" in x && typeof x.method === "string") req.method = x.method
|
||||
|
||||
if (x.hasOwnProperty("method") && typeof x.method === "string")
|
||||
req.method = x.method
|
||||
|
||||
if (x.hasOwnProperty("endpoint") && typeof x.endpoint === "string")
|
||||
if ("endpoint" in x && typeof x.endpoint === "string")
|
||||
req.endpoint = x.endpoint
|
||||
|
||||
if (x.hasOwnProperty("preRequestScript") && typeof x.preRequestScript === "string")
|
||||
if ("preRequestScript" in x && typeof x.preRequestScript === "string")
|
||||
req.preRequestScript = x.preRequestScript
|
||||
|
||||
if (x.hasOwnProperty("testScript") && typeof x.testScript === "string")
|
||||
if ("testScript" in x && typeof x.testScript === "string")
|
||||
req.testScript = x.testScript
|
||||
|
||||
if (x.hasOwnProperty("body") && typeof x.body === "object" && !!x.body)
|
||||
req.body = x.body as any // TODO: Deep nested checks
|
||||
if ("body" in x) {
|
||||
const result = HoppRESTReqBody.safeParse(x.body)
|
||||
|
||||
if (x.hasOwnProperty("auth") && typeof x.auth === "object" && !!x.auth)
|
||||
req.auth = x.auth as any // TODO: Deep nested checks
|
||||
if (result.success) {
|
||||
req.body = result.data
|
||||
}
|
||||
}
|
||||
|
||||
if (x.hasOwnProperty("params") && Array.isArray(x.params))
|
||||
req.params = x.params // TODO: Deep nested checks
|
||||
if ("auth" in x) {
|
||||
const result = HoppRESTAuth.safeParse(x.auth)
|
||||
|
||||
if (x.hasOwnProperty("headers") && Array.isArray(x.headers))
|
||||
req.headers = x.headers // TODO: Deep nested checks
|
||||
if (result.success) {
|
||||
req.auth = result.data
|
||||
}
|
||||
}
|
||||
|
||||
if ("params" in x) {
|
||||
const result = HoppRESTParams.safeParse(x.params)
|
||||
|
||||
if (result.success) {
|
||||
req.params = result.data
|
||||
}
|
||||
}
|
||||
|
||||
if ("headers" in x) {
|
||||
const result = HoppRESTHeaders.safeParse(x.headers)
|
||||
|
||||
if (result.success) {
|
||||
req.headers = result.data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return req
|
||||
@@ -137,105 +150,51 @@ export function makeRESTRequest(
|
||||
x: Omit<HoppRESTRequest, "v">
|
||||
): HoppRESTRequest {
|
||||
return {
|
||||
...x,
|
||||
v: RESTReqSchemaVersion,
|
||||
...x,
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultRESTRequest(): HoppRESTRequest {
|
||||
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 ?? "Untitled request"
|
||||
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 {
|
||||
v: "1",
|
||||
endpoint: "https://echo.hoppscotch.io",
|
||||
name: "Untitled",
|
||||
params: [],
|
||||
headers: [],
|
||||
method: "GET",
|
||||
auth: {
|
||||
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 }
|
||||
},
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
body: {
|
||||
contentType: null,
|
||||
body: null,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given value is a HoppRESTRequest
|
||||
* @param x The value to check
|
||||
*
|
||||
* @deprecated This function is no longer recommended and is only here for legacy reasons
|
||||
* Use `HoppRESTRequest.is`/`HoppRESTRequest.isLatest` instead.
|
||||
*/
|
||||
export function isHoppRESTRequest(x: unknown): x is HoppRESTRequest {
|
||||
return HoppRESTRequest.isLatest(x)
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely parses a value into a HoppRESTRequest.
|
||||
* @param x The value to check
|
||||
*
|
||||
* @deprecated This function is no longer recommended and is only here for
|
||||
* legacy reasons. Use `HoppRESTRequest.safeParse` instead.
|
||||
*/
|
||||
export function translateToNewRequest(x: unknown): HoppRESTRequest {
|
||||
const result = HoppRESTRequest.safeParse(x)
|
||||
return result.type === "ok" ? result.value : getDefaultRESTRequest()
|
||||
}
|
||||
|
||||
39
packages/hoppscotch-data/src/rest/v/0.ts
Normal file
39
packages/hoppscotch-data/src/rest/v/0.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { defineVersion } from "verzod"
|
||||
import { z } from "zod"
|
||||
|
||||
export const V0_SCHEMA = z.object({
|
||||
id: z.optional(z.string()), // Firebase Firestore ID
|
||||
|
||||
url: z.string(),
|
||||
path: z.string(),
|
||||
headers: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
active: z.boolean()
|
||||
})
|
||||
),
|
||||
params: z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
active: z.boolean()
|
||||
})
|
||||
),
|
||||
name: z.string(),
|
||||
method: z.string(),
|
||||
preRequestScript: z.string(),
|
||||
testScript: z.string(),
|
||||
contentType: z.string(),
|
||||
body: z.string(),
|
||||
rawParams: z.optional(z.string()),
|
||||
auth: z.optional(z.string()),
|
||||
httpUser: z.optional(z.string()),
|
||||
httpPassword: z.optional(z.string()),
|
||||
bearerToken: z.optional(z.string()),
|
||||
})
|
||||
|
||||
export default defineVersion({
|
||||
initial: true,
|
||||
schema: V0_SCHEMA
|
||||
})
|
||||
209
packages/hoppscotch-data/src/rest/v/1.ts
Normal file
209
packages/hoppscotch-data/src/rest/v/1.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
import { defineVersion } from "verzod"
|
||||
import { z } from "zod"
|
||||
|
||||
import { V0_SCHEMA } from "./0"
|
||||
|
||||
export const FormDataKeyValue = z.object({
|
||||
key: z.string(),
|
||||
active: z.boolean()
|
||||
}).and(
|
||||
z.union([
|
||||
z.object({
|
||||
isFile: z.literal(true),
|
||||
value: z.array(z.instanceof(Blob))
|
||||
}),
|
||||
z.object({
|
||||
isFile: z.literal(false),
|
||||
value: z.string()
|
||||
})
|
||||
])
|
||||
)
|
||||
|
||||
export type FormDataKeyValue = z.infer<typeof FormDataKeyValue>
|
||||
|
||||
export const HoppRESTReqBodyFormData = z.object({
|
||||
contentType: z.literal("multipart/form-data"),
|
||||
body: z.array(FormDataKeyValue)
|
||||
})
|
||||
|
||||
export type HoppRESTReqBodyFormData = z.infer<typeof HoppRESTReqBodyFormData>
|
||||
|
||||
export const HoppRESTReqBody = z.union([
|
||||
z.object({
|
||||
contentType: z.literal(null),
|
||||
body: z.literal(null)
|
||||
}),
|
||||
z.object({
|
||||
contentType: z.literal("multipart/form-data"),
|
||||
body: FormDataKeyValue
|
||||
}),
|
||||
z.object({
|
||||
contentType: z.union([
|
||||
z.literal("application/json"),
|
||||
z.literal("application/ld+json"),
|
||||
z.literal("application/hal+json"),
|
||||
z.literal("application/vnd.api+json"),
|
||||
z.literal("application/xml"),
|
||||
z.literal("application/x-www-form-urlencoded"),
|
||||
z.literal("text/html"),
|
||||
z.literal("text/plain"),
|
||||
]),
|
||||
body: z.string()
|
||||
})
|
||||
])
|
||||
|
||||
export type HoppRESTReqBody = z.infer<typeof HoppRESTReqBody>
|
||||
|
||||
export const HoppRESTAuthNone = z.object({
|
||||
authType: z.literal("none")
|
||||
})
|
||||
|
||||
export type HoppRESTAuthNone = z.infer<typeof HoppRESTAuthNone>
|
||||
|
||||
export const HoppRESTAuthBasic = z.object({
|
||||
authType: z.literal("basic"),
|
||||
username: z.string(),
|
||||
password: z.string(),
|
||||
})
|
||||
|
||||
export type HoppRESTAuthBasic = z.infer<typeof HoppRESTAuthBasic>
|
||||
|
||||
export const HoppRESTAuthBearer = z.object({
|
||||
authType: z.literal("bearer"),
|
||||
token: z.string(),
|
||||
})
|
||||
|
||||
export type HoppRESTAuthBearer = z.infer<typeof HoppRESTAuthBearer>
|
||||
|
||||
export const HoppRESTAuthOAuth2 = z.object({
|
||||
authType: z.literal("oauth-2"),
|
||||
token: z.string(),
|
||||
oidcDiscoveryURL: z.string(),
|
||||
authURL: z.string(),
|
||||
accessTokenURL: z.string(),
|
||||
clientID: z.string(),
|
||||
scope: z.string(),
|
||||
})
|
||||
|
||||
export type HoppRESTAuthOAuth2 = z.infer<typeof HoppRESTAuthOAuth2>
|
||||
|
||||
export const HoppRESTAuthAPIKey = z.object({
|
||||
authType: z.literal("api-key"),
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
addTo: z.string(),
|
||||
})
|
||||
|
||||
export type HoppRESTAuthAPIKey = z.infer<typeof HoppRESTAuthAPIKey>
|
||||
|
||||
export const HoppRESTAuth = z.discriminatedUnion("authType", [
|
||||
HoppRESTAuthNone,
|
||||
HoppRESTAuthBasic,
|
||||
HoppRESTAuthBearer,
|
||||
HoppRESTAuthOAuth2,
|
||||
HoppRESTAuthAPIKey
|
||||
]).and(
|
||||
z.object({
|
||||
authActive: z.boolean(),
|
||||
})
|
||||
)
|
||||
|
||||
export type HoppRESTAuth = z.infer<typeof HoppRESTAuth>
|
||||
|
||||
export const HoppRESTParams = z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
active: z.boolean()
|
||||
})
|
||||
)
|
||||
|
||||
export type HoppRESTParams = z.infer<typeof HoppRESTParams>
|
||||
|
||||
export const HoppRESTHeaders = z.array(
|
||||
z.object({
|
||||
key: z.string(),
|
||||
value: z.string(),
|
||||
active: z.boolean()
|
||||
})
|
||||
)
|
||||
|
||||
export type HoppRESTHeaders = z.infer<typeof HoppRESTHeaders>
|
||||
|
||||
const V1_SCHEMA = z.object({
|
||||
v: z.literal("1"),
|
||||
id: z.optional(z.string()), // Firebase Firestore ID
|
||||
|
||||
name: z.string(),
|
||||
method: z.string(),
|
||||
endpoint: z.string(),
|
||||
params: HoppRESTParams,
|
||||
headers: HoppRESTHeaders,
|
||||
preRequestScript: z.string(),
|
||||
testScript: z.string(),
|
||||
|
||||
auth: HoppRESTAuth,
|
||||
|
||||
body: HoppRESTReqBody
|
||||
})
|
||||
|
||||
function parseRequestBody(x: z.infer<typeof V0_SCHEMA>): z.infer<typeof V1_SCHEMA>["body"] {
|
||||
return {
|
||||
contentType: "application/json",
|
||||
body: x.contentType === "application/json" ? x.rawParams ?? "" : "",
|
||||
}
|
||||
}
|
||||
|
||||
export function parseOldAuth(x: z.infer<typeof V0_SCHEMA>): z.infer<typeof V1_SCHEMA>["auth"] {
|
||||
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 }
|
||||
}
|
||||
|
||||
export default defineVersion({
|
||||
initial: false,
|
||||
schema: V1_SCHEMA,
|
||||
up(old: z.infer<typeof V0_SCHEMA>) {
|
||||
const { url, path, headers, params, name, method, preRequestScript, testScript } = old
|
||||
|
||||
const endpoint = `${url}${path}`
|
||||
const body = parseRequestBody(old)
|
||||
const auth = parseOldAuth(old)
|
||||
|
||||
const result: z.infer<typeof V1_SCHEMA> = {
|
||||
v: "1",
|
||||
endpoint,
|
||||
headers,
|
||||
params,
|
||||
name,
|
||||
method,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
body,
|
||||
auth,
|
||||
}
|
||||
|
||||
if (old.id) result.id = old.id
|
||||
|
||||
return result
|
||||
},
|
||||
})
|
||||
@@ -2,7 +2,7 @@
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"module": "esnext",
|
||||
"lib": ["esnext"],
|
||||
"lib": ["esnext", "DOM"],
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"strict": true,
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"module": "esnext",
|
||||
"lib": ["esnext"],
|
||||
"lib": ["esnext", "DOM"],
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"strict": true,
|
||||
"strictNullChecks": true,
|
||||
"skipLibCheck": true,
|
||||
"resolveJsonModule": true,
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["src/*.ts"]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module.exports = {
|
||||
preset: "ts-jest",
|
||||
testEnvironment: "node",
|
||||
testEnvironment: "jsdom",
|
||||
collectCoverage: true,
|
||||
setupFilesAfterEnv: ["./jest.setup.ts"],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user