Skip to content

Commit

Permalink
refactor: removed unnecssary type exports and updated logic and varia…
Browse files Browse the repository at this point in the history
…ble names across most files
  • Loading branch information
JoelJacobStephen committed Oct 5, 2023
1 parent 3592b8b commit 7101a1e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
4 changes: 2 additions & 2 deletions packages/hoppscotch-cli/src/handlers/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export const handleError = <T extends HoppErrorCode>(error: HoppError<T>) => {
case "MALFORMED_ENV_FILE":
ERROR_MSG = `The environment file is not of the correct format.`;
break;
case "MALFORMED_BULK_ENV_FILE":
ERROR_MSG = `CLI doesnt support bulk environments export.`;
case "BULK_ENV_FILE":
ERROR_MSG = `CLI doesn't support bulk environments export.`;
break;
case "MALFORMED_COLLECTION":
ERROR_MSG = `${error.path}\n${parseErrorData(error.data)}`;
Expand Down
27 changes: 14 additions & 13 deletions packages/hoppscotch-cli/src/options/test/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { error } from "../../types/errors";
import {
HoppEnvs,
HoppEnvPair,
HoppEnvArray,
HoppEnvObject,
HoppBulkEnvObject,
HoppEnvKeyPairObject,
HoppEnvExportObject,
HoppBulkEnvExportObject,
} from "../../types/request";
import { readJsonFile } from "../../utils/mutators";
/**
Expand All @@ -15,28 +15,29 @@ import { readJsonFile } from "../../utils/mutators";
export async function parseEnvsData(path: string) {
const contents = await readJsonFile(path);
const envPairs: Array<HoppEnvPair> = [];
const HoppEnvArrayResult = HoppEnvArray.safeParse(contents);
const HoppEnvObjectResult = HoppEnvObject.safeParse(contents);
const HoppBulkEnvObjectResult = HoppBulkEnvObject.safeParse(contents);
const HoppEnvKeyPairResult = HoppEnvKeyPairObject.safeParse(contents);
const HoppEnvExportObjectResult = HoppEnvExportObject.safeParse(contents);
const HoppBulkEnvExportObjectResult =
HoppBulkEnvExportObject.safeParse(contents);

// CLI doesnt support bulk environments export.
// Hence we check for this case and throw an error if it matches the format.
if (HoppBulkEnvObjectResult.success) {
throw error({ code: "MALFORMED_BULK_ENV_FILE", path, data: error });
if (HoppBulkEnvExportObjectResult.success) {
throw error({ code: "BULK_ENV_FILE", path, data: error });
}

// Checks if the environment file is of the correct format.
// If it doesnt match either of them, we throw an error.
if (!(HoppEnvArrayResult.success || HoppEnvObjectResult.success)) {
if (!(HoppEnvKeyPairResult.success || HoppEnvExportObjectResult.success)) {
throw error({ code: "MALFORMED_ENV_FILE", path, data: error });
}

if (HoppEnvArrayResult.success) {
for (const [key, value] of Object.entries(HoppEnvArrayResult.data)) {
if (HoppEnvKeyPairResult.success) {
for (const [key, value] of Object.entries(HoppEnvKeyPairResult.data)) {
envPairs.push({ key, value });
}
} else if (HoppEnvObjectResult.success) {
const { key, value } = HoppEnvObjectResult.data.variables[0];
} else if (HoppEnvExportObjectResult.success) {
const { key, value } = HoppEnvExportObjectResult.data.variables[0];
envPairs.push({ key, value });
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hoppscotch-cli/src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type HoppErrors = {
REQUEST_ERROR: HoppErrorData;
INVALID_ARGUMENT: HoppErrorData;
MALFORMED_ENV_FILE: HoppErrorPath & HoppErrorData;
MALFORMED_BULK_ENV_FILE: HoppErrorPath & HoppErrorData;
BULK_ENV_FILE: HoppErrorPath & HoppErrorData;
INVALID_FILE_TYPE: HoppErrorData;
};

Expand Down
13 changes: 5 additions & 8 deletions packages/hoppscotch-cli/src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ export type FormDataEntry = {

export type HoppEnvPair = { key: string; value: string };

export const HoppEnvArray = z.record(z.string(), z.string());
export const HoppEnvKeyPairObject = z.record(z.string(), z.string());

export type HoppEnvArray = z.infer<typeof HoppEnvArray>;

export const HoppEnvObject = z.object({
// Shape of the single environment export object that is exported from the app.
export const HoppEnvExportObject = z.object({
name: z.string(),
variables: z.array(
z.object({
Expand All @@ -24,7 +23,8 @@ export const HoppEnvObject = z.object({
),
});

export const HoppBulkEnvObject = z.array(
// Shape of the bulk environment export object that is exported from the app.
export const HoppBulkEnvExportObject = z.array(
z.object({
name: z.string(),
variables: z.array(
Expand All @@ -36,9 +36,6 @@ export const HoppBulkEnvObject = z.array(
})
);

export type HoppEnvObject = z.infer<typeof HoppEnvObject>;
export type HoppBulkEnvObject = z.infer<typeof HoppBulkEnvObject>;

export type HoppEnvs = {
global: HoppEnvPair[];
selected: HoppEnvPair[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import { useI18n } from "@composables/i18n"
import { useToast } from "@composables/toast"
import { TippyComponent } from "vue-tippy"
import { HoppSmartItem } from "@hoppscotch/ui"
import { exportJSON } from "~/helpers/import-export/export/envToJson"
import { exportAsJSON } from "~/helpers/import-export/export/environment"
const t = useI18n()
const toast = useToast()
Expand All @@ -150,10 +150,12 @@ const emit = defineEmits<{
const confirmRemove = ref(false)
const exportEnvironmentAsJSON = () =>
exportJSON(props.environment, props.environmentIndex)
const exportEnvironmentAsJSON = () => {
const { environment, environmentIndex } = props
exportAsJSON(environment, environmentIndex)
? toast.success(t("state.download_started"))
: toast.error(t("state.download_failed"))
}
const tippyActions = ref<TippyComponent | null>(null)
const options = ref<TippyComponent | null>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import IconTrash2 from "~icons/lucide/trash-2"
import IconMoreVertical from "~icons/lucide/more-vertical"
import { TippyComponent } from "vue-tippy"
import { HoppSmartItem } from "@hoppscotch/ui"
import { exportJSON } from "~/helpers/import-export/export/envToJson"
import { exportAsJSON } from "~/helpers/import-export/export/environment"
const t = useI18n()
const toast = useToast()
Expand All @@ -140,7 +140,7 @@ const emit = defineEmits<{
const confirmRemove = ref(false)
const exportEnvironmentAsJSON = () =>
exportJSON(props.environment)
exportAsJSON(props.environment)
? toast.success(t("state.download_started"))
: toast.error(t("state.download_failed"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ import { Environment } from "@hoppscotch/data"
import { TeamEnvironment } from "~/helpers/teams/TeamEnvironment"

const getEnvironmentJson = (
environment: TeamEnvironment | Environment,
environmentObj: TeamEnvironment | Environment,
environmentIndex?: number | "Global" | null
) => {
if ("environment" in environment) {
const { ...newEnvironment } = environment.environment
delete newEnvironment.id

return environment.id !== null
? JSON.stringify(newEnvironment, null, 2)
: undefined
} else {
const { ...newEnvironment } = environment
delete newEnvironment.id

return environmentIndex !== null
? JSON.stringify(newEnvironment, null, 2)
: undefined
}
const newEnvironment =
"environment" in environmentObj
? environmentObj.environment
: environmentObj

delete newEnvironment.id

const environmentId =
environmentIndex || environmentIndex === 0
? environmentIndex
: environmentObj.id

return environmentId !== null
? JSON.stringify(newEnvironment, null, 2)
: undefined
}

export const exportJSON = (
environment: Environment | TeamEnvironment,
export const exportAsJSON = (
environmentObj: Environment | TeamEnvironment,
environmentIndex?: number | "Global" | null
): boolean => {
const dataToWrite = environmentIndex
? getEnvironmentJson(environment, environmentIndex)
: getEnvironmentJson(environment)
const dataToWrite = getEnvironmentJson(environmentObj, environmentIndex)

if (!dataToWrite) return false

Expand Down

0 comments on commit 7101a1e

Please sign in to comment.