feat: use tags as folders when importing from openapi (#3846)
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
|||||||
HoppCollection,
|
HoppCollection,
|
||||||
makeCollection,
|
makeCollection,
|
||||||
HoppRESTRequestVariable,
|
HoppRESTRequestVariable,
|
||||||
|
HoppRESTRequest,
|
||||||
} from "@hoppscotch/data"
|
} from "@hoppscotch/data"
|
||||||
import { pipe, flow } from "fp-ts/function"
|
import { pipe, flow } from "fp-ts/function"
|
||||||
import * as A from "fp-ts/Array"
|
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 TE from "fp-ts/TaskEither"
|
||||||
import * as RA from "fp-ts/ReadonlyArray"
|
import * as RA from "fp-ts/ReadonlyArray"
|
||||||
import { IMPORTER_INVALID_FILE_FORMAT } from "."
|
import { IMPORTER_INVALID_FILE_FORMAT } from "."
|
||||||
|
import { cloneDeep } from "lodash-es"
|
||||||
|
|
||||||
export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
|
export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
|
||||||
|
|
||||||
@@ -580,7 +582,13 @@ const convertPathToHoppReqs = (
|
|||||||
? openAPIUrl + openAPIPath.slice(1)
|
? openAPIUrl + openAPIPath.slice(1)
|
||||||
: openAPIUrl + openAPIPath
|
: openAPIUrl + openAPIPath
|
||||||
|
|
||||||
return makeRESTRequest({
|
const res: {
|
||||||
|
request: HoppRESTRequest
|
||||||
|
metadata: {
|
||||||
|
tags: string[]
|
||||||
|
}
|
||||||
|
} = {
|
||||||
|
request: makeRESTRequest({
|
||||||
name: info.operationId ?? info.summary ?? "Untitled Request",
|
name: info.operationId ?? info.summary ?? "Untitled Request",
|
||||||
method: method.toUpperCase(),
|
method: method.toUpperCase(),
|
||||||
endpoint,
|
endpoint,
|
||||||
@@ -603,7 +611,13 @@ const convertPathToHoppReqs = (
|
|||||||
requestVariables: parseOpenAPIVariables(
|
requestVariables: parseOpenAPIVariables(
|
||||||
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
|
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
|
||||||
),
|
),
|
||||||
})
|
}),
|
||||||
|
metadata: {
|
||||||
|
tags: info.tags ?? [],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// Disable Readonly
|
// Disable Readonly
|
||||||
@@ -622,10 +636,38 @@ const convertOpenApiDocsToHopp = (
|
|||||||
)
|
)
|
||||||
.flat()
|
.flat()
|
||||||
|
|
||||||
|
const requestsByTags: Record<string, Array<HoppRESTRequest>> = {}
|
||||||
|
const requestsWithoutTags: Array<HoppRESTRequest> = []
|
||||||
|
|
||||||
|
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({
|
return makeCollection({
|
||||||
name,
|
name,
|
||||||
folders: [],
|
folders: Object.entries(requestsByTags).map(([name, paths]) =>
|
||||||
|
makeCollection({
|
||||||
|
name,
|
||||||
requests: paths,
|
requests: paths,
|
||||||
|
folders: [],
|
||||||
|
auth: { authType: "inherit", authActive: true },
|
||||||
|
headers: [],
|
||||||
|
})
|
||||||
|
),
|
||||||
|
requests: requestsWithoutTags,
|
||||||
auth: { authType: "inherit", authActive: true },
|
auth: { authType: "inherit", authActive: true },
|
||||||
headers: [],
|
headers: [],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user