Skip to content

Commit 3da902d

Browse files
ybbondNTBBloodbath
authored andcommitted
feat: add config option for custom dynamic variables
1 parent 2542929 commit 3da902d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ use {
7676
},
7777
-- Jump to request line on run
7878
jump_to_request = false,
79-
env_file = '.env'
79+
env_file = '.env',
80+
custom_dynamic_variables = {},
8081
})
8182
end
8283
}
@@ -113,6 +114,8 @@ To run `rest.nvim` you should map the following commands:
113114
- `highlight` allows to enable and configure the highlighting of the selected request when send,
114115
- `jump_to_request` moves the cursor to the selected request line when send,
115116
- `env_file` specifies file name that consist environment variables (default: .env)
117+
- `custom_dynamic_variables` allows to extend or overwrite built-in dynamic variable functions
118+
(default: {})
116119

117120
## Usage
118121

Diff for: doc/rest-nvim.txt

+17
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ The following dynamic variables are currenty supported:
162162
To use dynamic variables, the following syntax is used: `{{DYNAMIC_VARIABLE}}`,
163163
e.g. `{{$uuid}}`
164164

165+
You can extend or overwrite built-in dynamic variables, with the config key
166+
`custom_dynamic_variables`:
167+
168+
`require("rest-nvim").setup({`
169+
` custom_dynamic_variables = {`
170+
` -- overwrite built-in`
171+
` ['$uuid'] = function()`
172+
` return "$uuid"`
173+
` end,`
174+
` -- add new dynamic variable function`
175+
` ["$date"] = function()`
176+
` local os_date = os.date('%Y-%m-%d')`
177+
` return os_date`
178+
` end,`
179+
` },`
180+
`})`
181+
165182

166183
===============================================================================
167184
KNOWN ISSUES *rest-nvim-issues*

Diff for: lua/rest-nvim/config/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local config = {
1414
},
1515
jump_to_request = false,
1616
env_file = ".env",
17+
custom_dynamic_variables = {},
1718
}
1819

1920
--- Get a configuration value

Diff for: lua/rest-nvim/utils/init.lua

+4
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@ M.read_env_file = function()
7676
end
7777

7878
M.read_dynamic_variables = function()
79+
local from_config = config.get("custom_dynamic_variables") or {}
7980
local dynamic_variables = {
8081
["$uuid"] = M.uuid,
8182
["$timestamp"] = os.time,
8283
["$randomInt"] = function()
8384
return math.random(0, 1000)
8485
end,
8586
}
87+
for k, v in pairs(from_config) do
88+
dynamic_variables[k] = v
89+
end
8690
return dynamic_variables
8791
end
8892

0 commit comments

Comments
 (0)