Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature: specifying env file #1070

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ REST Client allows you to send HTTP request and view the response in Visual Stud
+ `{{$datetime rfc1123|iso8601 [offset option]}}`
+ `{{$localDatetime rfc1123|iso8601 [offset option]}}`
+ `{{$processEnv [%]envVarName}}`
+ `{{$dotenv [%]variableName}}`
+ `{{$dotenv [%]variableName [filename]}}`
+ `{{$aadToken [new] [public|cn|de|us|ppe] [<domain|tenantId>] [aud:<domain|tenantId>]}}`
- Easily create/update/delete environments and environment variables in setting file
- File variables can reference both custom and system variables
Expand Down Expand Up @@ -625,7 +625,7 @@ For example: Define a shell environment variable in `.bashrc` or similar on wind

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

* `{{$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.
* `{{$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`.
* `{{$randomInt min max}}`: Returns a random integer between min (included) and max (excluded)
* `{{$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}}`.
* `{{$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.
Expand All @@ -651,7 +651,7 @@ Content-Type: application/xml
Date: {{$datetime rfc1123}}

{
"user_name": "{{$dotenv USERNAME}}",
"user_name": "{{$dotenv USERNAME .env.local}}",
"request_id": "{{$guid}}",
"updated_at": "{{$timestamp}}",
"created_at": "{{$timestamp -1 d}}",
Expand Down
4 changes: 2 additions & 2 deletions src/models/httpVariableResolveResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export const enum ResolveWarningMessage {
ResponseBodyNotExist = "Response body of given request doesn't exist",
IncorrectDateTimeVariableFormat = 'Datetime system variable should follow format "{{$datetime rfc1123|iso8601 [integer y|M|w|d|h|m|s|ms]}}"',
IncorrectLocalDateTimeVariableFormat = 'Local datetime system variable should follow format "{{$localDatetime rfc1123|iso8601 [integer y|M|w|d|h|m|s|ms]}}"',
DotenvFileNotFound = '.env file is not found in the directory where current .http file exists',
DotenvFileNotFound = '.env file or specified file which should start with ".env" is not found in the directory where current .http file exists',
DotenvVariableNotFound = 'Given variable name is not found in .env file',
IncorrectHeaderName = 'No value is resolved for given header name',
IncorrectJSONPath = 'No value is resolved for given JSONPath',
IncorrectRandomIntegerVariableFormat = 'RandomInt system variable should follow format "{{$randomInt minInteger maxInteger}}"',
IncorrectProcessEnvVariableFormat = 'ProcessEnv system variable should follow format "{{$processEnv envVarName}}"',
IncorrectTimestampVariableFormat = 'Timestamp system variable should follow format "{{$timestamp [integer y|M|w|d|h|m|s|ms]}}"',
IncorrectDotenvVariableFormat = 'Dotenv variable should follow format "{{$dotenv variableName}}"',
IncorrectDotenvVariableFormat = 'Dotenv variable should follow format "{{$dotenv variableName [filename]}}"',
IncorrectXPath = 'No value is resolved for given XPath',
UnsupportedBodyContentType = 'Only JSON and XML response/request body is supported to query the result',
InvalidJSONPath = 'Invalid JSONPath query',
Expand Down
22 changes: 11 additions & 11 deletions src/utils/httpVariableProviders/systemVariableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SystemVariableProvider implements HttpVariableProvider {
private readonly randomIntegerRegex: RegExp = new RegExp(`\\${Constants.RandomIntVariableName}\\s(\\-?\\d+)\\s(\\-?\\d+)`);
private readonly processEnvRegex: RegExp = new RegExp(`\\${Constants.ProcessEnvVariableName}\\s(\\%)?(\\w+)`);

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

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

Expand Down Expand Up @@ -187,18 +187,18 @@ export class SystemVariableProvider implements HttpVariableProvider {

private registerDotenvVariable() {
this.resolveFuncs.set(Constants.DotenvVariableName, async (name, document) => {
let folderPath = path.dirname(document.fileName);
while (!await fs.pathExists(path.join(folderPath, '.env'))) {
folderPath = path.join(folderPath, '..');
if (folderPath === path.parse(process.cwd()).root) {
return { warning: ResolveWarningMessage.DotenvFileNotFound };
}
}
const absolutePath = path.join(folderPath, '.env');
const groups = this.dotenvRegex.exec(name);
if (groups !== null && groups.length === 3) {
if (groups !== null && groups.length === 4) {
const [, refToggle, key, dotEnvFileName] = groups;
let folderPath = path.dirname(document.fileName);
while (!await fs.pathExists(path.join(folderPath, dotEnvFileName || '.env'))) {
folderPath = path.join(folderPath, '..');
if (folderPath === path.parse(process.cwd()).root) {
return { warning: ResolveWarningMessage.DotenvFileNotFound };
}
}
const absolutePath = path.join(folderPath, dotEnvFileName || '.env');
const parsed = dotenv.parse(await fs.readFile(absolutePath));
const [, refToggle, key] = groups;
let dotEnvVarName = key;
if (refToggle !== undefined) {
dotEnvVarName = await this.resolveSettingsEnvironmentVariable(key);
Expand Down