#http-request #requests #http-server #script #dsl #env-var #running

bin+lib rested

用于轻松定义和运行对 HTTP 服务器请求的编程语言/解释器

12 个版本 (7 个重大更改)

0.11.0 2024 年 6 月 20 日
0.10.1 2024 年 5 月 2 日
0.8.0 2023 年 9 月 28 日

#712 in 网络编程

Download history 124/week @ 2024-04-26 174/week @ 2024-05-03 4/week @ 2024-05-17 2/week @ 2024-05-24 203/week @ 2024-06-14 59/week @ 2024-06-21

每月 861 次下载

GPL-2.0 许可证

220KB
6K SLoC

crates.io npm version

rested (实验性)

用于轻松定义和运行对 HTTP 服务器请求的编程语言/解释器。

为什么?

为了在开发过程中轻松测试 API,而 Postman 的体验很慢。作为一名专业编辑文本文件的人,似乎也有必要为这种用例提供一个 DSL。使用 shell 脚本中的 curl 命令比在 GUI 中点击要好的多。许多开发者已经通过这种方法取得了巨大成功,这很强大,因为 Linux(文件管道)很强大。但仍然可以通过 DSL 来简化。因此进行了这个实验。

演示

https://github.com/Gnarus-G/rested/assets/37311893/b03e5d14-658c-4086-8075-f518d6e8b6a1

安装 (命令行解释器)

crates.io

cargo install rested

或从 npmjs.com

npm install -g rstd

用法

Language/Interpreter for easily defining and running requests to an http server.

Usage: rstd [OPTIONS] <COMMAND>

Commands:
  run         Run a script written in the language
  fmt         Format a script written in the language
  scratch     Open your default editor to start editing a temporary file
  snap        Generate a static snapshot of the requests with all dynamic values evaluated
  env         Operate on the environment variables available in the runtime. Looking into the `.env.rd.json` in the current directory, or that in the home directory
  completion  Generate a completions file for a specified shell
  lsp         Start the rested language server
  config      Configure, or view current configurations
  help        Print this message or the help of the given subcommand(s)

Options:
  -l, --level <LEVEL>  Set log level, one of trace, debug, info, warn, error [default: info]
  -h, --help           Print help
  -V, --version        Print version

编写一个脚本,例如

// assuming file name requests.rd
@log
get https://jsonplaceholder.typicode.com/todos/1

使用 CLI 运行它。

rstd run requests.rd

功能

全局常量

set BASE_URL "https://127.0.0.1:8080/api/v2"

设置 BASE_URL,如下,允许您只请求路径名

// This will make a request to "https://127.0.0.1:8080/api/v2/potatoes"
get /potatoes

变量绑定

let token = "<token>"

// template string literals
let bearer_token = `Bearer ${token}`

定义请求头和请求体

post /potatoes {
   header "Authorization" "Bearer token"

   // json expressions
   body json({
       neet: 1337,
       stuff: [1, true, "three"]
   })
}

读取环境变量

set BASE_URL env("base-url")

post /tomatoes {
   header "Authorization" env("auth-token")
   body env("data")
}

设置环境变量(CLI)

rstd env set <name> <value>

也可以对变量进行命名空间划分。

rstd env set <name> <value> -n <namespace>
Operate on the environment variables available in the runtime. Looking into the `.env.rd.json` in the current directory, or that in the home directory

Usage: rstd env [OPTIONS] <COMMAND>

Commands:
  show  View environment variables available in the runtime
  edit  Edit environment variables in your default editor
  set   Set environment variables available in the runtime
  ns    Operate on the variables namespaces available in the runtime
  help  Print this message or the help of the given subcommand(s)

Options:
      --cwd            Set to look at the `.env.rd.json` file in the current working directory. Otherwise this command and its subcommands operate on the `.env.rd.json` file in your home directory
  -l, --level <LEVEL>  Set log level, one of trace, debug, info, warn, error [default: info]
  -h, --help           Print help

读取文件

post /tomatoes {
   body read("data.json")
}

属性

// prints response body to stdout
@log
get /yams
// prints response body to a file
@log("output/yams.json")
get /yams

还有更多,但我觉得这些属性有点丢脸,所以我们到此为止。

Neovim 插件

使用 restedlang.nvim 进行语法高亮和 Intellisense 与 lsp

依赖项

~19–31MB
~492K SLoC