|
| 1 | +# Standards for cookbook examples |
| 2 | + |
| 3 | +Everything here is published code that strangers copy into their own projects. These rules keep the examples |
| 4 | +consistent and safe to copy. New examples start by copying `examples/_template/`. |
| 5 | + |
| 6 | +## Structure |
| 7 | + |
| 8 | +- One example per directory under `examples/`. Self-contained: it runs from a fresh clone with no steps |
| 9 | + beyond the ones in its own README. |
| 10 | +- The `name` in `package.json` matches the directory name. |
| 11 | +- Declare a Node floor in `engines` wherever an example has a `package.json`. The repo floor is `.nvmrc`. |
| 12 | +- An example with no `package.json` has no machine-checkable floor, so its README must not document flags |
| 13 | + that depend on a specific runtime version. Use plain environment variables instead. |
| 14 | + |
| 15 | +## Credentials |
| 16 | + |
| 17 | +- Read keys from the environment. Never hard-code one, never print one, never commit one. |
| 18 | +- Ship a `.env.example` with placeholder values. Next.js examples use `.env.local.example`, because that is |
| 19 | + the file Next loads. |
| 20 | +- Gitignore the real env file. |
| 21 | + |
| 22 | +## Formatting and linting |
| 23 | + |
| 24 | +- `.prettierrc` at the root governs JavaScript, TypeScript, JSON, CSS and Markdown. `ruff.toml` governs |
| 25 | + Python. |
| 26 | +- Every example with a `package.json` exposes `npm run format`. |
| 27 | +- CI runs `prettier --check` and `ruff check` on every pull request. Run both locally first. |
| 28 | + |
| 29 | +## Failure behaviour |
| 30 | + |
| 31 | +Expected failures — no API key set, a rejected key, a network error — print one line the reader can act on |
| 32 | +and exit non-zero. A stack trace on any of those paths is a bug. The missing-key path is the most common |
| 33 | +first-run experience an example has, so it deserves the most care. |
| 34 | + |
| 35 | +## README shape |
| 36 | + |
| 37 | +The same order in every example, so a reader can skim any of them: |
| 38 | + |
| 39 | +1. What it does, in a sentence or two, with a link to the companion guide where one exists |
| 40 | +2. Requirements |
| 41 | +3. Setup |
| 42 | +4. Run |
| 43 | +5. What happens when you do |
| 44 | + |
| 45 | +Prefer fewer claims to more. Every version number, flag and command in a README is a promise somebody has to |
| 46 | +keep true. |
| 47 | + |
| 48 | +## Before you open a pull request |
| 49 | + |
| 50 | +Whoever opens the pull request has read and run the code in it. Before asking for review: |
| 51 | + |
| 52 | +- Run the example from a fresh clone with no credentials set, then with a wrong key, and read what it prints. |
| 53 | +- Copy every command out of your README and run it, in the order written. |
| 54 | +- Run the formatter and the linter. |
| 55 | +- Where the same flow ships in two languages, diff them. A disagreement means one of the two is wrong. |
0 commit comments