environments.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import { cloneDeep } from "lodash"
  2. import isEqual from "lodash/isEqual"
  3. import { combineLatest } from "rxjs"
  4. import { distinctUntilChanged, map, pluck } from "rxjs/operators"
  5. import DispatchingStore, {
  6. defineDispatchers,
  7. } from "~/newstore/DispatchingStore"
  8. export type Environment = {
  9. name: string
  10. variables: {
  11. key: string
  12. value: string
  13. }[]
  14. }
  15. const defaultEnvironmentsState = {
  16. environments: [
  17. {
  18. name: "My Environment Variables",
  19. variables: [],
  20. },
  21. ] as Environment[],
  22. globals: [] as Environment["variables"],
  23. // Current environment index specifies the index
  24. // -1 means no environments are selected
  25. currentEnvironmentIndex: -1,
  26. }
  27. type EnvironmentStore = typeof defaultEnvironmentsState
  28. const dispatchers = defineDispatchers({
  29. setCurrentEnviromentIndex(
  30. { environments }: EnvironmentStore,
  31. { newIndex }: { newIndex: number }
  32. ) {
  33. if (newIndex >= environments.length || newIndex <= -2) {
  34. // console.log(
  35. // `Ignoring possibly invalid current environment index assignment (value: ${newIndex})`
  36. // )
  37. return {}
  38. }
  39. return {
  40. currentEnvironmentIndex: newIndex,
  41. }
  42. },
  43. appendEnvironments(
  44. { environments }: EnvironmentStore,
  45. { envs }: { envs: Environment[] }
  46. ) {
  47. return {
  48. environments: [...environments, ...envs],
  49. }
  50. },
  51. replaceEnvironments(
  52. _: EnvironmentStore,
  53. { environments }: { environments: Environment[] }
  54. ) {
  55. return {
  56. environments,
  57. }
  58. },
  59. createEnvironment(
  60. { environments }: EnvironmentStore,
  61. { name }: { name: string }
  62. ) {
  63. return {
  64. environments: [
  65. ...environments,
  66. {
  67. name,
  68. variables: [],
  69. },
  70. ],
  71. }
  72. },
  73. duplicateEnvironment(
  74. { environments }: EnvironmentStore,
  75. { envIndex }: { envIndex: number }
  76. ) {
  77. const newEnvironment = environments.find((_, index) => index === envIndex)
  78. if (!newEnvironment) {
  79. return {
  80. environments,
  81. }
  82. }
  83. return {
  84. environments: [
  85. ...environments,
  86. {
  87. ...cloneDeep(newEnvironment),
  88. name: `${newEnvironment.name} - Duplicate`,
  89. },
  90. ],
  91. }
  92. },
  93. deleteEnvironment(
  94. { environments, currentEnvironmentIndex }: EnvironmentStore,
  95. { envIndex }: { envIndex: number }
  96. ) {
  97. return {
  98. environments: environments.filter((_, index) => index !== envIndex),
  99. currentEnvironmentIndex:
  100. envIndex === currentEnvironmentIndex ? -1 : currentEnvironmentIndex,
  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$ = combineLatest([
  259. environments$,
  260. selectedEnvIndex$,
  261. ]).pipe(
  262. map(([envs, selectedIndex]) => {
  263. if (selectedIndex === -1) {
  264. const env: Environment = {
  265. name: "No environment",
  266. variables: [],
  267. }
  268. return env
  269. } else {
  270. return envs[selectedIndex]
  271. }
  272. })
  273. )
  274. /**
  275. * Stream returning all the environment variables accessible in
  276. * the current state (Global + The Selected Environment).
  277. * NOTE: The source environment attribute will be "Global" for Global Env as source.
  278. */
  279. export const aggregateEnvs$ = combineLatest([
  280. currentEnvironment$,
  281. globalEnv$,
  282. ]).pipe(
  283. map(([selectedEnv, globalVars]) => {
  284. const results: { key: string; value: string; sourceEnv: string }[] = []
  285. selectedEnv.variables.forEach(({ key, value }) =>
  286. results.push({ key, value, sourceEnv: selectedEnv.name })
  287. )
  288. globalVars.forEach(({ key, value }) =>
  289. results.push({ key, value, sourceEnv: "Global" })
  290. )
  291. return results
  292. }),
  293. distinctUntilChanged(isEqual)
  294. )
  295. export function getCurrentEnvironment(): Environment {
  296. if (environmentsStore.value.currentEnvironmentIndex === -1) {
  297. return {
  298. name: "No environment",
  299. variables: [],
  300. }
  301. }
  302. return environmentsStore.value.environments[
  303. environmentsStore.value.currentEnvironmentIndex
  304. ]
  305. }
  306. export function setCurrentEnvironment(newEnvIndex: number) {
  307. environmentsStore.dispatch({
  308. dispatcher: "setCurrentEnviromentIndex",
  309. payload: {
  310. newIndex: newEnvIndex,
  311. },
  312. })
  313. }
  314. export function getLegacyGlobalEnvironment(): Environment | null {
  315. const envs = environmentsStore.value.environments
  316. const el = envs.find(
  317. (env) => env.name === "globals" || env.name === "Globals"
  318. )
  319. return el ?? null
  320. }
  321. export function getGlobalVariables(): Environment["variables"] {
  322. return environmentsStore.value.globals
  323. }
  324. export function addGlobalEnvVariable(entry: Environment["variables"][number]) {
  325. environmentsStore.dispatch({
  326. dispatcher: "addGlobalVariable",
  327. payload: {
  328. entry,
  329. },
  330. })
  331. }
  332. export function setGlobalEnvVariables(entries: Environment["variables"]) {
  333. environmentsStore.dispatch({
  334. dispatcher: "setGlobalVariables",
  335. payload: {
  336. entries,
  337. },
  338. })
  339. }
  340. export function clearGlobalEnvVariables() {
  341. environmentsStore.dispatch({
  342. dispatcher: "clearGlobalVariables",
  343. payload: {},
  344. })
  345. }
  346. export function removeGlobalEnvVariable(envIndex: number) {
  347. environmentsStore.dispatch({
  348. dispatcher: "removeGlobalVariable",
  349. payload: {
  350. envIndex,
  351. },
  352. })
  353. }
  354. export function updateGlobalEnvVariable(
  355. envIndex: number,
  356. updatedEntry: Environment["variables"][number]
  357. ) {
  358. environmentsStore.dispatch({
  359. dispatcher: "updateGlobalVariable",
  360. payload: {
  361. envIndex,
  362. updatedEntry,
  363. },
  364. })
  365. }
  366. export function replaceEnvironments(newEnvironments: any[]) {
  367. environmentsStore.dispatch({
  368. dispatcher: "replaceEnvironments",
  369. payload: {
  370. environments: newEnvironments,
  371. },
  372. })
  373. }
  374. export function appendEnvironments(envs: Environment[]) {
  375. environmentsStore.dispatch({
  376. dispatcher: "appendEnvironments",
  377. payload: {
  378. envs,
  379. },
  380. })
  381. }
  382. export function createEnvironment(envName: string) {
  383. environmentsStore.dispatch({
  384. dispatcher: "createEnvironment",
  385. payload: {
  386. name: envName,
  387. },
  388. })
  389. }
  390. export function duplicateEnvironment(envIndex: number) {
  391. environmentsStore.dispatch({
  392. dispatcher: "duplicateEnvironment",
  393. payload: {
  394. envIndex,
  395. },
  396. })
  397. }
  398. export function deleteEnvironment(envIndex: number) {
  399. environmentsStore.dispatch({
  400. dispatcher: "deleteEnvironment",
  401. payload: {
  402. envIndex,
  403. },
  404. })
  405. }
  406. export function renameEnvironment(envIndex: number, newName: string) {
  407. environmentsStore.dispatch({
  408. dispatcher: "renameEnvironment",
  409. payload: {
  410. envIndex,
  411. newName,
  412. },
  413. })
  414. }
  415. export function updateEnvironment(envIndex: number, updatedEnv: Environment) {
  416. environmentsStore.dispatch({
  417. dispatcher: "updateEnvironment",
  418. payload: {
  419. envIndex,
  420. updatedEnv,
  421. },
  422. })
  423. }
  424. export function setEnvironmentVariables(
  425. envIndex: number,
  426. vars: { key: string; value: string }[]
  427. ) {
  428. environmentsStore.dispatch({
  429. dispatcher: "setEnvironmentVariables",
  430. payload: {
  431. envIndex,
  432. vars,
  433. },
  434. })
  435. }
  436. export function addEnvironmentVariable(
  437. envIndex: number,
  438. { key, value }: { key: string; value: string }
  439. ) {
  440. environmentsStore.dispatch({
  441. dispatcher: "addEnvironmentVariable",
  442. payload: {
  443. envIndex,
  444. key,
  445. value,
  446. },
  447. })
  448. }
  449. export function removeEnvironmentVariable(
  450. envIndex: number,
  451. variableIndex: number
  452. ) {
  453. environmentsStore.dispatch({
  454. dispatcher: "removeEnvironmentVariable",
  455. payload: {
  456. envIndex,
  457. variableIndex,
  458. },
  459. })
  460. }
  461. export function updateEnvironmentVariable(
  462. envIndex: number,
  463. variableIndex: number,
  464. { key, value }: { key: string; value: string }
  465. ) {
  466. environmentsStore.dispatch({
  467. dispatcher: "updateEnvironmentVariable",
  468. payload: {
  469. envIndex,
  470. variableIndex,
  471. updatedKey: key,
  472. updatedValue: value,
  473. },
  474. })
  475. }
  476. export function getEnviroment(index: number) {
  477. return environmentsStore.value.environments[index]
  478. }