mistweaverco / kulala.nvim

A minimal 🀏 HTTP-client 🐼 interface πŸ–₯️ for Neovim ❀️.
https://kulala.mwco.app
MIT License
304 stars 5 forks source link

[Feature Request] Post request execution to store token so it can be used in subsequent requests #24

Closed skela closed 1 week ago

skela commented 2 weeks ago

Most of the time I work with endpoints that require authentication. Moreover, the tokens used in these auth operations expire often. Having to login, and then manually stick the token in the http-client.env.json (or .env file) is tedious and boring. Wouldn't it be nice if this could be streamlined a tad?

Here's 2 different ways of attacking the problem that i think could help with that:

First option: A env variable name + a .http file name prefix. And some kind of setting somewhere, that would let us specify what they are. Then if you triggered a http request in any of the http files, and kulala knows the env variable name is being used, it could trigger the special login .http file if the variable is missing or expired, before making the request.

Second option (and probably the easier one of the two): Allow us to define in the .http file itself underneath the request / header / payload info, some execution block, where we can run lua code, to make things happen, for example, parse the request response payload, and store whatever we want in either the .env file, or whatever, so it can be used in subsequent request in the same and other http files in that folder.

An example of how the second option could be defined:

POST {{API}}/accounts/login HTTP/1.1
Content-Type: application/json
Accept: application/json

{
    "username": "{{USERNAME}}",
    "password": "{{PASSWORD}}"
}

--{%

local body = context.json_decode(context.result.body)
context.set_env("TOKEN", body.accessToken)

--%}

###

GET {{API}}/apps/config HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Bearer {{TOKEN}}
App: {{APP}}

PS: Kulala is very promising ❀️

gorillamoe commented 2 weeks ago

Already working on it πŸ™‚

https://github.com/mistweaverco/kulala.nvim/issues/4#issuecomment-2198507863

It's not Lua code, but you can pipe the output into any command you want. This enables us to do exactly what you want πŸ‘πŸΎ

Thanks for the kind words πŸ™πŸΎβ€οΈ

gorillamoe commented 1 week ago

Should be fixed with: https://github.com/mistweaverco/kulala.nvim/releases/tag/v2.1.0