From 94e0c3ef640872fcaa8f7753d3f395d001db271d Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 26 Aug 2021 11:02:05 +0530 Subject: [PATCH] refactor: update codegen code --- components/http/CodegenModal.vue | 130 +++++++++++++------------------ helpers/codegen/codegen.ts | 101 +++++++++++++++++++++++- nuxt.config.js | 2 +- 3 files changed, 155 insertions(+), 78 deletions(-) diff --git a/components/http/CodegenModal.vue b/components/http/CodegenModal.vue index 72a5a01db..dc8e02cbc 100644 --- a/components/http/CodegenModal.vue +++ b/components/http/CodegenModal.vue @@ -26,8 +26,10 @@ :info-icon="gen.id === codegenType ? 'done' : ''" :active-info-icon="gen.id === codegenType" @click.native=" - codegenType = gen.id - $refs.options.tippy().hide() + () => { + codegenType = gen.id + options.tippy().hide() + } " /> @@ -68,82 +70,62 @@ - diff --git a/helpers/codegen/codegen.ts b/helpers/codegen/codegen.ts index 220d8934a..81c257c73 100644 --- a/helpers/codegen/codegen.ts +++ b/helpers/codegen/codegen.ts @@ -1,4 +1,9 @@ -import { HoppRESTHeader, HoppRESTParam } from "../types/HoppRESTRequest" +import { + FormDataKeyValue, + HoppRESTHeader, + HoppRESTParam, +} from "../types/HoppRESTRequest" +import { EffectiveHoppRESTRequest } from "../utils/EffectiveURL" import { CLibcurlCodegen } from "./generators/c-libcurl" import { CsRestsharpCodegen } from "./generators/cs-restsharp" import { CurlCodegen } from "./generators/curl" @@ -67,11 +72,11 @@ export type HoppCodegenContext = { bearerToken: string | null headers: HoppRESTHeader[] params: HoppRESTParam[] - bodyParams: any // TODO: Change this + bodyParams: FormDataKeyValue[] rawParams: string | null rawInput: boolean rawRequestBody: any - contentType: string + contentType: string | null queryString: string } @@ -86,3 +91,93 @@ export function generateCodeWithGenerator( return "" } + +function getCodegenAuth( + request: EffectiveHoppRESTRequest +): Pick< + HoppCodegenContext, + "auth" | "bearerToken" | "httpUser" | "httpPassword" +> { + if (!request.auth.authActive || request.auth.authType === "none") { + return { + auth: "None", + httpUser: null, + httpPassword: null, + bearerToken: null, + } + } + + if (request.auth.authType === "basic") { + return { + auth: "Basic Auth", + httpUser: request.auth.username, + httpPassword: request.auth.password, + bearerToken: null, + } + } else { + return { + auth: "Bearer Token", + httpUser: null, + httpPassword: null, + bearerToken: request.auth.token, + } + } +} + +function getCodegenGeneralRESTInfo( + request: EffectiveHoppRESTRequest +): Pick< + HoppCodegenContext, + | "name" + | "uri" + | "url" + | "method" + | "queryString" + | "pathName" + | "params" + | "headers" +> { + const urlObj = new URL(request.effectiveFinalURL) + + return { + name: request.name, + uri: request.effectiveFinalURL, + headers: request.effectiveFinalHeaders.map((x) => ({ ...x, active: true })), + params: request.effectiveFinalParams.map((x) => ({ ...x, active: true })), + method: request.method, + url: urlObj.origin, + queryString: urlObj.searchParams.toString(), + pathName: urlObj.pathname, + } +} + +function getCodegenReqBodyData( + request: EffectiveHoppRESTRequest +): Pick< + HoppCodegenContext, + "rawRequestBody" | "rawInput" | "contentType" | "bodyParams" | "rawParams" +> { + return { + contentType: request.body.contentType, + rawInput: request.body.contentType !== "multipart/form-data", + rawRequestBody: + request.body.contentType !== "multipart/form-data" + ? request.body.body + : null, + bodyParams: + request.body.contentType === "multipart/form-data" + ? request.body.body + : [], + rawParams: null, + } +} + +export function generateCodegenContext( + request: EffectiveHoppRESTRequest +): HoppCodegenContext { + return { + ...getCodegenAuth(request), + ...getCodegenGeneralRESTInfo(request), + ...getCodegenReqBodyData(request), + } +} diff --git a/nuxt.config.js b/nuxt.config.js index e833a6926..0554fcf30 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -125,7 +125,7 @@ export default { // https: //github.com/nuxt-community/google-fonts-module "@nuxtjs/google-fonts", // https://github.com/nuxt/typescript - "@nuxt/typescript-build", + ["@nuxt/typescript-build", { typeCheck: false }], // https://github.com/nuxt-community/dotenv-module "@nuxtjs/dotenv", // https://github.com/nuxt-community/composition-api