vscode-restclient icon indicating copy to clipboard operation
vscode-restclient copied to clipboard

[Feature] Import/require .(http|rest) file

Open mtxr opened this issue 6 years ago • 33 comments

  • 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 avatar Apr 03 '18 17:04 mtxr

@mtxr nice suggestion, I will consider it carefully.

Huachao avatar Apr 04 '18 01:04 Huachao

Any update on this?

mtxr avatar May 27 '18 13:05 mtxr

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

Huachao avatar May 27 '18 14:05 Huachao

Hi, any update on this? Really looking forward for this feature

djansyle avatar Dec 03 '19 05:12 djansyle

Hi, this feature would be really helpful to write DRY request files.

Currently I'm repeating the auth.http content in every .http file...

ribizli avatar Jan 13 '20 09:01 ribizli

This features would be really cool.

wallberg13 avatar Jul 03 '20 03:07 wallberg13

I'm interested in this, if you have any ideas on how you want to do it. i can help you build it out.

dv29 avatar Aug 12 '20 17:08 dv29

Any Update on this ??? , I am really looking forward to it

PhilzAce1 avatar Oct 29 '20 09:10 PhilzAce1

@dv29 want to try some implementation and get feedback?

mchelen-gov avatar Nov 05 '20 22:11 mchelen-gov

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 avatar Dec 01 '20 11:12 rdumont

@rdumont : Happy to see you're planning to work on this. Pls keep us informed.

jdriesen avatar Feb 18 '21 12:02 jdriesen

@rdumont we're also very much interested in this feature, please let us know of the progress! :)

mediangraph avatar Apr 01 '21 06:04 mediangraph

+1

KarFan-FS avatar Apr 28 '21 09:04 KarFan-FS

+1

brentw-infotrack avatar May 14 '21 04:05 brentw-infotrack

100% this needs to be a feature! ++1

jercle avatar Jul 07 '21 04:07 jercle

+1

kolec1 avatar Jul 09 '21 09:07 kolec1

+1

vahidmosi avatar Oct 21 '21 15:10 vahidmosi

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.

hiagodotme avatar Nov 04 '21 21:11 hiagodotme

100% this needs to be a feature! ++1 any update on this?

duan-duan avatar Jan 06 '22 03:01 duan-duan

+1 for this request.

cthomas-dwre avatar May 04 '22 12:05 cthomas-dwre

+1 for this, any update for this?

wilzyang avatar May 27 '22 02:05 wilzyang

+1 . Hopefully this feature gets added.

nirajbhatt7 avatar Jul 19 '22 22:07 nirajbhatt7

+1 . this is what my want!

huangkemeng avatar Aug 27 '22 07:08 huangkemeng

+1, it would be very good

constanedes avatar Oct 22 '22 07:10 constanedes

+1 for this

klutzer avatar May 23 '23 21:05 klutzer

+1

JacobSandin avatar Jun 19 '23 12:06 JacobSandin

+1

yclkvnc avatar Jul 22 '23 17:07 yclkvnc

@Huachao supporting?

xakepp35 avatar Aug 23 '23 14:08 xakepp35

This feature will be very nice, any update of this issue ? 🙏

sebast-c avatar Oct 10 '23 12:10 sebast-c

+1

syahrezafauzi avatar Nov 02 '23 04:11 syahrezafauzi