-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Conversation
There was a problem hiding this 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 tolocalStorage
and needs to conform to the schema. Necessary changes have to be made to the relevant dispatcher methods following this. For instance, the case withsetGlobalVariables
:
- 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 theGlobalEnvironment
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 theentityReference
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.
63e17f0
to
f15ee29
Compare
f15ee29
to
89a7d96
Compare
89a7d96
to
197e34f
Compare
b534a03
to
f09194f
Compare
This PR introduces a new versioned entity for the global environment.
Partly addresses #4160.
Closes HFE-429.
Schema reference
What's changed
global-environment
directory withinhoppscotch-data
.verzod
as a dependency underhoppscotch-selfhost-web
.