Initial environment state system refactor

This commit is contained in:
Andrew Bastin
2021-06-04 22:41:07 -04:00
parent c3f713c0bd
commit 5bfeb541fc
11 changed files with 430 additions and 226 deletions

View File

@@ -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) => {

View File

@@ -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<string, string> = {}
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.
}