feat: introduces ability to export single environment variables and allow CLI to accept the export format used by the app (#3380)

* feat: add ability to export a single environment

* refactor: export environment without id

* feat: introducing zod for checking json format for environment variables

* refactor: new zod specific type for HoppEnvPair

* feat: add ability to export single environment in team environment

* refactor: moved zod as a dependency to devDependency

* refactor: separated repeating logic to helper file

* refactor: removed unnecessary to string operation

* chore: rearranged smart item placement

* refactor: introduced error type when a bulk environment export is used in cli

* refactor: removed unnecssary type exports and updated logic and variable names across most files

* refactor: better logic for type shapes

* chore: bump hoppscotch-cli package version

---------

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Joel Jacob Stephen
2023-10-06 11:21:54 +05:30
committed by GitHub
parent 2694731c36
commit 185c225297
11 changed files with 196 additions and 258 deletions

View File

@@ -0,0 +1,48 @@
import { Environment } from "@hoppscotch/data"
import { TeamEnvironment } from "~/helpers/teams/TeamEnvironment"
import { cloneDeep } from "lodash-es"
const getEnvironmentJson = (
environmentObj: TeamEnvironment | Environment,
environmentIndex?: number | "Global" | null
) => {
const newEnvironment =
"environment" in environmentObj
? cloneDeep(environmentObj.environment)
: cloneDeep(environmentObj)
delete newEnvironment.id
const environmentId =
environmentIndex || environmentIndex === 0
? environmentIndex
: environmentObj.id
return environmentId !== null
? JSON.stringify(newEnvironment, null, 2)
: undefined
}
export const exportAsJSON = (
environmentObj: Environment | TeamEnvironment,
environmentIndex?: number | "Global" | null
): boolean => {
const dataToWrite = getEnvironmentJson(environmentObj, environmentIndex)
if (!dataToWrite) return false
const file = new Blob([dataToWrite], { type: "application/json" })
const a = document.createElement("a")
const url = URL.createObjectURL(file)
a.href = url
// Extracts the path from url, removes fragment identifier and query parameters if any, appends the ".json" extension, and assigns it
a.download = `${url.split("/").pop()!.split("#")[0].split("?")[0]}.json`
document.body.appendChild(a)
a.click()
setTimeout(() => {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}, 0)
return true
}