refactor: move rawKeyValue and templating to hopp/data + rewrite rawKeyValue parsing
This commit is contained in:
@@ -8,13 +8,13 @@ import {
|
||||
ViewPlugin,
|
||||
} from "@codemirror/view"
|
||||
import * as E from "fp-ts/Either"
|
||||
import { parseTemplateStringE } from "@hoppscotch/data"
|
||||
import { StreamSubscriberFunc } from "~/helpers/utils/composables"
|
||||
import {
|
||||
AggregateEnvironment,
|
||||
aggregateEnvs$,
|
||||
getAggregateEnvs,
|
||||
} from "~/newstore/environments"
|
||||
import { parseTemplateStringE } from "~/helpers/templating"
|
||||
|
||||
const HOPP_ENVIRONMENT_REGEX = /(<<\w+>>)/g
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Environment } from "@hoppscotch/data"
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
@@ -7,7 +8,6 @@ import {
|
||||
} from "firebase/firestore"
|
||||
import { currentUser$ } from "./auth"
|
||||
import {
|
||||
Environment,
|
||||
environments$,
|
||||
globalEnv$,
|
||||
replaceEnvironments,
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import * as A from "fp-ts/Array"
|
||||
import * as RA from "fp-ts/ReadonlyArray"
|
||||
import * as S from "fp-ts/string"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import { stringArrayJoin } from "./functional/array"
|
||||
|
||||
export type RawKeyValueEntry = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
|
||||
const parseRawKeyValueEntry = (str: string): RawKeyValueEntry => {
|
||||
const trimmed = str.trim()
|
||||
const inactive = trimmed.startsWith("#")
|
||||
|
||||
const [key, value] = trimmed.split(":").map(S.trim)
|
||||
|
||||
return {
|
||||
key: inactive ? key.replaceAll(/^#+\s*/g, "") : key, // Remove comment hash and early space
|
||||
value,
|
||||
active: !inactive,
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRawKeyValueEntries = flow(
|
||||
S.split("\n"),
|
||||
RA.filter((x) => x.trim().length > 0), // Remove lines which are empty
|
||||
RA.map(parseRawKeyValueEntry),
|
||||
RA.toArray
|
||||
)
|
||||
|
||||
export const rawKeyValueEntriesToString = (entries: RawKeyValueEntry[]) =>
|
||||
pipe(
|
||||
entries,
|
||||
A.map(({ key, value, active }) =>
|
||||
active ? `${key}: ${value}` : `# ${key}: ${value}`
|
||||
),
|
||||
stringArrayJoin("\n")
|
||||
)
|
||||
@@ -9,12 +9,10 @@ import {
|
||||
FormDataKeyValue,
|
||||
HoppRESTReqBody,
|
||||
ValidContentTypes,
|
||||
} from "@hoppscotch/data"
|
||||
import {
|
||||
parseRawKeyValueEntries,
|
||||
rawKeyValueEntriesToString,
|
||||
RawKeyValueEntry,
|
||||
} from "../rawKeyValue"
|
||||
} from "@hoppscotch/data"
|
||||
|
||||
const ANY_TYPE = Symbol("TRANSITION_RULESET_IGNORE_TYPE")
|
||||
// eslint-disable-next-line no-redeclare
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
import * as E from "fp-ts/Either"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { Environment } from "~/newstore/environments"
|
||||
|
||||
const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<<myVariable>>"
|
||||
|
||||
/**
|
||||
* How much times can we expand environment variables
|
||||
*/
|
||||
const ENV_MAX_EXPAND_LIMIT = 10
|
||||
|
||||
/**
|
||||
* Error state when there is a suspected loop while
|
||||
* recursively expanding variables
|
||||
*/
|
||||
const ENV_EXPAND_LOOP = "ENV_EXPAND_LOOP" as const
|
||||
|
||||
export function parseBodyEnvVariablesE(
|
||||
body: string,
|
||||
env: Environment["variables"]
|
||||
) {
|
||||
let result = body
|
||||
let depth = 0
|
||||
|
||||
while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) {
|
||||
result = result.replace(/<<\w+>>/g, (key) => {
|
||||
const found = env.find(
|
||||
(envVar) => envVar.key === key.replace(/[<>]/g, "")
|
||||
)
|
||||
return found ? found.value : key
|
||||
})
|
||||
|
||||
depth++
|
||||
}
|
||||
|
||||
return depth > ENV_MAX_EXPAND_LIMIT
|
||||
? E.left(ENV_EXPAND_LOOP)
|
||||
: E.right(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `parseBodyEnvVariablesE` instead.
|
||||
*/
|
||||
export const parseBodyEnvVariables = (
|
||||
body: string,
|
||||
env: Environment["variables"]
|
||||
) =>
|
||||
pipe(
|
||||
parseBodyEnvVariablesE(body, env),
|
||||
E.getOrElse(() => body)
|
||||
)
|
||||
|
||||
export function parseTemplateStringE(
|
||||
str: string,
|
||||
variables: Environment["variables"]
|
||||
) {
|
||||
if (!variables || !str) {
|
||||
return E.right(str)
|
||||
}
|
||||
|
||||
let result = str
|
||||
let depth = 0
|
||||
|
||||
while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) {
|
||||
result = decodeURI(encodeURI(result)).replace(
|
||||
REGEX_ENV_VAR,
|
||||
(_, p1) => variables.find((x) => x.key === p1)?.value || ""
|
||||
)
|
||||
depth++
|
||||
}
|
||||
|
||||
return depth > ENV_MAX_EXPAND_LIMIT
|
||||
? E.left(ENV_EXPAND_LOOP)
|
||||
: E.right(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `parseTemplateStringE` instead
|
||||
*/
|
||||
export const parseTemplateString = (
|
||||
str: string,
|
||||
variables: Environment["variables"]
|
||||
) =>
|
||||
pipe(
|
||||
parseTemplateStringE(str, variables),
|
||||
E.getOrElse(() => str)
|
||||
)
|
||||
@@ -7,13 +7,15 @@ import {
|
||||
FormDataKeyValue,
|
||||
HoppRESTReqBody,
|
||||
HoppRESTRequest,
|
||||
parseTemplateString,
|
||||
parseBodyEnvVariables,
|
||||
parseRawKeyValueEntries,
|
||||
Environment,
|
||||
} from "@hoppscotch/data"
|
||||
import { parseTemplateString, parseBodyEnvVariables } from "../templating"
|
||||
import { arrayFlatMap, arraySort } from "../functional/array"
|
||||
import { toFormData } from "../functional/formData"
|
||||
import { tupleToRecord } from "../functional/record"
|
||||
import { parseRawKeyValueEntries } from "../rawKeyValue"
|
||||
import { Environment, getGlobalVariables } from "~/newstore/environments"
|
||||
import { getGlobalVariables } from "~/newstore/environments"
|
||||
|
||||
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user