From 6dbaf524ce0be7909855c64563563662f4d5d91a Mon Sep 17 00:00:00 2001 From: Akash K <57758277+amk-dev@users.noreply.github.com> Date: Thu, 7 Mar 2024 19:55:46 +0530 Subject: [PATCH] feat: use tags as folders when importing from openapi (#3846) --- .../helpers/import-export/import/openapi.ts | 84 ++++++++++++++----- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts index 14748a56c..cc3d8cb9c 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts @@ -17,6 +17,7 @@ import { HoppCollection, makeCollection, HoppRESTRequestVariable, + HoppRESTRequest, } from "@hoppscotch/data" import { pipe, flow } from "fp-ts/function" import * as A from "fp-ts/Array" @@ -25,6 +26,7 @@ import * as O from "fp-ts/Option" import * as TE from "fp-ts/TaskEither" import * as RA from "fp-ts/ReadonlyArray" import { IMPORTER_INVALID_FILE_FORMAT } from "." +import { cloneDeep } from "lodash-es" export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const @@ -580,30 +582,42 @@ const convertPathToHoppReqs = ( ? openAPIUrl + openAPIPath.slice(1) : openAPIUrl + openAPIPath - return makeRESTRequest({ - name: info.operationId ?? info.summary ?? "Untitled Request", - method: method.toUpperCase(), - endpoint, + const res: { + request: HoppRESTRequest + metadata: { + tags: string[] + } + } = { + request: makeRESTRequest({ + name: info.operationId ?? info.summary ?? "Untitled Request", + method: method.toUpperCase(), + endpoint, - // We don't need to worry about reference types as the Dereferencing pass should remove them - params: parseOpenAPIParams( - (info.parameters as OpenAPIParamsType[] | undefined) ?? [] - ), - headers: parseOpenAPIHeaders( - (info.parameters as OpenAPIParamsType[] | undefined) ?? [] - ), + // We don't need to worry about reference types as the Dereferencing pass should remove them + params: parseOpenAPIParams( + (info.parameters as OpenAPIParamsType[] | undefined) ?? [] + ), + headers: parseOpenAPIHeaders( + (info.parameters as OpenAPIParamsType[] | undefined) ?? [] + ), - auth: parseOpenAPIAuth(doc, info), + auth: parseOpenAPIAuth(doc, info), - body: parseOpenAPIBody(doc, info), + body: parseOpenAPIBody(doc, info), - preRequestScript: "", - testScript: "", + preRequestScript: "", + testScript: "", - requestVariables: parseOpenAPIVariables( - (info.parameters as OpenAPIParamsType[] | undefined) ?? [] - ), - }) + requestVariables: parseOpenAPIVariables( + (info.parameters as OpenAPIParamsType[] | undefined) ?? [] + ), + }), + metadata: { + tags: info.tags ?? [], + }, + } + + return res }), // Disable Readonly @@ -622,10 +636,38 @@ const convertOpenApiDocsToHopp = ( ) .flat() + const requestsByTags: Record> = {} + const requestsWithoutTags: Array = [] + + paths.forEach(({ metadata, request }) => { + const tags = metadata.tags + + if (tags.length === 0) { + requestsWithoutTags.push(request) + return + } + + for (const tag of tags) { + if (!requestsByTags[tag]) { + requestsByTags[tag] = [] + } + + requestsByTags[tag].push(cloneDeep(request)) + } + }) + return makeCollection({ name, - folders: [], - requests: paths, + folders: Object.entries(requestsByTags).map(([name, paths]) => + makeCollection({ + name, + requests: paths, + folders: [], + auth: { authType: "inherit", authActive: true }, + headers: [], + }) + ), + requests: requestsWithoutTags, auth: { authType: "inherit", authActive: true }, headers: [], })