diff --git a/.nvmrc b/.nvmrc index 158c006..7fd0237 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v14.16.0 +v16.15.0 diff --git a/src/lib/converter.ts b/src/lib/converter.ts index bd6789f..aaf75de 100644 --- a/src/lib/converter.ts +++ b/src/lib/converter.ts @@ -6,7 +6,8 @@ import { logger, group, jsonSchemaParser, - convertEnvString, + createEnvDynamicValue, + createDynamicString, } from 'utils' import Paw from 'types/paw.d' @@ -29,12 +30,25 @@ const parserOptions: SwaggerParser.Options = { }, } +function validURL(urlstring: string): URL { + try { + const url = new URL(urlstring, 'https://echo.paw.cloud') + return url + } catch (err) { + return new URL('https://echo.paw.cloud') + } +} + export default class PawConverter { private readonly context: Paw.Context private readonly requestGroups: MapKeyedWithString = {} private readonly envManagers: MapKeyedWithString = {} private readonly parserOptions: SwaggerParser.Options = { ...parserOptions } + private envDomain!: Paw.EnvironmentDomain + private env!: Paw.Environment + private baseURL: string[] = [] + public filename: string = '' public apiParser: SwaggerParser public groupedRequest: GroupedRequestType[] = [] @@ -49,6 +63,23 @@ export default class PawConverter { */ this.apiParser = parser + const document = this.apiParser.api + const { title } = document.info + + this.envDomain = + this.context.getEnvironmentDomainByName(title ?? 'Default Group') ?? + this.context.createEnvironmentDomain(title ?? 'Default Group') + + this.env = + this.envDomain.getEnvironmentByName('Production') || + this.envDomain.createEnvironment('Production') + + const baseURL = + this.envDomain.getVariableByName('baseURL') || + this.envDomain.createEnvironmentVariable('baseURL') + + baseURL.setValue('https://echo.paw.cloud', this.env) + // set or initialize the import's environment. this.setEnvironment() @@ -89,7 +120,10 @@ export default class PawConverter { const document = this.apiParser.api const { title } = document.info if (!this.envManagers[title]) { - this.envManagers[title] = new EnvironmentManager(this.context, title) + this.envManagers[title] = new EnvironmentManager( + this.context, + title ?? 'Default Group', + ) return this.envManagers[title] } return this.envManagers[title] @@ -214,8 +248,6 @@ export default class PawConverter { */ private createRequest(item: any, index?: number, array?: any[]): void { const document = this.apiParser.api as OpenAPIV3.Document - const { title } = document.info - const request = this.context.createRequest( item.summary || item.operationId || item.path, item.method, @@ -248,9 +280,16 @@ export default class PawConverter { item.path, this.getEnviroment(), request, - ) + ).fullUrl + + const enVarBaseURL = this.envDomain.getVariableByName( + 'baseURL', + ) as Paw.EnvironmentVariable - request.url = requestURL.fullUrl + request.url = createDynamicString( + createEnvDynamicValue(enVarBaseURL.id), + requestURL, + ) if (item.group.trim() !== '') { this.requestGroups[item.group].appendChild(request) @@ -262,7 +301,7 @@ export default class PawConverter { * @summary * * @param requestBody - * @returns {Object} + * @returns {Object}ss */ private setRequesBody( request: Paw.Request, @@ -286,8 +325,6 @@ export default class PawConverter { request: Paw.Request, ): Paw.Request { if (params.length === 0) return request - const envManager = this.getEnviroment() - function mapParameters( param: ExtendedParamObject, index?: number, @@ -364,7 +401,6 @@ export default class PawConverter { securityTypes: OpenAPIV3.SecurityRequirementObject[], request: Paw.Request, ): Paw.Request { - const envManager = this.getEnviroment() const document = this.apiParser.api as OpenAPIV3.Document // Because there are no references to refer to if (!document.components || !document.components.securitySchemes) @@ -433,7 +469,7 @@ export default class PawConverter { private importServers() { const document = this.apiParser.api as OpenAPIV3.Document if (document.servers) { - document.servers.forEach((serverObject) => { + document.servers.forEach((serverObject, index, arr) => { if (serverObject.variables) { Object.entries(serverObject.variables).forEach( ([variableName, variableObject]) => { @@ -444,6 +480,21 @@ export default class PawConverter { ) }, ) + } else { + let envVar + + if (arr.length > 2) { + envVar = + this.envDomain.getVariableByName('baseURL-' + (index + 1)) ?? + this.envDomain.createEnvironmentVariable('baseURL-' + (index + 1)) + } else { + envVar = + this.envDomain.getVariableByName('baseURL') ?? + this.envDomain.createEnvironmentVariable('baseURL') + } + + envVar.setCurrentValue(validURL(serverObject.url).href) + // this.baseURL.push(envVar.id) } }) } diff --git a/src/lib/importer.ts b/src/lib/importer.ts index 79675a2..39f6a53 100644 --- a/src/lib/importer.ts +++ b/src/lib/importer.ts @@ -47,7 +47,7 @@ export default class OpenAPIv3Importer implements Paw.Importer { if (!doc || !doc.openapi) return 0 return ( - doc.openapi.substr(0, 1) === '3.0' && // allowed versions 3.0.x.* + doc.openapi.substring(0, 1) === '3.0' && // allowed versions 3.0.x.* typeof doc.info === 'object' && typeof doc.paths === 'object' && Object.keys(doc.paths).length > 0 diff --git a/src/types/paw.d.ts b/src/types/paw.d.ts index d4a5d1a..8b828db 100755 --- a/src/types/paw.d.ts +++ b/src/types/paw.d.ts @@ -28,7 +28,7 @@ declare global { requestInput: Paw.Request } - type DynamicStringComponent = string | DynamicValue + type DynamicStringComponent = string | DynamicValue | DynamicString class DynamicString { length: number components: DynamicStringComponent[] diff --git a/src/utils/dynamic-values.ts b/src/utils/dynamic-values.ts index 4d36ece..5c3c926 100644 --- a/src/utils/dynamic-values.ts +++ b/src/utils/dynamic-values.ts @@ -90,7 +90,7 @@ export function convertEnvString( while ((match = re.exec(s))) { // push any string here before if (match.index > idx) { - components.push(s.substr(idx, match.index - idx)) + components.push(s.substring(idx, match.index - idx)) } if (envManager.hasEnvironmentVariable(match[1])) { @@ -112,7 +112,7 @@ export function convertEnvString( // add remaining string if (idx < s.length) { - components.push(s.substr(idx)) + components.push(s.substring(idx)) } // return diff --git a/src/utils/environment.ts b/src/utils/environment.ts index 4fbb8e7..3704205 100644 --- a/src/utils/environment.ts +++ b/src/utils/environment.ts @@ -7,7 +7,7 @@ export default class EnvironmentManager { private context: Paw.Context private environmentDomain: Paw.EnvironmentDomain | null constructor(context: Paw.Context, name?: string | null) { - this.name = name || 'OpenAPI Environment' + this.name = name || 'Default Group' this.envName = name || 'Default' this.context = context this.environmentDomain = null @@ -16,13 +16,10 @@ export default class EnvironmentManager { private getEnvironmentDomain(): Paw.EnvironmentDomain { const context = this.context - if (!this.environmentDomain) { - this.environmentDomain = context.getEnvironmentDomainByName(this.name) - if (!this.environmentDomain) { - this.environmentDomain = context.createEnvironmentDomain(this.name) - this.environmentDomain.createEnvironment(this.envName) - } - } + this.environmentDomain = + context.getEnvironmentDomainByName(this.name) ?? + context.createEnvironmentDomain(this.name) + return this.environmentDomain } diff --git a/src/utils/paw-url.ts b/src/utils/paw-url.ts index 08602af..e49b321 100644 --- a/src/utils/paw-url.ts +++ b/src/utils/paw-url.ts @@ -43,11 +43,18 @@ export default class PawURL { } const url = baseURL.href.replace(/%7B/g, '{').replace(/%7D/g, '}') + '/' + this.hostname = baseURL.hostname - this.pathname = baseURL.pathname + this.pathname = + baseURL.pathname.replace(/%7B/g, '{').replace(/%7D/g, '}') + '/' + this.port = baseURL.port - this.fullUrl = convertEnvString(url, request, envManager) as DynamicString + this.fullUrl = convertEnvString( + this.pathname, + request, + envManager, + ) as DynamicString return this }