From c3aedac77e6b171ccfb6313a3693dd8f8ad4df07 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Mon, 3 Jan 2022 10:48:18 +0530 Subject: [PATCH] fix: issues with har generation --- .../components/http/CodegenModal.vue | 19 +++++++++++++++++-- .../hoppscotch-app/helpers/new-codegen/har.ts | 15 +++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/hoppscotch-app/components/http/CodegenModal.vue b/packages/hoppscotch-app/components/http/CodegenModal.vue index 9901aed4..175162ea 100644 --- a/packages/hoppscotch-app/components/http/CodegenModal.vue +++ b/packages/hoppscotch-app/components/http/CodegenModal.vue @@ -96,6 +96,7 @@ import { CodegenName, generateCode, } from "~/helpers/new-codegen" +import { makeRESTRequest } from "~/../hoppscotch-data/dist" const t = useI18n() @@ -118,13 +119,27 @@ const errorState = ref(false) const requestCode = computed(() => { const effectiveRequest = getEffectiveRESTRequest( - request.value as any, + request.value, getCurrentEnvironment() ) if (!props.show) return "" - const result = generateCode(codegenType.value, effectiveRequest) + const result = generateCode( + codegenType.value, + makeRESTRequest({ + ...effectiveRequest, + headers: effectiveRequest.effectiveFinalHeaders.map((header) => ({ + ...header, + active: true, + })), + params: effectiveRequest.effectiveFinalParams.map((param) => ({ + ...param, + active: true, + })), + endpoint: effectiveRequest.effectiveFinalURL, + }) + ) if (O.isSome(result)) { errorState.value = false diff --git a/packages/hoppscotch-app/helpers/new-codegen/har.ts b/packages/hoppscotch-app/helpers/new-codegen/har.ts index d8e1bdfc..04b81f59 100644 --- a/packages/hoppscotch-app/helpers/new-codegen/har.ts +++ b/packages/hoppscotch-app/helpers/new-codegen/har.ts @@ -2,7 +2,7 @@ import * as Har from "har-format" import { HoppRESTRequest } from "@hoppscotch/data" import { FieldEquals, objectFieldIncludes } from "../typeutils" -// We support HAR Spec 1.2 +// Hoppscotch support HAR Spec 1.2 // For more info on the spec: http://www.softwareishard.com/blog/har-12-spec/ const buildHarHeaders = (req: HoppRESTRequest): Har.Header[] => { @@ -69,9 +69,6 @@ const buildHarPostParams = ( const buildHarPostData = (req: HoppRESTRequest): Har.PostData | undefined => { if (!req.body.contentType) return undefined - if (!objectFieldIncludes(req, "method", ["POST", "PUT"] as const)) - return undefined - if ( objectFieldIncludes(req.body, "contentType", [ "application/x-www-form-urlencoded", @@ -82,13 +79,11 @@ const buildHarPostData = (req: HoppRESTRequest): Har.PostData | undefined => { mimeType: req.body.contentType, // By default assume JSON ? params: buildHarPostParams(req as any), } - } else { - if (!req.body.contentType) return undefined + } - return { - mimeType: req.body.contentType, // Let's assume by default content type is JSON - text: req.body.body, - } + return { + mimeType: req.body.contentType, // Let's assume by default content type is JSON + text: req.body.body, } }