diff --git a/.babelrc b/.babelrc index 23dd526..a506b84 100755 --- a/.babelrc +++ b/.babelrc @@ -1,8 +1,12 @@ { "presets": [ [ - "@babel/env", + "@babel/preset-env", { + "corejs": "3", + "useBuiltIns": "usage", + "include": ["web.url", "web.url-search-params", "web.url.to-json"], + "exclude": ["es.promise"], "targets": { "safari": "6" } diff --git a/src/lib/converter.ts b/src/lib/converter.ts index 7849c63..bd6789f 100644 --- a/src/lib/converter.ts +++ b/src/lib/converter.ts @@ -435,14 +435,15 @@ export default class PawConverter { if (document.servers) { document.servers.forEach((serverObject) => { if (serverObject.variables) { - Object.entries(serverObject.variables).forEach(([variableName, variableObject]) => { - this.getEnviroment() - .setEnvironmentVariableValue( + Object.entries(serverObject.variables).forEach( + ([variableName, variableObject]) => { + this.getEnviroment().setEnvironmentVariableValue( variableName, variableObject.default || '', true /* only assign if value is empty */, ) - }) + }, + ) } }) } diff --git a/src/utils/paw-url.ts b/src/utils/paw-url.ts index fc019a4..f9f71bc 100644 --- a/src/utils/paw-url.ts +++ b/src/utils/paw-url.ts @@ -2,6 +2,7 @@ import { OpenAPIV3 } from 'openapi-types' import Paw from 'types/paw' import EnvironmentManager from './environment' import { convertEnvString } from './dynamic-values' +import logger from './console' export interface PawURLOptions { openApi: OpenAPIV3.Document @@ -12,11 +13,11 @@ export interface PawURLOptions { } export default class PawURL { - hostname: string - pathname: string - port: string - fullUrl: string | DynamicString - + public hostname: string + public pathname: string + public port: string + public fullUrl: string | DynamicString + public defaultURL = 'https://echo.paw.cloud' constructor( pathItem: OpenAPIV3.PathItemObject, openApi: OpenAPIV3.Document, @@ -24,79 +25,34 @@ export default class PawURL { envManager: EnvironmentManager, request: Paw.Request, ) { - let server: OpenAPIV3.ServerObject = { url: '' } - let match: RegExpMatchArray | null = [] + logger.log(pathName) + let baseURL = this.createURL() if (pathItem.servers && pathItem.servers.length > 0) { - this.fullUrl = `${PawURL.removeSlashFromEnd( - pathItem.servers[0].url, - )}${pathName}` - // eslint-disable-next-line prefer-destructuring - server = pathItem.servers[0] - } else if (openApi.servers && openApi.servers.length > 0) { - this.fullUrl = `${PawURL.removeSlashFromEnd( - openApi.servers[0].url, - )}${pathName}` - // eslint-disable-next-line prefer-destructuring - server = openApi.servers[0] + baseURL = this.createURL(pathItem.servers[0].url) + logger.log(baseURL.href) } - this.fullUrl = convertEnvString( - this.fullUrl as string, - request, - envManager, - ) - - if (typeof this.fullUrl === 'string') { - match = this.fullUrl.match( - /^([^:]+):\/\/([^:/]+)(?::([0-9]*))?(?:(\/.*))?$/i, - ) - } else { - match = (this.fullUrl as DynamicString) - .getEvaluatedString() - .match(/^([^:]+):\/\/([^:/]+)(?::([0-9]*))?(?:(\/.*))?$/i) - } - - if (match) { - if (match[2]) { - let host = 'http' - if (match[1]) { - // eslint-disable-next-line prefer-destructuring - host = match[1] - } - - this.hostname = PawURL.addSlashAtEnd(`${host}://${match[2]}`) - } - - if (match[3]) { - // eslint-disable-next-line prefer-destructuring - this.port = match[3] - } - - if (match[4]) { - this.pathname = PawURL.addSlashAtEnd(match[4]).replace( - new RegExp('//', 'g'), - '/', - ) - } else { - this.pathname = '/' - } + if (openApi.servers && openApi.servers.length > 0) { + baseURL = this.createURL(openApi.servers[0].url) } - } - static addSlashAtEnd(variable: string): string { - if (variable[variable.length - 1] !== '/') { - return `${variable}/` - } + baseURL.pathname += pathName.replace(/^\//, '') + const url = baseURL.href.replace(/%7B/g, '{').replace(/%7D/g, '}') + '/' + this.hostname = baseURL.hostname + this.pathname = baseURL.pathname + this.port = baseURL.port - return variable + this.fullUrl = convertEnvString(url, request, envManager) as DynamicString + return this } - static removeSlashFromEnd(variable: string): string { - if (variable[variable.length - 1] === '/') { - return variable.substr(0, variable.length - 1) + public createURL(url?: string): URL { + if (!url) return new URL(this.defaultURL) + try { + return new URL(url) + } catch (error) { + return new URL(url, this.defaultURL) } - - return variable } } diff --git a/tsconfig.json b/tsconfig.json index 7f01505..bc2a80e 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "noImplicitAny": true, "module": "commonjs", "target": "es5", - "lib": ["ES2015", "ES2020"], + "allowJs": true, + "lib": ["dom", "ES2015", "ES2020"], "esModuleInterop": true, "strictNullChecks": true, "noEmit": true, diff --git a/webpack.config.babel.js b/webpack.config.babel.js index cc97bee..a8172ae 100755 --- a/webpack.config.babel.js +++ b/webpack.config.babel.js @@ -6,7 +6,7 @@ const { name, identifier } = PKG.config const webpackConfig = { target: 'webworker', devtool: 'none', - entry: './src/index.ts', + entry: ['./src/index.ts'], stats: { outputPath: true, maxModules: 1,