Skip to content

Commit 169795e

Browse files
committed
Add feature: specifying env file
1 parent d8528c2 commit 169795e

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ REST Client allows you to send HTTP request and view the response in Visual Stud
3737
+ `{{$datetime rfc1123|iso8601 [offset option]}}`
3838
+ `{{$localDatetime rfc1123|iso8601 [offset option]}}`
3939
+ `{{$processEnv [%]envVarName}}`
40-
+ `{{$dotenv [%]variableName}}`
40+
+ `{{$dotenv [%]variableName [filename]}}`
4141
+ `{{$aadToken [new] [public|cn|de|us|ppe] [<domain|tenantId>] [aud:<domain|tenantId>]}}`
4242
- Easily create/update/delete environments and environment variables in setting file
4343
- File variables can reference both custom and system variables
@@ -625,7 +625,7 @@ For example: Define a shell environment variable in `.bashrc` or similar on wind
625625

626626
`%`: Optional. If specified, treats envVarName as an extension setting environment variable, and uses the value of that for the lookup.
627627

628-
* `{{$dotenv [%]variableName}}`: Returns the environment value stored in the [`.env`](https://github.com/motdotla/dotenv) file which exists in the same directory of your `.http` file.
628+
* `{{$dotenv [%]variableName [filename]}}`: Returns the environment value stored in the file starts with [`.env`](https://github.com/motdotla/dotenv) which exists in the same (or direct parent) directory of your `.http` file. If no file specified, default filename is `.env`.
629629
* `{{$randomInt min max}}`: Returns a random integer between min (included) and max (excluded)
630630
* `{{$timestamp [offset option]}}`: Add UTC timestamp of now. You can even specify any date time based on current time in the format `{{$timestamp number option}}`, e.g., to represent 3 hours ago, simply `{{$timestamp -3 h}}`; to represent the day after tomorrow, simply `{{$timestamp 2 d}}`.
631631
* `{{$datetime rfc1123|iso8601|"custom format"|'custom format' [offset option]}}`: Add a datetime string in either _ISO8601_, _RFC1123_ or a custom display format. You can even specify a date time relative to the current date similar to `timestamp` like: `{{$datetime iso8601 1 y}}` to represent a year later in _ISO8601_ format. If specifying a custom format, wrap it in single or double quotes like: `{{$datetime "DD-MM-YYYY" 1 y}}`. The date is formatted using Day.js, read [here](https://day.js.org/docs/en/get-set/get#list-of-all-available-units) for information on format strings.
@@ -651,7 +651,7 @@ Content-Type: application/xml
651651
Date: {{$datetime rfc1123}}
652652
653653
{
654-
"user_name": "{{$dotenv USERNAME}}",
654+
"user_name": "{{$dotenv USERNAME .env.local}}",
655655
"request_id": "{{$guid}}",
656656
"updated_at": "{{$timestamp}}",
657657
"created_at": "{{$timestamp -1 d}}",

src/models/httpVariableResolveResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ export const enum ResolveWarningMessage {
2727
ResponseBodyNotExist = "Response body of given request doesn't exist",
2828
IncorrectDateTimeVariableFormat = 'Datetime system variable should follow format "{{$datetime rfc1123|iso8601 [integer y|M|w|d|h|m|s|ms]}}"',
2929
IncorrectLocalDateTimeVariableFormat = 'Local datetime system variable should follow format "{{$localDatetime rfc1123|iso8601 [integer y|M|w|d|h|m|s|ms]}}"',
30-
DotenvFileNotFound = '.env file is not found in the directory where current .http file exists',
30+
DotenvFileNotFound = '.env file or specified file which should start with ".env" is not found in the directory where current .http file exists',
3131
DotenvVariableNotFound = 'Given variable name is not found in .env file',
3232
IncorrectHeaderName = 'No value is resolved for given header name',
3333
IncorrectJSONPath = 'No value is resolved for given JSONPath',
3434
IncorrectRandomIntegerVariableFormat = 'RandomInt system variable should follow format "{{$randomInt minInteger maxInteger}}"',
3535
IncorrectProcessEnvVariableFormat = 'ProcessEnv system variable should follow format "{{$processEnv envVarName}}"',
3636
IncorrectTimestampVariableFormat = 'Timestamp system variable should follow format "{{$timestamp [integer y|M|w|d|h|m|s|ms]}}"',
37-
IncorrectDotenvVariableFormat = 'Dotenv variable should follow format "{{$dotenv variableName}}"',
37+
IncorrectDotenvVariableFormat = 'Dotenv variable should follow format "{{$dotenv variableName [filename]}}"',
3838
IncorrectXPath = 'No value is resolved for given XPath',
3939
UnsupportedBodyContentType = 'Only JSON and XML response/request body is supported to query the result',
4040
InvalidJSONPath = 'Invalid JSONPath query',

src/utils/httpVariableProviders/systemVariableProvider.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class SystemVariableProvider implements HttpVariableProvider {
3232
private readonly randomIntegerRegex: RegExp = new RegExp(`\\${Constants.RandomIntVariableName}\\s(\\-?\\d+)\\s(\\-?\\d+)`);
3333
private readonly processEnvRegex: RegExp = new RegExp(`\\${Constants.ProcessEnvVariableName}\\s(\\%)?(\\w+)`);
3434

35-
private readonly dotenvRegex: RegExp = new RegExp(`\\${Constants.DotenvVariableName}\\s(\\%)?([\\w-.]+)`);
35+
private readonly dotenvRegex: RegExp = new RegExp(`\\${Constants.DotenvVariableName}\\s(\\%)?([\\w-.]+)(?:\\s(\\.env[\\w-.]*))?`);
3636

3737
private readonly requestUrlRegex: RegExp = /^(?:[^\s]+\s+)([^:]*:\/\/\/?[^/\s]*\/?)/;
3838

@@ -187,18 +187,18 @@ export class SystemVariableProvider implements HttpVariableProvider {
187187

188188
private registerDotenvVariable() {
189189
this.resolveFuncs.set(Constants.DotenvVariableName, async (name, document) => {
190-
let folderPath = path.dirname(document.fileName);
191-
while (!await fs.pathExists(path.join(folderPath, '.env'))) {
192-
folderPath = path.join(folderPath, '..');
193-
if (folderPath === path.parse(process.cwd()).root) {
194-
return { warning: ResolveWarningMessage.DotenvFileNotFound };
195-
}
196-
}
197-
const absolutePath = path.join(folderPath, '.env');
198190
const groups = this.dotenvRegex.exec(name);
199-
if (groups !== null && groups.length === 3) {
191+
if (groups !== null && groups.length === 4) {
192+
const [, refToggle, key, dotEnvFileName] = groups;
193+
let folderPath = path.dirname(document.fileName);
194+
while (!await fs.pathExists(path.join(folderPath, dotEnvFileName || '.env'))) {
195+
folderPath = path.join(folderPath, '..');
196+
if (folderPath === path.parse(process.cwd()).root) {
197+
return { warning: ResolveWarningMessage.DotenvFileNotFound };
198+
}
199+
}
200+
const absolutePath = path.join(folderPath, dotEnvFileName || '.env');
200201
const parsed = dotenv.parse(await fs.readFile(absolutePath));
201-
const [, refToggle, key] = groups;
202202
let dotEnvVarName = key;
203203
if (refToggle !== undefined) {
204204
dotEnvVarName = await this.resolveSettingsEnvironmentVariable(key);

0 commit comments

Comments
 (0)