localpersistence.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* eslint-disable no-restricted-globals, no-restricted-syntax */
  2. import clone from "lodash/clone"
  3. import assign from "lodash/assign"
  4. import isEmpty from "lodash/isEmpty"
  5. import * as O from "fp-ts/Option"
  6. import { pipe } from "fp-ts/function"
  7. import {
  8. settingsStore,
  9. bulkApplySettings,
  10. defaultSettings,
  11. applySetting,
  12. HoppAccentColor,
  13. HoppBgColor,
  14. } from "./settings"
  15. import {
  16. restHistoryStore,
  17. graphqlHistoryStore,
  18. setRESTHistoryEntries,
  19. setGraphqlHistoryEntries,
  20. translateToNewRESTHistory,
  21. translateToNewGQLHistory,
  22. } from "./history"
  23. import {
  24. restCollectionStore,
  25. graphqlCollectionStore,
  26. setGraphqlCollections,
  27. setRESTCollections,
  28. translateToNewRESTCollection,
  29. translateToNewGQLCollection,
  30. } from "./collections"
  31. import {
  32. replaceEnvironments,
  33. environments$,
  34. Environment,
  35. addGlobalEnvVariable,
  36. setGlobalEnvVariables,
  37. globalEnv$,
  38. selectedEnvIndex$,
  39. setCurrentEnvironment,
  40. } from "./environments"
  41. import { restRequest$, setRESTRequest } from "./RESTSession"
  42. import { translateToNewRequest } from "~/helpers/types/HoppRESTRequest"
  43. function checkAndMigrateOldSettings() {
  44. const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
  45. if (isEmpty(vuexData)) return
  46. const { postwoman } = vuexData
  47. if (!isEmpty(postwoman?.settings)) {
  48. const settingsData = assign(clone(defaultSettings), postwoman.settings)
  49. window.localStorage.setItem("settings", JSON.stringify(settingsData))
  50. delete postwoman.settings
  51. window.localStorage.setItem("vuex", JSON.stringify(vuexData))
  52. }
  53. if (postwoman?.collections) {
  54. window.localStorage.setItem(
  55. "collections",
  56. JSON.stringify(postwoman.collections)
  57. )
  58. delete postwoman.collections
  59. window.localStorage.setItem("vuex", JSON.stringify(vuexData))
  60. }
  61. if (postwoman?.collectionsGraphql) {
  62. window.localStorage.setItem(
  63. "collectionsGraphql",
  64. JSON.stringify(postwoman.collectionsGraphql)
  65. )
  66. delete postwoman.collectionsGraphql
  67. window.localStorage.setItem("vuex", JSON.stringify(vuexData))
  68. }
  69. if (postwoman?.environments) {
  70. window.localStorage.setItem(
  71. "environments",
  72. JSON.stringify(postwoman.environments)
  73. )
  74. delete postwoman.environments
  75. window.localStorage.setItem("vuex", JSON.stringify(vuexData))
  76. }
  77. if (window.localStorage.getItem("THEME_COLOR")) {
  78. const themeColor = window.localStorage.getItem("THEME_COLOR")
  79. applySetting("THEME_COLOR", themeColor as HoppAccentColor)
  80. window.localStorage.removeItem("THEME_COLOR")
  81. }
  82. if (window.localStorage.getItem("nuxt-color-mode")) {
  83. const color = window.localStorage.getItem("nuxt-color-mode") as HoppBgColor
  84. applySetting("BG_COLOR", color)
  85. window.localStorage.removeItem("nuxt-color-mode")
  86. }
  87. }
  88. function setupSettingsPersistence() {
  89. const settingsData = JSON.parse(
  90. window.localStorage.getItem("settings") || "{}"
  91. )
  92. if (settingsData) {
  93. bulkApplySettings(settingsData)
  94. }
  95. settingsStore.subject$.subscribe((settings) => {
  96. window.localStorage.setItem("settings", JSON.stringify(settings))
  97. })
  98. }
  99. function setupHistoryPersistence() {
  100. const restHistoryData = JSON.parse(
  101. window.localStorage.getItem("history") || "[]"
  102. ).map(translateToNewRESTHistory)
  103. const graphqlHistoryData = JSON.parse(
  104. window.localStorage.getItem("graphqlHistory") || "[]"
  105. ).map(translateToNewGQLHistory)
  106. setRESTHistoryEntries(restHistoryData)
  107. setGraphqlHistoryEntries(graphqlHistoryData)
  108. restHistoryStore.subject$.subscribe(({ state }) => {
  109. window.localStorage.setItem("history", JSON.stringify(state))
  110. })
  111. graphqlHistoryStore.subject$.subscribe(({ state }) => {
  112. window.localStorage.setItem("graphqlHistory", JSON.stringify(state))
  113. })
  114. }
  115. function setupCollectionsPersistence() {
  116. const restCollectionData = JSON.parse(
  117. window.localStorage.getItem("collections") || "[]"
  118. ).map(translateToNewRESTCollection)
  119. const graphqlCollectionData = JSON.parse(
  120. window.localStorage.getItem("collectionsGraphql") || "[]"
  121. ).map(translateToNewGQLCollection)
  122. setRESTCollections(restCollectionData)
  123. setGraphqlCollections(graphqlCollectionData)
  124. restCollectionStore.subject$.subscribe(({ state }) => {
  125. window.localStorage.setItem("collections", JSON.stringify(state))
  126. })
  127. graphqlCollectionStore.subject$.subscribe(({ state }) => {
  128. window.localStorage.setItem("collectionsGraphql", JSON.stringify(state))
  129. })
  130. }
  131. function setupEnvironmentsPersistence() {
  132. const environmentsData: Environment[] = JSON.parse(
  133. window.localStorage.getItem("environments") || "[]"
  134. )
  135. // Check if a global env is defined and if so move that to globals
  136. const globalIndex = environmentsData.findIndex(
  137. (x) => x.name.toLowerCase() === "globals"
  138. )
  139. if (globalIndex !== -1) {
  140. const globalEnv = environmentsData[globalIndex]
  141. globalEnv.variables.forEach((variable) => addGlobalEnvVariable(variable))
  142. // Remove global from environments
  143. environmentsData.splice(globalIndex, 1)
  144. // Just sync the changes manually
  145. window.localStorage.setItem(
  146. "environments",
  147. JSON.stringify(environmentsData)
  148. )
  149. }
  150. replaceEnvironments(environmentsData)
  151. environments$.subscribe((envs) => {
  152. window.localStorage.setItem("environments", JSON.stringify(envs))
  153. })
  154. }
  155. function setupSelectedEnvPersistence() {
  156. const selectedEnvIndex = pipe(
  157. // Value from local storage can be nullable
  158. O.fromNullable(window.localStorage.getItem("selectedEnvIndex")),
  159. O.map(parseInt), // If not null, parse to integer
  160. O.chain(
  161. O.fromPredicate(
  162. Number.isInteger // Check if the number is proper int (not NaN)
  163. )
  164. ),
  165. O.getOrElse(() => -1) // If all the above conditions pass, we are good, else set default value (-1)
  166. )
  167. setCurrentEnvironment(selectedEnvIndex)
  168. selectedEnvIndex$.subscribe((index) => {
  169. window.localStorage.setItem("selectedEnvIndex", index.toString())
  170. })
  171. }
  172. function setupGlobalEnvsPersistence() {
  173. const globals: Environment["variables"] = JSON.parse(
  174. window.localStorage.getItem("globalEnv") || "[]"
  175. )
  176. setGlobalEnvVariables(globals)
  177. globalEnv$.subscribe((vars) => {
  178. window.localStorage.setItem("globalEnv", JSON.stringify(vars))
  179. })
  180. }
  181. function setupRequestPersistence() {
  182. const localRequest = JSON.parse(
  183. window.localStorage.getItem("restRequest") || "null"
  184. )
  185. if (localRequest) {
  186. const parsedLocal = translateToNewRequest(localRequest)
  187. setRESTRequest(parsedLocal)
  188. }
  189. restRequest$.subscribe((req) => {
  190. window.localStorage.setItem("restRequest", JSON.stringify(req))
  191. })
  192. }
  193. export function setupLocalPersistence() {
  194. checkAndMigrateOldSettings()
  195. setupSettingsPersistence()
  196. setupRequestPersistence()
  197. setupHistoryPersistence()
  198. setupCollectionsPersistence()
  199. setupGlobalEnvsPersistence()
  200. setupEnvironmentsPersistence()
  201. setupSelectedEnvPersistence()
  202. }
  203. /**
  204. * Gets a value in LocalStorage.
  205. *
  206. * NOTE: Use LocalStorage to only store non-reactive simple data
  207. * For more complex data, use stores and connect it to localpersistence
  208. */
  209. export function getLocalConfig(name: string) {
  210. return window.localStorage.getItem(name)
  211. }
  212. /**
  213. * Sets a value in LocalStorage.
  214. *
  215. * NOTE: Use LocalStorage to only store non-reactive simple data
  216. * For more complex data, use stores and connect it to localpersistence
  217. */
  218. export function setLocalConfig(key: string, value: string) {
  219. window.localStorage.setItem(key, value)
  220. }
  221. /**
  222. * Clear config value in LocalStorage.
  223. * @param key Key to be cleared
  224. */
  225. export function removeLocalConfig(key: string) {
  226. window.localStorage.removeItem(key)
  227. }