feat: request variables (#3825)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -1,293 +0,0 @@
|
||||
import {
|
||||
FormDataKeyValue,
|
||||
HoppRESTAuth,
|
||||
HoppRESTHeader,
|
||||
HoppRESTParam,
|
||||
HoppRESTReqBody,
|
||||
HoppRESTRequest,
|
||||
RESTReqSchemaVersion,
|
||||
ValidContentTypes,
|
||||
} from "@hoppscotch/data"
|
||||
import { BehaviorSubject, combineLatest, map } from "rxjs"
|
||||
import { applyBodyTransition } from "~/helpers/rules/BodyTransition"
|
||||
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
||||
|
||||
export class RESTRequest {
|
||||
public v$ = new BehaviorSubject<typeof RESTReqSchemaVersion>(
|
||||
RESTReqSchemaVersion
|
||||
)
|
||||
public name$ = new BehaviorSubject("Untitled")
|
||||
public endpoint$ = new BehaviorSubject("https://echo.hoppscotch.io/")
|
||||
public params$ = new BehaviorSubject<HoppRESTParam[]>([])
|
||||
public headers$ = new BehaviorSubject<HoppRESTHeader[]>([])
|
||||
public method$ = new BehaviorSubject("GET")
|
||||
public auth$ = new BehaviorSubject<HoppRESTAuth>({
|
||||
authType: "none",
|
||||
authActive: true,
|
||||
})
|
||||
public preRequestScript$ = new BehaviorSubject("")
|
||||
public testScript$ = new BehaviorSubject("")
|
||||
public body$ = new BehaviorSubject<HoppRESTReqBody>({
|
||||
contentType: null,
|
||||
body: null,
|
||||
})
|
||||
|
||||
public response$ = new BehaviorSubject<HoppRESTResponse | null>(null)
|
||||
|
||||
get request$() {
|
||||
// any of above changes construct requests
|
||||
return combineLatest([
|
||||
this.v$,
|
||||
this.name$,
|
||||
this.endpoint$,
|
||||
this.params$,
|
||||
this.headers$,
|
||||
this.method$,
|
||||
this.auth$,
|
||||
this.preRequestScript$,
|
||||
this.testScript$,
|
||||
this.body$,
|
||||
]).pipe(
|
||||
map(
|
||||
([
|
||||
v,
|
||||
name,
|
||||
endpoint,
|
||||
params,
|
||||
headers,
|
||||
method,
|
||||
auth,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
body,
|
||||
]) => ({
|
||||
v,
|
||||
name,
|
||||
endpoint,
|
||||
params,
|
||||
headers,
|
||||
method,
|
||||
auth,
|
||||
preRequestScript,
|
||||
testScript,
|
||||
body,
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
get contentType$() {
|
||||
return this.body$.pipe(map((body) => body.contentType))
|
||||
}
|
||||
|
||||
get bodyContent$() {
|
||||
return this.body$.pipe(map((body) => body.body))
|
||||
}
|
||||
|
||||
get headersCount$() {
|
||||
return this.headers$.pipe(
|
||||
map(
|
||||
(params) =>
|
||||
params.filter((x) => x.active && (x.key !== "" || x.value !== ""))
|
||||
.length
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
get paramsCount$() {
|
||||
return this.params$.pipe(
|
||||
map(
|
||||
(params) =>
|
||||
params.filter((x) => x.active && (x.key !== "" || x.value !== ""))
|
||||
.length
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
setName(name: string) {
|
||||
this.name$.next(name)
|
||||
}
|
||||
|
||||
setEndpoint(newURL: string) {
|
||||
this.endpoint$.next(newURL)
|
||||
}
|
||||
|
||||
setMethod(newMethod: string) {
|
||||
this.method$.next(newMethod)
|
||||
}
|
||||
|
||||
setParams(entries: HoppRESTParam[]) {
|
||||
this.params$.next(entries)
|
||||
}
|
||||
|
||||
addParam(newParam: HoppRESTParam) {
|
||||
const newParams = this.params$.value.concat(newParam)
|
||||
this.params$.next(newParams)
|
||||
}
|
||||
|
||||
updateParam(index: number, updatedParam: HoppRESTParam) {
|
||||
const newParams = this.params$.value.map((param, i) =>
|
||||
i === index ? updatedParam : param
|
||||
)
|
||||
this.params$.next(newParams)
|
||||
}
|
||||
|
||||
deleteParam(index: number) {
|
||||
const newParams = this.params$.value.filter((_, i) => i !== index)
|
||||
this.params$.next(newParams)
|
||||
}
|
||||
|
||||
deleteAllParams() {
|
||||
this.params$.next([])
|
||||
}
|
||||
|
||||
setHeaders(entries: HoppRESTHeader[]) {
|
||||
this.headers$.next(entries)
|
||||
}
|
||||
|
||||
addHeader(newHeader: HoppRESTHeader) {
|
||||
const newHeaders = this.headers$.value.concat(newHeader)
|
||||
this.headers$.next(newHeaders)
|
||||
}
|
||||
|
||||
updateHeader(index: number, updatedHeader: HoppRESTHeader) {
|
||||
const newHeaders = this.headers$.value.map((header, i) =>
|
||||
i === index ? updatedHeader : header
|
||||
)
|
||||
this.headers$.next(newHeaders)
|
||||
}
|
||||
|
||||
deleteHeader(index: number) {
|
||||
const newHeaders = this.headers$.value.filter((_, i) => i !== index)
|
||||
this.headers$.next(newHeaders)
|
||||
}
|
||||
|
||||
deleteAllHeaders() {
|
||||
this.headers$.next([])
|
||||
}
|
||||
|
||||
setContentType(newContentType: ValidContentTypes | null) {
|
||||
// TODO: persist body evenafter switching content typees
|
||||
this.body$.next(applyBodyTransition(this.body$.value, newContentType))
|
||||
}
|
||||
|
||||
setBody(newBody: string | FormDataKeyValue[] | null) {
|
||||
const body = { ...this.body$.value }
|
||||
body.body = newBody
|
||||
this.body$.next({ ...body })
|
||||
}
|
||||
|
||||
addFormDataEntry(entry: FormDataKeyValue) {
|
||||
if (this.body$.value.contentType !== "multipart/form-data") return {}
|
||||
const body: HoppRESTReqBody = {
|
||||
contentType: "multipart/form-data",
|
||||
body: [...this.body$.value.body, entry],
|
||||
}
|
||||
this.body$.next(body)
|
||||
}
|
||||
|
||||
deleteFormDataEntry(index: number) {
|
||||
// Only perform update if the current content-type is formdata
|
||||
if (this.body$.value.contentType !== "multipart/form-data") return {}
|
||||
|
||||
const body: HoppRESTReqBody = {
|
||||
contentType: "multipart/form-data",
|
||||
body: [...this.body$.value.body.filter((_, i) => i !== index)],
|
||||
}
|
||||
|
||||
this.body$.next(body)
|
||||
}
|
||||
|
||||
updateFormDataEntry(index: number, entry: FormDataKeyValue) {
|
||||
// Only perform update if the current content-type is formdata
|
||||
if (this.body$.value.contentType !== "multipart/form-data") return {}
|
||||
|
||||
const body: HoppRESTReqBody = {
|
||||
contentType: "multipart/form-data",
|
||||
body: [
|
||||
...this.body$.value.body.map((oldEntry, i) =>
|
||||
i === index ? entry : oldEntry
|
||||
),
|
||||
],
|
||||
}
|
||||
this.body$.next(body)
|
||||
}
|
||||
|
||||
deleteAllFormDataEntries() {
|
||||
// Only perform update if the current content-type is formdata
|
||||
if (this.body$.value.contentType !== "multipart/form-data") return {}
|
||||
|
||||
const body: HoppRESTReqBody = {
|
||||
contentType: "multipart/form-data",
|
||||
body: [],
|
||||
}
|
||||
this.body$.next(body)
|
||||
}
|
||||
|
||||
setRequestBody(newBody: HoppRESTReqBody) {
|
||||
this.body$.next(newBody)
|
||||
}
|
||||
|
||||
setAuth(newAuth: HoppRESTAuth) {
|
||||
this.auth$.next(newAuth)
|
||||
}
|
||||
|
||||
setPreRequestScript(newScript: string) {
|
||||
this.preRequestScript$.next(newScript)
|
||||
}
|
||||
|
||||
setTestScript(newScript: string) {
|
||||
this.testScript$.next(newScript)
|
||||
}
|
||||
|
||||
updateResponse(response: HoppRESTResponse | null) {
|
||||
this.response$.next(response)
|
||||
}
|
||||
|
||||
setRequest(request: HoppRESTRequest) {
|
||||
this.v$.next(RESTReqSchemaVersion)
|
||||
this.name$.next(request.name)
|
||||
this.endpoint$.next(request.endpoint)
|
||||
this.params$.next(request.params)
|
||||
this.headers$.next(request.headers)
|
||||
this.method$.next(request.method)
|
||||
this.auth$.next(request.auth)
|
||||
this.preRequestScript$.next(request.preRequestScript)
|
||||
this.testScript$.next(request.testScript)
|
||||
this.body$.next(request.body)
|
||||
}
|
||||
|
||||
getRequest() {
|
||||
return {
|
||||
v: this.v$.value,
|
||||
name: this.name$.value,
|
||||
endpoint: this.endpoint$.value,
|
||||
params: this.params$.value,
|
||||
headers: this.headers$.value,
|
||||
method: this.method$.value,
|
||||
auth: this.auth$.value,
|
||||
preRequestScript: this.preRequestScript$.value,
|
||||
testScript: this.testScript$.value,
|
||||
body: this.body$.value,
|
||||
}
|
||||
}
|
||||
|
||||
resetRequest() {
|
||||
this.v$.next(RESTReqSchemaVersion)
|
||||
this.name$.next("")
|
||||
this.endpoint$.next("")
|
||||
this.params$.next([])
|
||||
this.headers$.next([])
|
||||
this.method$.next("GET")
|
||||
this.auth$.next({
|
||||
authType: "none",
|
||||
authActive: false,
|
||||
})
|
||||
this.preRequestScript$.next("")
|
||||
this.testScript$.next("")
|
||||
this.body$.next({
|
||||
contentType: null,
|
||||
body: null,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import { Environment } from "@hoppscotch/data"
|
||||
import {
|
||||
Environment,
|
||||
HoppRESTHeaders,
|
||||
HoppRESTRequestVariable,
|
||||
} from "@hoppscotch/data"
|
||||
import { SandboxTestResult, TestDescriptor } from "@hoppscotch/js-sandbox"
|
||||
import { runTestScript } from "@hoppscotch/js-sandbox/web"
|
||||
import * as A from "fp-ts/Array"
|
||||
@@ -65,10 +69,17 @@ const getTestableBody = (
|
||||
return x
|
||||
}
|
||||
|
||||
const combineEnvVariables = (envs: {
|
||||
global: Environment["variables"]
|
||||
selected: Environment["variables"]
|
||||
}) => [...envs.selected, ...envs.global]
|
||||
const combineEnvVariables = (variables: {
|
||||
environments: {
|
||||
selected: Environment["variables"]
|
||||
global: Environment["variables"]
|
||||
}
|
||||
requestVariables: Environment["variables"]
|
||||
}) => [
|
||||
...variables.requestVariables,
|
||||
...variables.environments.selected,
|
||||
...variables.environments.global,
|
||||
]
|
||||
|
||||
export const executedResponses$ = new Subject<
|
||||
HoppRESTResponse & { type: "success" | "fail " }
|
||||
@@ -122,6 +133,38 @@ const updateEnvironmentsWithSecret = (
|
||||
return updatedEnv
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the environment list to a list with unique keys with value
|
||||
* @param envs The environment list to be transformed
|
||||
* @returns The transformed environment list with keys with value
|
||||
*/
|
||||
const filterNonEmptyEnvironmentVariables = (
|
||||
envs: Environment["variables"]
|
||||
): Environment["variables"] => {
|
||||
const envsMap = new Map<string, Environment["variables"][number]>()
|
||||
|
||||
envs.forEach((env) => {
|
||||
if (env.secret) {
|
||||
envsMap.set(env.key, env)
|
||||
} else if (envsMap.has(env.key)) {
|
||||
const existingEnv = envsMap.get(env.key)
|
||||
|
||||
if (
|
||||
existingEnv &&
|
||||
"value" in existingEnv &&
|
||||
existingEnv.value === "" &&
|
||||
env.value !== ""
|
||||
) {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
} else {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
})
|
||||
|
||||
return Array.from(envsMap.values())
|
||||
}
|
||||
|
||||
export function runRESTRequest$(
|
||||
tab: Ref<HoppTab<HoppRESTDocument>>
|
||||
): [
|
||||
@@ -175,15 +218,40 @@ export function runRESTRequest$(
|
||||
requestHeaders = [...tab.value.document.request.headers]
|
||||
}
|
||||
|
||||
const finalRequestVariables =
|
||||
tab.value.document.request.requestVariables.map(
|
||||
(v: HoppRESTRequestVariable) => {
|
||||
if (v.active) {
|
||||
return {
|
||||
key: v.key,
|
||||
value: v.value,
|
||||
secret: false,
|
||||
}
|
||||
}
|
||||
return []
|
||||
}
|
||||
)
|
||||
|
||||
const finalRequest = {
|
||||
...tab.value.document.request,
|
||||
auth: requestAuth ?? { authType: "none", authActive: false },
|
||||
headers: requestHeaders,
|
||||
headers: requestHeaders as HoppRESTHeaders,
|
||||
}
|
||||
|
||||
const finalEnvs = {
|
||||
requestVariables: finalRequestVariables as Environment["variables"],
|
||||
environments: envs.right,
|
||||
}
|
||||
|
||||
const finalEnvsWithNonEmptyValues = filterNonEmptyEnvironmentVariables(
|
||||
combineEnvVariables(finalEnvs)
|
||||
)
|
||||
|
||||
const effectiveRequest = getEffectiveRESTRequest(finalRequest, {
|
||||
id: "env-id",
|
||||
v: 1,
|
||||
name: "Env",
|
||||
variables: combineEnvVariables(envs.right),
|
||||
variables: finalEnvsWithNonEmptyValues,
|
||||
})
|
||||
|
||||
const [stream, cancelRun] = createRESTNetworkRequestStream(effectiveRequest)
|
||||
|
||||
@@ -38,6 +38,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -141,6 +142,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -158,6 +160,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -181,6 +184,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -209,6 +213,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -239,6 +244,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -268,6 +274,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -291,6 +298,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -312,6 +320,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -335,6 +344,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -366,6 +376,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -430,6 +441,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -458,6 +470,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -515,6 +528,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -560,6 +574,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -607,6 +622,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -621,6 +637,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -645,6 +662,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -662,6 +680,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -685,6 +704,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -702,6 +722,7 @@ const samples = [
|
||||
params: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -725,6 +746,7 @@ const samples = [
|
||||
],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -747,6 +769,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -767,6 +790,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -788,6 +812,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -808,6 +833,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
{
|
||||
@@ -839,6 +865,7 @@ const samples = [
|
||||
headers: [],
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
requestVariables: [],
|
||||
}),
|
||||
},
|
||||
]
|
||||
|
||||
@@ -181,5 +181,6 @@ export const parseCurlCommand = (curlCommand: string) => {
|
||||
testScript: defaultRESTReq.testScript,
|
||||
auth,
|
||||
body: finalBody,
|
||||
requestVariables: defaultRESTReq.requestVariables,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import { StreamSubscriberFunc } from "@composables/stream"
|
||||
import {
|
||||
AggregateEnvironment,
|
||||
aggregateEnvsWithSecrets$,
|
||||
getAggregateEnvsWithSecrets,
|
||||
getAggregateEnvs,
|
||||
getCurrentEnvironment,
|
||||
getSelectedEnvironmentType,
|
||||
} from "~/newstore/environments"
|
||||
@@ -23,15 +23,45 @@ import IconUsers from "~icons/lucide/users?raw"
|
||||
import IconEdit from "~icons/lucide/edit?raw"
|
||||
import { SecretEnvironmentService } from "~/services/secret-environment.service"
|
||||
import { getService } from "~/modules/dioc"
|
||||
import { RESTTabService } from "~/services/tab/rest"
|
||||
|
||||
const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g
|
||||
|
||||
const HOPP_ENV_HIGHLIGHT =
|
||||
"cursor-help transition rounded px-1 focus:outline-none mx-0.5 env-highlight"
|
||||
const HOPP_ENV_HIGHLIGHT_FOUND = "env-found"
|
||||
const HOPP_ENV_HIGHLIGHT_NOT_FOUND = "env-not-found"
|
||||
|
||||
const HOPP_REQUEST_VARIABLE_HIGHLIGHT = "request-variable-highlight"
|
||||
const HOPP_ENVIRONMENT_HIGHLIGHT = "environment-variable-highlight"
|
||||
const HOPP_GLOBAL_ENVIRONMENT_HIGHLIGHT = "global-variable-highlight"
|
||||
const HOPP_ENV_HIGHLIGHT_NOT_FOUND = "environment-not-found-highlight"
|
||||
|
||||
const secretEnvironmentService = getService(SecretEnvironmentService)
|
||||
const restTabs = getService(RESTTabService)
|
||||
|
||||
/**
|
||||
* Transforms the environment list to a list with unique keys with value
|
||||
* @param envs The environment list to be transformed
|
||||
* @returns The transformed environment list with keys with value
|
||||
*/
|
||||
const filterNonEmptyEnvironmentVariables = (
|
||||
envs: AggregateEnvironment[]
|
||||
): AggregateEnvironment[] => {
|
||||
const envsMap = new Map<string, AggregateEnvironment>()
|
||||
|
||||
envs.forEach((env) => {
|
||||
if (envsMap.has(env.key)) {
|
||||
const existingEnv = envsMap.get(env.key)
|
||||
|
||||
if (existingEnv?.value === "" && env.value !== "") {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
} else {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
})
|
||||
|
||||
return Array.from(envsMap.values())
|
||||
}
|
||||
|
||||
const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
hoverTooltip(
|
||||
@@ -67,7 +97,12 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
|
||||
const parsedEnvKey = text.slice(start - from, end - from)
|
||||
|
||||
const tooltipEnv = aggregateEnvs.find((env) => env.key === parsedEnvKey)
|
||||
const envsWithNoEmptyValues =
|
||||
filterNonEmptyEnvironmentVariables(aggregateEnvs)
|
||||
|
||||
const tooltipEnv = envsWithNoEmptyValues.find(
|
||||
(env) => env.key === parsedEnvKey
|
||||
)
|
||||
|
||||
const envName = tooltipEnv?.sourceEnv ?? "Choose an Environment"
|
||||
|
||||
@@ -95,7 +130,12 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
|
||||
const result = parseTemplateStringE(envValue, aggregateEnvs)
|
||||
|
||||
const finalEnv = E.isLeft(result) ? "error" : result.right
|
||||
let finalEnv = E.isLeft(result) ? "error" : result.right
|
||||
|
||||
// If the request variable has an secret variable
|
||||
// parseTemplateStringE is passed the secret value which has value undefined
|
||||
// So, we need to check if the result is undefined and then set the finalEnv to ******
|
||||
if (finalEnv === "undefined") finalEnv = "******"
|
||||
|
||||
const selectedEnvType = getSelectedEnvironmentType()
|
||||
|
||||
@@ -123,11 +163,16 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
invokeActionType = "modals.my.environment.edit"
|
||||
}
|
||||
|
||||
invokeAction(invokeActionType, {
|
||||
envName: tooltipEnv?.sourceEnv !== "Global" ? envName : "Global",
|
||||
variableName: parsedEnvKey,
|
||||
isSecret: tooltipEnv?.secret,
|
||||
})
|
||||
if (tooltipEnv?.sourceEnv === "RequestVariable") {
|
||||
restTabs.currentActiveTab.value.document.optionTabPreference =
|
||||
"requestVariables"
|
||||
} else {
|
||||
invokeAction(invokeActionType, {
|
||||
envName: tooltipEnv?.sourceEnv === "Global" ? "Global" : envName,
|
||||
variableName: parsedEnvKey,
|
||||
isSecret: tooltipEnv?.secret,
|
||||
})
|
||||
}
|
||||
})
|
||||
editIcon.innerHTML = `<span class="inline-flex items-center justify-center my-1">${IconEdit}</span>`
|
||||
tooltip.appendChild(editIcon)
|
||||
@@ -165,11 +210,16 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
)
|
||||
|
||||
function checkEnv(env: string, aggregateEnvs: AggregateEnvironment[]) {
|
||||
const className = aggregateEnvs.find(
|
||||
let className = HOPP_ENV_HIGHLIGHT_NOT_FOUND
|
||||
|
||||
const envSource = aggregateEnvs.find(
|
||||
(k: { key: string }) => k.key === env.slice(2, -2)
|
||||
)
|
||||
? HOPP_ENV_HIGHLIGHT_FOUND
|
||||
: HOPP_ENV_HIGHLIGHT_NOT_FOUND
|
||||
)?.sourceEnv
|
||||
|
||||
if (envSource === "RequestVariable")
|
||||
className = HOPP_REQUEST_VARIABLE_HIGHLIGHT
|
||||
else if (envSource === "Global") className = HOPP_GLOBAL_ENVIRONMENT_HIGHLIGHT
|
||||
else if (envSource !== undefined) className = HOPP_ENVIRONMENT_HIGHLIGHT
|
||||
|
||||
return Decoration.mark({
|
||||
class: `${HOPP_ENV_HIGHLIGHT} ${className}`,
|
||||
@@ -185,7 +235,10 @@ const getMatchDecorator = (aggregateEnvs: AggregateEnvironment[]) =>
|
||||
export const environmentHighlightStyle = (
|
||||
aggregateEnvs: AggregateEnvironment[]
|
||||
) => {
|
||||
const decorator = getMatchDecorator(aggregateEnvs)
|
||||
const envsWithNoEmptyValues =
|
||||
filterNonEmptyEnvironmentVariables(aggregateEnvs)
|
||||
|
||||
const decorator = getMatchDecorator(envsWithNoEmptyValues)
|
||||
|
||||
return ViewPlugin.define(
|
||||
(view) => ({
|
||||
@@ -209,10 +262,45 @@ export class HoppEnvironmentPlugin {
|
||||
subscribeToStream: StreamSubscriberFunc,
|
||||
private editorView: Ref<EditorView | undefined>
|
||||
) {
|
||||
this.envs = getAggregateEnvsWithSecrets()
|
||||
const aggregateEnvs = getAggregateEnvs()
|
||||
const currentTab = restTabs.currentActiveTab.value
|
||||
|
||||
watch(
|
||||
currentTab.document.request,
|
||||
(reqVariables) => {
|
||||
this.envs = [
|
||||
...reqVariables.requestVariables.map(({ key, value }) => ({
|
||||
key,
|
||||
value,
|
||||
sourceEnv: "RequestVariable",
|
||||
secret: false,
|
||||
})),
|
||||
...aggregateEnvs,
|
||||
]
|
||||
|
||||
this.editorView.value?.dispatch({
|
||||
effects: this.compartment.reconfigure([
|
||||
cursorTooltipField(this.envs),
|
||||
environmentHighlightStyle(this.envs),
|
||||
]),
|
||||
})
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
|
||||
subscribeToStream(aggregateEnvsWithSecrets$, (envs) => {
|
||||
this.envs = envs
|
||||
this.envs = [
|
||||
...currentTab.document.request.requestVariables.map(
|
||||
({ key, value }) => ({
|
||||
key,
|
||||
value,
|
||||
sourceEnv: "RequestVariable",
|
||||
secret: false,
|
||||
})
|
||||
),
|
||||
...envs,
|
||||
]
|
||||
|
||||
this.editorView.value?.dispatch({
|
||||
effects: this.compartment.reconfigure([
|
||||
cursorTooltipField(this.envs),
|
||||
@@ -251,7 +339,7 @@ export class HoppReactiveEnvPlugin {
|
||||
]),
|
||||
})
|
||||
},
|
||||
{ immediate: true }
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
knownContentTypes,
|
||||
makeCollection,
|
||||
makeRESTRequest,
|
||||
HoppRESTRequestVariable,
|
||||
} from "@hoppscotch/data"
|
||||
|
||||
import * as A from "fp-ts/Array"
|
||||
@@ -28,14 +29,27 @@ type UnwrapPromise<T extends Promise<any>> = T extends Promise<infer Y>
|
||||
type InsomniaDoc = UnwrapPromise<ReturnType<typeof convert>>
|
||||
type InsomniaResource = ImportRequest
|
||||
|
||||
// insomnia-importers v3.6.0 doesn't provide a type for path parameters and they have deprecated the library
|
||||
type InsomniaPathParameter = {
|
||||
name: string
|
||||
value: string
|
||||
}
|
||||
|
||||
type InsomniaFolderResource = ImportRequest & { _type: "request_group" }
|
||||
type InsomniaRequestResource = ImportRequest & { _type: "request" }
|
||||
type InsomniaRequestResource = ImportRequest & {
|
||||
_type: "request"
|
||||
} & {
|
||||
pathParameters?: InsomniaPathParameter[]
|
||||
}
|
||||
|
||||
const parseInsomniaDoc = (content: string) =>
|
||||
TO.tryCatch(() => convert(content))
|
||||
|
||||
const replacePathVarTemplating = (expression: string) =>
|
||||
expression.replaceAll(/:([^/]+)/g, "<<$1>>")
|
||||
|
||||
const replaceVarTemplating = (expression: string) =>
|
||||
replaceInsomniaTemplating(expression)
|
||||
pipe(expression, replacePathVarTemplating, replaceInsomniaTemplating)
|
||||
|
||||
const getFoldersIn = (
|
||||
folder: InsomniaFolderResource | null,
|
||||
@@ -177,6 +191,15 @@ const getHoppReqParams = (req: InsomniaRequestResource): HoppRESTParam[] =>
|
||||
active: !(param.disabled ?? false),
|
||||
})) ?? []
|
||||
|
||||
const getHoppReqVariables = (
|
||||
req: InsomniaRequestResource
|
||||
): HoppRESTRequestVariable[] =>
|
||||
req.pathParameters?.map((variable) => ({
|
||||
key: replaceVarTemplating(variable.name),
|
||||
value: replaceVarTemplating(variable.value ?? ""),
|
||||
active: true,
|
||||
})) ?? []
|
||||
|
||||
const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
|
||||
makeRESTRequest({
|
||||
name: req.name ?? "Untitled Request",
|
||||
@@ -189,6 +212,8 @@ const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
|
||||
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
|
||||
requestVariables: getHoppReqVariables(req),
|
||||
})
|
||||
|
||||
const getHoppFolder = (
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
makeRESTRequest,
|
||||
HoppCollection,
|
||||
makeCollection,
|
||||
HoppRESTRequestVariable,
|
||||
} from "@hoppscotch/data"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import * as A from "fp-ts/Array"
|
||||
@@ -82,6 +83,27 @@ const parseOpenAPIParams = (params: OpenAPIParamsType[]): HoppRESTParam[] =>
|
||||
)
|
||||
)
|
||||
|
||||
const parseOpenAPIVariables = (
|
||||
variables: OpenAPIParamsType[]
|
||||
): HoppRESTRequestVariable[] =>
|
||||
pipe(
|
||||
variables,
|
||||
|
||||
A.filterMap(
|
||||
flow(
|
||||
O.fromPredicate((param) => param.in === "path"),
|
||||
O.map(
|
||||
(param) =>
|
||||
<HoppRESTRequestVariable>{
|
||||
key: param.name,
|
||||
value: "", // TODO: Can we do anything more ? (parse default values maybe)
|
||||
active: true,
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
const parseOpenAPIHeaders = (params: OpenAPIParamsType[]): HoppRESTHeader[] =>
|
||||
pipe(
|
||||
params,
|
||||
@@ -577,6 +599,10 @@ const convertPathToHoppReqs = (
|
||||
|
||||
preRequestScript: "",
|
||||
testScript: "",
|
||||
|
||||
requestVariables: parseOpenAPIVariables(
|
||||
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
|
||||
),
|
||||
})
|
||||
}),
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
QueryParam,
|
||||
RequestAuthDefinition,
|
||||
VariableDefinition,
|
||||
Variable,
|
||||
} from "postman-collection"
|
||||
import {
|
||||
HoppRESTAuth,
|
||||
@@ -18,6 +19,7 @@ import {
|
||||
ValidContentTypes,
|
||||
knownContentTypes,
|
||||
FormDataKeyValue,
|
||||
HoppRESTRequestVariable,
|
||||
} from "@hoppscotch/data"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import * as S from "fp-ts/string"
|
||||
@@ -91,6 +93,25 @@ const getHoppReqParams = (item: Item): HoppRESTParam[] => {
|
||||
)
|
||||
}
|
||||
|
||||
const getHoppReqVariables = (item: Item) => {
|
||||
return pipe(
|
||||
item.request.url.variables.all(),
|
||||
A.filter(
|
||||
(variable): variable is Variable =>
|
||||
variable.key !== undefined &&
|
||||
variable.key !== null &&
|
||||
variable.key.length > 0
|
||||
),
|
||||
A.map((variable) => {
|
||||
return <HoppRESTRequestVariable>{
|
||||
key: replacePMVarTemplating(variable.key ?? ""),
|
||||
value: replacePMVarTemplating(variable.value ?? ""),
|
||||
active: !variable.disabled,
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
type PMRequestAuthDef<
|
||||
AuthType extends
|
||||
RequestAuthDefinition["type"] = RequestAuthDefinition["type"],
|
||||
@@ -280,6 +301,7 @@ const getHoppRequest = (item: Item): HoppRESTRequest => {
|
||||
params: getHoppReqParams(item),
|
||||
auth: getHoppReqAuth(item),
|
||||
body: getHoppReqBody(item),
|
||||
requestVariables: getHoppReqVariables(item),
|
||||
|
||||
// TODO: Decide about this
|
||||
preRequestScript: "",
|
||||
|
||||
@@ -14,8 +14,8 @@ import { SecretEnvironmentService } from "~/services/secret-environment.service"
|
||||
const secretEnvironmentService = getService(SecretEnvironmentService)
|
||||
|
||||
const unsecretEnvironments = (
|
||||
global: Environment["variables"],
|
||||
selected: Environment
|
||||
selected: Environment,
|
||||
global: Environment["variables"]
|
||||
) => {
|
||||
const resolvedGlobalWithSecrets = global.map((globalVar, index) => {
|
||||
const secretVar = secretEnvironmentService.getSecretEnvironmentVariable(
|
||||
@@ -65,8 +65,8 @@ const unsecretEnvironments = (
|
||||
|
||||
export const getCombinedEnvVariables = () => {
|
||||
const reformedVars = unsecretEnvironments(
|
||||
getGlobalVariables(),
|
||||
getCurrentEnvironment()
|
||||
getCurrentEnvironment(),
|
||||
getGlobalVariables()
|
||||
)
|
||||
return {
|
||||
global: cloneDeep(reformedVars.global),
|
||||
|
||||
@@ -17,4 +17,5 @@ export const getDefaultRESTRequest = (): HoppRESTRequest => ({
|
||||
contentType: null,
|
||||
body: null,
|
||||
},
|
||||
requestVariables: [],
|
||||
})
|
||||
|
||||
@@ -22,7 +22,6 @@ import {
|
||||
import { arrayFlatMap, arraySort } from "../functional/array"
|
||||
import { toFormData } from "../functional/formData"
|
||||
import { tupleWithSameKeysToRecord } from "../functional/record"
|
||||
import { getGlobalVariables } from "~/newstore/environments"
|
||||
|
||||
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
|
||||
/**
|
||||
@@ -34,6 +33,7 @@ export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
|
||||
effectiveFinalHeaders: { key: string; value: string }[]
|
||||
effectiveFinalParams: { key: string; value: string }[]
|
||||
effectiveFinalBody: FormData | string | null
|
||||
effectiveFinalRequestVariables: { key: string; value: string }[]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,38 +313,53 @@ export function getEffectiveRESTRequest(
|
||||
request: HoppRESTRequest,
|
||||
environment: Environment
|
||||
): EffectiveHoppRESTRequest {
|
||||
const envVariables = [...environment.variables, ...getGlobalVariables()]
|
||||
|
||||
const effectiveFinalHeaders = pipe(
|
||||
getComputedHeaders(request, envVariables).map((h) => h.header),
|
||||
getComputedHeaders(request, environment.variables).map((h) => h.header),
|
||||
A.concat(request.headers),
|
||||
A.filter((x) => x.active && x.key !== ""),
|
||||
A.map((x) => ({
|
||||
active: true,
|
||||
key: parseTemplateString(x.key, envVariables),
|
||||
value: parseTemplateString(x.value, envVariables),
|
||||
key: parseTemplateString(x.key, environment.variables),
|
||||
value: parseTemplateString(x.value, environment.variables),
|
||||
}))
|
||||
)
|
||||
|
||||
const effectiveFinalParams = pipe(
|
||||
getComputedParams(request, envVariables).map((p) => p.param),
|
||||
getComputedParams(request, environment.variables).map((p) => p.param),
|
||||
A.concat(request.params),
|
||||
A.filter((x) => x.active && x.key !== ""),
|
||||
A.map((x) => ({
|
||||
active: true,
|
||||
key: parseTemplateString(x.key, envVariables),
|
||||
value: parseTemplateString(x.value, envVariables),
|
||||
key: parseTemplateString(x.key, environment.variables),
|
||||
value: parseTemplateString(x.value, environment.variables),
|
||||
}))
|
||||
)
|
||||
|
||||
const effectiveFinalBody = getFinalBodyFromRequest(request, envVariables)
|
||||
const effectiveFinalRequestVariables = pipe(
|
||||
request.requestVariables,
|
||||
A.filter((x) => x.active && x.key !== ""),
|
||||
A.map((x) => ({
|
||||
active: true,
|
||||
key: parseTemplateString(x.key, environment.variables),
|
||||
value: parseTemplateString(x.value, environment.variables),
|
||||
}))
|
||||
)
|
||||
|
||||
const effectiveFinalBody = getFinalBodyFromRequest(
|
||||
request,
|
||||
environment.variables
|
||||
)
|
||||
|
||||
return {
|
||||
...request,
|
||||
effectiveFinalURL: parseTemplateString(request.endpoint, envVariables),
|
||||
effectiveFinalURL: parseTemplateString(
|
||||
request.endpoint,
|
||||
environment.variables
|
||||
),
|
||||
effectiveFinalHeaders,
|
||||
effectiveFinalParams,
|
||||
effectiveFinalBody,
|
||||
effectiveFinalRequestVariables,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user