From 2531cd813323b1bd83925cc6f2b219042ac119f2 Mon Sep 17 00:00:00 2001 From: Hilmar Nooitgedagt Date: Sun, 28 Mar 2021 04:04:31 +0200 Subject: [PATCH 1/3] feature(Faker): Added Faker.js library to system variables --- README.md | 3 +++ package-lock.json | 5 +++++ package.json | 1 + src/common/constants.ts | 2 ++ src/models/httpVariableResolveResult.ts | 1 + src/utils/httpElementFactory.ts | 8 +++++++- .../systemVariableProvider.ts | 19 +++++++++++++++++++ 7 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a2dccc8..9401b5dc 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ REST Client allows you to send HTTP request and view the response in Visual Stud + `{{$processEnv [%]envVarName}}` + `{{$dotenv [%]variableName}}` + `{{$aadToken [new] [public|cn|de|us|ppe] [] [aud:]}}` + - Generate realistic looking fake data using the faker.js library + + `{{$faker name.lastName}}` generates a random lastname + + For full Faker API checkout https://github.com/Marak/faker.js/blob/master/Readme.md - Easily create/update/delete environments and environment variables in setting file - File variables can reference both custom and system variables - Support environment switch diff --git a/package-lock.json b/package-lock.json index 386f9a10..605a151e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1848,6 +1848,11 @@ "resolved": "http://registry.npm.taobao.org/extsprintf/download/extsprintf-1.3.0.tgz", "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=" }, + "faker": { + "version": "5.5.1", + "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.1.tgz", + "integrity": "sha512-JCFXcVesClQ6RlSWmBMFQmGDAZq6hjbJAFgKALx31uPaQC6p3SWT2ojaJGD8bMQri+8VNqDOkI2F8QC+ABvZng==" + }, "fast-deep-equal": { "version": "1.1.0", "resolved": "http://registry.npm.taobao.org/fast-deep-equal/download/fast-deep-equal-1.1.0.tgz", diff --git a/package.json b/package.json index 0c3cdbd4..5633ba72 100644 --- a/package.json +++ b/package.json @@ -651,6 +651,7 @@ "dayjs": "^1.8.20", "dotenv": "^8.2.0", "encodeurl": "^1.0.1", + "faker": "^5.5.1", "filesize": "^3.3.0", "fs-extra": "^5.0.0", "got": "^9.6.0", diff --git a/src/common/constants.ts b/src/common/constants.ts index fa954ac3..0d10429a 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -23,6 +23,8 @@ export const AzureActiveDirectoryVariableName = "$aadToken"; export const AzureActiveDirectoryDescription = "Prompts to sign in to Azure AD and adds the token to the request"; export const AzureActiveDirectoryV2TokenVariableName = "$aadV2Token"; export const AzureActiveDirectoryV2TokenDescription = "Prompts to sign in to Azure AD V2 and adds the token to the request"; +export const FakerVariableName = "$faker"; +export const FakerDescription = "Generate realistic fake data for testing"; /** * NOTE: The client id represents an AAD app people sign in to. The client id is sent to AAD to indicate what app diff --git a/src/models/httpVariableResolveResult.ts b/src/models/httpVariableResolveResult.ts index 33a61c98..3a43c4c9 100644 --- a/src/models/httpVariableResolveResult.ts +++ b/src/models/httpVariableResolveResult.ts @@ -32,6 +32,7 @@ export const enum ResolveWarningMessage { 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}}"', + IncorrectFakerVariableFormat = 'Faker expression should follow format "$faker .}}"', 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}}"', diff --git a/src/utils/httpElementFactory.ts b/src/utils/httpElementFactory.ts index 0c49a895..bfc1abd0 100644 --- a/src/utils/httpElementFactory.ts +++ b/src/utils/httpElementFactory.ts @@ -142,7 +142,13 @@ export class HttpElementFactory { null, Constants.AzureActiveDirectoryV2TokenDescription, new SnippetString(`{{$\${name:${Constants.AzureActiveDirectoryV2TokenVariableName.slice(1)}}}}`))); - + originalElements.push(new HttpElement( + Constants.FakerVariableName, + ElementType.SystemVariable, + null, + Constants.FakerDescription, + new SnippetString(`{{$\${name:${Constants.FakerVariableName.slice(1)}} {1:expression}}}`))); + // add environment custom variables const environmentVariables = await EnvironmentVariableProvider.Instance.getAll(); for (const { name, value } of environmentVariables) { diff --git a/src/utils/httpVariableProviders/systemVariableProvider.ts b/src/utils/httpVariableProviders/systemVariableProvider.ts index 09c33169..6a0c3c9b 100644 --- a/src/utils/httpVariableProviders/systemVariableProvider.ts +++ b/src/utils/httpVariableProviders/systemVariableProvider.ts @@ -14,6 +14,7 @@ import { AadV2TokenProvider } from '../aadV2TokenProvider'; import { HttpClient } from '../httpClient'; import { EnvironmentVariableProvider } from './environmentVariableProvider'; import { HttpVariable, HttpVariableContext, HttpVariableProvider } from './httpVariableProvider'; +import { Faker } from 'faker' const uuidv4 = require('uuid/v4'); @@ -25,6 +26,7 @@ type ResolveSystemVariableFunc = (name: string, document: TextDocument, context: export class SystemVariableProvider implements HttpVariableProvider { private readonly clipboard: Clipboard; + private readonly faker: Faker; private readonly resolveFuncs: Map = new Map(); private readonly timestampRegex: RegExp = new RegExp(`\\${Constants.TimeStampVariableName}(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`); private readonly datetimeRegex: RegExp = new RegExp(`\\${Constants.DateTimeVariableName}\\s(rfc1123|iso8601|\'.+\'|\".+\")(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`); @@ -38,6 +40,8 @@ export class SystemVariableProvider implements HttpVariableProvider { private readonly aadRegex: RegExp = new RegExp(`\\s*\\${Constants.AzureActiveDirectoryVariableName}(\\s+(${Constants.AzureActiveDirectoryForceNewOption}))?(\\s+(ppe|public|cn|de|us))?(\\s+([^\\.]+\\.[^\\}\\s]+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))?(\\s+aud:([^\\.]+\\.[^\\}\\s]+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))?\\s*`); + private readonly fakerRegex: RegExp = new RegExp(`\\${Constants.FakerVariableName}\\s+(\\w+\\..+)`); + private readonly innerSettingsEnvironmentVariableProvider: EnvironmentVariableProvider = EnvironmentVariableProvider.Instance; private static _instance: SystemVariableProvider; @@ -50,6 +54,7 @@ export class SystemVariableProvider implements HttpVariableProvider { } private constructor() { + this.faker = require('faker'); this.clipboard = env.clipboard; this.registerTimestampVariable(); this.registerDateTimeVariable(); @@ -60,6 +65,7 @@ export class SystemVariableProvider implements HttpVariableProvider { this.registerDotenvVariable(); this.registerAadTokenVariable(); this.registerAadV2TokenVariable(); + this.registerFakerVariable(); } public readonly type: VariableType = VariableType.System; @@ -292,6 +298,19 @@ export class SystemVariableProvider implements HttpVariableProvider { return {value: token}; }); } + + private registerFakerVariable() { + this.resolveFuncs.set(Constants.FakerVariableName, async name => { + const groups = this.fakerRegex.exec(name); + if (groups !== null && groups.length === 2) { + const [, expression] = groups; + return { value: (this.faker.fake("{{" + expression + "}}")) }; + } + + return { warning: ResolveWarningMessage.IncorrectFakerVariableFormat }; + }); + } + private async resolveSettingsEnvironmentVariable(name: string) { if (await this.innerSettingsEnvironmentVariableProvider.has(name)) { const { value, error, warning } = await this.innerSettingsEnvironmentVariableProvider.get(name); From 76956d8ceae21d3ee1e286313b9f42234c6a1169 Mon Sep 17 00:00:00 2001 From: Hilmar Nooitgedagt Date: Sun, 28 Mar 2021 20:24:28 +0200 Subject: [PATCH 2/3] Moved faker reference --- src/utils/httpVariableProviders/systemVariableProvider.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/utils/httpVariableProviders/systemVariableProvider.ts b/src/utils/httpVariableProviders/systemVariableProvider.ts index 6a0c3c9b..aa81ee5c 100644 --- a/src/utils/httpVariableProviders/systemVariableProvider.ts +++ b/src/utils/httpVariableProviders/systemVariableProvider.ts @@ -14,7 +14,7 @@ import { AadV2TokenProvider } from '../aadV2TokenProvider'; import { HttpClient } from '../httpClient'; import { EnvironmentVariableProvider } from './environmentVariableProvider'; import { HttpVariable, HttpVariableContext, HttpVariableProvider } from './httpVariableProvider'; -import { Faker } from 'faker' +import faker from 'faker'; const uuidv4 = require('uuid/v4'); @@ -26,7 +26,6 @@ type ResolveSystemVariableFunc = (name: string, document: TextDocument, context: export class SystemVariableProvider implements HttpVariableProvider { private readonly clipboard: Clipboard; - private readonly faker: Faker; private readonly resolveFuncs: Map = new Map(); private readonly timestampRegex: RegExp = new RegExp(`\\${Constants.TimeStampVariableName}(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`); private readonly datetimeRegex: RegExp = new RegExp(`\\${Constants.DateTimeVariableName}\\s(rfc1123|iso8601|\'.+\'|\".+\")(?:\\s(\\-?\\d+)\\s(y|Q|M|w|d|h|m|s|ms))?`); @@ -54,7 +53,6 @@ export class SystemVariableProvider implements HttpVariableProvider { } private constructor() { - this.faker = require('faker'); this.clipboard = env.clipboard; this.registerTimestampVariable(); this.registerDateTimeVariable(); @@ -304,7 +302,7 @@ export class SystemVariableProvider implements HttpVariableProvider { const groups = this.fakerRegex.exec(name); if (groups !== null && groups.length === 2) { const [, expression] = groups; - return { value: (this.faker.fake("{{" + expression + "}}")) }; + return { value: (faker.fake("{{" + expression + "}}")) }; } return { warning: ResolveWarningMessage.IncorrectFakerVariableFormat }; From 8064c87c7468a2ec0345459743ec933574da880a Mon Sep 17 00:00:00 2001 From: Hilmar Nooitgedagt Date: Mon, 29 Mar 2021 10:59:57 +0200 Subject: [PATCH 3/3] Fixed tslint errors --- src/utils/httpElementFactory.ts | 2 +- src/utils/httpVariableProviders/systemVariableProvider.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/httpElementFactory.ts b/src/utils/httpElementFactory.ts index bfc1abd0..c97798b9 100644 --- a/src/utils/httpElementFactory.ts +++ b/src/utils/httpElementFactory.ts @@ -148,7 +148,7 @@ export class HttpElementFactory { null, Constants.FakerDescription, new SnippetString(`{{$\${name:${Constants.FakerVariableName.slice(1)}} {1:expression}}}`))); - + // add environment custom variables const environmentVariables = await EnvironmentVariableProvider.Instance.getAll(); for (const { name, value } of environmentVariables) { diff --git a/src/utils/httpVariableProviders/systemVariableProvider.ts b/src/utils/httpVariableProviders/systemVariableProvider.ts index aa81ee5c..6089384c 100644 --- a/src/utils/httpVariableProviders/systemVariableProvider.ts +++ b/src/utils/httpVariableProviders/systemVariableProvider.ts @@ -2,6 +2,7 @@ import * as adal from 'adal-node'; import dayjs, { Dayjs, OpUnitType } from 'dayjs'; import utc from 'dayjs/plugin/utc'; import * as dotenv from 'dotenv'; +import faker from 'faker'; import * as fs from 'fs-extra'; import * as path from 'path'; import { Clipboard, commands, env, QuickPickItem, QuickPickOptions, TextDocument, Uri, window } from 'vscode'; @@ -14,7 +15,6 @@ import { AadV2TokenProvider } from '../aadV2TokenProvider'; import { HttpClient } from '../httpClient'; import { EnvironmentVariableProvider } from './environmentVariableProvider'; import { HttpVariable, HttpVariableContext, HttpVariableProvider } from './httpVariableProvider'; -import faker from 'faker'; const uuidv4 = require('uuid/v4');