oysandvik94 / curl.nvim

Run requests with curl, formatted with jq, and persisted commands according to your own workflow
106 stars 3 forks source link

Feature: run a Lua script after the request ends #56

Open name-jerry opened 1 week ago

name-jerry commented 1 week ago

For example, save the token from the response result into a variable for future use.

oysandvik94 commented 1 week ago

@name-jerry Are you thinking this is something that would be added in the config, and put in an environment variable?

name-jerry commented 1 week ago

@name-jerry Are you thinking this is something that would be added in the config, and put in an environment variable?

I have seen how to configure environment variables using config. I want to add a default variable such as response; It is used to store the returned results and then run lua to retrieve the variables for later requests

kareem-abdul commented 6 days ago

@oysandvik94 I also think this would be a nice to have feature.

@name-jerry Are you thinking this is something that would be added in the config, and put in an environment variable?

I think, the easiest method would be to have the ability to define a script per collection. I'm thinking it would look something like this,

And if the user wants the script to be only executed on a specific curl request, they could add specific conditions inside the script like this

if req.path == "/api/v1/login" and req.method == "POST" then
     vim.fn.setenv("TOKEN", res.json.token)
     return
end

Now that I thinking about it, this method could solve #29 and #12 i.e, you could define a pre-request command, that would create a lua script which will be executed before every single curl request in a collection. And in that file could define your envs, tokens etc like so

vim.fn.setenv("HOST", "http://localhost:8080")
vim.fn.setenv("TOKEN", "....")
name-jerry commented 5 days ago

@oysandvik94 I also think this would be a nice to have feature.

@name-jerry Are you thinking this is something that would be added in the config, and put in an environment variable?

I think, the easiest method would be to have the ability to define a script per collection. I'm thinking it would look something like this,

  • :CurlCollection post-request would ask for user to select the collection they want the script for, or use the currently opened one or be explicit :CurlCollection post-request <collection-name>.
  • This would then create a lua file in the data directory which would be executed after every curl request made in that collection.

And if the user wants the script to be only executed on a specific curl request, they could add specific conditions inside the script like this

if req.path == "/api/v1/login" and req.method == "POST" then
     vim.fn.setenv("TOKEN", res.json.token)
     return
end

Now that I thinking about it, this method could solve #29 and #12 i.e, you could define a pre-request command, that would create a lua script which will be executed before every single curl request in a collection. And in that file could define your envs, tokens etc like so

vim.fn.setenv("HOST", "http://localhost:8080")
vim.fn.setenv("TOKEN", "....")

It's a great idea to reuse code, but my idea is more like resty's implementation;or something like the following:

curl http://url/api/v1/login
-H 'Content-Type: application/json'
-d
{
  username: "admin",
  password:"xx"
 -- username: "user",
 -- password:"xx"
}
###lua
-- The response variable contains the result returned by the above cURL command.
ctx.token=response.json().token
###

curl http://url/api/v1/resource-name
-H "Authorization: Bearer $token"
-H 'Content-Type: application/json'
-d
{
id:xx
}

This way I can quickly switch accounts as needed.

oysandvik94 commented 5 days ago

I see what you guys mean! This is probably possible, I like both suggestions. Biggest issue at the moment is that curl.nvim is kinda dumb when it comes to parsing. It really just grabs the curl command under the cursor, does some minor cleanup before executing it, then seperates the response body from the headers and just shows the response to the user. So it doesn't do any parsing really to get things such as method, path, headers or whatever. I do want to implement that at some point though.

But it shouldnt be hard to just grab a field from the response json using lua's json thing.

Just a wild idea: I'm guessing 99% of use cases of adding lua scripts is to store something from the respone to use it in another request, which in 90% of cases is a bearer token. Can we just add some syntax to the curl stuff itself?

const tokenResponse = curl http://url/api/v1/login
-H 'Content-Type: application/json'
-d
{
  username: "admin",
  password:"xx"
}

curl http://url/api/v1/resource-name
-H "Authorization: Bearer $tokenRespones.token"
-H 'Content-Type: application/json'
-d
{
id:xx
}
name-jerry commented 5 days ago

I see what you guys mean! This is probably possible, I like both suggestions. Biggest issue at the moment is that curl.nvim is kinda dumb when it comes to parsing. It really just grabs the curl command under the cursor, does some minor cleanup before executing it, then seperates the response body from the headers and just shows the response to the user. So it doesn't do any parsing really to get things such as method, path, headers or whatever. I do want to implement that at some point though.

But it shouldnt be hard to just grab a field from the response json using lua's json thing.

Just a wild idea: I'm guessing 99% of use cases of adding lua scripts is to store something from the respone to use it in another request, which in 90% of cases is a bearer token. Can we just add some syntax to the curl stuff itself?

const tokenResponse = curl http://url/api/v1/login
-H 'Content-Type: application/json'
-d
{
  username: "admin",
  password:"xx"
}

curl http://url/api/v1/resource-name
-H "Authorization: Bearer $tokenRespones.token"
-H 'Content-Type: application/json'
-d
{
id:xx
}

It's a great idea to let curl just be curl;I'm pretty sure that's what I want

kareem-abdul commented 5 days ago

I see what you guys mean! This is probably possible, I like both suggestions. Biggest issue at the moment is that curl.nvim is kinda dumb when it comes to parsing. It really just grabs the curl command under the cursor, does some minor cleanup before executing it, then seperates the response body from the headers and just shows the response to the user. So it doesn't do any parsing really to get things such as method, path, headers or whatever. I do want to implement that at some point though.

But it shouldnt be hard to just grab a field from the response json using lua's json thing.

Just a wild idea: I'm guessing 99% of use cases of adding lua scripts is to store something from the respone to use it in another request, which in 90% of cases is a bearer token. Can we just add some syntax to the curl stuff itself?

const tokenResponse = curl http://url/api/v1/login
-H 'Content-Type: application/json'
-d
{
  username: "admin",
  password:"xx"
}

curl http://url/api/v1/resource-name
-H "Authorization: Bearer $tokenRespones.token"
-H 'Content-Type: application/json'
-d
{
id:xx
}

It's a great idea to let curl just be curl;I'm pretty sure that's what I want

Yup, I agree as well

oysandvik94 commented 5 days ago

Sweet, I'll try this weekend. With this I can eventually formalize the syntax and create an LSP too maybe for autocompletion and stuff.