From 2fd9eb0767f22d358b5cdcc24960bc54ab3fb943 Mon Sep 17 00:00:00 2001 From: Rishabh Agarwal Date: Mon, 18 Apr 2022 19:29:45 +0530 Subject: [PATCH] refactor: address comments --- .../hoppscotch-app/helpers/functional/json.ts | 3 + .../import-export/import/openapi/exampleV2.ts | 60 ++++++++++--------- .../import-export/import/openapi/index.ts | 14 +++-- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/packages/hoppscotch-app/helpers/functional/json.ts b/packages/hoppscotch-app/helpers/functional/json.ts index 2ba1317c2..a9cbaa7a5 100644 --- a/packages/hoppscotch-app/helpers/functional/json.ts +++ b/packages/hoppscotch-app/helpers/functional/json.ts @@ -7,3 +7,6 @@ import * as O from "fp-ts/Option" */ export const safeParseJSON = (str: string): O.Option => O.tryCatch(() => JSON.parse(str)) + +export const prettyPrintStringifyJSON = (jsonObject: any): O.Option => + O.tryCatch(() => JSON.stringify(jsonObject, null, "\t")) diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV2.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV2.ts index eac44722c..2923bc12f 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV2.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/openapi/exampleV2.ts @@ -2,6 +2,7 @@ import { OpenAPIV2 } from "openapi-types" import * as O from "fp-ts/Option" import { pipe, flow } from "fp-ts/function" import * as A from "fp-ts/Array" +import { prettyPrintStringifyJSON } from "~/helpers/functional/json" type PrimitiveSchemaType = "string" | "integer" | "number" | "boolean" @@ -53,52 +54,51 @@ const isSchemaTypeObject = (schemaType: string): schemaType is "object" => const getSampleEnumValueOrPlaceholder = ( schema: OpenAPIV2.SchemaObject -): RequestBodyExampleType => { - const enumValue = pipe( +): RequestBodyExampleType => + pipe( schema.enum, O.fromNullable, - O.map((enums) => enums[0] as RequestBodyExampleType) - ) - - if (O.isSome(enumValue)) return enumValue.value - - return pipe( - schema, - getSchemaTypeFromSchemaObject, - O.filter(isSchemaTypePrimitive), - O.map(getPrimitiveTypePlaceholder), + O.map((enums) => enums[0] as RequestBodyExampleType), + O.altW(() => + pipe( + schema, + getSchemaTypeFromSchemaObject, + O.filter(isSchemaTypePrimitive), + O.map(getPrimitiveTypePlaceholder) + ) + ), O.getOrElseW(() => "") ) -} const generateExampleArrayFromOpenAPIV2ItemsObject = ( items: OpenAPIV2.ItemsObject -): RequestBodyExampleType => { +): RequestBodyExampleType => // ItemsObject can not hold type "object" // https://swagger.io/specification/v2/#itemsObject // TODO : Handle array of objects // https://stackoverflow.com/questions/60490974/how-to-define-an-array-of-objects-in-openapi-2-0 - const primitivePlaceholder = pipe( + pipe( items, O.fromPredicate( flow((items) => items.type as SchemaType, isSchemaTypePrimitive) ), - O.map(getSampleEnumValueOrPlaceholder) - ) - - if (O.isSome(primitivePlaceholder)) - return Array.of(primitivePlaceholder.value, primitivePlaceholder.value) - - // If the type is not primitive, it is "array" - // items property is required if type is array - return Array.of( - generateExampleArrayFromOpenAPIV2ItemsObject( - items.items as OpenAPIV2.ItemsObject + O.map( + flow(getSampleEnumValueOrPlaceholder, (arrayItem) => + Array.of(arrayItem, arrayItem) + ) + ), + O.getOrElse(() => + // If the type is not primitive, it is "array" + // items property is required if type is array + Array.of( + generateExampleArrayFromOpenAPIV2ItemsObject( + items.items as OpenAPIV2.ItemsObject + ) + ) ) ) -} const generateRequestBodyExampleFromOpenAPIV2BodySchema = ( schema: OpenAPIV2.SchemaObject @@ -180,5 +180,9 @@ export const generateRequestBodyExampleFromOpenAPIV2Body = ( ) ), O.getOrElse(() => "" as RequestBodyExampleType), - (requestBodyExample) => JSON.stringify(requestBodyExample, null, "\t") // Using a tab character mimics standard pretty-print appearance + (requestBodyExample) => + pipe( + prettyPrintStringifyJSON(requestBodyExample), + O.getOrElse(() => "") + ) ) diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi/index.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi/index.ts index 1ce452edd..66525f9df 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/openapi/index.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/openapi/index.ts @@ -29,6 +29,7 @@ import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "../" import { generateRequestBodyExampleFromMediaObject as generateExampleV31 } from "./exampleV31" import { generateRequestBodyExampleFromMediaObject as generateExampleV3 } from "./exampleV3" import { generateRequestBodyExampleFromOpenAPIV2Body } from "./exampleV2" +import { prettyPrintStringifyJSON } from "~/helpers/functional/json" const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const @@ -205,12 +206,13 @@ const parseOpenAPIV3Body = ( OpenAPIV3.MediaTypeObject | OpenAPIV31.MediaTypeObject ] = objs[0] - const exampleBody = JSON.stringify( - isV31Request - ? generateExampleV31(media as OpenAPIV31.MediaTypeObject) - : generateExampleV3(media as OpenAPIV3.MediaTypeObject), - null, - "\t" + const exampleBody = pipe( + prettyPrintStringifyJSON( + isV31Request + ? generateExampleV31(media as OpenAPIV31.MediaTypeObject) + : generateExampleV3(media as OpenAPIV3.MediaTypeObject) + ), + O.getOrElse(() => "") ) return contentType in knownContentTypes