feat(common): support importing OpenAPI YAML definitions from URL (#5098)

Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com>
This commit is contained in:
Anwarul Islam 2025-05-27 14:03:33 +06:00 committed by GitHub
parent b414496943
commit cf442c5450
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View file

@ -23,9 +23,10 @@
<p class="flex flex-col">
<input
v-model="inputChooseGistToImportFrom"
v-focus
:placeholder="t('import.from_url')"
type="url"
class="input"
:placeholder="`${t('import.from_url')}`"
/>
</p>
@ -49,7 +50,7 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { useService } from "dioc/vue"
import * as E from "fp-ts/Either"
import * as O from "fp-ts/Option"
import { parseBodyAsJSON } from "~/helpers/functional/json"
import { parseBodyAsJSONOrYAML } from "~/helpers/functional/json"
const interceptorService = useService(KernelInterceptorService)
@ -98,7 +99,7 @@ const urlFetchLogic =
return E.left("REQUEST_FAILED")
}
const responsePayload = parseBodyAsJSON<unknown>(res.right.body)
const responsePayload = parseBodyAsJSONOrYAML<unknown>(res.right.body)
if (O.isSome(responsePayload)) {
// stringify the response payload

View file

@ -5,6 +5,7 @@ import { pipe, flow } from "fp-ts/function"
import { MediaType, RelayResponseBody } from "@hoppscotch/kernel"
import { decodeToString } from "~/helpers/functional/parse"
import { safeParseJSONOrYAML } from "./yaml"
type SafeParseJSON = {
(str: string, convertToArray: true): O.Option<Array<unknown>>
@ -49,3 +50,18 @@ export const parseBodyAsJSON = <T>(body: RelayResponseBody): O.Option<T> =>
O.filter((type) => type.includes(MediaType.APPLICATION_JSON)),
O.chain(() => parseBytesToJSON<T>(body.body))
)
/**
* Parses response body as JSON or YAML content
* @param body Response body from RelayResponse
* @returns Option containing parsed data or none if parsing fails
*/
export const parseBodyAsJSONOrYAML = <T>(
body: RelayResponseBody
): O.Option<T> =>
pipe(
body.body,
decodeToString,
E.fold(() => O.none, safeParseJSONOrYAML),
O.map((data) => data as T)
)