diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts index 2299dda1d..82d01568b 100644 --- a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts +++ b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts @@ -5,6 +5,7 @@ import { OpenAPIV3_1 as OpenAPIV31, } from "openapi-types" import SwaggerParser from "@apidevtools/swagger-parser" +import yaml from "js-yaml" import { FormDataKeyValue, HoppRESTAuth, @@ -28,11 +29,11 @@ import { Collection, makeCollection } from "~/newstore/collections" const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const // TODO: URL Import Support -// TODO: YAMLLLLLLL import support!!!!! -// TODO: Oauth! const safeParseJSON = (str: string) => O.tryCatch(() => JSON.parse(str)) +const safeParseYAML = (str: string) => O.tryCatch(() => yaml.load(str)) + const objectHasProperty = ( obj: unknown, propName: T @@ -573,13 +574,23 @@ const convertOpenApiDocToHopp = ( ]) } +const parseOpenAPIDocContent = (str: string) => + pipe( + str, + safeParseJSON, + O.match( + () => safeParseYAML(str), + (data) => O.of(data) + ) + ) + export default defineImporter({ name: "Swagger/OpenAPI v3 Schema", steps: [ step({ stepName: "FILE_OR_URL_IMPORT", metadata: { - acceptedFileTypes: "application/json", + acceptedFileTypes: ".json, .yaml, .yml", }, }), ] as const, @@ -587,7 +598,7 @@ export default defineImporter({ pipe( // See if we can parse JSON properly fileContent, - safeParseJSON, + parseOpenAPIDocContent, TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT), // Try validating, else the importer is invalid file format diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.json index b976317ac..7b8a22663 100644 --- a/packages/hoppscotch-app/package.json +++ b/packages/hoppscotch-app/package.json @@ -84,6 +84,7 @@ "graphql-tag": "^2.12.6", "httpsnippet": "^2.0.0", "io-ts": "^2.2.16", + "js-yaml": "^4.1.0", "json-loader": "^0.5.7", "lodash": "^4.17.21", "mustache": "^4.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f8e3735d7..88bfca941 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -138,6 +138,7 @@ importers: io-ts: ^2.2.16 jest: ^27.4.5 jest-serializer-vue: ^2.0.2 + js-yaml: ^4.1.0 json-loader: ^0.5.7 lodash: ^4.17.21 mustache: ^4.2.0 @@ -230,6 +231,7 @@ importers: graphql-tag: 2.12.6_graphql@15.7.2 httpsnippet: 2.0.0 io-ts: 2.2.16_fp-ts@2.11.5 + js-yaml: 4.1.0 json-loader: 0.5.7 lodash: 4.17.21 mustache: 4.2.0