123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /* eslint-disable no-restricted-globals, no-restricted-syntax */
- import clone from "lodash/clone"
- import assign from "lodash/assign"
- import isEmpty from "lodash/isEmpty"
- import {
- settingsStore,
- bulkApplySettings,
- defaultSettings,
- applySetting,
- HoppAccentColor,
- HoppBgColor,
- } from "./settings"
- import {
- restHistoryStore,
- graphqlHistoryStore,
- setRESTHistoryEntries,
- setGraphqlHistoryEntries,
- translateToNewRESTHistory,
- translateToNewGQLHistory,
- } from "./history"
- import {
- restCollectionStore,
- graphqlCollectionStore,
- setGraphqlCollections,
- setRESTCollections,
- translateToNewRESTCollection,
- translateToNewGQLCollection,
- } from "./collections"
- import {
- replaceEnvironments,
- environments$,
- Environment,
- addGlobalEnvVariable,
- setGlobalEnvVariables,
- globalEnv$,
- } from "./environments"
- import { restRequest$, setRESTRequest } from "./RESTSession"
- import { translateToNewRequest } from "~/helpers/types/HoppRESTRequest"
- function checkAndMigrateOldSettings() {
- const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
- if (isEmpty(vuexData)) return
- const { postwoman } = vuexData
- if (!isEmpty(postwoman?.settings)) {
- const settingsData = assign(clone(defaultSettings), postwoman.settings)
- window.localStorage.setItem("settings", JSON.stringify(settingsData))
- delete postwoman.settings
- window.localStorage.setItem("vuex", JSON.stringify(vuexData))
- }
- if (postwoman?.collections) {
- window.localStorage.setItem(
- "collections",
- JSON.stringify(postwoman.collections)
- )
- delete postwoman.collections
- window.localStorage.setItem("vuex", JSON.stringify(vuexData))
- }
- if (postwoman?.collectionsGraphql) {
- window.localStorage.setItem(
- "collectionsGraphql",
- JSON.stringify(postwoman.collectionsGraphql)
- )
- delete postwoman.collectionsGraphql
- window.localStorage.setItem("vuex", JSON.stringify(vuexData))
- }
- if (postwoman?.environments) {
- window.localStorage.setItem(
- "environments",
- JSON.stringify(postwoman.environments)
- )
- delete postwoman.environments
- window.localStorage.setItem("vuex", JSON.stringify(vuexData))
- }
- if (window.localStorage.getItem("THEME_COLOR")) {
- const themeColor = window.localStorage.getItem("THEME_COLOR")
- applySetting("THEME_COLOR", themeColor as HoppAccentColor)
- window.localStorage.removeItem("THEME_COLOR")
- }
- if (window.localStorage.getItem("nuxt-color-mode")) {
- const color = window.localStorage.getItem("nuxt-color-mode") as HoppBgColor
- applySetting("BG_COLOR", color)
- window.localStorage.removeItem("nuxt-color-mode")
- }
- }
- function setupSettingsPersistence() {
- const settingsData = JSON.parse(
- window.localStorage.getItem("settings") || "{}"
- )
- if (settingsData) {
- bulkApplySettings(settingsData)
- }
- settingsStore.subject$.subscribe((settings) => {
- window.localStorage.setItem("settings", JSON.stringify(settings))
- })
- }
- function setupHistoryPersistence() {
- const restHistoryData = JSON.parse(
- window.localStorage.getItem("history") || "[]"
- ).map(translateToNewRESTHistory)
- const graphqlHistoryData = JSON.parse(
- window.localStorage.getItem("graphqlHistory") || "[]"
- ).map(translateToNewGQLHistory)
- setRESTHistoryEntries(restHistoryData)
- setGraphqlHistoryEntries(graphqlHistoryData)
- restHistoryStore.subject$.subscribe(({ state }) => {
- window.localStorage.setItem("history", JSON.stringify(state))
- })
- graphqlHistoryStore.subject$.subscribe(({ state }) => {
- window.localStorage.setItem("graphqlHistory", JSON.stringify(state))
- })
- }
- function setupCollectionsPersistence() {
- const restCollectionData = JSON.parse(
- window.localStorage.getItem("collections") || "[]"
- ).map(translateToNewRESTCollection)
- const graphqlCollectionData = JSON.parse(
- window.localStorage.getItem("collectionsGraphql") || "[]"
- ).map(translateToNewGQLCollection)
- setRESTCollections(restCollectionData)
- setGraphqlCollections(graphqlCollectionData)
- restCollectionStore.subject$.subscribe(({ state }) => {
- window.localStorage.setItem("collections", JSON.stringify(state))
- })
- graphqlCollectionStore.subject$.subscribe(({ state }) => {
- window.localStorage.setItem("collectionsGraphql", JSON.stringify(state))
- })
- }
- function setupEnvironmentsPersistence() {
- const environmentsData: Environment[] = JSON.parse(
- window.localStorage.getItem("environments") || "[]"
- )
- // Check if a global env is defined and if so move that to globals
- const globalIndex = environmentsData.findIndex(
- (x) => x.name.toLowerCase() === "globals"
- )
- if (globalIndex !== -1) {
- const globalEnv = environmentsData[globalIndex]
- globalEnv.variables.forEach((variable) => addGlobalEnvVariable(variable))
- // Remove global from environments
- environmentsData.splice(globalIndex, 1)
- // Just sync the changes manually
- window.localStorage.setItem(
- "environments",
- JSON.stringify(environmentsData)
- )
- }
- replaceEnvironments(environmentsData)
- environments$.subscribe((envs) => {
- window.localStorage.setItem("environments", JSON.stringify(envs))
- })
- }
- function setupGlobalEnvsPersistence() {
- const globals: Environment["variables"] = JSON.parse(
- window.localStorage.getItem("globalEnv") || "[]"
- )
- setGlobalEnvVariables(globals)
- globalEnv$.subscribe((vars) => {
- window.localStorage.setItem("globalEnv", JSON.stringify(vars))
- })
- }
- function setupRequestPersistence() {
- const localRequest = JSON.parse(
- window.localStorage.getItem("restRequest") || "null"
- )
- if (localRequest) {
- const parsedLocal = translateToNewRequest(localRequest)
- setRESTRequest(parsedLocal)
- }
- restRequest$.subscribe((req) => {
- window.localStorage.setItem("restRequest", JSON.stringify(req))
- })
- }
- export function setupLocalPersistence() {
- checkAndMigrateOldSettings()
- setupSettingsPersistence()
- setupRequestPersistence()
- setupHistoryPersistence()
- setupCollectionsPersistence()
- setupGlobalEnvsPersistence()
- setupEnvironmentsPersistence()
- }
- /**
- * Gets a value in LocalStorage.
- *
- * NOTE: Use LocalStorage to only store non-reactive simple data
- * For more complex data, use stores and connect it to localpersistence
- */
- export function getLocalConfig(name: string) {
- return window.localStorage.getItem(name)
- }
- /**
- * Sets a value in LocalStorage.
- *
- * NOTE: Use LocalStorage to only store non-reactive simple data
- * For more complex data, use stores and connect it to localpersistence
- */
- export function setLocalConfig(key: string, value: string) {
- window.localStorage.setItem(key, value)
- }
- /**
- * Clear config value in LocalStorage.
- * @param key Key to be cleared
- */
- export function removeLocalConfig(key: string) {
- window.localStorage.removeItem(key)
- }
|