Skip to content

Commit

Permalink
chore(server-url-placeholder): enVar for server objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pongstr authored Jun 28, 2022
1 parent fa2ba6f commit db33795
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v14.16.0
v16.15.0
73 changes: 62 additions & 11 deletions src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
logger,
group,
jsonSchemaParser,
convertEnvString,
createEnvDynamicValue,
createDynamicString,
} from 'utils'
import Paw from 'types/paw.d'

Expand All @@ -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<Paw.RequestGroup> = {}
private readonly envManagers: MapKeyedWithString<EnvironmentManager> = {}
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[] = []
Expand All @@ -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()

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -262,7 +301,7 @@ export default class PawConverter {
* @summary
*
* @param requestBody
* @returns {Object<Paw.Request>}
* @returns {Object<Paw.Request>}ss
*/
private setRequesBody(
request: Paw.Request,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]) => {
Expand All @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/types/paw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare global {
requestInput: Paw.Request
}

type DynamicStringComponent = string | DynamicValue
type DynamicStringComponent = string | DynamicValue | DynamicString
class DynamicString {
length: number
components: DynamicStringComponent[]
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dynamic-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
11 changes: 9 additions & 2 deletions src/utils/paw-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit db33795

Please sign in to comment.