refactor: lazy compute codegen code and error handling
This commit is contained in:
@@ -121,6 +121,8 @@ const requestCode = computed(() => {
|
|||||||
getCurrentEnvironment()
|
getCurrentEnvironment()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (!props.show) return ""
|
||||||
|
|
||||||
const result = generateCode(codegenType.value, effectiveRequest)
|
const result = generateCode(codegenType.value, effectiveRequest)
|
||||||
|
|
||||||
if (O.isSome(result)) {
|
if (O.isSome(result)) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import HTTPSnippet from "httpsnippet"
|
import HTTPSnippet from "httpsnippet"
|
||||||
import { HoppRESTRequest } from "@hoppscotch/data"
|
import { HoppRESTRequest } from "@hoppscotch/data"
|
||||||
import * as O from "fp-ts/Option"
|
import * as O from "fp-ts/Option"
|
||||||
|
import * as E from "fp-ts/Either"
|
||||||
import { pipe } from "fp-ts/function"
|
import { pipe } from "fp-ts/function"
|
||||||
import { buildHarRequest } from "./har"
|
import { buildHarRequest } from "./har"
|
||||||
|
|
||||||
@@ -200,16 +201,24 @@ export const generateCode = (
|
|||||||
const codegenInfo = CodegenDefinitions.find((v) => v.name === codegen)!
|
const codegenInfo = CodegenDefinitions.find((v) => v.name === codegen)!
|
||||||
|
|
||||||
return pipe(
|
return pipe(
|
||||||
O.of(
|
E.tryCatch(
|
||||||
// Returns a string if valid, false if not
|
() =>
|
||||||
new HTTPSnippet({
|
new HTTPSnippet({
|
||||||
...buildHarRequest(req),
|
...buildHarRequest(req),
|
||||||
}).convert(codegenInfo.lang, codegenInfo.mode, {
|
}).convert(codegenInfo.lang, codegenInfo.mode, {
|
||||||
indent: " ",
|
indent: " ",
|
||||||
})
|
}),
|
||||||
|
(e) => e
|
||||||
),
|
),
|
||||||
|
|
||||||
// Only allow string output to pass through, else none
|
// Only allow string output to pass through, else none
|
||||||
O.chain(O.fromPredicate((val): val is string => typeof val === "string"))
|
E.chainW(
|
||||||
|
E.fromPredicate(
|
||||||
|
(val): val is string => typeof val === "string",
|
||||||
|
() => "code generator failed" as const
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
|
O.fromEither
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user