joolfe / postman-to-openapi

🛸 Convert postman collection to OpenAPI
MIT License
596 stars 104 forks source link

Error on scrapeURL #136

Closed KillianLeroux closed 3 years ago

KillianLeroux commented 3 years ago

Hi!

I tried to execute library with the latest version (1.16.0) and I have this error:

TypeError [ERR_INVALID_URL]: Invalid URL: {{base_url}}/companies/:companyId
    at onParseError (internal/url.js:279:9)
    at new URL (internal/url.js:355:5)
    at scrapeURL (C:\Users\kikit\Desktop\LamacomptaApiDoc\bin\p2o_original\node_modules\postman-to-openapi\lib\index.js:358:18)
    at postmanToOpenApi (C:\Users\kikit\Desktop\LamacomptaApiDoc\bin\p2o_original\node_modules\postman-to-openapi\lib\index.js:40:58) {
  input: '{{base_url}}/companies/:companyId',
  code: 'ERR_INVALID_URL'
}

Apparently it's because variables don't replaced ( here: {{base_url}} ).

It's just a note, I know it's already planned to add this feature ;)

My function "setVars" provided here: https://github.com/joolfe/postman-to-openapi/issues/131#issuecomment-889942833 could be useful:

/**
 * Populate variables in provided string
 *
 * @param {string} string    String to process
 * @param {array}  variables Array of variables for replacements
 *
 * @returns {string} String with variables populated
 */
function setVars(string, variables) {
  if (~string.search(/{{\s*[\w\s-]+\s*}}/g)) {
    const varKey = string.replace(/[{*|}*]/g, '').trim()

    return getVarValue(variables, varKey)
  }

  return string
}

Kisses :)

devNoiseConsulting commented 3 years ago

Release 1.16.0 should fix this. You will need to create an options.json file and set the replaceVars key to true. Though you might run into another error if the collection file doesn't have a variable key. I just submitted a PR (#137) to fix that.

KillianLeroux commented 3 years ago

"base_url" is an environment variable, not de collection variable, so the value isn't in Collection JSON Export. With previous version it wasn't a problem: I added "servers" in options.json and all was OK.

How do you use Postman for the URL between several environments? One request per environment? Or you use "Environment" feature with "base_url" variable?

Example:

image

image

joolfe commented 3 years ago

Hi @KillianLeroux,

I have to change the scrapeURL function as with the var replacement in collections v2.1 after replacing variables in the url the structure was not correct, I didn´t know that before replace if you use a base_url was working fine, I will have a look to find a solution so without replacing this is still valid, mean while you just can put a base_url variable for replace and continue using the servers options and everything should works.

Best Regards.

devNoiseConsulting commented 3 years ago

@joolfe I think that comment was meant for @KillianLeroux.