AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

Cannot require a customized js file #285

Open slackwareer opened 1 month ago

slackwareer commented 1 month ago

A js file which exports some functions is required by a http script, however the requiring result is only an empty object.

JS file: test.js

const TestFunc = () => {
  console.log("test");
};

exports.TestFunc = TestFunc

test.http file

### Test Common Functions
{{
    const test = require('./test.js')
    console.log(test);
}}

Guess what is printed... It turns out to be just an empty object. image

So how can I write a js file which can be required by the http file?

AnWeber commented 1 month ago
### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc()); 
}}

You need to call your method

See https://github.com/httpyac/httpyac.github.io/blob/main/examples/script/scriptRequire.http

slackwareer commented 1 month ago
### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc()); 
}}

You need to call your method

I called the method with following codes:

### Test Common Functions
{{
    const test = require('./test.js')
    test.TestFunc()
}}

Unfortunately I got nothing output...

AnWeber commented 1 month ago

It works, but not the way you expect it to. It works directly on the command line. In vscode it would also work, but the console output is not the output channel but really stdout.

works

For scripts that I execute directly, I overwrite the console output so that it appears in the OutputChannel. For require scripts I have not yet found a trick for this, so it writes directly to stdout. Pass the function the reference to the console then it would work.

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc(console)); 
}}
const TestFunc = (c) => {
  c.log("test");
};

exports.TestFunc = TestFunc
slackwareer commented 1 month ago

It works, but not the way you expect it to. It works directly on the command line. In vscode it would also work, but the console output is not the output channel but really stdout.

works works

For scripts that I execute directly, I overwrite the console output so that it appears in the OutputChannel. For require scripts I have not yet found a trick for this, so it writes directly to stdout. Pass the function the reference to the console then it would work.

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc(console)); 
}}
const TestFunc = (c) => {
  c.log("test");
};

exports.TestFunc = TestFunc

Yeah ,passing the outer console object to the required function works, it print to OutputChannel and the log can be read in the httpyac Console.

And if there is a way to overwrite the global console.log method or overwrite the origin required script to override the console.log method in the script ? Or maybe it's a problem that if it's worthy to do such stuff ......

AnWeber commented 1 month ago

And if there is a way to overwrite the global console.log method or overwrite the origin required script to override the console.log method in the script

PR is welcome. I've been looking into NodeJS vm for a while, but haven't found it. If you have an idea, please feel free.

alekdavisintel commented 1 month ago

@AnWeber Quote of the day: "It works, but not the way you expect it to." :-)