diff --git a/components/environments/Add.vue b/components/environments/Add.vue
index d838ce31d..5f04aafd8 100644
--- a/components/environments/Add.vue
+++ b/components/environments/Add.vue
@@ -36,48 +36,26 @@
-
diff --git a/components/environments/Edit.vue b/components/environments/Edit.vue
index dc998a525..6b25f6b4e 100644
--- a/components/environments/Edit.vue
+++ b/components/environments/Edit.vue
@@ -32,7 +32,7 @@
@@ -113,60 +96,44 @@
-
diff --git a/components/environments/Environment.vue b/components/environments/Environment.vue
index 066c3edb9..cf610c2aa 100644
--- a/components/environments/Environment.vue
+++ b/components/environments/Environment.vue
@@ -40,11 +40,11 @@
-
diff --git a/components/environments/index.vue b/components/environments/index.vue
index 10933f0ea..777c665f8 100644
--- a/components/environments/index.vue
+++ b/components/environments/index.vue
@@ -75,8 +75,11 @@
diff --git a/helpers/fb.js b/helpers/fb.js
index 881a34cdd..450ee2f66 100644
--- a/helpers/fb.js
+++ b/helpers/fb.js
@@ -16,6 +16,7 @@ import {
graphqlCollectionStore,
setGraphqlCollections,
} from "~/newstore/collections"
+import { environments$, replaceEnvironments } from "~/newstore/environments"
// Initialize Firebase, copied from cloud console
const firebaseConfig = {
@@ -45,7 +46,6 @@ export class FirebaseInstance {
this.idToken = null
this.currentFeeds = []
this.currentSettings = []
- this.currentEnvironments = []
this.currentUser$ = new ReplaySubject(1)
this.idToken$ = new ReplaySubject(1)
@@ -55,6 +55,7 @@ export class FirebaseInstance {
let loadedGraphqlHistory = false
let loadedRESTCollections = false
let loadedGraphqlCollections = false
+ let loadedEnvironments = false
graphqlCollectionStore.subject$.subscribe(({ state }) => {
if (
@@ -113,7 +114,7 @@ export class FirebaseInstance {
})
settingsStore.dispatches$.subscribe((dispatch) => {
- if (this.currentSettings && loadedSettings) {
+ if (this.currentUser && loadedSettings) {
if (dispatch.dispatcher === "bulkApplySettings") {
Object.keys(dispatch.payload).forEach((key) => {
this.writeSettings(key, dispatch.payload[key])
@@ -127,6 +128,12 @@ export class FirebaseInstance {
}
})
+ environments$.subscribe((envs) => {
+ if (this.currentUser && loadedEnvironments) {
+ this.writeEnvironments(envs)
+ }
+ })
+
this.app.auth().onIdTokenChanged((user) => {
if (user) {
user.getIdToken().then((token) => {
@@ -292,9 +299,9 @@ export class FirebaseInstance {
environment.id = doc.id
environments.push(environment)
})
- if (environments.length > 0) {
- this.currentEnvironments = environments[0].environment
- }
+ loadedEnvironments = false
+ replaceEnvironments(environments[0].environment)
+ loadedEnvironments = true
})
} else {
this.currentUser = null
diff --git a/helpers/preRequest.js b/helpers/preRequest.ts
similarity index 50%
rename from helpers/preRequest.js
rename to helpers/preRequest.ts
index ad8581cfb..d8b10e9e4 100644
--- a/helpers/preRequest.js
+++ b/helpers/preRequest.ts
@@ -1,15 +1,34 @@
-export default function getEnvironmentVariablesFromScript(script) {
- const _variables = {}
+import {
+ getCurrentEnvironment,
+ getGlobalEnvironment,
+} from "~/newstore/environments"
+
+export default function getEnvironmentVariablesFromScript(script: any) {
+ const _variables: Record = {}
+
+ const currentEnv = getCurrentEnvironment()
+
+ for (const variable of currentEnv.variables) {
+ _variables[variable.key] = variable.value
+ }
+
+ const globalEnv = getGlobalEnvironment()
+
+ if (globalEnv) {
+ for (const variable of globalEnv.variables) {
+ _variables[variable.key] = variable.value
+ }
+ }
try {
// the pw object is the proxy by which pre-request scripts can pass variables to the request.
// for security and control purposes, this is the only way a pre-request script should modify variables.
const pw = {
environment: {
- set: (key, value) => (_variables[key] = value),
+ set: (key: string, value: string) => (_variables[key] = value),
},
env: {
- set: (key, value) => (_variables[key] = value),
+ set: (key: string, value: string) => (_variables[key] = value),
},
// globals that the script is allowed to have access to.
}
diff --git a/newstore/environments.ts b/newstore/environments.ts
new file mode 100644
index 000000000..8702bf496
--- /dev/null
+++ b/newstore/environments.ts
@@ -0,0 +1,330 @@
+import { pluck } from "rxjs/operators"
+import DispatchingStore, {
+ defineDispatchers,
+} from "~/newstore/DispatchingStore"
+
+export type Environment = {
+ name: string
+ variables: {
+ key: string
+ value: string
+ }[]
+}
+
+const defaultEnvironmentsState = {
+ environments: [
+ {
+ name: "My Environment Variables",
+ variables: [],
+ },
+ ] as Environment[],
+
+ // Current environment index specifies the index
+ // -1 means no environments are selected
+ currentEnvironmentIndex: -1,
+}
+
+type EnvironmentStore = typeof defaultEnvironmentsState
+
+const dispatchers = defineDispatchers({
+ setCurrentEnviromentIndex(
+ { environments }: EnvironmentStore,
+ { newIndex }: { newIndex: number }
+ ) {
+ if (newIndex >= environments.length || newIndex <= -2) {
+ console.log(
+ `Ignoring possibly invalid current environment index assignment (value: ${newIndex})`
+ )
+
+ return {}
+ }
+
+ return {
+ currentEnvironmentIndex: newIndex,
+ }
+ },
+ replaceEnvironments(
+ _: EnvironmentStore,
+ { environments }: { environments: Environment[] }
+ ) {
+ return {
+ environments,
+ }
+ },
+ createEnvironment(
+ { environments }: EnvironmentStore,
+ { name }: { name: string }
+ ) {
+ return {
+ environments: [
+ ...environments,
+ {
+ name,
+ variables: [],
+ },
+ ],
+ }
+ },
+ deleteEnvironment(
+ { environments, currentEnvironmentIndex }: EnvironmentStore,
+ { envIndex }: { envIndex: number }
+ ) {
+ return {
+ environments: environments.filter((_, index) => index !== envIndex),
+ currentEnvironmentIndex:
+ envIndex === currentEnvironmentIndex ? -1 : currentEnvironmentIndex,
+ }
+ },
+ renameEnvironment(
+ { environments }: EnvironmentStore,
+ { envIndex, newName }: { envIndex: number; newName: string }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex
+ ? {
+ ...env,
+ name: newName,
+ }
+ : env
+ ),
+ }
+ },
+ updateEnvironment(
+ { environments }: EnvironmentStore,
+ { envIndex, updatedEnv }: { envIndex: number; updatedEnv: Environment }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex ? updatedEnv : env
+ ),
+ }
+ },
+ addEnvironmentVariable(
+ { environments }: EnvironmentStore,
+ { envIndex, key, value }: { envIndex: number; key: string; value: string }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex
+ ? {
+ ...env,
+ variables: [...env.variables, { key, value }],
+ }
+ : env
+ ),
+ }
+ },
+ removeEnvironmentVariable(
+ { environments }: EnvironmentStore,
+ { envIndex, variableIndex }: { envIndex: number; variableIndex: number }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex
+ ? {
+ ...env,
+ variables: env.variables.filter(
+ (_, vIndex) => vIndex !== variableIndex
+ ),
+ }
+ : env
+ ),
+ }
+ },
+ setEnvironmentVariables(
+ { environments }: EnvironmentStore,
+ {
+ envIndex,
+ vars,
+ }: { envIndex: number; vars: { key: string; value: string }[] }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex
+ ? {
+ ...env,
+ variables: vars,
+ }
+ : env
+ ),
+ }
+ },
+ updateEnvironmentVariable(
+ { environments }: EnvironmentStore,
+ {
+ envIndex,
+ variableIndex,
+ updatedKey,
+ updatedValue,
+ }: {
+ envIndex: number
+ variableIndex: number
+ updatedKey: string
+ updatedValue: string
+ }
+ ) {
+ return {
+ environments: environments.map((env, index) =>
+ index === envIndex
+ ? {
+ ...env,
+ variables: env.variables.map((v, vIndex) =>
+ vIndex === variableIndex
+ ? { key: updatedKey, value: updatedValue }
+ : v
+ ),
+ }
+ : env
+ ),
+ }
+ },
+})
+
+export const environmentsStore = new DispatchingStore(
+ defaultEnvironmentsState,
+ dispatchers
+)
+
+export const environments$ = environmentsStore.subject$.pipe(
+ pluck("environments")
+)
+
+export const selectedEnvIndex$ = environmentsStore.subject$.pipe(
+ pluck("currentEnvironmentIndex")
+)
+
+export function getCurrentEnvironment(): Environment {
+ if (environmentsStore.value.currentEnvironmentIndex === -1) {
+ return {
+ name: "No Environment",
+ variables: [],
+ }
+ }
+
+ return environmentsStore.value.environments[
+ environmentsStore.value.currentEnvironmentIndex
+ ]
+}
+
+export function setCurrentEnvironment(newEnvIndex: number) {
+ environmentsStore.dispatch({
+ dispatcher: "setCurrentEnviromentIndex",
+ payload: {
+ newIndex: newEnvIndex,
+ },
+ })
+}
+
+export function getGlobalEnvironment(): Environment | null {
+ const envs = environmentsStore.value.environments
+
+ const el = envs.find(
+ (env) => env.name === "globals" || env.name === "Globals"
+ )
+
+ return el ?? null
+}
+
+export function replaceEnvironments(newEnvironments: any[]) {
+ environmentsStore.dispatch({
+ dispatcher: "replaceEnvironments",
+ payload: {
+ environments: newEnvironments,
+ },
+ })
+}
+
+export function createEnvironment(envName: string) {
+ environmentsStore.dispatch({
+ dispatcher: "createEnvironment",
+ payload: {
+ name: envName,
+ },
+ })
+}
+
+export function deleteEnvironment(envIndex: number) {
+ environmentsStore.dispatch({
+ dispatcher: "deleteEnvironment",
+ payload: {
+ envIndex,
+ },
+ })
+}
+
+export function renameEnvironment(envIndex: number, newName: string) {
+ environmentsStore.dispatch({
+ dispatcher: "renameEnvironment",
+ payload: {
+ envIndex,
+ newName,
+ },
+ })
+}
+
+export function updateEnvironment(envIndex: number, updatedEnv: Environment) {
+ environmentsStore.dispatch({
+ dispatcher: "updateEnvironment",
+ payload: {
+ envIndex,
+ updatedEnv,
+ },
+ })
+}
+
+export function setEnvironmentVariables(
+ envIndex: number,
+ vars: { key: string; value: string }[]
+) {
+ environmentsStore.dispatch({
+ dispatcher: "setEnvironmentVariables",
+ payload: {
+ envIndex,
+ vars,
+ },
+ })
+}
+
+export function addEnvironmentVariable(
+ envIndex: number,
+ { key, value }: { key: string; value: string }
+) {
+ environmentsStore.dispatch({
+ dispatcher: "addEnvironmentVariable",
+ payload: {
+ envIndex,
+ key,
+ value,
+ },
+ })
+}
+
+export function removeEnvironmentVariable(
+ envIndex: number,
+ variableIndex: number
+) {
+ environmentsStore.dispatch({
+ dispatcher: "removeEnvironmentVariable",
+ payload: {
+ envIndex,
+ variableIndex,
+ },
+ })
+}
+
+export function updateEnvironmentVariable(
+ envIndex: number,
+ variableIndex: number,
+ { key, value }: { key: string; value: string }
+) {
+ environmentsStore.dispatch({
+ dispatcher: "updateEnvironmentVariable",
+ payload: {
+ envIndex,
+ variableIndex,
+ updatedKey: key,
+ updatedValue: value,
+ },
+ })
+}
diff --git a/newstore/localpersistence.ts b/newstore/localpersistence.ts
index f59890c51..ea7436ce4 100644
--- a/newstore/localpersistence.ts
+++ b/newstore/localpersistence.ts
@@ -14,6 +14,7 @@ import {
setGraphqlCollections,
setRESTCollections,
} from "./collections"
+import { replaceEnvironments, environments$ } from "./environments"
function checkAndMigrateOldSettings() {
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
@@ -44,6 +45,14 @@ function checkAndMigrateOldSettings() {
delete vuexData.postwoman.collectionsGraphql
window.localStorage.setItem("vuex", JSON.stringify(vuexData))
}
+
+ if (vuexData.postwoman && vuexData.postwoman.environments) {
+ const envs = vuexData.postwoman.environments
+ window.localStorage.setItem("environments", JSON.stringify(envs))
+
+ delete vuexData.postwoman.environments
+ window.localStorage.setItem("vuex", JSON.stringify(vuexData))
+ }
}
function setupSettingsPersistence() {
@@ -102,10 +111,23 @@ function setupCollectionsPersistence() {
})
}
+function setupEnvironmentsPersistence() {
+ const environmentsData = JSON.parse(
+ window.localStorage.getItem("environments") || "[]"
+ )
+
+ replaceEnvironments(environmentsData)
+
+ environments$.subscribe((envs) => {
+ window.localStorage.setItem("environments", JSON.stringify(envs))
+ })
+}
+
export function setupLocalPersistence() {
checkAndMigrateOldSettings()
setupSettingsPersistence()
setupHistoryPersistence()
setupCollectionsPersistence()
+ setupEnvironmentsPersistence()
}
diff --git a/pages/index.vue b/pages/index.vue
index 1fdcb4881..2059ca4ca 100644
--- a/pages/index.vue
+++ b/pages/index.vue
@@ -567,7 +567,7 @@
-
+
@@ -1217,29 +1217,6 @@ export default {
},
},
methods: {
- useSelectedEnvironment(args) {
- let environment = args.environment
- let environments = args.environments
- let preRequestScriptString = ""
- for (let variable of environment.variables) {
- preRequestScriptString += `pw.env.set('${variable.key}', '${variable.value}');\n`
- }
- for (let env of environments) {
- if (env.name === environment.name) {
- continue
- }
-
- if (env.name === "Globals" || env.name === "globals") {
- preRequestScriptString += this.useSelectedEnvironment({
- environment: env,
- environments,
- })
- }
- }
- this.preRequestScript = preRequestScriptString
- this.showPreRequestScript = true
- return preRequestScriptString
- },
checkCollections() {
const checkCollectionAvailability =
this.$store.state.postwoman.collections &&