Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make global environment a versioned entity #4216

Merged
merged 4 commits into from
Sep 23, 2024

Conversation

ipalakchopra
Copy link
Contributor

@ipalakchopra ipalakchopra commented Jul 28, 2024

This PR introduces a new versioned entity for the global environment.
Partly addresses #4160.

Closes HFE-429.

import { GlobalEnvironment } from "@hoppscotch/data"

const result = GlobalEnvironment.safeParse(globalEnvData)
 
if (result.type === "ok") {
  // ...
} else {
  // ...
}

Schema reference

const V0_SCHEMA = z.array(
  z.union([
    z.object({
      key: z.string(),
      value: z.string(),
      secret: z.literal(false),
    }),
    z.object({
      key: z.string(),
      secret: z.literal(true),
    }),
    z.object({
      key: z.string(),
      value: z.string(),
    }),
  ])
)
const V1_SCHEMA = z.object({
  v: z.literal(1),
  variables: z.array(
    z.union([
      z.object({
        key: z.string(),
        secret: z.literal(true),
      }),
      z.object({
        key: z.string(),
        value: z.string(),
        secret: z.literal(false),
      }),
    ])
  ),
})

What's changed

  • Introduce schema definitions and related updates to represent the global environment as a versioned entity compiled under a new global-environment directory within hoppscotch-data.
  • Update the usage across to consume the versioned entity and perform the necessary migration ensuring the latest version is dealt with, syncing context, for instance.
  • Adds verzod as a dependency under hoppscotch-selfhost-web.

Copy link
Member

@jamesgeorge007 jamesgeorge007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the proposed schema changes are in place, the existing usages have to be extended to consume the GlobalEnvironment versioned entity:

  • For the persistence service, update the maintained schema to be based on the versioned entity.
     // Validate data read from localStorage
-    const result = GLOBAL_ENV_SCHEMA.safeParse(globalEnvData)
-    if (result.success) {
-      globalEnvData = result.data
+    const result = GlobalEnvironment.safeParse(globalEnvData)
+    if (result.type === "ok") {
+      globalEnvData = result.value
     } else {
       this.showErrorToast(globalEnvKey)
       window.localStorage.setItem(
         `${globalEnvKey}-backup`,
         JSON.stringify(globalEnvData)
       )
     } 
-    setGlobalEnvVariables(globalEnvData as Environment["variables"])
+    setGlobalEnvVariables(globalEnvData)
  • Update the default state for global environments at the store from an empty array to { v: 1, variables: [] }. We're changing the store representation since it effectively gets written to localStorage and needs to conform to the schema. Necessary changes have to be made to the relevant dispatcher methods following this. For instance, the case with setGlobalVariables:
-  setGlobalVariables(_, { entries }: { entries: Environment["variables"] }) {
+  setGlobalVariables(_, { entries }: { entries: GlobalEnvironment }) {
     return {
       globals: entries,
     }
  • While fetching global environments in the syncing context (sync actions in the personal workspace and pulls data from upstream while the app boots and writes to localStorage), we'll have to bring in the GlobalEnvironment versioned entity to translate any data conforming to the old format (v0) to the latest version (v1).

verzod needs to be added as a dependency under packages/hoppscotch-selfhost-web and the entityReference function has to be imported from the same.

     if (globalEnv) {
+      const globalEnvVariableEntries = JSON.parse(globalEnv.variables)
+
+      const result = entityReference(GlobalEnvironment).safeParse(
+        globalEnvVariableEntries
+      )
+
       runDispatchWithOutSyncing(() => {
-        setGlobalEnvVariables(JSON.parse(globalEnv.variables))
+        setGlobalEnvVariables(
+          result.success ? result.data : globalEnvVariableEntries
+        )
         setGlobalEnvID(globalEnv.id)
       })
     }

The above steps are to be optionally extended to the SH Desktop app.

packages/hoppscotch-data/src/global-environments/v/0.ts Outdated Show resolved Hide resolved
packages/hoppscotch-data/src/global-environments/v/0.ts Outdated Show resolved Hide resolved
packages/hoppscotch-data/src/global-environments/v/1.ts Outdated Show resolved Hide resolved
packages/hoppscotch-data/src/global-environments/v/1.ts Outdated Show resolved Hide resolved
packages/hoppscotch-data/src/global-environments/index.ts Outdated Show resolved Hide resolved
packages/hoppscotch-data/src/global-environments/index.ts Outdated Show resolved Hide resolved
@jamesgeorge007 jamesgeorge007 changed the title refactor: created versioned entities for global environments refactor: make global environment a versioned entity Aug 2, 2024
@jamesgeorge007 jamesgeorge007 changed the base branch from main to next September 11, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update more public data structures to be versioned entities
4 participants