From 95953557de447da94358fb8d09e67d8558709245 Mon Sep 17 00:00:00 2001 From: nivedin Date: Mon, 11 Dec 2023 17:41:02 +0530 Subject: [PATCH] refactor: update all hoppCollection type to remove generic pattern --- .../hoppscotch-cli/src/types/collections.ts | 4 +- packages/hoppscotch-cli/src/types/request.ts | 2 +- packages/hoppscotch-cli/src/utils/checks.ts | 11 +-- .../hoppscotch-cli/src/utils/collections.ts | 94 +++++++++---------- packages/hoppscotch-cli/src/utils/mutators.ts | 34 +++---- .../app/spotlight/entry/GQLRequest.vue | 4 +- .../app/spotlight/entry/RESTRequest.vue | 4 +- .../src/components/collections/Collection.vue | 8 +- .../components/collections/ImportExport.vue | 13 +-- .../components/collections/MyCollections.vue | 22 ++--- .../src/components/collections/Properties.vue | 4 +- .../components/collections/graphql/Add.vue | 4 +- .../collections/graphql/Collection.vue | 6 +- .../components/collections/graphql/Edit.vue | 4 +- .../components/collections/graphql/Folder.vue | 4 +- .../collections/graphql/ImportExport.vue | 6 +- .../components/collections/graphql/index.vue | 14 +-- .../src/components/collections/index.vue | 36 +++---- .../ImportExportSteps/MyCollectionImport.vue | 4 +- .../src/helpers/backend/helpers.ts | 5 +- .../src/helpers/collection/collection.ts | 6 +- .../src/helpers/collection/request.ts | 2 +- .../import-export/export/gqlCollections.ts | 6 +- .../import-export/export/myCollections.ts | 6 +- .../helpers/import-export/import/hoppGql.ts | 6 +- .../helpers/import-export/import/insomnia.ts | 2 +- .../helpers/import-export/import/openapi.ts | 5 +- .../helpers/import-export/import/postman.ts | 2 +- .../helpers/teams/TeamCollectionAdapter.ts | 3 +- .../src/newstore/collections.ts | 84 +++++++---------- .../hoppscotch-common/src/pages/import.vue | 6 +- .../persistence/__tests__/__mocks__/index.ts | 15 +-- .../collections/collections.platform.ts | 12 +-- .../platform/collections/collections.sync.ts | 2 +- .../collections/gqlCollections.sync.ts | 2 +- .../collections/collections.platform.ts | 12 +-- .../platform/collections/collections.sync.ts | 2 +- .../collections/gqlCollections.sync.ts | 2 +- 38 files changed, 201 insertions(+), 257 deletions(-) diff --git a/packages/hoppscotch-cli/src/types/collections.ts b/packages/hoppscotch-cli/src/types/collections.ts index 0b341fc4b..f5483e2d4 100644 --- a/packages/hoppscotch-cli/src/types/collections.ts +++ b/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[]; + collections: HoppCollection[]; envs: HoppEnvs; delay?: number; }; diff --git a/packages/hoppscotch-cli/src/types/request.ts b/packages/hoppscotch-cli/src/types/request.ts index 9e24ff7c8..2407542f1 100644 --- a/packages/hoppscotch-cli/src/types/request.ts +++ b/packages/hoppscotch-cli/src/types/request.ts @@ -33,7 +33,7 @@ export type HoppEnvs = { export type CollectionStack = { path: string; - collection: HoppCollection; + collection: HoppCollection; }; export type RequestReport = { diff --git a/packages/hoppscotch-cli/src/utils/checks.ts b/packages/hoppscotch-cli/src/utils/checks.ts index dc184b528..f935cbe4a 100644 --- a/packages/hoppscotch-cli/src/utils/checks.ts +++ b/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 =

( * @returns True, if unknown parameter is valid Hoppscotch REST Collection; * False, otherwise. */ -export const isRESTCollection = ( - param: unknown -): param is HoppCollection => { +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. diff --git a/packages/hoppscotch-cli/src/utils/collections.ts b/packages/hoppscotch-cli/src/utils/collections.ts index c671d7c96..f13245513 100644 --- a/packages/hoppscotch-cli/src/utils/collections.ts +++ b/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 => - { - const envs: HoppEnvs = param.envs; - const delay = param.delay ?? 0; - const requestsReport: RequestReport[] = []; - const collectionStack: CollectionStack[] = getCollectionStack( - param.collections - ); +export const collectionsRunner = async ( + param: CollectionRunnerParam +): Promise => { + 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.pop(); + while (collectionStack.length) { + // Pop out top-most collection from stack to be processed. + const { collection, path } = 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, - }; + // 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)}`)); + // Request processing initiated message. + log(WARN(`\nRunning: ${bold(requestPath)}`)); - // Processing current request. - const result = await processRequest(processRequestParams)(); + // 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; + // 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, - }); - } + // 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[] -): CollectionStack[] => +const getCollectionStack = (collections: HoppCollection[]): CollectionStack[] => pipe( collections, A.map( diff --git a/packages/hoppscotch-cli/src/utils/mutators.ts b/packages/hoppscotch-cli/src/utils/mutators.ts index 1015ed2de..79053ed12 100644 --- a/packages/hoppscotch-cli/src/utils/mutators.ts +++ b/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 { - 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 { * 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, + * @returns For successful parsing we get array of HoppCollection, */ export async function parseCollectionData( path: string -): Promise[]> { - let contents = await readJsonFile(path) +): Promise { + 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[] -}; + return maybeArrayOfCollections as HoppCollection[]; +} diff --git a/packages/hoppscotch-common/src/components/app/spotlight/entry/GQLRequest.vue b/packages/hoppscotch-common/src/components/app/spotlight/entry/GQLRequest.vue index b91ef903c..bc27b93c8 100644 --- a/packages/hoppscotch-common/src/components/app/spotlight/entry/GQLRequest.vue +++ b/packages/hoppscotch-common/src/components/app/spotlight/entry/GQLRequest.vue @@ -13,7 +13,7 @@