Skip to content

Commit

Permalink
feat: add config option for custom dynamic variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ybbond authored and NTBBloodbath committed Nov 20, 2021
1 parent 2542929 commit 3da902d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ use {
},
-- Jump to request line on run
jump_to_request = false,
env_file = '.env'
env_file = '.env',
custom_dynamic_variables = {},
})
end
}
Expand Down Expand Up @@ -113,6 +114,8 @@ To run `rest.nvim` you should map the following commands:
- `highlight` allows to enable and configure the highlighting of the selected request when send,
- `jump_to_request` moves the cursor to the selected request line when send,
- `env_file` specifies file name that consist environment variables (default: .env)
- `custom_dynamic_variables` allows to extend or overwrite built-in dynamic variable functions
(default: {})

## Usage

Expand Down
17 changes: 17 additions & 0 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ The following dynamic variables are currenty supported:
To use dynamic variables, the following syntax is used: `{{DYNAMIC_VARIABLE}}`,
e.g. `{{$uuid}}`

You can extend or overwrite built-in dynamic variables, with the config key
`custom_dynamic_variables`:

`require("rest-nvim").setup({`
` custom_dynamic_variables = {`
` -- overwrite built-in`
` ['$uuid'] = function()`
` return "$uuid"`
` end,`
` -- add new dynamic variable function`
` ["$date"] = function()`
` local os_date = os.date('%Y-%m-%d')`
` return os_date`
` end,`
` },`
`})`


===============================================================================
KNOWN ISSUES *rest-nvim-issues*
Expand Down
1 change: 1 addition & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local config = {
},
jump_to_request = false,
env_file = ".env",
custom_dynamic_variables = {},
}

--- Get a configuration value
Expand Down
4 changes: 4 additions & 0 deletions lua/rest-nvim/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ M.read_env_file = function()
end

M.read_dynamic_variables = function()
local from_config = config.get("custom_dynamic_variables") or {}
local dynamic_variables = {
["$uuid"] = M.uuid,
["$timestamp"] = os.time,
["$randomInt"] = function()
return math.random(0, 1000)
end,
}
for k, v in pairs(from_config) do
dynamic_variables[k] = v
end
return dynamic_variables
end

Expand Down

0 comments on commit 3da902d

Please sign in to comment.