From 293e4bcc82b8d7f1d8af3ea0c5af656c696c842a Mon Sep 17 00:00:00 2001 From: Marco Kellershoff Date: Thu, 1 Aug 2024 00:09:12 +0200 Subject: [PATCH] feat(parser): add env-header-key meta tag This closes #87 --- ...-environment-variables-based-on-headers.md | 38 +++++++++++++++++++ ...onment-variables-based-on-response-json.md | 2 +- docs/sidebars.ts | 1 + lua/kulala/cmd/init.lua | 2 + lua/kulala/globals/init.lua | 2 +- lua/kulala/internal_processing/init.lua | 32 ++++++++++++++++ package.json | 2 +- 7 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 docs/docs/usage/dynamically-setting-environment-variables-based-on-headers.md diff --git a/docs/docs/usage/dynamically-setting-environment-variables-based-on-headers.md b/docs/docs/usage/dynamically-setting-environment-variables-based-on-headers.md new file mode 100644 index 0000000..3eadcac --- /dev/null +++ b/docs/docs/usage/dynamically-setting-environment-variables-based-on-headers.md @@ -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}}" +} +``` diff --git a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md index 45889f7..217321d 100644 --- a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md +++ b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md @@ -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. diff --git a/docs/sidebars.ts b/docs/sidebars.ts index 5d3e8cb..984ce94 100644 --- a/docs/sidebars.ts +++ b/docs/sidebars.ts @@ -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", diff --git a/lua/kulala/cmd/init.lua b/lua/kulala/cmd/init.lua index a03f068..c77b466 100644 --- a/lua/kulala/cmd/init.lua +++ b/lua/kulala/cmd/init.lua @@ -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 diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 7be5c68..b9e2ee2 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.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" diff --git a/lua/kulala/internal_processing/init.lua b/lua/kulala/internal_processing/init.lua index 6fefb00..4fd6ccf 100644 --- a/lua/kulala/internal_processing/init.lua +++ b/lua/kulala/internal_processing/init.lua @@ -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 @@ -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 diff --git a/package.json b/package.json index 6e02797..3164bee 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "kulala.nvim", - "version": "2.7.1" + "version": "2.8.0" }