feat: effective url environment templates work with headers, url and params

This commit is contained in:
Andrew Bastin
2021-07-31 22:21:14 -04:00
parent 7354951d5a
commit fcb194d08a
4 changed files with 55 additions and 29 deletions

View File

@@ -99,7 +99,7 @@
icon="import_export" icon="import_export"
@click.native=" @click.native="
showCurlImportModal = !showCurlImportModal showCurlImportModal = !showCurlImportModal
$refs.sendOptions.tippy().hide() sendOptions.tippy().hide()
" "
/> />
<SmartItem <SmartItem
@@ -107,7 +107,7 @@
icon="code" icon="code"
@click.native=" @click.native="
showCodegenModal = !showCodegenModal showCodegenModal = !showCodegenModal
$refs.sendOptions.tippy().hide() sendOptions.tippy().hide()
" "
/> />
<SmartItem <SmartItem
@@ -116,7 +116,7 @@
icon="clear_all" icon="clear_all"
@click.native=" @click.native="
clearContent() clearContent()
$refs.sendOptions.tippy().hide() sendOptions.tippy().hide()
" "
/> />
</tippy> </tippy>
@@ -158,7 +158,7 @@
:icon="hasNavigatorShare ? 'share' : 'content_copy'" :icon="hasNavigatorShare ? 'share' : 'content_copy'"
@click.native=" @click.native="
copyRequest() copyRequest()
$refs.saveOptions.tippy().hide() saveOptions.tippy().hide()
" "
/> />
<SmartItem <SmartItem
@@ -167,7 +167,7 @@
icon="create_new_folder" icon="create_new_folder"
@click.native=" @click.native="
showSaveRequestModal = true showSaveRequestModal = true
$refs.saveOptions.tippy().hide() saveOptions.tippy().hide()
" "
/> />
</tippy> </tippy>
@@ -240,7 +240,10 @@ export default defineComponent({
const hasNavigatorShare = !!navigator.share const hasNavigatorShare = !!navigator.share
const options = ref<Vue | null>(null) // Template refs
//
const options = ref<any | null>(null)
const saveOptions = ref<any | null>(null)
const newSendRequest = () => { const newSendRequest = () => {
loading.value = true loading.value = true
@@ -276,7 +279,7 @@ export default defineComponent({
const onSelectMethod = (method: string) => { const onSelectMethod = (method: string) => {
updateMethod(method) updateMethod(method)
// Vue-tippy has no typescript support yet // Vue-tippy has no typescript support yet
;(options.value as any).tippy().hide() options.value.tippy().hide()
} }
const clearContent = () => { const clearContent = () => {
@@ -365,7 +368,9 @@ export default defineComponent({
EXPERIMENTAL_URL_BAR_ENABLED: useSetting("EXPERIMENTAL_URL_BAR_ENABLED"), EXPERIMENTAL_URL_BAR_ENABLED: useSetting("EXPERIMENTAL_URL_BAR_ENABLED"),
// Template refs
options, options,
saveOptions,
} }
}, },
}) })

View File

@@ -1,10 +0,0 @@
export default function parseTemplateString(string, variables) {
if (!variables || !string) {
return string
}
const searchTerm = /<<([^>]*)>>/g // "<<myVariable>>"
return decodeURI(encodeURI(string)).replace(
searchTerm,
(_, p1) => variables[p1] || ""
)
}

15
helpers/templating.ts Normal file
View File

@@ -0,0 +1,15 @@
import { Environment } from "~/newstore/environments"
export default function parseTemplateString(
str: string,
variables: Environment["variables"]
) {
if (!variables || !str) {
return str
}
const searchTerm = /<<([^>]*)>>/g // "<<myVariable>>"
return decodeURI(encodeURI(str)).replace(
searchTerm,
(_, p1) => variables.find((x) => x.key === p1)?.value || ""
)
}

View File

@@ -1,6 +1,7 @@
import { combineLatest, Observable } from "rxjs" import { combineLatest, Observable } from "rxjs"
import { map } from "rxjs/operators" import { map } from "rxjs/operators"
import { HoppRESTRequest } from "../types/HoppRESTRequest" import { HoppRESTRequest } from "../types/HoppRESTRequest"
import parseTemplateString from "../templating"
import { Environment } from "~/newstore/environments" import { Environment } from "~/newstore/environments"
export interface EffectiveHoppRESTRequest extends HoppRESTRequest { export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
@@ -24,22 +25,37 @@ export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
*/ */
export function getEffectiveRESTRequest( export function getEffectiveRESTRequest(
request: HoppRESTRequest, request: HoppRESTRequest,
_environment: Environment environment: Environment
): EffectiveHoppRESTRequest { ): EffectiveHoppRESTRequest {
// TODO: Change this
return { return {
...request, ...request,
effectiveFinalURL: request.endpoint, effectiveFinalURL: parseTemplateString(
effectiveFinalHeaders: request.headers.filter( request.endpoint,
environment.variables
),
effectiveFinalHeaders: request.headers
.filter(
(x) => (x) =>
x.key !== "" && // Remove empty keys x.key !== "" && // Remove empty keys
x.active // Only active x.active // Only active
), )
effectiveFinalParams: request.params.filter( .map((x) => ({
// Parse out environment template strings
active: true,
key: parseTemplateString(x.key, environment.variables),
value: parseTemplateString(x.value, environment.variables),
})),
effectiveFinalParams: request.params
.filter(
(x) => (x) =>
x.key !== "" && // Remove empty keys x.key !== "" && // Remove empty keys
x.active // Only active x.active // Only active
), )
.map((x) => ({
active: true,
key: parseTemplateString(x.key, environment.variables),
value: parseTemplateString(x.value, environment.variables),
})),
} }
} }