vscode-extensions - 迅雷客户端 : simplest way to add script to request

标签 vscode-extensions thunderclient

我尝试弄清楚如何添加自定义代码到我的请求中,类似于 Postman 中的预请求脚本,例如:


const items = JSON.parse(pm.collectionVariables.get('items'))

const randomItem = _.sample(items)

pm.variables.set('randomItemId', randomItem.id)
pm.variables.set('randomItem', JSON.stringify(randomItem));

我知道 2023 年 3 月导入外部库处于测试阶段,但我们假设它可以正常工作。

我知道我需要创建过滤器:

第一个问题:在哪里可以选择创建一个?我希望它将与我的项目代码分开。

第二个问题:我可以直接在请求中添加过滤器代码(可见以快速检查请求中发生的情况)吗?或者我需要为每个需要新自定义代码的请求创建过滤器😔

我尝试遵循文档,但没有明确的信息在哪里添加过滤器文件:https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md

最佳答案

我部分地弄清楚了它,因为看起来脚本是在集合级别注入(inject)的。

先决条件 - 导航到 Thunder 客户端:

  1. 创建环境(Env 选项卡 -> 菜单 -> 新环境)我们称之为 baseEnv
  2. baseEnv中设置空v变量并保存
  3. 创建集合(“集合”选项卡 -> 菜单 ->“新建集合”)我们将其命名为 myCol
  4. myCol 中,转到菜单 -> 设置 -> 环境并附加环境baseEnv

创建脚本:

  1. 决定保存脚本的位置(即单独的存储库)
  2. 创建文件,即 script.js
  3. 向脚本添加函数:
    async function testFunc() {
        console.log("Test log message");
        return "TEST";
    }
    
    module.exports = [testFunc];
    

要将自定义脚本添加到集合中:

  1. 转至收藏集并找到您的收藏集
  2. 打开收藏菜单 (...)
  3. 打开“设置”选项卡
  4. 选择带有脚本的文件
  5. 保存

使用脚本:

  1. 集合中的克里特岛请求(菜单 -> 新请求)
  2. 网址:https://httpbin.org/anything
  3. 在 header 中添加 header A-test ,其值为:{{v | testFunc}} enter image description here
  4. 发送请求!

结果: enter image description here

如果脚本未加载且函数未执行,请尝试:

  • 再次加载脚本并保存
  • 类型表达式{{v | testFunc}} 并保存

扩展脚本:

  1. 在其中放置包含 2 个脚本的文件:
  2. 带有脚本的示例文件 https://github.com/rangav/thunder-client-support/blob/master/docs/custom-filters.js
  3. 类型为 https://github.com/rangav/thunder-client-support/blob/master/docs/tc-types.d.ts 的示例文件

关于vscode-extensions - 迅雷客户端 : simplest way to add script to request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75787024/

相关文章:

visual-studio-code - 在 README.md 中插入视频作为 vscode 的扩展

visual-studio-code - VS Code 中的可点击链接 API

visual-studio-code - 在 vscode 扩展中列出文件夹中的文件

visual-studio-code - 如何在 VS Code 快速选择器中将项目设置为默认值?

typescript - 显示与 VS Code 源代码控制扩展的差异