Skip to content

Commit

Permalink
Merge pull request #62 from mistweaverco/feat/docs
Browse files Browse the repository at this point in the history
chore(docs): add magic-variables + formatting
  • Loading branch information
gorillamoe authored Jul 17, 2024
2 parents 6ebb042 + 0e12385 commit 8e1db36
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
39 changes: 39 additions & 0 deletions docs/docs/usage/automatic-response-formatting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Automatic Response Formatting

If you want to automatically format the response of an HTTP request,
you can use simply add an `accept` header with the desired format.

For example, if you want to receive the response in JSON format, you can add the `accept: application/json` header.

```http title="automatic-response-formatting.http"
POST https://httpbin.org/post HTTP/1.1
content-type: application/json
accept: application/json
{
"uuid": "{{$uuid}}",
"timestamp": "{{$timestamp}}",
"date": "{{$date}}",
"randomInt": "{{$randomInt}}",
}
```

**NOTE:** You need to have external tools to format the response.
For example, `jq` for JSON, `xmllint` for XML and HTML, etc.

### Supported Formats

- JSON: `application/json`
- XML: `application/xml`
- HTML: `text/html`

### Default formatters

```lua title="default-formatters.lua"
formatters = {
json = { "jq", "." },
xml = { "xmllint", "--format", "-" },
html = { "xmllint", "--format", "--html", "-" },
}
```
28 changes: 28 additions & 0 deletions docs/docs/usage/magic-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Magic Variables

There is a predefined set of magic variables that you can use in your HTTP requests.

They all start with a `$` sign.

- `{{$uuid}}` - Generates a random UUID.
- `{{$timestamp}}` - Generates a timestamp.
- `{{$date}}` - Generates a date (YYYY-MM-DD).
- `{{$randomInt}}` - Generates a random integer (between 0 and 9999999).

To test this feature, create a file with the `.http` extension and write your HTTP requests in it.

```http title="magic-variables.http"
POST https://httpbin.org/post HTTP/1.1
content-type: application/json
accept: application/json
{
"uuid": "{{$uuid}}",
"timestamp": "{{$timestamp}}",
"date": "{{$date}}",
"randomInt": "{{$randomInt}}",
}
###
```
14 changes: 1 addition & 13 deletions lua/kulala/parser/dynamic_vars.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
local FS = require("kulala.utils.fs")
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local STRING_UTILS = require("kulala.utils.string")
local M = {}

local random = math.random
Expand All @@ -18,14 +15,6 @@ local function uuid()
end)
end

local previous_response_body = function()
local previous_response = FS.read_file(GLOBALS.BODY_FILE)
if not previous_response then
return ""
end
return STRING_UTILS.trim(previous_response)
end

---Retrieve all dynamic variables from both rest.nvim and the ones declared by
---the user on his configuration
---@return { [string]: fun():string }[] An array-like table of tables which contains dynamic variables definition
Expand All @@ -37,9 +26,8 @@ function M.retrieve_all()
return os.date("%Y-%m-%d")
end,
["$timestamp"] = os.time,
["$previousResponseBody"] = previous_response_body,
["$randomInt"] = function()
return math.random(0, 1000)
return math.random(0, 9999999)
end,
}

Expand Down

0 comments on commit 8e1db36

Please sign in to comment.