From d74d1d66b88cfb5ed514855af0560fc879a448ee Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 5 Jan 2022 10:31:06 -0500 Subject: [PATCH] Add full URL to path for imported queries --- .../controllers/query/import/sources/base/index.ts | 4 +++- .../api/controllers/query/import/sources/curl.ts | 10 +++++++--- .../controllers/query/import/sources/openapi2.ts | 14 +++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/controllers/query/import/sources/base/index.ts b/packages/server/src/api/controllers/query/import/sources/base/index.ts index 06e8dcfeff..e666fdc193 100644 --- a/packages/server/src/api/controllers/query/import/sources/base/index.ts +++ b/packages/server/src/api/controllers/query/import/sources/base/index.ts @@ -1,7 +1,7 @@ import { Query, QueryParameter } from "../../../../../../definitions/datasource" +import { URL } from "url" export interface ImportInfo { - url: string name: string } @@ -23,6 +23,7 @@ export abstract class ImportSource { name: string, method: string, path: string, + url: URL, queryString: string, headers: object = {}, parameters: QueryParameter[] = [], @@ -33,6 +34,7 @@ export abstract class ImportSource { const transformer = "return data" const schema = {} path = this.processPath(path) + path = `${url.origin}/${path}` queryString = this.processQuery(queryString) const requestBody = JSON.stringify(body, null, 2) diff --git a/packages/server/src/api/controllers/query/import/sources/curl.ts b/packages/server/src/api/controllers/query/import/sources/curl.ts index b55d24403b..d72441ab12 100644 --- a/packages/server/src/api/controllers/query/import/sources/curl.ts +++ b/packages/server/src/api/controllers/query/import/sources/curl.ts @@ -60,16 +60,19 @@ export class Curl extends ImportSource { return true } + getUrl = (): URL => { + return new URL(this.curl.raw_url) + } + getInfo = async (): Promise => { - const url = new URL(this.curl.url) + const url = this.getUrl() return { - url: url.origin, name: url.hostname, } } getQueries = async (datasourceId: string): Promise => { - const url = new URL(this.curl.raw_url) + const url = this.getUrl() const name = url.pathname const path = url.pathname const method = this.curl.method @@ -87,6 +90,7 @@ export class Curl extends ImportSource { name, method, path, + url, queryString, headers, [], diff --git a/packages/server/src/api/controllers/query/import/sources/openapi2.ts b/packages/server/src/api/controllers/query/import/sources/openapi2.ts index 35dab163f6..b079db21e8 100644 --- a/packages/server/src/api/controllers/query/import/sources/openapi2.ts +++ b/packages/server/src/api/controllers/query/import/sources/openapi2.ts @@ -2,6 +2,7 @@ import { ImportInfo } from "./base" import { Query, QueryParameter } from "../../../../../definitions/datasource" import { OpenAPIV2 } from "openapi-types" import { OpenAPISource } from "./base/openapi" +import { URL } from "url" const parameterNotRef = ( param: OpenAPIV2.Parameter | OpenAPIV2.ReferenceObject @@ -55,20 +56,22 @@ export class OpenAPI2 extends OpenAPISource { } } - getInfo = async (): Promise => { + getUrl = (): URL => { const scheme = this.document.schemes?.includes("https") ? "https" : "http" const basePath = this.document.basePath || "" const host = this.document.host || "" - const url = `${scheme}://${host}${basePath}` - const name = this.document.info.title || "Swagger Import" + return new URL(`${scheme}://${host}${basePath}`) + } + getInfo = async (): Promise => { + const name = this.document.info.title || "Swagger Import" return { - url: url, - name: name, + name } } getQueries = async (datasourceId: string): Promise => { + const url = this.getUrl() const queries = [] for (let [path, pathItem] of Object.entries(this.document.paths)) { @@ -145,6 +148,7 @@ export class OpenAPI2 extends OpenAPISource { name, methodName, path, + url, queryString, headers, parameters,