123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- import { Environment } from "@hoppscotch/data"
- import { cloneDeep } from "lodash"
- import isEqual from "lodash/isEqual"
- import { combineLatest, Observable } from "rxjs"
- import { distinctUntilChanged, map, pluck } from "rxjs/operators"
- import DispatchingStore, {
- defineDispatchers,
- } from "~/newstore/DispatchingStore"
- const defaultEnvironmentsState = {
- environments: [
- {
- name: "My Environment Variables",
- variables: [],
- },
- ] as Environment[],
- globals: [] as Environment["variables"],
- // 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,
- }
- },
- appendEnvironments(
- { environments }: EnvironmentStore,
- { envs }: { envs: Environment[] }
- ) {
- return {
- environments: [...environments, ...envs],
- }
- },
- replaceEnvironments(
- _: EnvironmentStore,
- { environments }: { environments: Environment[] }
- ) {
- return {
- environments,
- }
- },
- createEnvironment(
- { environments }: EnvironmentStore,
- { name }: { name: string }
- ) {
- return {
- environments: [
- ...environments,
- {
- name,
- variables: [],
- },
- ],
- }
- },
- duplicateEnvironment(
- { environments }: EnvironmentStore,
- { envIndex }: { envIndex: number }
- ) {
- const newEnvironment = environments.find((_, index) => index === envIndex)
- if (!newEnvironment) {
- return {
- environments,
- }
- }
- return {
- environments: [
- ...environments,
- {
- ...cloneDeep(newEnvironment),
- name: `${newEnvironment.name} - Duplicate`,
- },
- ],
- }
- },
- deleteEnvironment(
- { environments, currentEnvironmentIndex }: EnvironmentStore,
- { envIndex }: { envIndex: number }
- ) {
- let newCurrEnvIndex = currentEnvironmentIndex
- // Scenario 1: Currently Selected Env is removed -> Set currently selected to none
- if (envIndex === currentEnvironmentIndex) newCurrEnvIndex = -1
- // Scenario 2: Currently Selected Env Index > Deletion Index -> Current Selection Index Shifts One Position to the left -> Correct Env Index by moving back 1 index
- if (envIndex < currentEnvironmentIndex)
- newCurrEnvIndex = currentEnvironmentIndex - 1
- // Scenario 3: Currently Selected Env Index < Deletion Index -> No change happens at selection position -> Noop
- return {
- environments: environments.filter((_, index) => index !== envIndex),
- currentEnvironmentIndex: newCurrEnvIndex,
- }
- },
- 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
- ),
- }
- },
- setGlobalVariables(_, { entries }: { entries: Environment["variables"] }) {
- return {
- globals: entries,
- }
- },
- clearGlobalVariables() {
- return {
- globals: [],
- }
- },
- addGlobalVariable(
- { globals },
- { entry }: { entry: Environment["variables"][number] }
- ) {
- return {
- globals: [...globals, entry],
- }
- },
- removeGlobalVariable({ globals }, { envIndex }: { envIndex: number }) {
- return {
- globals: globals.filter((_, i) => i !== envIndex),
- }
- },
- updateGlobalVariable(
- { globals },
- {
- envIndex,
- updatedEntry,
- }: { envIndex: number; updatedEntry: Environment["variables"][number] }
- ) {
- return {
- globals: globals.map((x, i) => (i !== envIndex ? x : updatedEntry)),
- }
- },
- })
- export const environmentsStore = new DispatchingStore(
- defaultEnvironmentsState,
- dispatchers
- )
- export const environments$ = environmentsStore.subject$.pipe(
- pluck("environments"),
- distinctUntilChanged()
- )
- export const globalEnv$ = environmentsStore.subject$.pipe(
- pluck("globals"),
- distinctUntilChanged()
- )
- export const selectedEnvIndex$ = environmentsStore.subject$.pipe(
- pluck("currentEnvironmentIndex"),
- distinctUntilChanged()
- )
- export const currentEnvironment$ = environmentsStore.subject$.pipe(
- map(({ currentEnvironmentIndex, environments }) => {
- if (currentEnvironmentIndex === -1) {
- const env: Environment = {
- name: "No environment",
- variables: [],
- }
- return env
- } else {
- return environments[currentEnvironmentIndex]
- }
- })
- )
- export type AggregateEnvironment = {
- key: string
- value: string
- sourceEnv: string
- }
- /**
- * Stream returning all the environment variables accessible in
- * the current state (Global + The Selected Environment).
- * NOTE: The source environment attribute will be "Global" for Global Env as source.
- */
- export const aggregateEnvs$: Observable<AggregateEnvironment[]> = combineLatest(
- [currentEnvironment$, globalEnv$]
- ).pipe(
- map(([selectedEnv, globalVars]) => {
- const results: AggregateEnvironment[] = []
- selectedEnv.variables.forEach(({ key, value }) =>
- results.push({ key, value, sourceEnv: selectedEnv.name })
- )
- globalVars.forEach(({ key, value }) =>
- results.push({ key, value, sourceEnv: "Global" })
- )
- return results
- }),
- distinctUntilChanged(isEqual)
- )
- export function getAggregateEnvs() {
- const currentEnv = getCurrentEnvironment()
- return [
- ...currentEnv.variables.map(
- (x) =>
- <AggregateEnvironment>{
- key: x.key,
- value: x.value,
- sourceEnv: currentEnv.name,
- }
- ),
- ...getGlobalVariables().map(
- (x) =>
- <AggregateEnvironment>{
- key: x.key,
- value: x.value,
- sourceEnv: "Global",
- }
- ),
- ]
- }
- 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 getLegacyGlobalEnvironment(): Environment | null {
- const envs = environmentsStore.value.environments
- const el = envs.find(
- (env) => env.name === "globals" || env.name === "Globals"
- )
- return el ?? null
- }
- export function getGlobalVariables(): Environment["variables"] {
- return environmentsStore.value.globals
- }
- export function addGlobalEnvVariable(entry: Environment["variables"][number]) {
- environmentsStore.dispatch({
- dispatcher: "addGlobalVariable",
- payload: {
- entry,
- },
- })
- }
- export function setGlobalEnvVariables(entries: Environment["variables"]) {
- environmentsStore.dispatch({
- dispatcher: "setGlobalVariables",
- payload: {
- entries,
- },
- })
- }
- export function clearGlobalEnvVariables() {
- environmentsStore.dispatch({
- dispatcher: "clearGlobalVariables",
- payload: {},
- })
- }
- export function removeGlobalEnvVariable(envIndex: number) {
- environmentsStore.dispatch({
- dispatcher: "removeGlobalVariable",
- payload: {
- envIndex,
- },
- })
- }
- export function updateGlobalEnvVariable(
- envIndex: number,
- updatedEntry: Environment["variables"][number]
- ) {
- environmentsStore.dispatch({
- dispatcher: "updateGlobalVariable",
- payload: {
- envIndex,
- updatedEntry,
- },
- })
- }
- export function replaceEnvironments(newEnvironments: any[]) {
- environmentsStore.dispatch({
- dispatcher: "replaceEnvironments",
- payload: {
- environments: newEnvironments,
- },
- })
- }
- export function appendEnvironments(envs: Environment[]) {
- environmentsStore.dispatch({
- dispatcher: "appendEnvironments",
- payload: {
- envs,
- },
- })
- }
- export function createEnvironment(envName: string) {
- environmentsStore.dispatch({
- dispatcher: "createEnvironment",
- payload: {
- name: envName,
- },
- })
- }
- export function duplicateEnvironment(envIndex: number) {
- environmentsStore.dispatch({
- dispatcher: "duplicateEnvironment",
- payload: {
- envIndex,
- },
- })
- }
- 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,
- },
- })
- }
- export function getEnviroment(index: number) {
- return environmentsStore.value.environments[index]
- }
|