localpersistence.ts 6.7 KB

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