Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

refactor: lazy compute codegen code and error handling

Andrew Bastin2ec401c7662d
diff --git a/packages/hoppscotch-app/components/http/CodegenModal.vue b/packages/hoppscotch-app/components/http/CodegenModal.vue
index 4a47c9764..dcafba0d7 100644
--- a/packages/hoppscotch-app/components/http/CodegenModal.vue
+++ b/packages/hoppscotch-app/components/http/CodegenModal.vue
@@ -121,6 +121,8 @@ const requestCode = computed(() => {
getCurrentEnvironment()
)
+ if (!props.show) return ""
+
const result = generateCode(codegenType.value, effectiveRequest)
if (O.isSome(result)) {
diff --git a/packages/hoppscotch-app/helpers/new-codegen/index.ts b/packages/hoppscotch-app/helpers/new-codegen/index.ts
index c9630c4aa..c1dbf8e8a 100644
--- a/packages/hoppscotch-app/helpers/new-codegen/index.ts
+++ b/packages/hoppscotch-app/helpers/new-codegen/index.ts
@@ -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
)
}