environments.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. import { Environment } from "@hoppscotch/data"
  2. import { cloneDeep } from "lodash"
  3. import isEqual from "lodash/isEqual"
  4. import { combineLatest, Observable } from "rxjs"
  5. import { distinctUntilChanged, map, pluck } from "rxjs/operators"
  6. import DispatchingStore, {
  7. defineDispatchers,
  8. } from "~/newstore/DispatchingStore"
  9. const defaultEnvironmentsState = {
  10. environments: [
  11. {
  12. name: "My Environment Variables",
  13. variables: [],
  14. },
  15. ] as Environment[],
  16. globals: [] as Environment["variables"],
  17. // Current environment index specifies the index
  18. // -1 means no environments are selected
  19. currentEnvironmentIndex: -1,
  20. }
  21. type EnvironmentStore = typeof defaultEnvironmentsState
  22. const dispatchers = defineDispatchers({
  23. setCurrentEnviromentIndex(
  24. { environments }: EnvironmentStore,
  25. { newIndex }: { newIndex: number }
  26. ) {
  27. if (newIndex >= environments.length || newIndex <= -2) {
  28. // console.log(
  29. // `Ignoring possibly invalid current environment index assignment (value: ${newIndex})`
  30. // )
  31. return {}
  32. }
  33. return {
  34. currentEnvironmentIndex: newIndex,
  35. }
  36. },
  37. appendEnvironments(
  38. { environments }: EnvironmentStore,
  39. { envs }: { envs: Environment[] }
  40. ) {
  41. return {
  42. environments: [...environments, ...envs],
  43. }
  44. },
  45. replaceEnvironments(
  46. _: EnvironmentStore,
  47. { environments }: { environments: Environment[] }
  48. ) {
  49. return {
  50. environments,
  51. }
  52. },
  53. createEnvironment(
  54. { environments }: EnvironmentStore,
  55. { name }: { name: string }
  56. ) {
  57. return {
  58. environments: [
  59. ...environments,
  60. {
  61. name,
  62. variables: [],
  63. },
  64. ],
  65. }
  66. },
  67. duplicateEnvironment(
  68. { environments }: EnvironmentStore,
  69. { envIndex }: { envIndex: number }
  70. ) {
  71. const newEnvironment = environments.find((_, index) => index === envIndex)
  72. if (!newEnvironment) {
  73. return {
  74. environments,
  75. }
  76. }
  77. return {
  78. environments: [
  79. ...environments,
  80. {
  81. ...cloneDeep(newEnvironment),
  82. name: `${newEnvironment.name} - Duplicate`,
  83. },
  84. ],
  85. }
  86. },
  87. deleteEnvironment(
  88. { environments, currentEnvironmentIndex }: EnvironmentStore,
  89. { envIndex }: { envIndex: number }
  90. ) {
  91. let newCurrEnvIndex = currentEnvironmentIndex
  92. // Scenario 1: Currently Selected Env is removed -> Set currently selected to none
  93. if (envIndex === currentEnvironmentIndex) newCurrEnvIndex = -1
  94. // 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
  95. if (envIndex < currentEnvironmentIndex)
  96. newCurrEnvIndex = currentEnvironmentIndex - 1
  97. // Scenario 3: Currently Selected Env Index < Deletion Index -> No change happens at selection position -> Noop
  98. return {
  99. environments: environments.filter((_, index) => index !== envIndex),
  100. currentEnvironmentIndex: newCurrEnvIndex,
  101. }
  102. },
  103. renameEnvironment(
  104. { environments }: EnvironmentStore,
  105. { envIndex, newName }: { envIndex: number; newName: string }
  106. ) {
  107. return {
  108. environments: environments.map((env, index) =>
  109. index === envIndex
  110. ? {
  111. ...env,
  112. name: newName,
  113. }
  114. : env
  115. ),
  116. }
  117. },
  118. updateEnvironment(
  119. { environments }: EnvironmentStore,
  120. { envIndex, updatedEnv }: { envIndex: number; updatedEnv: Environment }
  121. ) {
  122. return {
  123. environments: environments.map((env, index) =>
  124. index === envIndex ? updatedEnv : env
  125. ),
  126. }
  127. },
  128. addEnvironmentVariable(
  129. { environments }: EnvironmentStore,
  130. { envIndex, key, value }: { envIndex: number; key: string; value: string }
  131. ) {
  132. return {
  133. environments: environments.map((env, index) =>
  134. index === envIndex
  135. ? {
  136. ...env,
  137. variables: [...env.variables, { key, value }],
  138. }
  139. : env
  140. ),
  141. }
  142. },
  143. removeEnvironmentVariable(
  144. { environments }: EnvironmentStore,
  145. { envIndex, variableIndex }: { envIndex: number; variableIndex: number }
  146. ) {
  147. return {
  148. environments: environments.map((env, index) =>
  149. index === envIndex
  150. ? {
  151. ...env,
  152. variables: env.variables.filter(
  153. (_, vIndex) => vIndex !== variableIndex
  154. ),
  155. }
  156. : env
  157. ),
  158. }
  159. },
  160. setEnvironmentVariables(
  161. { environments }: EnvironmentStore,
  162. {
  163. envIndex,
  164. vars,
  165. }: { envIndex: number; vars: { key: string; value: string }[] }
  166. ) {
  167. return {
  168. environments: environments.map((env, index) =>
  169. index === envIndex
  170. ? {
  171. ...env,
  172. variables: vars,
  173. }
  174. : env
  175. ),
  176. }
  177. },
  178. updateEnvironmentVariable(
  179. { environments }: EnvironmentStore,
  180. {
  181. envIndex,
  182. variableIndex,
  183. updatedKey,
  184. updatedValue,
  185. }: {
  186. envIndex: number
  187. variableIndex: number
  188. updatedKey: string
  189. updatedValue: string
  190. }
  191. ) {
  192. return {
  193. environments: environments.map((env, index) =>
  194. index === envIndex
  195. ? {
  196. ...env,
  197. variables: env.variables.map((v, vIndex) =>
  198. vIndex === variableIndex
  199. ? { key: updatedKey, value: updatedValue }
  200. : v
  201. ),
  202. }
  203. : env
  204. ),
  205. }
  206. },
  207. setGlobalVariables(_, { entries }: { entries: Environment["variables"] }) {
  208. return {
  209. globals: entries,
  210. }
  211. },
  212. clearGlobalVariables() {
  213. return {
  214. globals: [],
  215. }
  216. },
  217. addGlobalVariable(
  218. { globals },
  219. { entry }: { entry: Environment["variables"][number] }
  220. ) {
  221. return {
  222. globals: [...globals, entry],
  223. }
  224. },
  225. removeGlobalVariable({ globals }, { envIndex }: { envIndex: number }) {
  226. return {
  227. globals: globals.filter((_, i) => i !== envIndex),
  228. }
  229. },
  230. updateGlobalVariable(
  231. { globals },
  232. {
  233. envIndex,
  234. updatedEntry,
  235. }: { envIndex: number; updatedEntry: Environment["variables"][number] }
  236. ) {
  237. return {
  238. globals: globals.map((x, i) => (i !== envIndex ? x : updatedEntry)),
  239. }
  240. },
  241. })
  242. export const environmentsStore = new DispatchingStore(
  243. defaultEnvironmentsState,
  244. dispatchers
  245. )
  246. export const environments$ = environmentsStore.subject$.pipe(
  247. pluck("environments"),
  248. distinctUntilChanged()
  249. )
  250. export const globalEnv$ = environmentsStore.subject$.pipe(
  251. pluck("globals"),
  252. distinctUntilChanged()
  253. )
  254. export const selectedEnvIndex$ = environmentsStore.subject$.pipe(
  255. pluck("currentEnvironmentIndex"),
  256. distinctUntilChanged()
  257. )
  258. export const currentEnvironment$ = environmentsStore.subject$.pipe(
  259. map(({ currentEnvironmentIndex, environments }) => {
  260. if (currentEnvironmentIndex === -1) {
  261. const env: Environment = {
  262. name: "No environment",
  263. variables: [],
  264. }
  265. return env
  266. } else {
  267. return environments[currentEnvironmentIndex]
  268. }
  269. })
  270. )
  271. export type AggregateEnvironment = {
  272. key: string
  273. value: string
  274. sourceEnv: string
  275. }
  276. /**
  277. * Stream returning all the environment variables accessible in
  278. * the current state (Global + The Selected Environment).
  279. * NOTE: The source environment attribute will be "Global" for Global Env as source.
  280. */
  281. export const aggregateEnvs$: Observable<AggregateEnvironment[]> = combineLatest(
  282. [currentEnvironment$, globalEnv$]
  283. ).pipe(
  284. map(([selectedEnv, globalVars]) => {
  285. const results: AggregateEnvironment[] = []
  286. selectedEnv.variables.forEach(({ key, value }) =>
  287. results.push({ key, value, sourceEnv: selectedEnv.name })
  288. )
  289. globalVars.forEach(({ key, value }) =>
  290. results.push({ key, value, sourceEnv: "Global" })
  291. )
  292. return results
  293. }),
  294. distinctUntilChanged(isEqual)
  295. )
  296. export function getAggregateEnvs() {
  297. const currentEnv = getCurrentEnvironment()
  298. return [
  299. ...currentEnv.variables.map(
  300. (x) =>
  301. <AggregateEnvironment>{
  302. key: x.key,
  303. value: x.value,
  304. sourceEnv: currentEnv.name,
  305. }
  306. ),
  307. ...getGlobalVariables().map(
  308. (x) =>
  309. <AggregateEnvironment>{
  310. key: x.key,
  311. value: x.value,
  312. sourceEnv: "Global",
  313. }
  314. ),
  315. ]
  316. }
  317. export function getCurrentEnvironment(): Environment {
  318. if (environmentsStore.value.currentEnvironmentIndex === -1) {
  319. return {
  320. name: "No environment",
  321. variables: [],
  322. }
  323. }
  324. return environmentsStore.value.environments[
  325. environmentsStore.value.currentEnvironmentIndex
  326. ]
  327. }
  328. export function setCurrentEnvironment(newEnvIndex: number) {
  329. environmentsStore.dispatch({
  330. dispatcher: "setCurrentEnviromentIndex",
  331. payload: {
  332. newIndex: newEnvIndex,
  333. },
  334. })
  335. }
  336. export function getLegacyGlobalEnvironment(): Environment | null {
  337. const envs = environmentsStore.value.environments
  338. const el = envs.find(
  339. (env) => env.name === "globals" || env.name === "Globals"
  340. )
  341. return el ?? null
  342. }
  343. export function getGlobalVariables(): Environment["variables"] {
  344. return environmentsStore.value.globals
  345. }
  346. export function addGlobalEnvVariable(entry: Environment["variables"][number]) {
  347. environmentsStore.dispatch({
  348. dispatcher: "addGlobalVariable",
  349. payload: {
  350. entry,
  351. },
  352. })
  353. }
  354. export function setGlobalEnvVariables(entries: Environment["variables"]) {
  355. environmentsStore.dispatch({
  356. dispatcher: "setGlobalVariables",
  357. payload: {
  358. entries,
  359. },
  360. })
  361. }
  362. export function clearGlobalEnvVariables() {
  363. environmentsStore.dispatch({
  364. dispatcher: "clearGlobalVariables",
  365. payload: {},
  366. })
  367. }
  368. export function removeGlobalEnvVariable(envIndex: number) {
  369. environmentsStore.dispatch({
  370. dispatcher: "removeGlobalVariable",
  371. payload: {
  372. envIndex,
  373. },
  374. })
  375. }
  376. export function updateGlobalEnvVariable(
  377. envIndex: number,
  378. updatedEntry: Environment["variables"][number]
  379. ) {
  380. environmentsStore.dispatch({
  381. dispatcher: "updateGlobalVariable",
  382. payload: {
  383. envIndex,
  384. updatedEntry,
  385. },
  386. })
  387. }
  388. export function replaceEnvironments(newEnvironments: any[]) {
  389. environmentsStore.dispatch({
  390. dispatcher: "replaceEnvironments",
  391. payload: {
  392. environments: newEnvironments,
  393. },
  394. })
  395. }
  396. export function appendEnvironments(envs: Environment[]) {
  397. environmentsStore.dispatch({
  398. dispatcher: "appendEnvironments",
  399. payload: {
  400. envs,
  401. },
  402. })
  403. }
  404. export function createEnvironment(envName: string) {
  405. environmentsStore.dispatch({
  406. dispatcher: "createEnvironment",
  407. payload: {
  408. name: envName,
  409. },
  410. })
  411. }
  412. export function duplicateEnvironment(envIndex: number) {
  413. environmentsStore.dispatch({
  414. dispatcher: "duplicateEnvironment",
  415. payload: {
  416. envIndex,
  417. },
  418. })
  419. }
  420. export function deleteEnvironment(envIndex: number) {
  421. environmentsStore.dispatch({
  422. dispatcher: "deleteEnvironment",
  423. payload: {
  424. envIndex,
  425. },
  426. })
  427. }
  428. export function renameEnvironment(envIndex: number, newName: string) {
  429. environmentsStore.dispatch({
  430. dispatcher: "renameEnvironment",
  431. payload: {
  432. envIndex,
  433. newName,
  434. },
  435. })
  436. }
  437. export function updateEnvironment(envIndex: number, updatedEnv: Environment) {
  438. environmentsStore.dispatch({
  439. dispatcher: "updateEnvironment",
  440. payload: {
  441. envIndex,
  442. updatedEnv,
  443. },
  444. })
  445. }
  446. export function setEnvironmentVariables(
  447. envIndex: number,
  448. vars: { key: string; value: string }[]
  449. ) {
  450. environmentsStore.dispatch({
  451. dispatcher: "setEnvironmentVariables",
  452. payload: {
  453. envIndex,
  454. vars,
  455. },
  456. })
  457. }
  458. export function addEnvironmentVariable(
  459. envIndex: number,
  460. { key, value }: { key: string; value: string }
  461. ) {
  462. environmentsStore.dispatch({
  463. dispatcher: "addEnvironmentVariable",
  464. payload: {
  465. envIndex,
  466. key,
  467. value,
  468. },
  469. })
  470. }
  471. export function removeEnvironmentVariable(
  472. envIndex: number,
  473. variableIndex: number
  474. ) {
  475. environmentsStore.dispatch({
  476. dispatcher: "removeEnvironmentVariable",
  477. payload: {
  478. envIndex,
  479. variableIndex,
  480. },
  481. })
  482. }
  483. export function updateEnvironmentVariable(
  484. envIndex: number,
  485. variableIndex: number,
  486. { key, value }: { key: string; value: string }
  487. ) {
  488. environmentsStore.dispatch({
  489. dispatcher: "updateEnvironmentVariable",
  490. payload: {
  491. envIndex,
  492. variableIndex,
  493. updatedKey: key,
  494. updatedValue: value,
  495. },
  496. })
  497. }
  498. export function getEnviroment(index: number) {
  499. return environmentsStore.value.environments[index]
  500. }