refactor: lazy compute codegen code and error handling

This commit is contained in:
Andrew Bastin
2022-01-01 16:08:15 +05:30
parent 314c0968b1
commit 2ec401c766
2 changed files with 19 additions and 8 deletions

View File

@@ -121,6 +121,8 @@ const requestCode = computed(() => {
getCurrentEnvironment()
)
if (!props.show) return ""
const result = generateCode(codegenType.value, effectiveRequest)
if (O.isSome(result)) {

View File

@@ -1,6 +1,7 @@
import HTTPSnippet from "httpsnippet"
import { HoppRESTRequest } from "@hoppscotch/data"
import * as O from "fp-ts/Option"
import * as E from "fp-ts/Either"
import { pipe } from "fp-ts/function"
import { buildHarRequest } from "./har"
@@ -200,16 +201,24 @@ export const generateCode = (
const codegenInfo = CodegenDefinitions.find((v) => v.name === codegen)!
return pipe(
O.of(
// Returns a string if valid, false if not
new HTTPSnippet({
...buildHarRequest(req),
}).convert(codegenInfo.lang, codegenInfo.mode, {
indent: " ",
})
E.tryCatch(
() =>
new HTTPSnippet({
...buildHarRequest(req),
}).convert(codegenInfo.lang, codegenInfo.mode, {
indent: " ",
}),
(e) => e
),
// 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
)
}