From 6de07a51846c644198a1ea4960eecb5d13a6c2c0 Mon Sep 17 00:00:00 2001 From: Marco Kellershoff Date: Sun, 21 Jul 2024 22:11:16 +0200 Subject: [PATCH] feat(env): add DEFAULT_HEADERS This closes #60. --- ...dotenv-and-http-client.env.json-support.md | 31 +++++++++++++++++ lua/kulala/globals/init.lua | 2 +- lua/kulala/parser/env.lua | 28 ++++++++------- lua/kulala/parser/init.lua | 14 ++++++++ lua/kulala/ui/selector.lua | 34 +++++++++---------- lua/telescope/_extensions/kulala.lua | 21 +++++++++--- package.json | 2 +- 7 files changed, 97 insertions(+), 35 deletions(-) diff --git a/docs/docs/usage/dotenv-and-http-client.env.json-support.md b/docs/docs/usage/dotenv-and-http-client.env.json-support.md index 552caf8..bf0e4fb 100644 --- a/docs/docs/usage/dotenv-and-http-client.env.json-support.md +++ b/docs/docs/usage/dotenv-and-http-client.env.json-support.md @@ -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" +} +``` diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 1fd01c6..1062f16 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -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" diff --git a/lua/kulala/parser/env.lua b/lua/kulala/parser/env.lua index c9a2f80..6c08f3a 100644 --- a/lua/kulala/parser/env.lua +++ b/lua/kulala/parser/env.lua @@ -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 = {} @@ -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 diff --git a/lua/kulala/parser/init.lua b/lua/kulala/parser/init.lua index a83823e..dbbbedf 100644 --- a/lua/kulala/parser/init.lua +++ b/lua/kulala/parser/init.lua @@ -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") @@ -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") diff --git a/lua/kulala/ui/selector.lua b/lua/kulala/ui/selector.lua index b80ff2d..fe73a2d 100644 --- a/lua/kulala/ui/selector.lua +++ b/lua/kulala/ui/selector.lua @@ -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 diff --git a/lua/telescope/_extensions/kulala.lua b/lua/telescope/_extensions/kulala.lua index 7294ca2..fa660f5 100644 --- a/lua/telescope/_extensions/kulala.lua +++ b/lua/telescope/_extensions/kulala.lua @@ -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 @@ -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 diff --git a/package.json b/package.json index 41f73ce..be6496f 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "kulala.nvim", - "version": "2.3.0" + "version": "2.4.0" }