Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add json schema for http-client.env.json #243

Merged
merged 9 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
.env
http-client.env.json
!/schemas/http-client.env.json
*.http
*graphql-schema.json
/http-examples
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/getting-started/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ Example:
If enabled, Kulala searches for
`.vscode/settings.json` or `*.code-workspace`
files in the current directory and
its parents to read the `rest-client.environmentVariables` definitions
(`$shared` will be treated as `_base`).
its parents to read the `rest-client.environmentVariables` definitions.

If `http-client.env.json` is also present,
it'll be merged (and overwrites variables from VSCode).
Expand Down
11 changes: 7 additions & 4 deletions docs/docs/usage/dotenv-and-http-client.env.json-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define environment variables in it.

```json title="http-client.env.json"
{
"$schema": "https://raw.githubusercontent.com/mistweaverco/kulala.nvim/main/schemas/http-client.env.schema.json",
"dev": {
"API_KEY": "your-api-key"
},
Expand Down Expand Up @@ -90,15 +91,17 @@ Authorization: Bearer {{API_KEY}}

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

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

```json title="http-client.env.json"
{
"_base": {
"DEFAULT_HEADERS": {
"$schema": "https://raw.githubusercontent.com/mistweaverco/kulala.nvim/main/schemas/http-client.env.schema.json",
"$shared": {
"$default_headers": {
"Content-Type": "application/json",
"Accept": "application/json"
},
},
"dev": {
"API_KEY": "your-api-key"
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local function default_data()
return {
selected_env = nil, -- string - name of selected env
http_client_env = nil, -- table of envs from http-client.env.json
http_client_env_base = nil, -- table of base env values which should be applied to all requests
http_client_env_shared = nil, -- table of base env values which should be applied to all requests
env = {}, -- table of envs from document sources
scope_nr = nil, -- number - buffer number of the current scope
}
Expand Down
23 changes: 12 additions & 11 deletions lua/kulala/parser/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ M.get_env = function()
env[key] = value
end

DB.update().http_client_env_base = {}
DB.update().http_client_env_shared = {}
DB.update().http_client_env = {}

if Config.get().vscode_rest_client_environmentvars then
Expand All @@ -29,8 +29,8 @@ M.get_env = function()
if settings and settings["rest-client.environmentVariables"] then
local f = settings["rest-client.environmentVariables"]
if f["$shared"] then
DB.update().http_client_env_base =
vim.tbl_deep_extend("force", DB.find_unique("http_client_env_base"), f["$shared"])
DB.update().http_client_env_shared =
vim.tbl_deep_extend("force", DB.find_unique("http_client_env_shared"), f["$shared"])
end
f["$shared"] = nil
DB.update().http_client_env = vim.tbl_deep_extend("force", DB.find_unique("http_client_env"), f)
Expand All @@ -44,8 +44,8 @@ M.get_env = function()
if settings and settings["rest-client.environmentVariables"] then
local f = settings["rest-client.environmentVariables"]
if f["$shared"] then
DB.update().http_client_env_base =
vim.tbl_deep_extend("force", DB.find_unique("http_client_env_base"), f["$shared"])
DB.update().http_client_env_shared =
vim.tbl_deep_extend("force", DB.find_unique("http_client_env_shared"), f["$shared"])
end
f["$shared"] = nil
DB.update().http_client_env = vim.tbl_deep_extend("force", DB.find_unique("http_client_env"), f)
Expand All @@ -68,16 +68,17 @@ M.get_env = function()

if http_client_env_json then
local f = vim.fn.json_decode(vim.fn.readfile(http_client_env_json))
if f._base then
DB.update().http_client_env_base = vim.tbl_deep_extend("force", DB.find_unique("http_client_env_base"), f._base)
if f["$shared"] then
DB.update().http_client_env_shared =
vim.tbl_deep_extend("force", DB.find_unique("http_client_env_shared"), f["$shared"])
end
f._base = nil
f["$shared"] = nil
DB.update().http_client_env = vim.tbl_deep_extend("force", DB.find_unique("http_client_env"), f)
end

local http_client_env_base = DB.find_unique("http_client_env_base") or {}
for key, value in pairs(http_client_env_base) do
if key ~= "DEFAULT_HEADERS" then
local http_client_env_shared = DB.find_unique("http_client_env_shared") or {}
for key, value in pairs(http_client_env_shared) do
if key ~= "$default_headers" then
env[key] = value
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ function M.parse(start_request_linenr)
end
end

-- Merge headers from the _base environment if it exists
if DB.find_unique("http_client_env_base") then
local default_headers = DB.find_unique("http_client_env_base")["DEFAULT_HEADERS"]
-- Merge headers from the $shared environment if it exists
if DB.find_unique("http_client_env_shared") then
local default_headers = DB.find_unique("http_client_env_shared")["$default_headers"]
if default_headers then
for key, value in pairs(default_headers) do
key = key:lower()
Expand Down
3 changes: 3 additions & 0 deletions lua/kulala/ui/selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function M.select_env()

local envs = {}
for key, _ in pairs(http_client_env) do
if key ~= "$schema" and key ~= "$shared" then
table.insert(envs, key)
end
table.insert(envs, key)
end

Expand Down
4 changes: 3 additions & 1 deletion lua/telescope/_extensions/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ local function kulala_env_select(_)

local envs = {}
for key, _ in pairs(http_client_env) do
table.insert(envs, key)
if key ~= "$schema" and key ~= "$shared" then
table.insert(envs, key)
end
end

pickers
Expand Down
43 changes: 43 additions & 0 deletions schemas/http-client.env.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$id": "https://raw.githubusercontent.com/mistweaverco/kulala.nvim/main/schemas/http-client.env.schema.json",
"title": "HTTP Client Environment Variables",
"description": "The environment variables required for the HTTP client",
"type": "object",
"properties": {
"$schema": {
"type": "string"
},
"$shared": {
"type": "object",
"properties": {
"$default_headers": {
"type": "object",
"patternProperties": {
"^.*$": {
"type": "string"
}
},
"additionalProperties": false
}
},
"patternProperties": {
"^([A-Za-z0-9_]+)$": {
"type": ["string", "number"]
}
},
"additionalProperties": false
}
},
"patternProperties": {
"^.*$": {
"type": "object",
"patternProperties": {
"^([A-Za-z0-9_]+)$": {
"type": ["string", "number"]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}