vscode-restclient
vscode-restclient copied to clipboard
[Feature] Import/require .(http|rest) file
- VSCode Version: 1.21.1
- OS Version: Ubuntu 18.04
- REST Client Version: 0.18.1 It would be nice to be able to import/require file. Eg.
#### login.http
# @name login
@url = https://myawesomesite.com
POST {{url}}/api/auth HTTP/1.1
{
"username":"user",
"pass": "pass"
}
Then you could import/require files like:
import './login.http'
GET {{url}}/api/list HTTP/1.1
Authorization: Bearer {{login.response.body.$.data.id}}
@mtxr nice suggestion, I will consider it carefully.
Any update on this?
I am so sorry that I'm busy in my work, and I also put the feature that file variable can reference other variable in first priority
Hi, any update on this? Really looking forward for this feature
Hi, this feature would be really helpful to write DRY request files.
Currently I'm repeating the auth.http
content in every .http
file...
This features would be really cool.
I'm interested in this, if you have any ideas on how you want to do it. i can help you build it out.
Any Update on this ??? , I am really looking forward to it
@dv29 want to try some implementation and get feedback?
I'm also very interested in this, and thinking about giving it a shot. But first, we need to agree on a few things.
The statement syntax
I would prefer to follow the pattern of prefixing keywords with @
inside a comment, like it is done with @name
. So what do you think about the following?
# @import './login.http'
GET {{url}}/api/list HTTP/1.1
Authorization: Bearer {{login.response.body.$.data.id}}
The keyword name
I see that we have a few options: include
, import
, require
.
My personal preference is for include
, since this is basically going to be treated like a preprocessor include, as if we had copied and pasted the code into the new file. Any objections?
The tasks
- [ ] Introduce include/import/require resolution to the send request command
- [ ] Update
http
language definition to support the new include/import/require statement - [ ] Document usage in the readme
- [ ] (optional) offer auto-complete for included file paths
Corner cases
- Nested includes: can we agree that file paths will always be relative to each file that called the
@include
statement?
Anything else?
@rdumont : Happy to see you're planning to work on this. Pls keep us informed.
@rdumont we're also very much interested in this feature, please let us know of the progress! :)
+1
+1
100% this needs to be a feature! ++1
+1
+1
Hi guys, I know it's not official and it's by far the best solution to a problem like this. If I had the time and had some experience developing extensions for vscode I would help with a more complete implementation.
I wrote a script that helped me with this question, although the files get big it helped me organize some things.
I was inspired by @rdumont suggestion
To use it create an examples/src
folder, inside that folder just create your .http
files.
To import a file just enter the code # @import "filename.http"
;
After writing your requests, just run the node build.js
command. The build.js
file should be inside the examples
folder. When executing the command the compiled files will be in the folder dist/**.http
file build.js
const fs = require('fs');
const list_file = fs.readdirSync(__dirname + '/src').filter(item => item.indexOf('_') !== 0);
var file_cache = {};
function compile(fileToBuild) {
// checking cache file
if(file_cache[fileToBuild]) return file_cache[fileToBuild];
// file path
let FILE_PATH = __dirname + `/src/${fileToBuild}`;
if(!fs.existsSync(FILE_PATH)) throw `File: ${fileToBuild} not found.`
let content = fs.readFileSync(FILE_PATH).toString();
// checking imports
let imports = content.matchAll(/# @import "(.*)"/gi)
// analyzing imports
for (const file of imports){
let importContent = compile(file[1]);
content = content.replace(file[0], importContent);
}
file_cache[fileToBuild] = content;
return content;
}
// removing dist folder
fs.rmdirSync(__dirname + '/dist', { recursive: true });
fs.mkdirSync(__dirname + '/dist')
for(let i = 0; i < list_file.length; i++) {
console.log('[Compiling] ' + list_file[i]);
let compiledFile = compile(list_file[i]);
fs.writeFileSync(__dirname + '/dist/'+list_file[i], compiledFile);
}
console.log('Done!');
Attention: Files starting with _ will not be generated in
examples/dist
but can be included in files normally.
I know this is by far the expected solution, but I hope it helps someone who is waiting for this feature.
100% this needs to be a feature! ++1 any update on this?
+1 for this request.
+1 for this, any update for this?
+1 . Hopefully this feature gets added.
+1 . this is what my want!
+1, it would be very good
+1 for this
+1
+1
@Huachao supporting?
This feature will be very nice, any update of this issue ? 🙏
+1