Skip to content

Commit

Permalink
Improve scripts (#252)
Browse files Browse the repository at this point in the history
* feat(scripts): make it work with most jetbrains stuff

See: https://www.jetbrains.com/help/idea/http-response-reference.html#request-properties

* chore(version): bump

* feat(docs): request-reference

* feat(scripts): even closer to jetbrains

* fix(docs): linter
  • Loading branch information
gorillamoe authored Oct 1, 2024
1 parent 3b53dbb commit 6d6865c
Show file tree
Hide file tree
Showing 27 changed files with 4,919 additions and 335 deletions.
81 changes: 78 additions & 3 deletions docs/docs/scripts/client-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,81 @@

These helper functions are available in the client object in scripts.

- `client.global.set(key, value)` - Set a global variable.
Persisted across script runs and Neovim restarts.
- `client.global.get(key)` - Get a global variable.
## client.global.set

Set a variable.

Variables are persisted across script runs and Neovim restarts.

```javascript
client.global.set("SOME_TOKEN", "123");
```

## client.global.get

Get a variable.

Variables are persisted across script runs and Neovim restarts.

```javascript
client.log(client.global.get("SOME_TOKEN"));
```

## client.log

Logs arbitrary data to the console.

```javascript
client.log("Hello, world!");
```

## client.test

:::warning

Not yet implemented.

:::

## client.assert

:::warning

Not yet implemented.

:::

## client.exit

Terminates execution of the response handler script.

```javascript
client.exit();
```
### client.isEmpty

Checks whether the `global` object has no variables defined.

```javascript
const isEmpty = client.isEmpty();
if (isEmpty) {
client.log("No global variables defined");
}
```


## client.clear

Removes the `varName` variable from the global variables storage.

```javascript
client.clear("SOME_TOKEN");
```

## client.clearAll

Removes all variables from the global variables storage.

```javascript
client.clearAll();
```
118 changes: 114 additions & 4 deletions docs/docs/scripts/request-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,117 @@

These helper functions are available in the request object in scripts.

- `request.variables.get(key)` - Get a request variable.
Request variables are only available for the duration of the request.
- `request.variables.set(key, value)` - Set a request variable.
Request variables are only available for the duration of the request.
## request.variables.get

Get a request variable.

Request variables are only available for the duration of the request.

```javascript
client.log(request.variables.get("SOME_TOKEN"));
```

## request.variables.set

Set a request variable.

Request variables are only available for the duration of the request.

```javascript
request.variables.set("SOME_TOKEN, "123");
client.log(request.variables.get("SOME_TOKEN"));
```
## request.body.getRaw
Returns the request body in the raw format:
If the body contains variables,
their names are displayed instead of their values.
For example:
```javascript
client.log(request.body.getRaw());
```
## request.body.tryGetSubstituted
Returns the request body with variables substituted.
```javascript
client.log(request.body.tryGetSubstituted());
```
## request.environment.get
Retrieves a value of the environment variable identified
by its name or returns null if it doesn't exist.
```javascript
client.log(request.environment.get("SOME_ENV_VAR"));
```
## request.headers.all
Returns all request header objects.
Each header object has the following methods:
- `name()` - Returns the name of the header.
- `getRawValue()` - Returns the value of the header in the raw format.
- `tryGetSubstituted()` - Returns the value of the header with variables substituted.
```javascript
const headers = request.headers.all();
for (const header of headers) {
client.log(header.name());
client.log(header.getRawValue());
client.log(header.tryGetSubstituted());
}
```
## request.headers.findByName
Returns a request header object identified by its name.
The header object has the following methods:
- `name()` - Returns the name of the header.
- `getRawValue()` - Returns the value of the header in the raw format.
- `tryGetSubstituted()` - Returns the value of the header with variables substituted.
```javascript
const contentTypeHeader = request.headers.findByName("Content-Type");
if (contentTypeHeader) {
client.log(contentTypeHeader.name());
client.log(contentTypeHeader.getRawValue());
client.log(contentTypeHeader.tryGetSubstituted());
}
```
## request.method
Returns the request method.
Such as `GET`, `POST`, `PUT`, `DELETE`, etc.
```javascript
client.log(request.method());
```
## request.url.getRaw
Returns the request URL in the raw format, without any substitutions.
```javascript
client.log(request.url.getRaw());
```
## request.url.tryGetSubstituted
Returns the request URL with variables substituted.
```javascript
client.log(request.url.tryGetSubstituted());
```
27 changes: 21 additions & 6 deletions docs/docs/scripts/response-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@

These helper functions are available in the response object in scripts.

- `reponse.body` - The response body,
as a string, or json object if the response is json.
- `response.headers.valueOf(headerName)` - Get the value of a header.
- `response.headers.valuesOf(headerName)` - Retrieves the array
containing all values of the headerName response header.
Returns an empty array if the headerName response header doesn't exist.
## response.body

The response body, as a string, or json object if the response is json.

```javascript
client.log(response.body);
```

## response.headers

Returns all response header objects.

Each header object has the following methods:

- `header.valueOf(headerName)` - Get the value of a header.
- `header.valuesOf(headerName)` - Retrieves the object containing all values of the headerName response header. Returns null if the headerName response header doesn't exist.

```javascript
client.log(response.headers.valueOf("Content-Type"));
```

7 changes: 3 additions & 4 deletions lua/kulala/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local PARSER = require("kulala.parser")
local EXT_PROCESSING = require("kulala.external_processing")
local INT_PROCESSING = require("kulala.internal_processing")
local Api = require("kulala.api")
local Scripts = require("kulala.scripts")
local INLAY = require("kulala.inlay")
local UV = vim.loop
local Logger = require("kulala.logger")
Expand Down Expand Up @@ -61,7 +60,7 @@ end

---Runs the command and returns the result
---@param cmd table command to run
---@param callback function callback function
---@param callback function|nil callback function
M.run = function(cmd, callback)
vim.fn.jobstart(cmd, {
on_stderr = function(_, datalist)
Expand Down Expand Up @@ -124,7 +123,7 @@ M.run_parser = function(req, callback)
end
end
INT_PROCESSING.redirect_response_body_to_file(result.redirect_response_body_to_files)
Scripts.javascript.run("post_request", result.scripts.post_request)
PARSER.scripts.javascript.run("post_request", result.scripts.post_request)
Api.trigger("after_request")
end
Fs.delete_request_scripts_files()
Expand Down Expand Up @@ -179,7 +178,7 @@ M.run_parser_all = function(doc, callback)
end
end
INT_PROCESSING.redirect_response_body_to_file(result.redirect_response_body_to_files)
Scripts.javascript.run("post_request", result.scripts.post_request)
PARSER.scripts.javascript.run("post_request", result.scripts.post_request)
Api.trigger("after_request")
end
Fs.delete_request_scripts_files()
Expand Down
3 changes: 2 additions & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ local M = {}

local plugin_tmp_dir = FS.get_plugin_tmp_dir()

M.VERSION = "3.8.0"
M.VERSION = "4.0.0"
M.UI_ID = "kulala://ui"
M.SCRATCHPAD_ID = "kulala://scratchpad"
M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt"
M.BODY_FILE = plugin_tmp_dir .. "/body.txt"
M.STATS_FILE = plugin_tmp_dir .. "/stats.json"
M.REQUEST_FILE = plugin_tmp_dir .. "/request.json"
M.COOKIES_JAR_FILE = plugin_tmp_dir .. "/cookies.txt"
M.SCRIPT_PRE_OUTPUT_FILE = plugin_tmp_dir .. "/pre-script-output.txt"
M.SCRIPT_POST_OUTPUT_FILE = plugin_tmp_dir .. "/post-script-output.txt"
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local CONFIG = require("kulala.config")
local JUMPS = require("kulala.jumps")
local Graphql = require("kulala.graphql")
local Logger = require("kulala.logger")
local ScriptsUtils = require("kulala.scripts.utils")
local ScriptsUtils = require("kulala.parser.scripts.utils")
local M = {}

M.setup = function(config)
Expand Down
Loading

0 comments on commit 6d6865c

Please sign in to comment.