From 169795e341f9e465dd51d2e5dbbd2ce2f40b6e10 Mon Sep 17 00:00:00 2001 From: Rockheung Date: Sun, 2 Oct 2022 14:26:56 +0900 Subject: [PATCH] Add feature: specifying env file --- README.md | 6 ++--- src/models/httpVariableResolveResult.ts | 4 ++-- .../systemVariableProvider.ts | 22 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d47760e4..ca9f5155 100644 --- a/README.md +++ b/README.md @@ -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] [] [aud:]}}` - Easily create/update/delete environments and environment variables in setting file - 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 `%`: 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. @@ -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}}", diff --git a/src/models/httpVariableResolveResult.ts b/src/models/httpVariableResolveResult.ts index 33a61c98..cbfa74df 100644 --- a/src/models/httpVariableResolveResult.ts +++ b/src/models/httpVariableResolveResult.ts @@ -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', diff --git a/src/utils/httpVariableProviders/systemVariableProvider.ts b/src/utils/httpVariableProviders/systemVariableProvider.ts index 09c33169..fccd6018 100644 --- a/src/utils/httpVariableProviders/systemVariableProvider.ts +++ b/src/utils/httpVariableProviders/systemVariableProvider.ts @@ -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]*\/?)/; @@ -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);