Browse Source

refactor: update all hoppCollection type to remove generic pattern

nivedin 1 year ago
parent
commit
95953557de

+ 2 - 2
packages/hoppscotch-cli/src/types/collections.ts

@@ -1,8 +1,8 @@
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data";
+import { HoppCollection } from "@hoppscotch/data";
 import { HoppEnvs } from "./request";
 
 export type CollectionRunnerParam = {
-  collections: HoppCollection<HoppRESTRequest>[];
+  collections: HoppCollection[];
   envs: HoppEnvs;
   delay?: number;
 };

+ 1 - 1
packages/hoppscotch-cli/src/types/request.ts

@@ -33,7 +33,7 @@ export type HoppEnvs = {
 
 export type CollectionStack = {
   path: string;
-  collection: HoppCollection<HoppRESTRequest>;
+  collection: HoppCollection;
 };
 
 export type RequestReport = {

+ 2 - 9
packages/hoppscotch-cli/src/utils/checks.ts

@@ -1,8 +1,4 @@
-import {
-  HoppCollection,
-  HoppRESTRequest,
-  isHoppRESTRequest,
-} from "@hoppscotch/data";
+import { HoppCollection, isHoppRESTRequest } from "@hoppscotch/data";
 import * as A from "fp-ts/Array";
 import { CommanderError } from "commander";
 import { HoppCLIError, HoppErrnoException } from "../types/errors";
@@ -24,9 +20,7 @@ export const hasProperty = <P extends PropertyKey>(
  * @returns True, if unknown parameter is valid Hoppscotch REST Collection;
  * False, otherwise.
  */
-export const isRESTCollection = (
-  param: unknown
-): param is HoppCollection<HoppRESTRequest> => {
+export const isRESTCollection = (param: unknown): param is HoppCollection => {
   if (!!param && typeof param === "object") {
     if (!hasProperty(param, "v") || typeof param.v !== "number") {
       return false;
@@ -62,7 +56,6 @@ export const isRESTCollection = (
   return false;
 };
 
-
 /**
  * Checks if given error data is of type HoppCLIError, based on existence
  * of code property.

+ 52 - 54
packages/hoppscotch-cli/src/utils/collections.ts

@@ -3,7 +3,7 @@ import { pipe } from "fp-ts/function";
 import { bold } from "chalk";
 import { log } from "console";
 import round from "lodash/round";
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data";
+import { HoppCollection } from "@hoppscotch/data";
 import {
   HoppEnvs,
   CollectionStack,
@@ -41,58 +41,58 @@ const { WARN, FAIL } = exceptionColors;
  * @param param Data of hopp-collection with hopp-requests, envs to be processed.
  * @returns List of report for each processed request.
  */
-export const collectionsRunner =
-  async (param: CollectionRunnerParam): Promise<RequestReport[]> =>
-   {
-    const envs: HoppEnvs = param.envs;
-    const delay = param.delay ?? 0;
-    const requestsReport: RequestReport[] = [];
-    const collectionStack: CollectionStack[] = getCollectionStack(
-      param.collections
-    );
-
-    while (collectionStack.length) {
-      // Pop out top-most collection from stack to be processed.
-      const { collection, path } = <CollectionStack>collectionStack.pop();
-
-      // Processing each request in collection
-      for (const request of collection.requests) {
-        const _request = preProcessRequest(request);
-        const requestPath = `${path}/${_request.name}`;
-        const processRequestParams: ProcessRequestParams = {
-          path: requestPath,
-          request: _request,
-          envs,
-          delay,
-        };
-
-        // Request processing initiated message.
-        log(WARN(`\nRunning: ${bold(requestPath)}`));
-
-        // Processing current request.
-        const result = await processRequest(processRequestParams)();
-
-        // Updating global & selected envs with new envs from processed-request output.
-        const { global, selected } = result.envs;
-        envs.global = global;
-        envs.selected = selected;
-
-        // Storing current request's report.
-        const requestReport = result.report;
-        requestsReport.push(requestReport);
-      }
-
-      // Pushing remaining folders realted collection to stack.
-      for (const folder of collection.folders) {
-        collectionStack.push({
-          path: `${path}/${folder.name}`,
-          collection: folder,
-        });
-      }
+export const collectionsRunner = async (
+  param: CollectionRunnerParam
+): Promise<RequestReport[]> => {
+  const envs: HoppEnvs = param.envs;
+  const delay = param.delay ?? 0;
+  const requestsReport: RequestReport[] = [];
+  const collectionStack: CollectionStack[] = getCollectionStack(
+    param.collections
+  );
+
+  while (collectionStack.length) {
+    // Pop out top-most collection from stack to be processed.
+    const { collection, path } = <CollectionStack>collectionStack.pop();
+
+    // Processing each request in collection
+    for (const request of collection.requests) {
+      const _request = preProcessRequest(request);
+      const requestPath = `${path}/${_request.name}`;
+      const processRequestParams: ProcessRequestParams = {
+        path: requestPath,
+        request: _request,
+        envs,
+        delay,
+      };
+
+      // Request processing initiated message.
+      log(WARN(`\nRunning: ${bold(requestPath)}`));
+
+      // Processing current request.
+      const result = await processRequest(processRequestParams)();
+
+      // Updating global & selected envs with new envs from processed-request output.
+      const { global, selected } = result.envs;
+      envs.global = global;
+      envs.selected = selected;
+
+      // Storing current request's report.
+      const requestReport = result.report;
+      requestsReport.push(requestReport);
     }
 
-    return requestsReport;
-  };
+    // Pushing remaining folders realted collection to stack.
+    for (const folder of collection.folders) {
+      collectionStack.push({
+        path: `${path}/${folder.name}`,
+        collection: folder,
+      });
+    }
+  }
+
+  return requestsReport;
+};
 
 /**
  * Transforms collections to generate collection-stack which describes each collection's
@@ -100,9 +100,7 @@ export const collectionsRunner =
  * @param collections Hopp-collection objects to be mapped to collection-stack type.
  * @returns Mapped collections to collection-stack.
  */
-const getCollectionStack = (
-  collections: HoppCollection<HoppRESTRequest>[]
-): CollectionStack[] =>
+const getCollectionStack = (collections: HoppCollection[]): CollectionStack[] =>
   pipe(
     collections,
     A.map(

+ 18 - 16
packages/hoppscotch-cli/src/utils/mutators.ts

@@ -2,7 +2,7 @@ import fs from "fs/promises";
 import { FormDataEntry } from "../types/request";
 import { error } from "../types/errors";
 import { isRESTCollection, isHoppErrnoException } from "./checks";
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data";
+import { HoppCollection } from "@hoppscotch/data";
 
 /**
  * Parses array of FormDataEntry to FormData.
@@ -35,20 +35,20 @@ export const parseErrorMessage = (e: unknown) => {
 };
 
 export async function readJsonFile(path: string): Promise<unknown> {
-  if(!path.endsWith('.json')) {
-    throw error({ code: "INVALID_FILE_TYPE", data: path })
+  if (!path.endsWith(".json")) {
+    throw error({ code: "INVALID_FILE_TYPE", data: path });
   }
 
   try {
-    await fs.access(path)
+    await fs.access(path);
   } catch (e) {
-    throw error({ code: "FILE_NOT_FOUND", path: path })
+    throw error({ code: "FILE_NOT_FOUND", path: path });
   }
 
   try {
-    return JSON.parse((await fs.readFile(path)).toString())
-  } catch(e) {
-    throw error({ code: "UNKNOWN_ERROR", data: e })
+    return JSON.parse((await fs.readFile(path)).toString());
+  } catch (e) {
+    throw error({ code: "UNKNOWN_ERROR", data: e });
   }
 }
 
@@ -56,22 +56,24 @@ export async function readJsonFile(path: string): Promise<unknown> {
  * Parses collection json file for given path:context.path, and validates
  * the parsed collectiona array.
  * @param path Collection json file path.
- * @returns For successful parsing we get array of HoppCollection<HoppRESTRequest>,
+ * @returns For successful parsing we get array of HoppCollection,
  */
 export async function parseCollectionData(
   path: string
-): Promise<HoppCollection<HoppRESTRequest>[]> {
-  let contents = await readJsonFile(path)
+): Promise<HoppCollection[]> {
+  let contents = await readJsonFile(path);
 
-  const maybeArrayOfCollections: unknown[] = Array.isArray(contents) ? contents : [contents]
+  const maybeArrayOfCollections: unknown[] = Array.isArray(contents)
+    ? contents
+    : [contents];
 
-  if(maybeArrayOfCollections.some((x) => !isRESTCollection(x))) {
+  if (maybeArrayOfCollections.some((x) => !isRESTCollection(x))) {
     throw error({
       code: "MALFORMED_COLLECTION",
       path,
       data: "Please check the collection data.",
-    })
+    });
   }
 
-  return maybeArrayOfCollections as HoppCollection<HoppRESTRequest>[]
-};
+  return maybeArrayOfCollections as HoppCollection[];
+}

+ 2 - 2
packages/hoppscotch-common/src/components/app/spotlight/entry/GQLRequest.vue

@@ -13,7 +13,7 @@
 </template>
 
 <script setup lang="ts">
-import { HoppCollection, HoppGQLRequest } from "@hoppscotch/data"
+import { HoppCollection } from "@hoppscotch/data"
 import { computed } from "vue"
 import { graphqlCollectionStore } from "~/newstore/collections"
 
@@ -28,7 +28,7 @@ const pathFolders = computed(() => {
       .slice(0, -1)
       .map((x) => parseInt(x))
 
-    const pathItems: HoppCollection<HoppGQLRequest>[] = []
+    const pathItems: HoppCollection[] = []
 
     let currentFolder =
       graphqlCollectionStore.value.state[folderIndicies.shift()!]

+ 2 - 2
packages/hoppscotch-common/src/components/app/spotlight/entry/RESTRequest.vue

@@ -20,7 +20,7 @@
 </template>
 
 <script setup lang="ts">
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
+import { HoppCollection } from "@hoppscotch/data"
 import { computed } from "vue"
 import { restCollectionStore } from "~/newstore/collections"
 import { getMethodLabelColorClassOf } from "~/helpers/rest/labelColoring"
@@ -36,7 +36,7 @@ const pathFolders = computed(() => {
       .slice(0, -1)
       .map((x) => parseInt(x))
 
-    const pathItems: HoppCollection<HoppRESTRequest>[] = []
+    const pathItems: HoppCollection[] = []
 
     let currentFolder = restCollectionStore.value.state[folderIndicies.shift()!]
     pathItems.push(currentFolder)

+ 4 - 4
packages/hoppscotch-common/src/components/collections/Collection.vue

@@ -208,7 +208,7 @@ import IconFolder from "~icons/lucide/folder"
 import IconFolderOpen from "~icons/lucide/folder-open"
 import IconSettings2 from "~icons/lucide/settings-2"
 import { ref, computed, watch } from "vue"
-import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
+import { HoppCollection } from "@hoppscotch/data"
 import { useI18n } from "@composables/i18n"
 import { TippyComponent } from "vue-tippy"
 import { TeamCollection } from "~/helpers/teams/TeamCollection"
@@ -227,7 +227,7 @@ const props = withDefaults(
   defineProps<{
     id: string
     parentID?: string | null
-    data: HoppCollection<HoppRESTRequest> | TeamCollection
+    data: HoppCollection | TeamCollection
     /**
      * Collection component can be used for both collections and folders.
      * folderType is used to determine which one it is.
@@ -310,8 +310,8 @@ const collectionIcon = computed(() => {
 })
 
 const collectionName = computed(() => {
-  if ((props.data as HoppCollection<HoppRESTRequest>).name)
-    return (props.data as HoppCollection<HoppRESTRequest>).name
+  if ((props.data as HoppCollection).name)
+    return (props.data as HoppCollection).name
   return (props.data as TeamCollection).title
 })
 

+ 3 - 10
packages/hoppscotch-common/src/components/collections/ImportExport.vue

@@ -29,7 +29,6 @@ import { PropType, computed, ref } from "vue"
 import { useI18n } from "~/composables/i18n"
 import { useToast } from "~/composables/toast"
 import { HoppCollection } from "@hoppscotch/data"
-import { HoppRESTRequest } from "@hoppscotch/data"
 import { appendRESTCollections, restCollections$ } from "~/newstore/collections"
 import MyCollectionImport from "~/components/importExport/ImportExportSteps/MyCollectionImport.vue"
 import { GetMyTeamsQuery } from "~/helpers/backend/graphql"
@@ -88,9 +87,7 @@ const showImportFailedError = () => {
   toast.error(t("import.failed"))
 }
 
-const handleImportToStore = async (
-  collections: HoppCollection<HoppRESTRequest>[]
-) => {
+const handleImportToStore = async (collections: HoppCollection[]) => {
   const importResult =
     props.collectionsType.type === "my-collections"
       ? await importToPersonalWorkspace(collections)
@@ -104,18 +101,14 @@ const handleImportToStore = async (
   }
 }
 
-const importToPersonalWorkspace = (
-  collections: HoppCollection<HoppRESTRequest>[]
-) => {
+const importToPersonalWorkspace = (collections: HoppCollection[]) => {
   appendRESTCollections(collections)
   return E.right({
     success: true,
   })
 }
 
-const importToTeamsWorkspace = async (
-  collections: HoppCollection<HoppRESTRequest>[]
-) => {
+const importToTeamsWorkspace = async (collections: HoppCollection[]) => {
   if (!hasTeamWriteAccess.value || !selectedTeamID.value) {
     return E.left({
       success: false,

+ 11 - 11
packages/hoppscotch-common/src/components/collections/MyCollections.vue

@@ -358,7 +358,7 @@ export type Collection = {
   isLastItem: boolean
   data: {
     parentIndex: null
-    data: HoppCollection<HoppRESTRequest>
+    data: HoppCollection
   }
 }
 
@@ -367,7 +367,7 @@ type Folder = {
   isLastItem: boolean
   data: {
     parentIndex: string
-    data: HoppCollection<HoppRESTRequest>
+    data: HoppCollection
   }
 }
 
@@ -394,7 +394,7 @@ type CollectionType =
 
 const props = defineProps({
   filteredCollections: {
-    type: Array as PropType<HoppCollection<HoppRESTRequest>[]>,
+    type: Array as PropType<HoppCollection[]>,
     default: () => [],
     required: true,
   },
@@ -426,35 +426,35 @@ const emit = defineEmits<{
     event: "add-request",
     payload: {
       path: string
-      folder: HoppCollection<HoppRESTRequest>
+      folder: HoppCollection
     }
   ): void
   (
     event: "add-folder",
     payload: {
       path: string
-      folder: HoppCollection<HoppRESTRequest>
+      folder: HoppCollection
     }
   ): void
   (
     event: "edit-collection",
     payload: {
       collectionIndex: string
-      collection: HoppCollection<HoppRESTRequest>
+      collection: HoppCollection
     }
   ): void
   (
     event: "edit-folder",
     payload: {
       folderPath: string
-      folder: HoppCollection<HoppRESTRequest>
+      folder: HoppCollection
     }
   ): void
   (
     event: "edit-properties",
     payload: {
       collectionIndex: string
-      collection: HoppCollection<HoppRESTRequest>
+      collection: HoppCollection
     }
   ): void
   (
@@ -472,7 +472,7 @@ const emit = defineEmits<{
       request: HoppRESTRequest
     }
   ): void
-  (event: "export-data", payload: HoppCollection<HoppRESTRequest>): void
+  (event: "export-data", payload: HoppCollection): void
   (event: "remove-collection", payload: string): void
   (event: "remove-folder", payload: string): void
   (
@@ -686,10 +686,10 @@ const updateCollectionOrder = (
 type MyCollectionNode = Collection | Folder | Requests
 
 class MyCollectionsAdapter implements SmartTreeAdapter<MyCollectionNode> {
-  constructor(public data: Ref<HoppCollection<HoppRESTRequest>[]>) {}
+  constructor(public data: Ref<HoppCollection[]>) {}
 
   navigateToFolderWithIndexPath(
-    collections: HoppCollection<HoppRESTRequest>[],
+    collections: HoppCollection[],
     indexPaths: number[]
   ) {
     if (indexPaths.length === 0) return null

Some files were not shown because too many files changed in this diff