insomnia-documenter icon indicating copy to clipboard operation
insomnia-documenter copied to clipboard

Add support for nested environment variables

Open devhammed opened this issue 5 years ago • 2 comments

First of all, good job on this tool but I am currently using a nested environment variables structure for an API I am developing but the merging always conflicts with the base settings for endpoint...You can check it out here: https://fanfaro-accounting-api-docs.vercel.app

Base Settings:

{
  "base": "http://fanfaro.test/api"
}

Folder Settings (e.g auth):

{
  "base": "{{ base }}/auth"
}

Insomnia Documenter Output:

http://fanfaro.test/api

Expected Output:

http://fanfaro.test/api/auth


This works fine in the Insomnia app.

devhammed avatar Jul 02 '20 12:07 devhammed

Hello! Really sorry for the delay! I'll try to come up with a good strategy for this in the coming days.

jozsefsallai avatar Jul 31 '20 22:07 jozsefsallai

Hi,

Just started using the documentor - really pleased with the results so far - thanks!

However, I seem to have hit the same issue - I use environment variables in the body part of a post request, like this:

{
	"email": "_.email",
	"password": "_.password",
	"strategy": "couch"
}

Which results in the insomnia export file containing data like this:

"method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"email\": \"{{ _.email }}\",\n\t\"password\": \"{{_.password}}\",\n\t\"strategy\": \"couch\"\n}"}

This causes an error whilst loading the file of:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 2 column 13 of the JSON data

Note that whilst I enter _.email, it still resolves to {{_.email}}

  • I can use {{email}}, which of course resolves to {{email}} which has the same issue.

Experimenting, I found that by swapping {{_.email}} with \u007B\u007B _.email \u007D \u007D although - if you notice, there is a space between the last 2 hex codes....take the space out and you get an error again!!!

I've not bothered whether the {{ }} appear in the docs or not - I think that naming the vars better on my part will make the docs work - like:

{
	"email": "_.users_email",
	"password": "_.users_password",
	"strategy": "couch"
}

so all that remains is to get rid of the {{ }} in the insomnia.json file, or replace them... I used a small script to rewrite the export file, before moving to the web site:

var fs = require('fs')
 fs.readFile('insomnia.json', 'utf8', (err, data) => {
   if (err) { return console.log(err); }
   var str = data.replace(/{{ _\.(\w+) }}/g, "\\u007B\\u007B _.$1 \\u007D \\u007D");
   fs.writeFile('insomnia.json', str, 'utf8', (err) => { 
     if (err) { return console.log(err); }
   });
});

Hope that helps someone....

....if you are able to patch something similar, that would be great.

steve-allam avatar Sep 01 '21 14:09 steve-allam