feat: end-to-end example demonstrating the four core ideas
This commit is contained in:
3 files changed
+70
No files matched your search
@@ -0,0 +1,11 @@
|
||||
````markdown
|
||||
# green-tea example
|
||||
|
||||
```bash
|
||||
npx ts-node example/server.ts # or: npm run build && node dist/example/server.js
|
||||
curl -H 'x-token: u1' http://localhost:3000/api/users/9
|
||||
```
|
||||
|
||||
Demonstrates: typed context contract, graph-derived order, `app.inspect()`,
|
||||
and a logger plugin that observes the bus without touching the chain.
|
||||
````
|
||||
@@ -0,0 +1,43 @@
|
||||
import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin } from '../src/index';
|
||||
|
||||
interface User { id: string; name: string }
|
||||
|
||||
@Provider({ provides: 'db' })
|
||||
class Database {
|
||||
provide() {
|
||||
const users: Record<string, User> = { u1: { id: 'u1', name: 'Diego' } };
|
||||
return { db: { find: (token: string) => users[token] ?? null } };
|
||||
}
|
||||
}
|
||||
|
||||
@Step({ provides: 'user', needs: ['db', 'req'] })
|
||||
class Authenticate {
|
||||
run(ctx: { db: { find: (t: string) => User | null }; req: { headers: Record<string, string> } }) {
|
||||
const user = ctx.db.find(ctx.req.headers['x-token']);
|
||||
if (!user) throw new Unauthorized('invalid token');
|
||||
return { user };
|
||||
}
|
||||
}
|
||||
|
||||
@Route('/users')
|
||||
class UserController {
|
||||
@Get('/:id')
|
||||
@Transformer(JsonTransformer)
|
||||
getUser(ctx: { user: User; params: { id: string } }) {
|
||||
return { requested: ctx.params.id, you: ctx.user };
|
||||
}
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/api', providers: [Database], steps: [Authenticate], controllers: [UserController] })
|
||||
class ApiModule {}
|
||||
|
||||
const logger: Plugin = (api) => {
|
||||
api.bus.on('request:step:enter', (p) => console.log(`-> ${p.name}`));
|
||||
};
|
||||
|
||||
export const app = createApp({ modules: [ApiModule], plugins: [logger] });
|
||||
|
||||
if (require.main === module) {
|
||||
console.log('chain for /api/users/:id ->', app.inspect('/api/users/:id'));
|
||||
app.listen(3000).then(() => console.log('green-tea on http://localhost:3000'));
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { app } from '../example/server';
|
||||
|
||||
test('example: inspect shows graph-ordered chain', () => {
|
||||
expect(app.inspect('/api/users/:id').map((l) => `${l.kind}:${l.name}`))
|
||||
.toEqual(['provider:db', 'step:user', 'handler:getUser']);
|
||||
});
|
||||
|
||||
test('example: authenticated request returns the user', async () => {
|
||||
const server = await app.listen(0);
|
||||
const port = (server.address() as any).port;
|
||||
const res = await fetch(`http://127.0.0.1:${port}/api/users/9`, { headers: { 'x-token': 'u1' } });
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toEqual({ requested: '9', you: { id: 'u1', name: 'Diego' } });
|
||||
server.close();
|
||||
});
|
||||
Reference in new issue
Block a user