diff --git a/docs/docs/usage/public-methods.md b/docs/docs/usage/public-methods.md index d6defc60..59a85e8a 100644 --- a/docs/docs/usage/public-methods.md +++ b/docs/docs/usage/public-methods.md @@ -89,6 +89,19 @@ Additionally, you can clear a list of global variables by passing a table of variable names like so: `require('kulala').scripts_clear_global({'variable_name1', 'variable_name2'})`. +### clear_cached_files + +`require('kulala').clear_cached_files()` +clears all cached files. + +These files include: + +- last response body +- last response headers +- last request data +- global variables set via scripts +- compiled pre- and post-request scripts + ### download_graphql_schema You can download the schema of a GraphQL server with: diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 528404df..d9cf78e1 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -4,7 +4,7 @@ local M = {} local plugin_tmp_dir = FS.get_plugin_tmp_dir() -M.VERSION = "4.0.3" +M.VERSION = "4.0.4" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt" diff --git a/lua/kulala/init.lua b/lua/kulala/init.lua index 05601843..7e42edf4 100644 --- a/lua/kulala/init.lua +++ b/lua/kulala/init.lua @@ -7,6 +7,7 @@ local JUMPS = require("kulala.jumps") local Graphql = require("kulala.graphql") local Logger = require("kulala.logger") local ScriptsUtils = require("kulala.parser.scripts.utils") +local Fs = require("kulala.utils.fs") local M = {} M.setup = function(config) @@ -101,4 +102,10 @@ M.set_selected_env = function(env) vim.g.kulala_selected_env = env end +---Clears all cached files +---Useful when you want to clear all cached files +M.clear_cached_files = function() + Fs.clear_cached_files() +end + return M diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequestResponse.ts b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequestResponse.ts index f93867f3..fe683f8e 100644 --- a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequestResponse.ts +++ b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequestResponse.ts @@ -23,8 +23,7 @@ if (fs.existsSync(_RESPONSE_HEADERS_FILEPATH)) { continue; } const [key] = line.split(delimiter); - const lkey = key.toLowerCase(); - headers[lkey] = { + headers[key] = { name: key, value: line.slice(key.length + delimiter.length).trim() } @@ -44,16 +43,14 @@ export const Response = { body, headers: { valueOf: (headerName: string): string | null => { - const lkey = headerName.toLowerCase(); - if (lkey in headers) { - return headers[lkey].value; + if (headerName in headers) { + return headers[headerName].value; } return null; }, valuesOf: function (headerName: string): HeaderObject | null { - const lkey = headerName.toLowerCase(); - if (lkey in headers) { - return headers[lkey]; + if (headerName in headers) { + return headers[headerName]; } return null; }, diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts index 13b81032..e845b3c8 100644 --- a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts +++ b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts @@ -62,14 +62,12 @@ export const Request = { }, headers: { findByName: (headerName: string) => { - const hnl = headerName.toLowerCase(); - return getHeaderObject(hnl, req.headers_raw[hnl], req.headers[hnl]); + return getHeaderObject(headerName, req.headers_raw[headerName], req.headers[headerName]); }, all: function (): HeaderObject[] { const h = []; for (const [key, value] of Object.entries(req.headers)) { - const lkey = key.toLowerCase(); - const item = getHeaderObject(lkey, req.headers_raw[lkey], value); + const item = getHeaderObject(key, req.headers_raw[key], value); if (item !== null) { h.push(item); } diff --git a/lua/kulala/utils/fs.lua b/lua/kulala/utils/fs.lua index 72d74a53..0796a6cb 100644 --- a/lua/kulala/utils/fs.lua +++ b/lua/kulala/utils/fs.lua @@ -1,3 +1,4 @@ +local Logger = require("kulala.logger") local M = {} ---Get the OS @@ -198,7 +199,11 @@ M.get_request_scripts_dir = function() return dir end -M.delete_files_in_directory = function(dir) +---Delete all files in a directory +---@param dir string +---@param verbose boolean|nil +---@usage fs.delete_files_in_directory('tmp', true) +M.delete_files_in_directory = function(dir, verbose) -- Open the directory for scanning local scandir = vim.loop.fs_scandir(dir) if scandir then @@ -208,13 +213,16 @@ M.delete_files_in_directory = function(dir) if not name then break end - -- Only delete files, not directories - if type == "file" then + -- Only delete files, not directories except .gitingore + if type == "file" and name:match(".gitignore$") == nil then local filepath = M.join_paths(dir, name) local success, err = vim.loop.fs_unlink(filepath) if not success then print("Error deleting file:", filepath, err) end + if verbose and success then + Logger.info("Deleted file: " .. filepath) + end end end else @@ -316,4 +324,16 @@ M.read_file_lines = function(filename) return lines end +---Clears all cached files +M.clear_cached_files = function() + local tmp_dir = M.get_plugin_tmp_dir() + local scripts_dir = M.get_tmp_scripts_dir() + local request_scripts_dir = M.get_request_scripts_dir() + local compiled_pre_request_scripts = M.join_paths(M.get_scripts_dir(), "engines", "javascript", "lib", "dist") + M.delete_files_in_directory(tmp_dir, true) + M.delete_files_in_directory(scripts_dir, true) + M.delete_files_in_directory(request_scripts_dir, true) + M.delete_files_in_directory(compiled_pre_request_scripts, true) +end + return M