-
Notifications
You must be signed in to change notification settings - Fork 445
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
processing variables from external file #632
processing variables from external file #632
Conversation
src/utils/httpRequestParser.ts
Outdated
@@ -179,7 +180,10 @@ export class HttpRequestParser implements RequestParser { | |||
const inputFilePath = groups[1]; | |||
const fileAbsolutePath = await resolveRequestBodyPath(inputFilePath); | |||
if (fileAbsolutePath) { | |||
combinedStream.append(fs.createReadStream(fileAbsolutePath)); | |||
const buffer = await fs.readFile(fileAbsolutePath); | |||
const fileContent = buffer.toString('utf8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you simply convert the buffer to UTF-8 string, if the buffer encoding is not utf8
, this would make the final request body incorrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the best way of doing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that VSCode interally use the library jschardet
to guess textual file content encoding (https://github.com/microsoft/vscode/blob/ce1462776f7ada735ba82ffec28ea774e617a927/src/vs/workbench/services/textfile/common/encoding.ts#L234). Together with the following advice, if we add the request level setting for this feature, maybe we can also support providing an optional argument encoding
, if omit, we will guess the encoding for him/her. By default the setting is turned off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think we can default to utf8 and provide a way to override the default to keep it simple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added those changes, can you check?
src/utils/httpRequestParser.ts
Outdated
const resolvedContent = await VariableProcessor.processRawRequest(fileContent); | ||
combinedStream.append(resolvedContent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we really want to support this feature, I think we should add a switch(setting) to turn on/off this behavior that parsing the variables in the included body file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall setting? that seems a bit excessive, why not a request level setting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like instead of < filename
we could do <@ filename
to imply it contains variables needing resolving ? What do you think @Huachao
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added this with an optional encoding, seems like a nice option, brings in the default with an option to override and only processes variables in files if we signal it that way with the @
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice suggestion
src/utils/httpRequestParser.ts
Outdated
@@ -179,7 +180,10 @@ export class HttpRequestParser implements RequestParser { | |||
const inputFilePath = groups[1]; | |||
const fileAbsolutePath = await resolveRequestBodyPath(inputFilePath); | |||
if (fileAbsolutePath) { | |||
combinedStream.append(fs.createReadStream(fileAbsolutePath)); | |||
const buffer = await fs.readFile(fileAbsolutePath); | |||
const fileContent = buffer.toString('utf8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that VSCode interally use the library jschardet
to guess textual file content encoding (https://github.com/microsoft/vscode/blob/ce1462776f7ada735ba82ffec28ea774e617a927/src/vs/workbench/services/textfile/common/encoding.ts#L234). Together with the following advice, if we add the request level setting for this feature, maybe we can also support providing an optional argument encoding
, if omit, we will guess the encoding for him/her. By default the setting is turned off.
…flagged when referencing the file via an @ sign '<@' and optionally the encoding name (utf8 by default) '<@Latin1'
src/utils/httpRequestParser.ts
Outdated
const resolvedContent = await VariableProcessor.processRawRequest(fileContent); | ||
combinedStream.append(resolvedContent); | ||
} | ||
else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the tslint error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -21,7 +22,8 @@ enum ParseState { | |||
export class HttpRequestParser implements RequestParser { | |||
private readonly defaultMethod = 'GET'; | |||
private readonly queryStringLinePrefix = /^\s*[&\?]/; | |||
private readonly inputFileSyntax = /^<\s+(.+?)\s*$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentLInkProvider.ts
also uses the same regex to identify the file link, you also need to update the regex there since the Click link feature is broken when appendig @
after <
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good point, noticed the underscore stopped working after my change!
@@ -7,7 +7,7 @@ import { getWorkspaceRootPath } from '../utils/workspaceUtility'; | |||
|
|||
export class RequestBodyDocumentLinkProvider implements DocumentLinkProvider { | |||
|
|||
private _linkPattern = /^(\<\s+)(.+)(\s*)$/g; | |||
private _linkPattern = /^(\<(?:@?(?:\w+)?)?\s+)(.+)(\s*)$/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems in your regex, the encoding is supported with the <
syntax, in my option, it's only supported with the <@
syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted
@ricmrodrigues Merged, thanks |
What was missing was documenting how this feature works in the README file. |
|
@ricmrodrigues you can try the latest version 0.24.2 |
this is part of an issue I've raised, maybe missing something but I tested and it seemed to work :)