feat: move pre request script to new state system
This commit is contained in:
@@ -106,6 +106,7 @@ export default defineComponent({
|
|||||||
method,
|
method,
|
||||||
params,
|
params,
|
||||||
headers,
|
headers,
|
||||||
|
preRequestScript: "",
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
getGlobalEnvironment,
|
getGlobalEnvironment,
|
||||||
} from "~/newstore/environments"
|
} from "~/newstore/environments"
|
||||||
|
|
||||||
export default function getEnvironmentVariablesFromScript(script: any) {
|
export default function getEnvironmentVariablesFromScript(script: string) {
|
||||||
const _variables: Record<string, string> = {}
|
const _variables: Record<string, string> = {}
|
||||||
|
|
||||||
const currentEnv = getCurrentEnvironment()
|
const currentEnv = getCurrentEnvironment()
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export interface HoppRESTRequest {
|
|||||||
endpoint: string
|
endpoint: string
|
||||||
params: HoppRESTParam[]
|
params: HoppRESTParam[]
|
||||||
headers: HoppRESTHeader[]
|
headers: HoppRESTHeader[]
|
||||||
|
preRequestScript: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makeRESTRequest(
|
export function makeRESTRequest(
|
||||||
@@ -54,12 +55,17 @@ export function translateToNewRequest(x: any): HoppRESTRequest {
|
|||||||
|
|
||||||
const method = x.method
|
const method = x.method
|
||||||
|
|
||||||
return {
|
const preRequestScript = x.preRequestScript
|
||||||
|
|
||||||
|
const result: HoppRESTRequest = {
|
||||||
endpoint,
|
endpoint,
|
||||||
headers,
|
headers,
|
||||||
params,
|
params,
|
||||||
method,
|
method,
|
||||||
|
preRequestScript,
|
||||||
v: RESTReqSchemaVersion,
|
v: RESTReqSchemaVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { pluck, distinctUntilChanged, map, filter } from "rxjs/operators"
|
import { pluck, distinctUntilChanged, map, filter } from "rxjs/operators"
|
||||||
|
import { customRef, onBeforeUnmount, Ref } from "@nuxtjs/composition-api"
|
||||||
|
import { Subscription } from "rxjs"
|
||||||
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
|
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
|
||||||
import {
|
import {
|
||||||
HoppRESTHeader,
|
HoppRESTHeader,
|
||||||
@@ -17,7 +19,7 @@ function getParamsInURL(url: string): { key: string; value: string }[] {
|
|||||||
uriObj.searchParams.forEach((value, key) => {
|
uriObj.searchParams.forEach((value, key) => {
|
||||||
result.push({ key, value })
|
result.push({ key, value })
|
||||||
})
|
})
|
||||||
} catch (_e) {}
|
} catch (_e) { }
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -121,6 +123,7 @@ const defaultRESTSession: RESTSession = {
|
|||||||
params: [],
|
params: [],
|
||||||
headers: [],
|
headers: [],
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
preRequestScript: "// pw.env.set('variable', 'value');",
|
||||||
},
|
},
|
||||||
response: null,
|
response: null,
|
||||||
}
|
}
|
||||||
@@ -287,6 +290,14 @@ const dispatchers = defineDispatchers({
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
setPreRequestScript(curr: RESTSession, { newScript }: { newScript: string }) {
|
||||||
|
return {
|
||||||
|
request: {
|
||||||
|
...curr.request,
|
||||||
|
preRequestScript: newScript,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
updateResponse(
|
updateResponse(
|
||||||
_curr: RESTSession,
|
_curr: RESTSession,
|
||||||
{ updatedRes }: { updatedRes: HoppRESTResponse | null }
|
{ updatedRes }: { updatedRes: HoppRESTResponse | null }
|
||||||
@@ -405,6 +416,15 @@ export function deleteAllRESTHeaders() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setPreRequestScript(newScript: string) {
|
||||||
|
restSessionStore.dispatch({
|
||||||
|
dispatcher: "setPreRequestScript",
|
||||||
|
payload: {
|
||||||
|
newScript,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function updateRESTResponse(updatedRes: HoppRESTResponse | null) {
|
export function updateRESTResponse(updatedRes: HoppRESTResponse | null) {
|
||||||
restSessionStore.dispatch({
|
restSessionStore.dispatch({
|
||||||
dispatcher: "updateResponse",
|
dispatcher: "updateResponse",
|
||||||
@@ -454,6 +474,11 @@ export const restActiveHeadersCount$ = restHeaders$.pipe(
|
|||||||
map((params) => params.filter((x) => x.active).length)
|
map((params) => params.filter((x) => x.active).length)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export const restPreRequestScript$ = restSessionStore.subject$.pipe(
|
||||||
|
pluck("request", "preRequestScript"),
|
||||||
|
distinctUntilChanged()
|
||||||
|
)
|
||||||
|
|
||||||
export const restResponse$ = restSessionStore.subject$.pipe(
|
export const restResponse$ = restSessionStore.subject$.pipe(
|
||||||
pluck("response"),
|
pluck("response"),
|
||||||
distinctUntilChanged()
|
distinctUntilChanged()
|
||||||
@@ -465,3 +490,37 @@ export const completedRESTResponse$ = restResponse$.pipe(
|
|||||||
res !== null && res.type !== "loading" && res.type !== "network_fail"
|
res !== null && res.type !== "loading" && res.type !== "network_fail"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Vue 3 composable function that gives access to a ref
|
||||||
|
* which is updated to the preRequestScript value in the store.
|
||||||
|
* The ref value is kept in sync with the store and all writes
|
||||||
|
* to the ref are dispatched to the store as `setPreRequestScript`
|
||||||
|
* dispatches.
|
||||||
|
*/
|
||||||
|
export function usePreRequestScript(): Ref<string> {
|
||||||
|
let sub: Subscription | null = null
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
if (sub) {
|
||||||
|
sub.unsubscribe()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return customRef((track, trigger) => {
|
||||||
|
sub = restPreRequestScript$.subscribe(() => {
|
||||||
|
trigger()
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
get() {
|
||||||
|
track()
|
||||||
|
return restSessionStore.value.request.preRequestScript
|
||||||
|
},
|
||||||
|
set(value: string) {
|
||||||
|
trigger()
|
||||||
|
setPreRequestScript(value)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,7 +31,19 @@
|
|||||||
<span class="select-wrapper">
|
<span class="select-wrapper">
|
||||||
<input
|
<input
|
||||||
id="contentType"
|
id="contentType"
|
||||||
class="bg-primary rounded-lg flex font-semibold font-mono text-xs w-full py-2 px-4 transition truncate focus:outline-none"
|
class="
|
||||||
|
bg-primary
|
||||||
|
rounded-lg
|
||||||
|
flex
|
||||||
|
font-semibold font-mono
|
||||||
|
text-xs
|
||||||
|
w-full
|
||||||
|
py-2
|
||||||
|
px-4
|
||||||
|
transition
|
||||||
|
truncate
|
||||||
|
focus:outline-none
|
||||||
|
"
|
||||||
v-model="contentType"
|
v-model="contentType"
|
||||||
readonly
|
readonly
|
||||||
/>
|
/>
|
||||||
@@ -309,7 +321,17 @@
|
|||||||
>
|
>
|
||||||
<AppSection v-if="showPreRequestScript" label="preRequest">
|
<AppSection v-if="showPreRequestScript" label="preRequest">
|
||||||
<div
|
<div
|
||||||
class="bg-primary border-b border-dividerLight flex flex-1 pl-4 top-110px z-10 sticky items-center justify-between"
|
class="
|
||||||
|
bg-primary
|
||||||
|
border-b border-dividerLight
|
||||||
|
flex flex-1
|
||||||
|
pl-4
|
||||||
|
top-110px
|
||||||
|
z-10
|
||||||
|
sticky
|
||||||
|
items-center
|
||||||
|
justify-between
|
||||||
|
"
|
||||||
>
|
>
|
||||||
<label class="font-semibold text-xs">
|
<label class="font-semibold text-xs">
|
||||||
{{ $t("javascript_code") }}
|
{{ $t("javascript_code") }}
|
||||||
@@ -340,7 +362,17 @@
|
|||||||
<SmartTab :id="'tests'" :label="$t('tests')">
|
<SmartTab :id="'tests'" :label="$t('tests')">
|
||||||
<AppSection v-if="testsEnabled" label="postRequestTests">
|
<AppSection v-if="testsEnabled" label="postRequestTests">
|
||||||
<div
|
<div
|
||||||
class="bg-primary border-b border-dividerLight flex flex-1 pl-4 top-110px z-10 sticky items-center justify-between"
|
class="
|
||||||
|
bg-primary
|
||||||
|
border-b border-dividerLight
|
||||||
|
flex flex-1
|
||||||
|
pl-4
|
||||||
|
top-110px
|
||||||
|
z-10
|
||||||
|
sticky
|
||||||
|
items-center
|
||||||
|
justify-between
|
||||||
|
"
|
||||||
>
|
>
|
||||||
<label class="font-semibold text-xs">
|
<label class="font-semibold text-xs">
|
||||||
{{ $t("javascript_code") }}
|
{{ $t("javascript_code") }}
|
||||||
@@ -537,6 +569,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
import { defineComponent } from "@nuxtjs/composition-api"
|
||||||
import { Splitpanes, Pane } from "splitpanes"
|
import { Splitpanes, Pane } from "splitpanes"
|
||||||
import "splitpanes/dist/splitpanes.css"
|
import "splitpanes/dist/splitpanes.css"
|
||||||
|
|
||||||
@@ -566,19 +599,24 @@ import {
|
|||||||
restRequest$,
|
restRequest$,
|
||||||
restActiveParamsCount$,
|
restActiveParamsCount$,
|
||||||
restActiveHeadersCount$,
|
restActiveHeadersCount$,
|
||||||
|
usePreRequestScript,
|
||||||
} from "~/newstore/RESTSession"
|
} from "~/newstore/RESTSession"
|
||||||
import { map } from "rxjs/operators"
|
import { map } from "rxjs/operators"
|
||||||
|
|
||||||
export default {
|
export default defineComponent({
|
||||||
components: { Splitpanes, Pane },
|
components: { Splitpanes, Pane },
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
return {
|
||||||
|
preRequestScript: usePreRequestScript(),
|
||||||
|
}
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
showCurlImportModal: false,
|
showCurlImportModal: false,
|
||||||
showPreRequestScript: true,
|
showPreRequestScript: true,
|
||||||
testsEnabled: true,
|
testsEnabled: true,
|
||||||
testScript: "// pw.expect('variable').toBe('value');",
|
testScript: "// pw.expect('variable').toBe('value');",
|
||||||
preRequestScript: "// pw.env.set('variable', 'value');",
|
|
||||||
testReports: [],
|
testReports: [],
|
||||||
copyButton: '<i class="material-icons">content_copy</i>',
|
copyButton: '<i class="material-icons">content_copy</i>',
|
||||||
downloadButton: '<i class="material-icons">save_alt</i>',
|
downloadButton: '<i class="material-icons">save_alt</i>',
|
||||||
@@ -1864,5 +1902,5 @@ export default {
|
|||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
document.removeEventListener("keydown", this._keyListener)
|
document.removeEventListener("keydown", this._keyListener)
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user