Skip to content

Commit c2e7ab4

Browse files
fix: pre-request script sets variable locally
1 parent 315c624 commit c2e7ab4

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

Diff for: lua/rest-nvim/context.lua

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ local config = require("rest-nvim.config")
55
local M = {}
66

77
---@class rest.Context
8+
---global variables
89
---@field vars table<string,string>
10+
---local variables
11+
---@field lv table<string,string>
912
local Context = {}
1013
Context.__index = Context
1114

@@ -43,6 +46,7 @@ function Context:new()
4346
local obj = {
4447
__index = self,
4548
vars = {},
49+
lv = {},
4650
}
4751
setmetatable(obj, self)
4852
return obj
@@ -51,16 +55,30 @@ end
5155
---@param filepath string
5256
function Context:load_file(filepath)
5357
dotenv.load_file(filepath, function (key, value)
54-
self:set(key, value)
58+
self:set_global(key, value)
5559
end)
5660
end
5761

5862
---@param key string
5963
---@param value string
60-
function Context:set(key, value)
64+
function Context:set_global(key, value)
65+
vim.validate("key", key, "string")
66+
vim.validate("value", value, "string")
6167
self.vars[key] = value
6268
end
6369

70+
---@param key string
71+
---@param value string
72+
function Context:set_local(key, value)
73+
vim.validate("key", key, "string")
74+
vim.validate("value", value, "string")
75+
self.lv[key] = value
76+
end
77+
78+
function Context:clear_local()
79+
self.lv = {}
80+
end
81+
6482
---@param key string
6583
---@return nil|fun():string
6684
local function get_dynamic_vars(key)
@@ -79,7 +97,7 @@ function Context:resolve(key)
7997
-- find from dynamic variables
8098
local var = get_dynamic_vars(key)
8199
-- find from local variable table or vim.env
82-
return var and var() or self.vars[key] or vim.env[key] or ""
100+
return var and var() or self.lv[key] or self.vars[key] or vim.env[key] or ""
83101
end
84102

85103
M.Context = Context

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function parser.parse_variable_declaration(vd_node, source, ctx)
246246
local name = assert(get_node_field_text(vd_node, "name", source))
247247
local value = vim.trim(assert(get_node_field_text(vd_node, "value", source)))
248248
value = expand_variables(value, ctx)
249-
ctx:set(name, value)
249+
ctx:set_global(name, value)
250250
end
251251

252252
---@param node TSNode
@@ -410,6 +410,7 @@ function parser.parse(node, source, ctx)
410410
body = body,
411411
handlers = handlers,
412412
}
413+
ctx:clear_local()
413414
jar.load_cookies(req)
414415
return req
415416
end

Diff for: lua/rest-nvim/script/lua.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ local function create_prescript_env(ctx)
2424
variables = {
2525
---Set request-local variable
2626
set = function (key, value)
27-
ctx:set(key, value)
27+
ctx:set_local(key, value)
2828
end,
2929
---Retrieve variable in current request scope
3030
get = function (key)
@@ -65,7 +65,7 @@ local function create_handler_env(ctx, res)
6565
variables = {
6666
---Set request-local variable
6767
set = function (key, value)
68-
ctx:set(key, value)
68+
ctx:set_local(key, value)
6969
end,
7070
---Retrieve variable in current request scope
7171
get = function (key)

Diff for: spec/examples/examples_spec.lua

+4-5
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ describe("pre-request script", function ()
8282
local req1 = assert(parser.parse(req_nodes[1], source, ctx))
8383
assert.same("https://jsonplaceholder.typicode.com/posts/3", req1.url)
8484
end)
85-
-- TODO:
86-
-- it("local variables don't affect other requests", function ()
87-
-- local req2 = assert(parser.parse(req_nodes[2], source, ctx))
88-
-- assert.same("https://jsonplaceholder.typicode.com/posts/", req2.url)
89-
-- end)
85+
it("local variables don't affect other requests", function ()
86+
local req2 = assert(parser.parse(req_nodes[2], source, ctx))
87+
assert.same("https://jsonplaceholder.typicode.com/posts/", req2.url)
88+
end)
9089
end)

Diff for: spec/parser/http_parser_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ X-DATE: {{$date}}
255255
parser.parse_pre_request_script(script_node, source, c)
256256
assert.same({
257257
foo = "bar",
258-
}, c.vars)
258+
}, c.lv)
259259
end)
260260

261261
it("create context from source", function()

Diff for: spec/script_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("handler script", function ()
1111
local ctx = Context:new()
1212
vim.env["foo"] = "old"
1313
vim.env["baz"] = "old"
14-
ctx:set("bar", "old")
14+
ctx:set_global("bar", "old")
1515
local script = [[
1616
client.global.set("foo", "new")
1717
request.variables.set("bar", "new")

0 commit comments

Comments
 (0)