Add full URL to path for imported queries

This commit is contained in:
Rory Powell 2022-01-05 10:31:06 -05:00
parent cf3c5dccd0
commit f049916764
3 changed files with 19 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import { Query, QueryParameter } from "../../../../../../definitions/datasource" import { Query, QueryParameter } from "../../../../../../definitions/datasource"
import { URL } from "url"
export interface ImportInfo { export interface ImportInfo {
url: string
name: string name: string
} }
@ -23,6 +23,7 @@ export abstract class ImportSource {
name: string, name: string,
method: string, method: string,
path: string, path: string,
url: URL,
queryString: string, queryString: string,
headers: object = {}, headers: object = {},
parameters: QueryParameter[] = [], parameters: QueryParameter[] = [],
@ -33,6 +34,7 @@ export abstract class ImportSource {
const transformer = "return data" const transformer = "return data"
const schema = {} const schema = {}
path = this.processPath(path) path = this.processPath(path)
path = `${url.origin}/${path}`
queryString = this.processQuery(queryString) queryString = this.processQuery(queryString)
const requestBody = JSON.stringify(body, null, 2) const requestBody = JSON.stringify(body, null, 2)

View File

@ -60,16 +60,19 @@ export class Curl extends ImportSource {
return true return true
} }
getUrl = (): URL => {
return new URL(this.curl.raw_url)
}
getInfo = async (): Promise<ImportInfo> => { getInfo = async (): Promise<ImportInfo> => {
const url = new URL(this.curl.url) const url = this.getUrl()
return { return {
url: url.origin,
name: url.hostname, name: url.hostname,
} }
} }
getQueries = async (datasourceId: string): Promise<Query[]> => { getQueries = async (datasourceId: string): Promise<Query[]> => {
const url = new URL(this.curl.raw_url) const url = this.getUrl()
const name = url.pathname const name = url.pathname
const path = url.pathname const path = url.pathname
const method = this.curl.method const method = this.curl.method
@ -87,6 +90,7 @@ export class Curl extends ImportSource {
name, name,
method, method,
path, path,
url,
queryString, queryString,
headers, headers,
[], [],

View File

@ -2,6 +2,7 @@ import { ImportInfo } from "./base"
import { Query, QueryParameter } from "../../../../../definitions/datasource" import { Query, QueryParameter } from "../../../../../definitions/datasource"
import { OpenAPIV2 } from "openapi-types" import { OpenAPIV2 } from "openapi-types"
import { OpenAPISource } from "./base/openapi" import { OpenAPISource } from "./base/openapi"
import { URL } from "url"
const parameterNotRef = ( const parameterNotRef = (
param: OpenAPIV2.Parameter | OpenAPIV2.ReferenceObject param: OpenAPIV2.Parameter | OpenAPIV2.ReferenceObject
@ -55,20 +56,22 @@ export class OpenAPI2 extends OpenAPISource {
} }
} }
getInfo = async (): Promise<ImportInfo> => { getUrl = (): URL => {
const scheme = this.document.schemes?.includes("https") ? "https" : "http" const scheme = this.document.schemes?.includes("https") ? "https" : "http"
const basePath = this.document.basePath || "" const basePath = this.document.basePath || ""
const host = this.document.host || "<host>" const host = this.document.host || "<host>"
const url = `${scheme}://${host}${basePath}` return new URL(`${scheme}://${host}${basePath}`)
const name = this.document.info.title || "Swagger Import" }
getInfo = async (): Promise<ImportInfo> => {
const name = this.document.info.title || "Swagger Import"
return { return {
url: url, name
name: name,
} }
} }
getQueries = async (datasourceId: string): Promise<Query[]> => { getQueries = async (datasourceId: string): Promise<Query[]> => {
const url = this.getUrl()
const queries = [] const queries = []
for (let [path, pathItem] of Object.entries(this.document.paths)) { for (let [path, pathItem] of Object.entries(this.document.paths)) {
@ -145,6 +148,7 @@ export class OpenAPI2 extends OpenAPISource {
name, name,
methodName, methodName,
path, path,
url,
queryString, queryString,
headers, headers,
parameters, parameters,