Skip to content

Commit

Permalink
Merge pull request #89 from mistweaverco/feat/header-to-env
Browse files Browse the repository at this point in the history
feat(parser): add env-header-key meta tag
  • Loading branch information
gorillamoe authored Jul 31, 2024
2 parents e9702da + 293e4bc commit c346a97
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Dynamically setting environment variables based on headers

You can set environment variables based on the headers of a HTTP request.

Create a file with the `.http` extension and write your HTTP requests in it.

## Simple example

The headers of the first request can be obtained and used in the second request.
The keys of the headers are all lowercase,
even if they are written in uppercase or mixed-case in the request.

In this example, the `content-type` and `date` headers are received in the first request.
The headers received are actually `Content-Type` and `Date`, but they are converted to lowercase.

```http title="simple.http"
POST https://httpbin.org/post HTTP/1.1
content-type: application/json
accept: application/json
# @env-header-key HEADER_CONTENT_TYPE content-type
# @env-header-key HEADER_DATE date
{
"type": "very-simple"
}
###
POST https://httpbin.org/post HTTP/1.1
content-type: application/json
accept: application/json
{
"success": true,
"previous_request_header_content_type": "{{HEADER_CONTENT_TYPE}}",
"previous_request_header_date": "{{HEADER_DATE}}"
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dynamically setting environment variables based on response JSON

You can set environment variables based on the response JSON of an HTTP request.
You can set environment variables based on the response JSON of a HTTP request.

Create a file with the `.http` extension and write your HTTP requests in it.

Expand Down
1 change: 1 addition & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const sidebars: SidebarsConfig = {
"usage/automatic-response-formatting",
"usage/dotenv-and-http-client.env.json-support",
"usage/dynamically-setting-environment-variables-based-on-response-json",
"usage/dynamically-setting-environment-variables-based-on-headers",
"usage/file-to-variable",
"usage/graphql",
"usage/magic-variables",
Expand Down
2 changes: 2 additions & 0 deletions lua/kulala/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ M.run = function(result, callback)
if metadata then
if metadata.name == "env-json-key" then
INT_PROCESSING.env_json_key(metadata.value, body)
elseif metadata.name == "env-header-key" then
INT_PROCESSING.env_header_key(metadata.value)
elseif metadata.name == "stdin-cmd" then
EXT_PROCESSING.stdin_cmd(metadata.value, body)
elseif metadata.name == "env-stdin-cmd" then
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "2.7.1"
M.VERSION = "2.8.0"
M.UI_ID = "kulala://ui"
M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"
M.BODY_FILE = FS.get_plugin_tmp_dir() .. "/body.txt"
Expand Down
32 changes: 32 additions & 0 deletions lua/kulala/internal_processing/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local FS = require("kulala.utils.fs")
local GLOBALS = require("kulala.globals")
local M = {}

-- Function to access a nested key in a table dynamically
Expand All @@ -13,6 +15,36 @@ local function get_nested_value(t, key)
return value
end

local get_headers_as_table = function()
local headers_file = FS.read_file(GLOBALS.HEADERS_FILE)
local lines = vim.split(headers_file, "\r\n")
local headers_table = {}
for _, header in ipairs(lines) do
if header:find(":") ~= nil then
local kv = vim.split(header, ":")
local key = kv[1]:lower()
-- the value should be everything after the first colon
-- but we can't use slice and join because the value might contain colons
local value = header:sub(#key + 2)
headers_table[key] = value
end
end
return headers_table
end

M.env_header_key = function(cmd)
local headers = get_headers_as_table()
local kv = vim.split(cmd, " ")
local header_key = kv[2]
local variable_name = kv[1]
local value = headers[header_key:lower()]
if value == nil then
vim.notify("env-header-key --> Header not found.", vim.log.levels.ERROR)
else
vim.fn.setenv(variable_name, value)
end
end

M.env_json_key = function(cmd, body)
local json = vim.fn.json_decode(body)
if json == nil then
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "kulala.nvim",
"version": "2.7.1"
"version": "2.8.0"
}

0 comments on commit c346a97

Please sign in to comment.