@apisyouwonthate/style-guide

中文文档
1.5.0发布于 2023/06/26MITTypeScriptESM + CJS近 30 天下载 13.5万

Make your HTTP APIs better, faster, stronger, whether they are still being designed (API Design-First) or your organization has flopped various mismatched APIs into production and now you're thinking some consistency would be nice. Using Spectral and Open

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Backstory
  4. 🎉 Thanks
  5. 📜 License
  6. 🌲 Sponsor

APIs You Won't Hate: API Style Guide

Make your APIs "better" according to APIs You Won't Hate, with this slick command line tool that'll run on your OpenAPI.

Installation

npm install --save -D @apisyouwonthate/style-guide
npm install --save -D @stoplight/spectral-cli

Usage

Create a local ruleset that extends the style guide. In its most basic form this just tells Spectral what ruleset you want to use, but it will allow you to customise things, add your own rules, turn bits off if its causing trouble.

cd ~/src/<your-api>

echo 'extends: ["@apisyouwonthate/style-guide"]' > .spectral.yaml

If you're using VS Code or Stoplight Studio then the NPM modules will not be available. Instead you can use the CDN hosted version:

echo 'extends: ["https://unpkg.com/@apisyouwonthate/[email protected]/dist/ruleset.js"]' > .spectral.yaml

Note: You need to use the full URL with CDN hosted rulesets because Spectral cannot follow redirects through extends.

Next, use Spectral CLI to lint against your OpenAPI description. Don't have any OpenAPI? Record some HTTP traffic to make OpenAPI and then you can switch to API Design-First going forwards.

spectral lint api/openapi.yaml

You should see some output like this, letting you know there are a few more standards you could be using (shout-out to Standards.REST):

/Users/phil/src/protect-earth-api/api/openapi.yaml
  18:7   warning  api-health               Creating a `/health` endpoint is a simple solution for pull-based monitoring and manually checking the status of an API.  paths
  18:7   warning  api-home                 Stop forcing all API consumers to visit documentation for basic interactions when the API could do that itself.           paths
  36:30  warning  no-unknown-error-format  Every error response SHOULD support either RFC 7807 (https://tools.ietf.org/html/rfc7807) or the JSON:API Error format.   paths./v1/orders.post.responses[401].content.application/json
  96:30  warning  no-unknown-error-format  Every error response SHOULD support either RFC 7807 (https://tools.ietf.org/html/rfc7807) or the JSON:API Error format.   paths./v1/orders/{order}.get.responses[401].content.application/json
 112:30  warning  no-unknown-error-format  Every error response SHOULD support either RFC 7807 (https://tools.ietf.org/html/rfc7807) or the JSON:API Error format.   paths./v1/orders/{order}.get.responses[404].content.application/json

Now you have some things to work on for your API. Thankfully these are only warnings, which are not going to fail continuous integration (unless you want them to).

Backstory

You could write your API Style Guide as a giant manifesto and hope people see it, or you could automate your API style guide using a tool like Spectral so that your API Style Guide is enforced at the pull request level. This is an integral part of any successful API Governance program, otherwise you're all just wasting time covering the basics far too late in the game.

Spectral runs on top of OpenAPI and AsyncAPI, powering linting in editors, as a CLI tool, in continuous integration, etc., and comes with its own set of baked in OpenAPI v2/v3 rules. Making rules about how to write OpenAPI can help beginners write better OpenAPI, but its real power is using rules to make the actual APIs better and more consistent, before there is any programming involved.

This NPM package brings together all sorts of advice found in the books and blog posts from APIs You Won't Hate books. If you apply this to APIs in production, this is basically Phil Sturgeon judging your API for free instead of doing paid consulting. But if you can get this API Style Guide involved in the API Design-First workflow, you're getting free advice on how to design a better API before you waste any time coding, which then means fewer backwards-compatibility breaks as you fix things.

There are a bunch of other rulesets you can check out if you feel like making your own API Style Guides, or feel like contributing some new rules here via a pull request.

🎉 Thanks

📜 License

This repository is licensed under the MIT license.

🌲 Sponsor

If you'd like to say thanks for this style guide, throw some money at Protect Earth, a charity that plants trees all over the United Kingdom, which has secured a 64 acre ancient woodland to restore. Phil spends all his time on this, both planting trees and writing APIs believe it or not!

版本与发布记录

@apisyouwonthate/style-guide 共发布 19 个版本,首次发布于 2022/06/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2023/06/263 年前
1.4.02022/12/293 年前
1.3.22022/10/244 年前
1.3.12022/10/154 年前
1.3.02022/09/234 年前
1.2.02022/09/234 年前

关于 @apisyouwonthate/style-guide 的常见问题

@apisyouwonthate/style-guide 是什么?

Make your HTTP APIs better, faster, stronger, whether they are still being designed (API Design-First) or your organization has flopped various mismatched APIs into production and now you're thinking some consistency would be nice. Using Spectral and Open

如何安装 @apisyouwonthate/style-guide?

在项目目录运行 npm install @apisyouwonthate/style-guide,或使用 pnpm add / yarn add / bun add。

@apisyouwonthate/style-guide 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apisyouwonthate/style-guide 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apisyouwonthate/style-guide 可以商用吗?

@apisyouwonthate/style-guide 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apisyouwonthate/style-guide 的下载量有多少?

根据 npm 官方下载统计,@apisyouwonthate/style-guide 在 2026/10/06 – 2026/11/04 这 30 天内被下载 135,418 次,日均约 4,514 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apisyouwonthate/style-guide 还在维护吗?

最新版本 1.5.0 发布于 2023/06/26(3 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。