Compare commits
6
Commits
3b02734aa9
...
fa788d17da
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa788d17da | ||
|
|
4877ea1754 | ||
|
|
6eaa519059 | ||
|
|
06869e5f01 | ||
|
|
c7f4a35008 | ||
|
|
44c783439c |
No files matched your search
@@ -141,8 +141,36 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
before they boot or bind. Migrate by dropping `tls` from `createApp` and configuring TLS on the
|
||||
runtime's server, or terminating it at a proxy. An app without `tls` is unaffected.
|
||||
|
||||
- **`provide()` and `run()` must return an object holding the tokens they provide, and a handler may
|
||||
not return a `Response`.** Both mistakes used to pass silently:
|
||||
- **Providers and steps.** A provider that returned its value bare (`return client` instead of
|
||||
`return { db: client }`) booted. `db` was then `undefined` in every handler, and the client's own
|
||||
properties were spread over the request context. A step that forgot its `return` did the same per
|
||||
request. Now a provider that breaks the contract fails the boot, and a step fails the request. The
|
||||
error names the node and the fix: `provider 'db' returned an object with 'query', but it must return
|
||||
an object holding what it provides: return { db: <value> }`. Plugin nodes added with `scope.add` are
|
||||
held to the same rule for every token in their `provides`. A key whose value is `undefined` still
|
||||
counts (`return { cache: undefined }`).
|
||||
- **Handlers.** A handler that returned a Fetch `Response` was JSON-encoded and went out as `200 {}`,
|
||||
losing its status, headers and body. It now fails the request with a `TypeError` that says what to
|
||||
do instead.
|
||||
|
||||
**Migrate:**
|
||||
- Return `{ <token>: value }` from every `provide()` and `run()`.
|
||||
- Return a plain value from a handler, use `@Transformer` to set the status and headers, or throw an
|
||||
`HttpError` for an error status.
|
||||
|
||||
Code that already followed the documented contract is unaffected.
|
||||
|
||||
### Added
|
||||
|
||||
- **`llms.txt` ships in the package** (`node_modules/@green-tea/core/llms.txt`, and in the JSR
|
||||
package). It is a guide for LLM coding agents, which have no training data on green-tea and fall back
|
||||
on Express habits. It covers the mental model, a complete app, the decorators and built-in tokens,
|
||||
how responses are returned, the options agents usually need, the common mistakes with their correct
|
||||
form, testing, and how to inspect an app before editing it. Every code block typechecks against this
|
||||
release. Projects scaffolded by matcha point their `AGENTS.md` at it.
|
||||
|
||||
- **Every type a public signature uses can now be imported.** These were already in the
|
||||
declarations, but nothing exported them, so code could call the API and could not name what it
|
||||
passed or got back. From the root: `ModuleOptions`, `Ctor`, `BusLike`, `ArgSource`,
|
||||
|
||||
@@ -318,6 +318,7 @@ green-tea is **beta**, on the road to a release candidate. Express and Fastify h
|
||||
## Docs & development
|
||||
|
||||
- **[green-tea.expressive-tea.io/docs](https://green-tea.expressive-tea.io/docs)** — getting started, concepts (the graph, `flow`), and guides: routing, DI, validation, uploads, streaming, HTML, security, errors, introspection, OpenAPI, plugins, mesh, runtimes, testing. Its source lives in [green-tea-docs](https://github.com/Expressive-Tea/green-tea-docs).
|
||||
- **For LLM coding agents:** [`llms.txt`](./llms.txt) ships in the package (`node_modules/@green-tea/core/llms.txt`) and matches the installed version; the docs serve the whole site as one file at [`/docs/llms-full.txt`](https://green-tea.expressive-tea.io/docs/llms-full.txt).
|
||||
- **[matcha](https://github.com/Expressive-Tea/matcha)** — the CLI. `matcha new` scaffolds a project (Node, Deno, or Bun), `matcha create` generates and auto-wires controllers/steps/providers/modules, `matcha run` detects your runtime and watches. A standalone Rust binary — no JS runtime needed to install it.
|
||||
|
||||
> **Changing public API? The docs are a separate repository now, and nothing tells you when you break them.**
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
"./edge": "./src/edge.ts"
|
||||
},
|
||||
"publish": {
|
||||
"include": ["src", "README.md", "CHANGELOG.md", "LICENSE", "deno.json"]
|
||||
"include": ["src", "llms.txt", "README.md", "CHANGELOG.md", "LICENSE", "deno.json"]
|
||||
},
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
# green-tea
|
||||
|
||||
> green-tea (`@green-tea/core`) is a TypeScript HTTP framework for Node >= 22, Deno, Bun and Cloudflare Workers in which the request pipeline is a dependency graph, not a middleware chain. Providers (built once at boot) and steps (run per request) declare the tokens they `provides` and `needs`; green-tea derives the execution order, validates the wiring when the app is created, and runs only the part of the graph each route's handler asks for. Handlers declare their inputs with parameter decorators and return plain values. It is not Express, NestJS, Fastify or Hono: there is no `app.use`, no `next()`, no `req`/`res` to mutate, and no constructor injection. This file ships with the package and matches the installed version; read it before writing green-tea code.
|
||||
|
||||
## Mental model
|
||||
|
||||
- The app is a graph of tokens (strings). A `@Provider({ provides: 'db' })` builds an app-scope singleton once, at boot. A `@Step({ provides: 'user', needs: ['db', 'req'] })` runs per request.
|
||||
- Order is derived from `needs`/`provides` by a topological sort. Never order things by hand.
|
||||
- A handler declares its dependencies in its signature: `getUser(@needs('user') user, @param('id') id)`. The signature is the contract.
|
||||
- A route runs only the transitive closure of its handler's `@needs`. A step nothing on that route needs does not run.
|
||||
- The graph is validated by `createApp`: a needed token with no producer throws `handler 'x' needs 'usr' but nothing (local or mesh) provides it — did you mean 'user'?`.
|
||||
- One producer per token: two nodes providing the same token fail `createApp`. A node cannot need the token it provides (`cycle detected`).
|
||||
- `provide()` and `run(ctx)` return an object keyed by the token (`{ db }`, `{ user }`). It is merged into one per-request context object. Throw to stop the request.
|
||||
- No `app.use`, no `next()`, no `req.user = ...`, no `res.status().json()`. Return a value; throw an `HttpError` for an error status.
|
||||
- Classes are instantiated by the framework with no constructor arguments, once. Dependencies arrive through `ctx` (providers/steps) or argument decorators (handlers).
|
||||
|
||||
## Setup
|
||||
|
||||
`npm install @green-tea/core@beta` (every release, RCs included, publishes under the `beta` dist-tag until stable; optional peers: `ws` for `@Ws` on Node, `busboy` for multipart uploads). In `tsconfig.json` set `"experimentalDecorators": true`. `emitDecoratorMetadata` is not needed, and core imports `reflect-metadata` itself.
|
||||
|
||||
## Minimal complete app
|
||||
|
||||
```ts
|
||||
import { createApp, Module, Provider, Step, Route, Get, Post, needs, param, body, Unauthorized, NotFound } from '@green-tea/core';
|
||||
|
||||
type User = { id: string; name: string };
|
||||
|
||||
@Provider({ provides: 'db' })
|
||||
class Database {
|
||||
provide() {
|
||||
const users: Record<string, User> = { u1: { id: 'u1', name: 'Diego' } };
|
||||
return { db: { find: (id: string): User | null => users[id] ?? null } }; // keyed by the token
|
||||
}
|
||||
}
|
||||
|
||||
@Step({ provides: 'user', needs: ['db', 'req'] })
|
||||
class Authenticate {
|
||||
run(ctx: any) {
|
||||
const user = ctx.db.find(ctx.headers['x-token']);
|
||||
if (!user) throw new Unauthorized('invalid token'); // stops the request -> 401
|
||||
return { user }; // merged into ctx; continues
|
||||
}
|
||||
}
|
||||
|
||||
@Route('/users')
|
||||
class UserController {
|
||||
@Get('/me')
|
||||
me(@needs('user') user: User) { return user; } // 200 JSON; static /me wins over /:id
|
||||
|
||||
@Get('/:id')
|
||||
byId(@param('id') id: string, @needs('db') db: { find(id: string): User | null }) {
|
||||
const found = db.find(id);
|
||||
if (!found) throw new NotFound(`no user ${id}`);
|
||||
return found;
|
||||
}
|
||||
|
||||
@Post('/')
|
||||
create(@body() input: { name: string }) { return { created: input.name }; }
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/api', providers: [Database], steps: [Authenticate], controllers: [UserController] })
|
||||
class ApiModule {}
|
||||
|
||||
export const app = createApp({ modules: [ApiModule] });
|
||||
```
|
||||
|
||||
Serve it. Each entry point boots providers before binding, so a provider that fails rejects the call:
|
||||
|
||||
```ts
|
||||
await app.listen(3000); // Node: always await; rejects on EADDRINUSE or a failed provider
|
||||
import { serveDeno } from '@green-tea/core/deno';
|
||||
await serveDeno(app, { port: 8000 }); // Deno (async since beta.2: await it)
|
||||
import { serveBun } from '@green-tea/core/bun';
|
||||
await serveBun(app, { port: 3000 }); // Bun (async: await it)
|
||||
import { edgeHandler } from '@green-tea/core/edge';
|
||||
export default { fetch: edgeHandler(app) }; // Cloudflare Workers; needs compatibility_flags = ["nodejs_compat"]
|
||||
```
|
||||
|
||||
Use one entry point per file. `mountpoint: ''` mounts at the root. A controller is served only when listed in a module's `controllers`. `await app.boot()` boots without serving; `await app.close()` drains and runs teardown. The edge has no `listen()`, no filesystem (`static`, `@Html` file modes), no mesh and no teardown.
|
||||
|
||||
## Cheat sheet
|
||||
|
||||
### Class decorators
|
||||
|
||||
| Decorator | Signature | Notes |
|
||||
|---|---|---|
|
||||
| `@Module` | `Module({ mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[] })` | `mountpoint` is required |
|
||||
| `@Route` | `Route(prefix: string)` | marks a controller. There is no `@Controller` |
|
||||
| `@Provider` | `Provider({ provides: string; needs?: string[]; optional?: boolean })` | class has `provide(ctx)`, optional `dispose()`. No `export` option |
|
||||
| `@Step` | `Step({ provides: string; needs?: string[]; optional?: boolean; export?: boolean })` | class has `run(ctx)`. `export` is mesh-only |
|
||||
|
||||
### Method decorators
|
||||
|
||||
| Decorator | Signature | Transport |
|
||||
|---|---|---|
|
||||
| `@Get @Post @Put @Patch @Delete @Head @Options` | `(path: string, opts?: { duplicates?: 'array' \| 'last'; maxBodyBytes?: number; maxParts?: number; export?: boolean })` | buffered: must return a value |
|
||||
| `@Sse` | same | GET, `text/event-stream`: must return an `AsyncIterable` |
|
||||
| `@Stream` | same | GET, negotiated: SSE (`Accept: text/event-stream`), WebSocket (`Upgrade`), else ndjson |
|
||||
| `@Ws` | same | GET, WebSocket duplex: must return an `AsyncIterable` |
|
||||
| `@Transformer` | `Transformer(fn: TransformerFn)` | custom status/headers/body for one handler |
|
||||
| `@Html` | bare `@Html`, or `Html(path: string, opts?: { template?: boolean })` | buffered GET/POST only; not combinable with `@Transformer` |
|
||||
|
||||
Paths: `/:id`, constrained `/:id(\\d+)`, catch-all `/:path*`. Unmatched path -> 404, wrong method -> 405, a buffered GET answers HEAD, OPTIONS is automatic.
|
||||
|
||||
### Argument decorators
|
||||
|
||||
| Decorator | Injects | Forms |
|
||||
|---|---|---|
|
||||
| `@needs(key)` | a graph token (boot-validated edge) | `('user')` |
|
||||
| `@ctx()` | the whole context (`ctx.ip`, `ctx.protocol`, tokens) | `()` |
|
||||
| `@param` | route params | `()` `('id')` `('id', schema)` |
|
||||
| `@query` | parsed query string (values are strings) | `()` `('q')` `(['a','b'])` `(schema)` |
|
||||
| `@body` | parsed JSON / urlencoded / multipart body | `()` `('field')` `(schema)` |
|
||||
| `@headers` / `@header` | request headers, lower-case keys; lookup is case-insensitive | `()` `('authorization')` `(['a','b'])` `(schema)` / `('x-trace')` `('x-count', schema)` |
|
||||
| `@cookies` / `@cookie` | parsed `Cookie` header; `@cookie` is `undefined` when absent | `()` `('sid')` `(['a','b'])` `(schema)` / `('sid')` `('sid', schema)` |
|
||||
| `@inbound()` | incoming WebSocket messages (`AsyncIterable`) | `()` |
|
||||
| `@abort()` | `AbortSignal` that fires on disconnect | `()` |
|
||||
|
||||
### Tokens
|
||||
|
||||
- A handler may `@needs` any produced token plus the request tokens `req`, `params`, `query`, `body`, `headers`, `cookies`, `inbound`, `abort`.
|
||||
- A provider's or step's `needs` may list produced tokens, `req` and `params` only. `needs: ['headers']` (or `query`, `body`, `cookies`, `ip`) fails `createApp` with `missing dependency: headers needed by <step>`. Declare `needs: ['req']` and read `ctx.headers`, `ctx.query`, `ctx.body`, `ctx.params`, `ctx.req`, `ctx.ip`, `ctx.protocol`. A step does not get `ctx.cookies`: use `parseCookies(ctx.headers.cookie)`.
|
||||
- Built-in tokens: `logger` (the app `Logger`: `debug/info/warn/error(message, fields?)`), `rooms` (shared `Rooms`: `room<T>(name)` returns a multicast `Channel<T>`), `events` (read-only `{ on(event, listener) }` of the lifecycle bus).
|
||||
- Reserved names: `logger`, `rooms`, `events`, `bus`. Providing one fails boot. `@needs('bus')` fails boot; use `@needs('events')` or a plugin.
|
||||
|
||||
### What `provide()` and `run()` return
|
||||
|
||||
- `provide(ctx)` runs once at boot (may be async). `ctx` holds the values of its `needs`. It must return `{ [provides]: value }`. Returning anything else fails the boot (`provider 'db' returned …, but it must return … { db: <value> }`); a step's `run()` that does so fails the request.
|
||||
- `run(ctx)` runs per request (may be async). Return `{ [provides]: value }` to continue; throw to short-circuit. The returned object is `Object.assign`ed into the shared context.
|
||||
- Optional `dispose()` on a provider is awaited by `close()`, in reverse boot order (not on the edge).
|
||||
- `optional: true` on a provider: a failing boot is logged and degrades instead of aborting (`app.degraded()`).
|
||||
- One instance per class for the app's life: never keep per-request state on `this` in a step.
|
||||
|
||||
## Returning responses
|
||||
|
||||
- Plain value (object, array, string, number, `null`) -> `200`, JSON-encoded. A returned string is sent as a JSON string; use `@Html` for HTML.
|
||||
- `undefined` (no `return`) -> `204`, no body.
|
||||
- Never return a `Response`: it fails the request with a `TypeError`. Return a plain value; use `@Transformer` for status and headers.
|
||||
- Errors are thrown, not returned: `Unauthorized(msg?)` 401, `NotFound(msg?)` 404, `Redirect(location)` 302 + `Location`, `NotModified()` 304, `ValidationError` 422 (raised by schemas), `HttpError(status, message?, body?, headers?)` any status. Anything else -> 500 `{"error":"Internal Server Error"}`; the message is not leaked. Default body: `{"error": message}`, or `body` when given.
|
||||
- Check errors with `isHttpError(e)` / `isValidationError(e)`, not `instanceof` (fails across two copies of core).
|
||||
- Custom rendering of every error response (including 404/405/413): `createApp({ onError(error, req) { return { status, headers, body } | undefined } })`.
|
||||
- Header values are `string | string[]`. A `set-cookie` array is sent as one line per cookie; other arrays are joined with `, `.
|
||||
|
||||
```ts
|
||||
import { Route, Post, Transformer, setCookie, HttpError, type TransformerFn } from '@green-tea/core';
|
||||
|
||||
const created: TransformerFn = (value) => ({
|
||||
status: 201,
|
||||
headers: { 'content-type': 'application/json', 'set-cookie': [setCookie('session', 'abc', { maxAge: 3600 }), setCookie('theme', 'dark', { httpOnly: false })] },
|
||||
body: JSON.stringify(value),
|
||||
});
|
||||
|
||||
@Route('/auth')
|
||||
class Sessions {
|
||||
@Post('/login') @Transformer(created)
|
||||
login() { return { ok: true }; } // -> 201 with two Set-Cookie lines
|
||||
|
||||
@Post('/logout')
|
||||
logout() { throw new HttpError(401, 'signed out', undefined, { 'set-cookie': setCookie('session', '', { maxAge: 0 }) }); }
|
||||
}
|
||||
```
|
||||
|
||||
`setCookie(name, value, options?)` defaults to `HttpOnly; SameSite=Lax; Path=/; Secure` and throws on unsafe input. Pass `secure: false` for plain-HTTP dev hosts the browser will not trust. Sessions and signing are not in core.
|
||||
|
||||
## Validation, streaming, HTML
|
||||
|
||||
Pass any Standard Schema (zod >= 3.24, valibot, arktype) to `@body`, `@query`, `@headers`, `@param`. The handler gets the parsed/coerced output; failure -> `422 {"error":"Validation failed","source":"body","issues":[{"path","message"}]}`. `@param` takes the key first: `@param('id', IdSchema)`. Steps always see the raw `ctx.body`/`ctx.query`.
|
||||
|
||||
```ts
|
||||
import { z } from 'zod';
|
||||
import { Route, Post, Get, Sse, Ws, body, query, needs, inbound, header, sse, Rooms } from '@green-tea/core';
|
||||
|
||||
const CreateUser = z.object({ email: z.string().email() });
|
||||
const Page = z.object({ page: z.coerce.number() });
|
||||
|
||||
@Route('/live')
|
||||
class Live {
|
||||
@Post('/users')
|
||||
create(@body(CreateUser) user: { email: string }) { return { created: user.email }; }
|
||||
|
||||
@Get('/list')
|
||||
list(@query(Page) q: { page: number }) { return { page: q.page }; }
|
||||
|
||||
@Sse('/ticks') // each yield is one SSE event
|
||||
ticks(@header('last-event-id') from?: string) {
|
||||
return (async function* () {
|
||||
for (let n = Number(from ?? 0) + 1; n <= 3; n++) yield sse({ n }, { id: String(n) });
|
||||
})();
|
||||
}
|
||||
|
||||
@Ws('/room/:name') // consume @inbound, return the outbound iterable
|
||||
join(@needs('rooms') rooms: Rooms, @needs('params') p: { name: string }, @inbound() incoming: AsyncIterable<string>) {
|
||||
const room = rooms.room<string>(p.name);
|
||||
(async () => { for await (const msg of incoming) room.push(msg); })();
|
||||
return room;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- `channel<T>({ buffer? })` is a multicast `AsyncIterable` with `push`, `close`, `fail` (bounded, drop-oldest). Yield plain values for bare `data:` frames; `sse(data, { id, event, retry })` adds fields.
|
||||
- The decorator decides the transport, never the return type: an iterable from `@Get`, or a value from `@Sse`/`@Ws`, is a 500 `TransportMismatchError`.
|
||||
- A step that throws `Unauthorized` on a WebSocket route closes the socket with `4000 + status` (4401).
|
||||
- `@Html` returns a string as `text/html`; `@Html('page.html')` serves a file (relative to `views`, default `process.cwd()`); `@Html('page.html', { template: true })` renders it with the returned data (`{{ }}` engine, or `viewEngine`). `static: true` serves `./public`. No filesystem modes on the edge.
|
||||
|
||||
## createApp options agents commonly need
|
||||
|
||||
```ts
|
||||
const app = createApp({
|
||||
modules: [ApiModule], // required
|
||||
security: true, // default: nosniff, X-Frame-Options, Referrer-Policy, HSTS on TLS; false or SecurityOptions
|
||||
cors: { origins: ['https://app.example.com'], credentials: true }, // origins: string | string[] | '*' | (origin) => boolean
|
||||
trustProxy: false, // true: X-Forwarded-* feed ctx.ip / ctx.protocol
|
||||
limits: { maxBodyBytes: 1_000_000, requestTimeoutMs: 30_000, maxConcurrentRequests: 0 },
|
||||
health: true, // GET/HEAD /__live__ and /__ready__
|
||||
compression: false, // true or { threshold, encodings: ['br', 'gzip'] }
|
||||
etag: false, // weak ETag + 304 for buffered GET/HEAD
|
||||
static: false, // true -> ./public, a dir string, or { dir, cacheControl }
|
||||
logger: undefined, // any { debug, info, warn, error }; default structured JSON
|
||||
logRequests: false, // one log line per request
|
||||
handleSignals: false, // true: SIGINT/SIGTERM run close() and exit
|
||||
overrides: {}, // tests: replace a token's value or runner
|
||||
});
|
||||
```
|
||||
|
||||
Also: `plugins`, `hooks: [{ onShutdown }]`, `onError`, `shutdownTimeoutMs` (default 10000), `teardownTimeoutMs`, `devGraph` (`GET /__graph__`, dev only), `devOpenapi`, `bodyDuplicates`, `views`, `viewEngine`, `warnGraphDepth`. `tls: { key, cert }` works only with Node's `app.listen()`; `serveDeno()` and `serveBun()` reject an app that sets it. Use the platform or a proxy for TLS there.
|
||||
|
||||
To run something on every route regardless of `@needs` (global guard, audit), add a plugin step that provides nothing:
|
||||
|
||||
```ts
|
||||
import { Unauthorized, type Plugin } from '@green-tea/core';
|
||||
|
||||
export const apiKey: Plugin = {
|
||||
name: 'api-key',
|
||||
mount(api) {
|
||||
api.scope.add({ kind: 'step', name: 'apiKey', needs: ['req'], provides: [], run: (ctx) => {
|
||||
if (ctx.headers['x-api-key'] !== process.env.API_KEY) throw new Unauthorized('bad key');
|
||||
return {};
|
||||
} });
|
||||
},
|
||||
};
|
||||
// createApp({ modules: [ApiModule], plugins: [apiKey] })
|
||||
```
|
||||
|
||||
A plugin gets only `bus.on`, `scope.add` and `onShutdown`; it cannot reorder or remove other nodes.
|
||||
|
||||
## Common mistakes
|
||||
|
||||
1. `app.use(authMiddleware)` -> a `@Step({ provides: 'user', needs: ['req'] })` that handlers `@needs('user')`, or a plugin step with `provides: []` for every route.
|
||||
2. Calling `next()` -> `run(ctx)` returns `{ token: value }` to continue and throws to stop.
|
||||
3. `req.user = user` -> `return { user }` from the step, and `@needs('user') user` in the handler.
|
||||
4. `res.status(201).json(x)`, `res.cookie()`, `return new Response(...)` -> return the value, plus `@Transformer(fn)` for status/headers/`setCookie()`. A returned `Response` is sent as `{}`.
|
||||
5. `provide() { return new Pool() }` -> `provide() { return { db: new Pool() } }`. The bare value fails the boot.
|
||||
6. NestJS constructor injection (`constructor(private db: Db)`) -> nothing is injected. Use `needs` + `ctx.db` in providers/steps, `@needs('db')` in handlers.
|
||||
7. `@Step({ needs: ['headers'] })` (or `query`, `body`, `cookies`) -> boot error. Use `needs: ['req']` and read `ctx.headers`. For cookies: `parseCookies(ctx.headers.cookie)`.
|
||||
8. Expecting an auth step to protect every route -> it runs only where its token is needed. Guard with `@needs('user')` on each protected handler, or a `provides: []` plugin step.
|
||||
9. `@Controller('/users')` -> `@Route('/users')`, listed in a `@Module({ controllers: [...] })`.
|
||||
10. Returning an async generator from `@Get` -> declare `@Sse`, `@Stream` or `@Ws`. Returning a value from `@Sse` fails too.
|
||||
11. Returning an HTML string from `@Get` -> sent as a JSON string. Add `@Html`.
|
||||
12. `@needs('bus')` or providing `logger`/`rooms`/`events` -> boot error. Use `@needs('events')`, `@needs('logger')`, `@needs('rooms')`.
|
||||
13. Two classes `provides: 'user'` -> boot error. One producer per token; rename one, or use `overrides` in tests.
|
||||
14. `e instanceof HttpError` -> `isHttpError(e)`.
|
||||
15. `app.listen(3000)` without `await`, or `serveDeno(app)` without `await` -> boot and bind failures go unhandled. Always await.
|
||||
16. `@param(IdSchema)` -> `@param('id', IdSchema)`.
|
||||
17. Multipart `@body('title')` is `undefined` -> `@body() form: MultipartBody`, then `form.fields.title` / `form.files.avatar`.
|
||||
18. Inventing a token in `@needs('x')` -> check `matcha tokens` or `app.graph()` first; unknown tokens fail boot.
|
||||
|
||||
## Testing
|
||||
|
||||
No port needed: `app.fetch(new Request(...))` returns a `Response` on every runtime.
|
||||
|
||||
```ts
|
||||
import { createApp } from '@green-tea/core';
|
||||
|
||||
const test = createApp({
|
||||
modules: [ApiModule],
|
||||
overrides: {
|
||||
db: { find: () => ({ id: 't', name: 'Test' }) }, // plain value: wrapped as { db: value }
|
||||
user: () => ({ user: { id: 'stub' } }), // a function is the runner: return { token: value }
|
||||
},
|
||||
});
|
||||
const res = await test.fetch(new Request('http://test/api/users/me'));
|
||||
// res.status === 200; await res.json() -> { id: 'stub' }
|
||||
```
|
||||
|
||||
- An override must name an existing token; an unknown one throws on `boot()` or the first `fetch`, not in `createApp`.
|
||||
- A function value is treated as a runner. To override a token whose value is a function, wrap it: `{ hash: () => ({ hash: fakeHash }) }`.
|
||||
|
||||
## Inspect an existing app before editing it
|
||||
|
||||
```ts
|
||||
app.explain('/api/users/me');
|
||||
// { pattern, method: 'GET', transport: 'buffer', chain: [{ name, kind: 'provider'|'step'|'handler', origin, needs, provides }, ...] }
|
||||
app.graph(); // { nodes: [{ name, kind, origin, needs, provides }], routes: [{ pattern, method, transport, chain: string[] }] }
|
||||
app.toMermaid(); // or app.toDOT()
|
||||
```
|
||||
|
||||
`explain` takes the declared pattern (`/users/:id`, not `/users/42`) and throws `no route: ...` on a miss. `graph().nodes` includes the built-ins (`origin: 'builtin'`).
|
||||
|
||||
With matcha (the CLI; `routes`, `tokens` and `--json` need a matcha that lists them in `matcha --help`) run from the project root. It imports `export const app` from `src/app.ts` (override with `--entry`) and calls `app.ready()`, so providers are not booted:
|
||||
|
||||
```bash
|
||||
matcha routes --json # every route: method, pattern, transport, chain
|
||||
matcha tokens --json # every token: who provides it, who needs it
|
||||
matcha explain /users/:id --json # one route's chain, in execution order
|
||||
matcha graph --format json # the raw GraphView
|
||||
```
|
||||
|
||||
Run `matcha tokens` before adding `@needs('x')`: if nothing provides `x`, you must add the provider or step too. Request tokens (`req`, `params`, `body`, ...) are listed as coming from the request. Other generators: `matcha new`, `matcha create controller|step|provider|module|plugin <Name>` (wires the class into its module), `matcha add sse|stream|buffer`.
|
||||
|
||||
## Stability
|
||||
|
||||
- The public API of `@green-tea/core`, `/deno`, `/bun` and `/edge` is frozen from RC.1: between RC.1 and stable, fixes only. Check `### Breaking` in CHANGELOG.md.
|
||||
- Mesh (`mesh` option, `connectLink`, `MeshConfig`, teapot/teacup) is alpha and `@experimental`, gated by `createApp({ experimental: true })`. Do not use it unless the user asks.
|
||||
- The `Headers` type is deprecated; import `RequestHeaders` instead.
|
||||
|
||||
## Links
|
||||
|
||||
- Docs: https://green-tea.expressive-tea.io/docs/
|
||||
- Full docs in one file: https://green-tea.expressive-tea.io/docs/llms-full.txt
|
||||
- Source: https://github.com/Expressive-Tea/green-tea
|
||||
@@ -79,6 +79,7 @@
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"llms.txt",
|
||||
"assets",
|
||||
"README.md",
|
||||
"CHANGELOG.md",
|
||||
|
||||
+58
-3
@@ -567,6 +567,52 @@ function emptyRegistry(): Registry {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a provider's `provide()` or a step's `run()` so its result is checked against the contract:
|
||||
* an object holding every token the node provides. Without this, `return client` instead of
|
||||
* `return { db: client }` booted fine. `db` was then `undefined`, and the client's own properties
|
||||
* were spread over every request's context. The check stays synchronous for a synchronous node,
|
||||
* so a plain step pays a type test rather than an extra microtask on the hot path.
|
||||
*/
|
||||
function keyedRunner(kind: string, name: string, provides: readonly string[], run: Runner): Runner {
|
||||
const fail = (output: unknown): never => {
|
||||
const example = provides.map((token) => `${token}: <value>`).join(', ');
|
||||
throw new TypeError(
|
||||
`${kind} '${name}' returned ${describeOutput(output)}, but it must return an object holding what it ` +
|
||||
`provides: return { ${example} }`,
|
||||
);
|
||||
};
|
||||
|
||||
// A @Provider or @Step provides one token: test it directly rather than through `every` and its
|
||||
// callback, which measured as a 3-4% drop on a 3-step pipeline under Deno and Bun.
|
||||
const [only] = provides;
|
||||
const check =
|
||||
provides.length === 1
|
||||
? (output: unknown): unknown =>
|
||||
output !== null && typeof output === 'object' && only! in output ? output : fail(output)
|
||||
: (output: unknown): unknown =>
|
||||
output !== null && typeof output === 'object' && provides.every((token) => token in output)
|
||||
? output
|
||||
: fail(output);
|
||||
|
||||
return (ctx) => {
|
||||
const output = run(ctx);
|
||||
return output instanceof Promise ? output.then(check) : check(output);
|
||||
};
|
||||
}
|
||||
|
||||
function describeOutput(output: unknown): string {
|
||||
if (output === null || output === undefined) return String(output);
|
||||
if (typeof output !== 'object') return `a ${typeof output}`;
|
||||
const keys = Object.keys(output);
|
||||
return keys.length
|
||||
? `an object with ${keys
|
||||
.slice(0, 5)
|
||||
.map((k) => `'${k}'`)
|
||||
.join(', ')}`
|
||||
: 'an empty object';
|
||||
}
|
||||
|
||||
/** Registers a module's `@Provider` classes as app-scope provider nodes. */
|
||||
function collectProviders(providers: Ctor[], origin: string, registry: Registry): void {
|
||||
for (const ProviderClass of providers) {
|
||||
@@ -585,7 +631,10 @@ function collectProviders(providers: Ctor[], origin: string, registry: Registry)
|
||||
);
|
||||
const instance: any = new ProviderClass();
|
||||
registry.providerInstances.set(meta.provides, instance);
|
||||
registry.setRunner(meta.provides, (ctx) => instance.provide(ctx));
|
||||
registry.setRunner(
|
||||
meta.provides,
|
||||
keyedRunner('provider', meta.provides, [meta.provides], (ctx) => instance.provide(ctx)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -596,7 +645,10 @@ function collectSteps(steps: Ctor[], origin: string, registry: Registry): void {
|
||||
registry.stepNodes.push({ name: meta.provides, needs: meta.needs, provides: [meta.provides], origin });
|
||||
if (meta.export) registry.exportedSteps.push(meta.provides);
|
||||
const instance: any = new StepClass();
|
||||
registry.setRunner(meta.provides, (ctx) => instance.run(ctx));
|
||||
registry.setRunner(
|
||||
meta.provides,
|
||||
keyedRunner('step', meta.provides, [meta.provides], (ctx) => instance.run(ctx)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -854,7 +906,10 @@ function mountPlugins(plugins: Plugin[] | undefined, bus: Bus, registry: Registr
|
||||
const node = { name: scopeNode.name, needs: scopeNode.needs, provides: scopeNode.provides, origin: 'plugin' };
|
||||
if (scopeNode.kind === 'provider') registry.providerNodes.push(node);
|
||||
else registry.stepNodes.push(node);
|
||||
registry.setRunner(scopeNode.name, scopeNode.run);
|
||||
registry.setRunner(
|
||||
scopeNode.name,
|
||||
keyedRunner(`plugin ${scopeNode.kind}`, scopeNode.name, scopeNode.provides, scopeNode.run),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -132,6 +132,16 @@ export async function runPipeline(args: {
|
||||
try {
|
||||
context = await runSteps(steps, context, bus, correlation);
|
||||
const result = await handler(context);
|
||||
|
||||
// A handler returns a value and a transformer shapes the response. A Fetch `Response` is not a
|
||||
// value: JSON-encoded, it went out as `200 {}`, with its status, headers and body all lost.
|
||||
if (typeof Response !== 'undefined' && result instanceof Response) {
|
||||
throw new TypeError(
|
||||
'a handler returned a Response object, which green-tea does not send as is. Return a plain value ' +
|
||||
'(sent as JSON), use @Transformer to set the status and headers, or throw an HttpError for an error status',
|
||||
);
|
||||
}
|
||||
|
||||
const streaming = isAsyncIterable(result);
|
||||
const contract = TRANSPORT_RETURN[transport];
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ test('CARD-17 T1: /__ready__ 503 booting while a slow provider boots, /__live__
|
||||
class SlowDb {
|
||||
async provide() {
|
||||
await new Promise<void>((resolve) => (finish = resolve));
|
||||
return {};
|
||||
return { slowDb: {} };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Ctl], providers: [SlowDb] })
|
||||
|
||||
@@ -30,7 +30,7 @@ Deno.test('CARD-17 T1: /__ready__ 503 booting while a slow provider boots, /__li
|
||||
class SlowDb {
|
||||
async provide() {
|
||||
await new Promise<void>((resolve) => (finish = resolve));
|
||||
return {};
|
||||
return { slowDb: {} };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Ctl], providers: [SlowDb] })
|
||||
|
||||
+2
-2
@@ -51,7 +51,7 @@ describe('CARD-17: health probes through app.fetch', () => {
|
||||
class SlowDb {
|
||||
async provide() {
|
||||
await new Promise<void>((resolve) => (finish = resolve));
|
||||
return {};
|
||||
return { slowDb: {} };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Ctl], providers: [SlowDb] })
|
||||
@@ -76,7 +76,7 @@ describe('CARD-17: health probes through app.fetch', () => {
|
||||
class Watched {
|
||||
provide() {
|
||||
started = true;
|
||||
return {};
|
||||
return { watched: {} };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Ctl], providers: [Watched] })
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* The contracts a node and a handler are held to, enforced rather than assumed. `provide()` and
|
||||
* `run()` return an object holding every token they provide; a handler returns a value, not a
|
||||
* Fetch `Response`. Before RC.1, breaking either was silent: the token came out `undefined` (with
|
||||
* the returned object's own properties spread over the request context), or the response went
|
||||
* out as `200 {}`.
|
||||
*/
|
||||
import 'reflect-metadata';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createApp, Module, Provider, Route, Get, Step, needs } from '../src/index';
|
||||
import type { Plugin } from '../src/plugin';
|
||||
|
||||
const quiet = { debug() {}, info() {}, warn() {}, error() {} };
|
||||
|
||||
/** Captures the error a request failed with, and answers 500 with its message. */
|
||||
function capture() {
|
||||
const seen: { error?: unknown } = {};
|
||||
const onError = (error: unknown) => {
|
||||
seen.error = error;
|
||||
return { status: 500, headers: {}, body: JSON.stringify({ message: (error as Error).message }) };
|
||||
};
|
||||
return { seen, onError };
|
||||
}
|
||||
|
||||
describe('provide() must return an object keyed by its token', () => {
|
||||
it('a provider returning the bare value fails the boot, naming the fix', async () => {
|
||||
@Provider({ provides: 'db' })
|
||||
class Db {
|
||||
provide() {
|
||||
return { query: () => [] }; // the client itself, not { db: client }
|
||||
}
|
||||
}
|
||||
@Route('/')
|
||||
class C {
|
||||
@Get('/') get(@needs('db') db: unknown) {
|
||||
return { db: db !== undefined };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [Db], controllers: [C] })
|
||||
class M {}
|
||||
|
||||
const app = createApp({ modules: [M], logger: quiet });
|
||||
await expect(app.boot()).rejects.toThrow(
|
||||
"provider 'db' returned an object with 'query', but it must return an object holding what it provides: return { db: <value> }",
|
||||
);
|
||||
});
|
||||
|
||||
it('an async provider resolving to a non-object fails the boot too', async () => {
|
||||
@Provider({ provides: 'config' })
|
||||
class Config {
|
||||
async provide() {
|
||||
return 'production';
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [Config] })
|
||||
class M {}
|
||||
|
||||
await expect(createApp({ modules: [M], logger: quiet }).boot()).rejects.toThrow(
|
||||
"provider 'config' returned a string",
|
||||
);
|
||||
});
|
||||
|
||||
it('a keyed provider whose value is undefined is still keyed, and boots', async () => {
|
||||
@Provider({ provides: 'cache' })
|
||||
class Cache {
|
||||
provide() {
|
||||
return { cache: undefined };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [Cache] })
|
||||
class M {}
|
||||
|
||||
await expect(createApp({ modules: [M], logger: quiet }).boot()).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('run() must return an object keyed by its token', () => {
|
||||
it('a step returning nothing fails the request with a message that names the step and the fix', async () => {
|
||||
@Step({ provides: 'user', needs: ['req'] })
|
||||
class Auth {
|
||||
run() {
|
||||
// forgot `return { user }`
|
||||
}
|
||||
}
|
||||
@Route('/')
|
||||
class C {
|
||||
@Get('/me') me(@needs('user') user: unknown) {
|
||||
return { user };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', steps: [Auth], controllers: [C] })
|
||||
class M {}
|
||||
|
||||
const { seen, onError } = capture();
|
||||
const res = await createApp({ modules: [M], logger: quiet, onError }).fetch(new Request('http://x/me'));
|
||||
expect(res.status).toBe(500);
|
||||
expect((seen.error as Error).message).toBe(
|
||||
"step 'user' returned undefined, but it must return an object holding what it provides: return { user: <value> }",
|
||||
);
|
||||
});
|
||||
|
||||
it('a step returning its token keyed works as before', async () => {
|
||||
@Step({ provides: 'user', needs: ['req'] })
|
||||
class Auth {
|
||||
run() {
|
||||
return { user: 'ana' };
|
||||
}
|
||||
}
|
||||
@Route('/')
|
||||
class C {
|
||||
@Get('/me') me(@needs('user') user: string) {
|
||||
return { user };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', steps: [Auth], controllers: [C] })
|
||||
class M {}
|
||||
|
||||
const res = await createApp({ modules: [M], logger: quiet }).fetch(new Request('http://x/me'));
|
||||
expect(await res.json()).toEqual({ user: 'ana' });
|
||||
});
|
||||
|
||||
it('a plugin node is held to the same contract for every token it provides', async () => {
|
||||
const plugin: Plugin = {
|
||||
name: 'audit',
|
||||
mount(api) {
|
||||
api.scope.add({ kind: 'step', name: 'audit', needs: [], provides: ['audit', 'auditId'], run: () => ({ audit: true }) });
|
||||
},
|
||||
};
|
||||
@Route('/')
|
||||
class C {
|
||||
@Get('/') get(@needs('auditId') id: unknown) {
|
||||
return { id };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [C] })
|
||||
class M {}
|
||||
|
||||
const { seen, onError } = capture();
|
||||
const res = await createApp({ modules: [M], plugins: [plugin], logger: quiet, onError }).fetch(new Request('http://x/'));
|
||||
expect(res.status).toBe(500);
|
||||
expect((seen.error as Error).message).toContain("plugin step 'audit' returned an object with 'audit'");
|
||||
expect((seen.error as Error).message).toContain('return { audit: <value>, auditId: <value> }');
|
||||
});
|
||||
});
|
||||
|
||||
describe('a handler returns a value, not a Response', () => {
|
||||
it('a returned Response fails the request instead of going out as 200 {}', async () => {
|
||||
@Route('/')
|
||||
class C {
|
||||
@Get('/') get() {
|
||||
return new Response('created', { status: 201 });
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [C] })
|
||||
class M {}
|
||||
|
||||
const { seen, onError } = capture();
|
||||
const res = await createApp({ modules: [M], logger: quiet, onError }).fetch(new Request('http://x/'));
|
||||
expect(res.status).toBe(500);
|
||||
expect((seen.error as Error).message).toContain('a handler returned a Response object');
|
||||
expect((seen.error as Error).message).toContain('@Transformer');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user