Skip to content

Commit

Permalink
Merge pull request #64 from mistweaverco/feat/default-headers
Browse files Browse the repository at this point in the history
feat(env): add DEFAULT_HEADERS
  • Loading branch information
gorillamoe authored Jul 21, 2024
2 parents 8e1db36 + 6de07a5 commit 9f7d76a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 35 deletions.
31 changes: 31 additions & 0 deletions docs/docs/usage/dotenv-and-http-client.env.json-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,34 @@ Authorization: Bearer {{API_KEY}}
}
```

#### Default http headers

You can define default HTTP headers in the `http-client.env.json` file.

You need to put them in the special `_base` key and
the `DEFAULT_HEADERS` will be merged with the headers from the HTTP requests.

```json title="http-client.env.json"
{
"_base": {
"DEFAULT_HEADERS": {
"content-type": "application/json",
"accept": "application/json"
},
"dev": {
"API_KEY": "your-api-key"
}
}
```

Then, they are automatically added to the HTTP requests,
unless you override them.

```http title="examples.http"
POST https://httpbin.org/post HTTP/1.1
Authorization: Bearer {{API_KEY}}

{
"name": "John"
}
```
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.3.0"
M.VERSION = "2.4.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
28 changes: 16 additions & 12 deletions lua/kulala/parser/env.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local FS = require('kulala.utils.fs')
local GLOBAL_STORE = require('kulala.global_store')
local DYNAMIC_VARS = require('kulala.parser.dynamic_vars')
local FS = require("kulala.utils.fs")
local GLOBAL_STORE = require("kulala.global_store")
local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")

local M = {}

local http_client_env_json = FS.find_file_in_parent_dirs('http-client.env.json')
local dotenv = FS.find_file_in_parent_dirs('.env')
local http_client_env_json = FS.find_file_in_parent_dirs("http-client.env.json")
local dotenv = FS.find_file_in_parent_dirs(".env")

M.get_env = function()
local env = {}
Expand All @@ -14,23 +14,27 @@ M.get_env = function()
end
if http_client_env_json then
local f = vim.fn.json_decode(vim.fn.readfile(http_client_env_json))
GLOBAL_STORE.set('http_client_env_json', f)
if f._base then
GLOBAL_STORE.set("http_client_env_base", f._base)
end
f._base = nil
GLOBAL_STORE.set("http_client_env", f)
if not f then
vim.notify('http-client.env.json is not a valid json file', vim.log.levels.ERROR)
vim.notify("http-client.env.json is not a valid json file", vim.log.levels.ERROR)
return env
end
local selected_env_name = GLOBAL_STORE.get('selected_env')
local selected_env_name = GLOBAL_STORE.get("selected_env")
local selected_env = f[selected_env_name]
if selected_env then
env = vim.tbl_extend('force', env, selected_env)
env = vim.tbl_extend("force", env, selected_env)
end
elseif dotenv then
local dotenv_env = vim.fn.readfile(dotenv)
for _, line in ipairs(dotenv_env) do
if line:match('^%s*$') or line:match('^%s*#') then
return
if line:match("^%s*$") or line:match("^%s*#") then
return
end
local key, value = line:match('^%s*([^=]+)%s*=%s*(.*)%s*$')
local key, value = line:match("^%s*([^=]+)%s*=%s*(.*)%s*$")
if key and value then
env[key] = value
end
Expand Down
14 changes: 14 additions & 0 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local FS = require("kulala.utils.fs")
local GLOBALS = require("kulala.globals")
local GLOBAL_STORE = require("kulala.global_store")
local CONFIG = require("kulala.config")
local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")
local STRING_UTILS = require("kulala.utils.string")
Expand Down Expand Up @@ -292,6 +293,19 @@ function M.parse()
end
end

-- Merge headers from the _base environment if it exists
if GLOBAL_STORE.get("http_client_env_base") then
local default_headers = GLOBAL_STORE.get("http_client_env_base")["DEFAULT_HEADERS"]
if default_headers then
for key, value in pairs(default_headers) do
key = key:lower()
if res.headers[key] == nil then
res.headers[key] = value
end
end
end
end

-- build the command to exectute the request
table.insert(res.cmd, "curl")
table.insert(res.cmd, "-s")
Expand Down
34 changes: 17 additions & 17 deletions lua/kulala/ui/selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ local GLOBAL_STORE = require("kulala.global_store")
local M = {}

function M.select_env()
if not GLOBAL_STORE.get("http_client_env_json") then
return
end
if not GLOBAL_STORE.get("http_client_env") then
return
end

local envs = {}
for key, _ in pairs(GLOBAL_STORE.get("http_client_env_json")) do
table.insert(envs, key)
end
local envs = {}
for key, _ in pairs(GLOBAL_STORE.get("http_client_env")) do
table.insert(envs, key)
end

local opts = {
prompt = "Select env",
}
vim.ui.select(envs, opts, function(result)
if not result then
return
end
GLOBAL_STORE.set("selected_env", result)
vim.g.kulala_selected_env = result
end)
local opts = {
prompt = "Select env",
}
vim.ui.select(envs, opts, function(result)
if not result then
return
end
GLOBAL_STORE.set("selected_env", result)
vim.g.kulala_selected_env = result
end)
end

return M
21 changes: 17 additions & 4 deletions lua/telescope/_extensions/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values
local previewers = require("telescope.previewers")

local function kulala_env_select(_)
if not GLOBAL_STORE.get("http_client_env_json") then
if not GLOBAL_STORE.get("http_client_env") then
return
end

local envs = {}
for key, _ in pairs(GLOBAL_STORE.get("http_client_env_json")) do
for key, _ in pairs(GLOBAL_STORE.get("http_client_env")) do
table.insert(envs, key)
end

Expand All @@ -42,7 +42,20 @@ local function kulala_env_select(_)
end)
return true
end,
previewer = conf.grep_previewer({}),
previewer = previewers.new_buffer_previewer({
title = "Environment",
define_preview = function(self, entry)
local env = GLOBAL_STORE.get("http_client_env")[entry.value]
if env == nil then
return
end
local lines = {}
for key, value in pairs(env) do
table.insert(lines, string.format("%s: %s", key, value))
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
end,
}),
})
:find()
end
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.3.0"
"version": "2.4.0"
}

0 comments on commit 9f7d76a

Please sign in to comment.