Show HN: Yapi – 开源终端 API 客户端,面向高级用户
Show HN: Yapi – FOSS terminal API client for power users

原始链接: https://yapi.run/blog/what-is-yapi

## Yapi:强大的API测试工具 Yapi 是一款新的开源命令行工具,专为需要直接从终端测试 API 的高级用户设计。它支持 HTTP、gRPC、TCP 和 GraphQL(未来计划支持更多协议),允许进行全面的测试,包括跨不同协议的请求链。 主要功能包括:内置集成测试,带有期望和断言;语言服务器协议 (LSP) 用于 IDE 集成(目前支持 Neovim,计划支持 VSCode);以及 GitHub Actions 支持用于 CI/CD。Yapi 还通过配置文件简化了多个环境(开发、预发布、生产)的管理。 目前处于早期 Alpha 阶段,Yapi 正在积极开发中,并欢迎用户通过 GitHub Issues 提供反馈。它被设计为高度可配置和可管道化,将 JSON 输出到 stdout,其他信息输出到 stderr。 您可以从其 GitHub 仓库安装 Yapi,并创建请求文件来定义和执行您的 API 测试,开始使用。欢迎贡献!

## Yapi:一个基于终端的API客户端 Jamiepond分享了**Yapi**,一个免费且开源(FOSS)的终端API客户端 ([https://github.com/jamierpond/yapi](https://github.com/jamierpond/yapi)),专为高级用户和熟悉`nvim`和`tmux`等工具的用户设计。Yapi作为Postman、Bruno和Insomnia等流行GUI客户端的替代品,旨在提高生产力。 自上次分享以来,该项目已经成熟。评论者Dhruv3006强调了他们自己的类似项目**Voiden**,它利用可执行markdown进行文档编写和测试,优先考虑标准合规性和单一事实来源,以防止文档漂移。 这两个项目都表明了对替代API客户端解决方案日益增长的兴趣,特别是那些重视可读代码和CLI为中心的工作流程的解决方案。
相关文章

原文

Yapi is Postman, Insomnia or Bruno for the power user.

Yapi is an OSS command line tool that makes it easy to test APIs from your terminal. Yapi speaks HTTP, gRPC, TCP, GraphQL (and more coming soon).

yapi in action

Heads up! Yapi is early, early alpha software

I use yapi daily in my development workflow. However, yapi is a SUPER young project and will have bugs, missing features and rough edges.

If you download yapi, I would LOVE your feedback on how to make it better. Please open an issue if you have any suggestions or find any bugs!

Show me some examples!

POST

This request:


yapi: v1 
method: POST 
url: https://api.github.com/repos/jamierpond/yapi/issues

headers:
  Accept: application/vnd.github+json
  Authorization: Bearer ${GITHUB_PAT} 

body: 
  title: Help! yapi made me too productive.
  body: Now I can't stop YAPPIN' about yapi!

expect: 
  status: 201
  assert: 
    - .body == "Now I can't stop YAPPIN' about yapi!"

Gives you this response:

yapi run create-issue.yapi.yml
{
  "active_lock_reason": null,
  "assignee": null,
  "assignees": [],
  "author_association": "OWNER",
  "body": "Now I can't stop YAPPIN' about yapi!\n",
  
}

URL: https:
Time: 579.408625ms
Size: 2.3 kB (1 lines, 2288 chars)

[PASS] status check
[PASS] .body == "Now I can't stop YAPPIN' about yapi!"

(Only the JSON goes to stdout, the rest goes to stderr, so is pipeable!)

Yapi supports chaining requests between protocols

Multi-protocol chaining

Yapi makes it easy to chain requests and share data between them, even if they are different protocols.


yapi: v1
chain:
  - name: get_todo 
    url: https://jsonplaceholder.typicode.com/todos/1
    method: GET

  - name: grpc_hello 
    url: grpc://grpcb.in:9000
    service: hello.HelloService
    rpc: SayHello 
    plaintext: true

    body:
      greeting: $get_todo.title 

    expect:
      assert:
        - .reply == "hello delectus aut autem" 

Run this example in the yapi playground

Integration Testing with yapi

Yapi has built-in support for writing integration tests with expectations and assertions.

yapi: v1
method: GET
url: https://api.github.com/repos/jamierpond/yapi/issues/5
headers:
    Accept: application/vnd.github+json
expect:
    status: 200
    assert:
      - .state == "closed"
      - .closed_by.login == "jamierpond"

Run this example in the yapi playground

Yapi has an LSP Server for IDE Integration

Yapi comes with a Language Server Protocol (LSP) server that provides syntax highlighting, autocompletion and validation for yapi request files in editors that support LSP (Neovim, etc), you can also use the yapi Neovim plugin (still early days).

At some point I'll write the VSCode extension too, please make an issue if you think this is important!

GitHub Actions Support

I use yapi's GitHub Action to run integraion tests on the CI for this blog!

name: Integration Tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm ci

      - name: Build Application
        run: npm build

      - name: Run Yapi Integration Tests
        uses: jamierpond/yapi/[email protected]
        with:
          start: npm run start
          wait-on: http://localhost:3000/health
          command: yapi test ./tests -a

For example, here is the output of yapi running integration tests in GitHub Actions: yapi in GitHub Actions

Supports for Multiple Environments

Yapi make it easy to manage multiple environments (dev, staging, prod, etc). Define your environments in a yapi.config.yml file:

yapi: v1

default_environment: local

environments:
  local:
    url: http://localhost:3000
    env_file: .env.local
    vars:
      some_param: default_value

  prod:
    url: https://yapi.run
    env_file: .env.prod
    vars:
      some_param: some_value

Then run yapi with the desired environment:

yapi run my-request.yapi.yml --env prod

This also cleans up your request files a little, now you can use paths instead of full URLs:

yapi: v1
method: GET
path: /api/v1/status 

Getting Started with Yapi

To get started with yapi, simply install it using the instructions on the yapi GitHub repository and start creating your first request files!

Contributing to Yapi

Yapi is an open-source project maintained by just me, Jamie. If you find bugs or have feature requests, please open an issue on the yapi GitHub repository. Pull requests are very welcome too!

联系我们 contact @ memedata.com