feat: add support to openapi importer for yaml

This commit is contained in:
Andrew Bastin
2022-01-10 20:06:31 +05:30
parent b40b8070c1
commit 9e6a3883ac
3 changed files with 18 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import {
OpenAPIV3_1 as OpenAPIV31, OpenAPIV3_1 as OpenAPIV31,
} from "openapi-types" } from "openapi-types"
import SwaggerParser from "@apidevtools/swagger-parser" import SwaggerParser from "@apidevtools/swagger-parser"
import yaml from "js-yaml"
import { import {
FormDataKeyValue, FormDataKeyValue,
HoppRESTAuth, HoppRESTAuth,
@@ -28,11 +29,11 @@ import { Collection, makeCollection } from "~/newstore/collections"
const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
// TODO: URL Import Support // TODO: URL Import Support
// TODO: YAMLLLLLLL import support!!!!!
// TODO: Oauth!
const safeParseJSON = (str: string) => O.tryCatch(() => JSON.parse(str)) const safeParseJSON = (str: string) => O.tryCatch(() => JSON.parse(str))
const safeParseYAML = (str: string) => O.tryCatch(() => yaml.load(str))
const objectHasProperty = <T extends string>( const objectHasProperty = <T extends string>(
obj: unknown, obj: unknown,
propName: T 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({ export default defineImporter({
name: "Swagger/OpenAPI v3 Schema", name: "Swagger/OpenAPI v3 Schema",
steps: [ steps: [
step({ step({
stepName: "FILE_OR_URL_IMPORT", stepName: "FILE_OR_URL_IMPORT",
metadata: { metadata: {
acceptedFileTypes: "application/json", acceptedFileTypes: ".json, .yaml, .yml",
}, },
}), }),
] as const, ] as const,
@@ -587,7 +598,7 @@ export default defineImporter({
pipe( pipe(
// See if we can parse JSON properly // See if we can parse JSON properly
fileContent, fileContent,
safeParseJSON, parseOpenAPIDocContent,
TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT), TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT),
// Try validating, else the importer is invalid file format // Try validating, else the importer is invalid file format

View File

@@ -84,6 +84,7 @@
"graphql-tag": "^2.12.6", "graphql-tag": "^2.12.6",
"httpsnippet": "^2.0.0", "httpsnippet": "^2.0.0",
"io-ts": "^2.2.16", "io-ts": "^2.2.16",
"js-yaml": "^4.1.0",
"json-loader": "^0.5.7", "json-loader": "^0.5.7",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0", "mustache": "^4.2.0",

2
pnpm-lock.yaml generated
View File

@@ -138,6 +138,7 @@ importers:
io-ts: ^2.2.16 io-ts: ^2.2.16
jest: ^27.4.5 jest: ^27.4.5
jest-serializer-vue: ^2.0.2 jest-serializer-vue: ^2.0.2
js-yaml: ^4.1.0
json-loader: ^0.5.7 json-loader: ^0.5.7
lodash: ^4.17.21 lodash: ^4.17.21
mustache: ^4.2.0 mustache: ^4.2.0
@@ -230,6 +231,7 @@ importers:
graphql-tag: 2.12.6_graphql@15.7.2 graphql-tag: 2.12.6_graphql@15.7.2
httpsnippet: 2.0.0 httpsnippet: 2.0.0
io-ts: 2.2.16_fp-ts@2.11.5 io-ts: 2.2.16_fp-ts@2.11.5
js-yaml: 4.1.0
json-loader: 0.5.7 json-loader: 0.5.7
lodash: 4.17.21 lodash: 4.17.21
mustache: 4.2.0 mustache: 4.2.0