Skip to content

Commit

Permalink
feat(hurl.nvim): add support for fixture variables
Browse files Browse the repository at this point in the history
This is a workaround until built-in from hurl
  • Loading branch information
Dung Huynh Duc committed Jul 19, 2024
1 parent 9e1d64e commit 61ec2e7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 32 deletions.
131 changes: 99 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
- 🚀 Execute HTTP requests directly from `.hurl` files.
- 👁‍🗨 Multiple display modes for API response: popup or split.
- 🌈 Highly customizable through settings.
- 📦 Environment file support for managing environment variables.
- 🛠 Set environment variables with `HurlSetVariable` command.
- 📝 View and manage environment variables with `HurlManageVariable` command.
- 📜 View the response of your last HTTP request with `HurlShowLastResponse` command.

## Usage

Expand Down Expand Up @@ -126,6 +130,46 @@ To change the environment file name, use the `HurlSetEnvFile` command followed b
- Ensure that the new environment file exists in the directories where the plugin searches for it, as outlined in the [File Location](#file-location) section.
- This change will apply globally for the current session of Neovim. If you restart Neovim, it will revert to the default `vars.env` unless you change it again.

## Test fixtures

This is a feature that allows you to define custom variables in your `.hurl` files. You can define a list of custom variables with a name and a callback function that returns the value of the variable. The callback function is executed every time the variable is used in the `.hurl` file.

> Note
> This is a workaround to inject dynamic variables into the hurl command, refer https://github.com/Orange-OpenSource/hurl/issues?q=sort:updated-desc+is:open+label:%22topic:+generators%22
> -- This is a workaround to inject dynamic variables into the hurl command, refer https://github.com/Orange-OpenSource/hurl/issues?q=sort:updated-desc+is:open+label:%22topic:+generators%22
```lua
-- Custom below to add your own fixture variables
fixture_vars = {
{
name = 'random_int_number',
callback = function()
return math.random(1, 1000)
end,
},
{
name = 'random_float_number',
callback = function()
local result = math.random() * 10
return string.format('%.2f', result)
end,
},
}
```

Then you can use `{{random_int_number}}` and `{{random_float_number}}` in your `.hurl` files.

```hurl
POST https://api.example.com
Content-Type: application/json
{
"name": "Product ID {{random_int_number}}",
"price": {{random_float_number}}
}
```

## Demo

Check out the following demos to see `hurl.nvim` in action:
Expand Down Expand Up @@ -225,47 +269,70 @@ These key mappings are active within the popup windows that `hurl.nvim` displays

`hurl.nvim` can be customized with the following default configurations:

<details><summary>Default Options</summary>

</details>
```lua
local default_config = {
-- Toggle debugging information
debug = false, -- If true, logs will be saved at ~/.local/state/nvim/hurl.nvim.log

-- Set the display mode for the response: 'split' or 'popup'
mode = 'split',
-- Set the display mode for the response: 'split' or 'popup'
mode = 'split',

-- Split settings
split_position = "right",
split_size = "50%",
-- Split settings
split_position = "right",
split_size = "50%",

-- Popup settings
popup_position = '50%',
popup_size = {
width = 80,
height = 40,
},
-- Popup settings
popup_position = '50%',
popup_size = {
width = 80,
height = 40,
},

-- Default environment file name
env_file = {
'vars.env',
},
-- Default environment file name
env_file = {
'vars.env',
},

-- Specify formatters for different response types
formatters = {
json = { 'jq' }, -- Uses jq to format JSON responses
html = {
'prettier', -- Uses prettier to format HTML responses
'--parser',
'html',
},
xml = {
'tidy', -- Uses tidy to format XML responses
'-xml',
'-i',
'-q',
},
},
-- Default test fixtures
fixture_vars = {
{
name = 'random_int_number',
callback = function()
return math.random(1, 1000)
end,
},
{
name = 'random_float_number',
callback = function()
local result = math.random() \* 10
return string.format('%.2f', result)
end,
},
},

-- Specify formatters for different response types
formatters = {
json = { 'jq' }, -- Uses jq to format JSON responses
html = {
'prettier', -- Uses prettier to format HTML responses
'--parser',
'html',
},
xml = {
'tidy', -- Uses tidy to format XML responses
'-xml',
'-i',
'-q',
},
},
}
```

````

</details>

To apply these configurations, include them in your Neovim setup like this:

Expand All @@ -289,7 +356,7 @@ require('hurl').setup({
},
},
})
```
````

Adjust the settings as per your needs to enhance your development experience with `hurl.nvim`.

Expand Down
15 changes: 15 additions & 0 deletions lua/hurl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ local default_config = {
height = 40,
},
env_file = { 'vars.env' },
fixture_vars = {
{
name = 'random_int_number',
callback = function()
return math.random(1, 1000)
end,
},
{
name = 'random_float_number',
callback = function()
local result = math.random() * 10
return string.format('%.2f', result)
end,
},
},
find_env_files_in_folders = utils.find_env_files_in_folders,
formatters = {
json = { 'jq' },
Expand Down
9 changes: 9 additions & 0 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ local function execute_hurl_cmd(opts, callback)
end
end

-- Inject fixture variables into the command
-- This is a workaround to inject dynamic variables into the hurl command, refer https://github.com/Orange-OpenSource/hurl/issues?q=sort:updated-desc+is:open+label:%22topic:+generators%22
if _HURL_GLOBAL_CONFIG.fixture_vars then
for _, fixture in pairs(_HURL_GLOBAL_CONFIG.fixture_vars) do
table.insert(opts, '--variable')
table.insert(opts, fixture.name .. '=' .. fixture.callback())
end
end

-- Include the HTTP headers in the output and do not colorize output.
local cmd = vim.list_extend({ 'hurl', '-i', '--no-color' }, opts)
response = {}
Expand Down

0 comments on commit 61ec2e7

Please sign in to comment.