-
+
@@ -61,7 +58,6 @@ export default {
},
watch: {
active(color) {
-
localStorage.setItem("THEME_COLOR", color)
},
},
@@ -72,7 +68,7 @@ export default {
this.active = color
},
capitalized(color) {
- return `${color.charAt(0).toUpperCase()}${color.slice(1)}`
+ return `${color.charAt(0).toUpperCase()}${color.slice(1)}`
},
},
}
diff --git a/helpers/fb.js b/helpers/fb.js
index 881a34cdd..730e25857 100644
--- a/helpers/fb.js
+++ b/helpers/fb.js
@@ -16,6 +16,7 @@ import {
graphqlCollectionStore,
setGraphqlCollections,
} from "~/newstore/collections"
+import { environments$ } from "~/newstore/environments"
// Initialize Firebase, copied from cloud console
const firebaseConfig = {
@@ -55,6 +56,7 @@ export class FirebaseInstance {
let loadedGraphqlHistory = false
let loadedRESTCollections = false
let loadedGraphqlCollections = false
+ const loadedEnvironments = false
graphqlCollectionStore.subject$.subscribe(({ state }) => {
if (
@@ -113,7 +115,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 +129,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) => {
diff --git a/helpers/preRequest.js b/helpers/preRequest.ts
similarity index 60%
rename from helpers/preRequest.js
rename to helpers/preRequest.ts
index ad8581cfb..d8dd5466d 100644
--- a/helpers/preRequest.js
+++ b/helpers/preRequest.ts
@@ -1,15 +1,23 @@
-export default function getEnvironmentVariablesFromScript(script) {
- const _variables = {}
+import { getCurrentEnvironment } 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
+ }
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..4010a9be2
--- /dev/null
+++ b/newstore/environments.ts
@@ -0,0 +1,320 @@
+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 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 907ab9e4c..1c089e36b 100644
--- a/pages/index.vue
+++ b/pages/index.vue
@@ -541,7 +541,7 @@
-
+
@@ -1191,29 +1191,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 &&