Skip to content

Commit

Permalink
fix(paw-url): refactor url generation
Browse files Browse the repository at this point in the history
this commit refactors PawURL class, where it utilizes URL class
instead of RegEx to detect a valid url.

Fixes for issue:
- #11: import is empty - almost
- #12: not work on openapi 3.0.3
  • Loading branch information
pongstr committed Jun 19, 2021
1 parent c548afd commit ee295b8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 73 deletions.
6 changes: 5 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
)
})
},
)
}
})
}
Expand Down
92 changes: 26 additions & 66 deletions src/utils/paw-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,91 +13,50 @@ 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,
pathName: string,
envManager: EnvironmentManager,
request: Paw.Request,
) {
let server: OpenAPIV3.ServerObject = { url: '' }
let match: RegExpMatchArray | null = []
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 (openApi.servers && openApi.servers.length > 0) {
baseURL = this.createURL(openApi.servers[0].url)
}

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]
}
baseURL.pathname += pathName

if (match[4]) {
this.pathname = PawURL.addSlashAtEnd(match[4]).replace(
new RegExp('//', 'g'),
'/',
)
} else {
this.pathname = '/'
}
if (/^(\/\/)/g.test(baseURL.pathname)) {
baseURL.pathname = baseURL.pathname.replace(/^(\/\/)/g, '/')
}
}

static addSlashAtEnd(variable: string): string {
if (variable[variable.length - 1] !== '/') {
return `${variable}/`
}
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
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ee295b8

Please sign in to comment.