@venturekit/runtime 0.0.0-dev.20260308002709 → 0.0.0-dev.20260310105525
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -0
- package/package.json +4 -4
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @venturekit/runtime
|
|
2
|
+
|
|
3
|
+
> **Warning:** This package is in active development and not production-ready. APIs may change without notice.
|
|
4
|
+
|
|
5
|
+
Runtime utilities for [VentureKit](https://venturekit.dev) Lambda functions — handlers, context, middleware, logging, errors, and WebSocket support.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @venturekit/runtime@dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
`@venturekit/runtime` provides everything needed to write VentureKit route handlers:
|
|
16
|
+
|
|
17
|
+
- **Unified handler** that adapts to context (public vs authenticated)
|
|
18
|
+
- **Request context** with typed user, tenant, and request metadata
|
|
19
|
+
- **Response helpers** (`success`, `created`, `noContent`, `error`, `redirect`)
|
|
20
|
+
- **Structured errors** with HTTP status codes
|
|
21
|
+
- **Composable middleware** (logging, CORS, timeout, error boundary)
|
|
22
|
+
- **Structured logging** via Pino
|
|
23
|
+
- **WebSocket connection store** for real-time applications
|
|
24
|
+
|
|
25
|
+
## Handler
|
|
26
|
+
|
|
27
|
+
The `handler()` function is the primary API. It wraps your business logic with auth checks, body parsing, status code detection, middleware, and error handling.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { handler } from '@venturekit/runtime';
|
|
31
|
+
|
|
32
|
+
// Public endpoint (no scopes = no auth)
|
|
33
|
+
export const main = handler(async (_body, ctx, logger) => {
|
|
34
|
+
logger.info('Health check');
|
|
35
|
+
return { status: 'healthy', timestamp: ctx.timestamp.toISOString() };
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Authenticated endpoint (scopes = auth required)
|
|
39
|
+
export const main = handler(async (body, ctx, logger) => {
|
|
40
|
+
return { id: '123', name: body.name };
|
|
41
|
+
}, { scopes: ['api.write'] });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Status Code Detection
|
|
45
|
+
|
|
46
|
+
| HTTP Method | Default Status |
|
|
47
|
+
|-------------|---------------|
|
|
48
|
+
| `GET`, `PUT`, `PATCH` | `200 OK` |
|
|
49
|
+
| `POST` | `201 Created` |
|
|
50
|
+
| `DELETE` | `204 No Content` |
|
|
51
|
+
|
|
52
|
+
Override with `{ status: 200 }` in the handler config.
|
|
53
|
+
|
|
54
|
+
### Transactional Handlers
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
export const main = handler(async (body, ctx, logger) => {
|
|
58
|
+
// ctx.tx is a database transaction — auto-commits on success, rolls back on error
|
|
59
|
+
await ctx.tx.query('INSERT INTO tasks (title) VALUES ($1)', [body.title]);
|
|
60
|
+
return { created: true };
|
|
61
|
+
}, { scopes: ['tasks.write'], transactional: true });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Context
|
|
65
|
+
|
|
66
|
+
Every handler receives a `RequestContext`:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
interface RequestContext {
|
|
70
|
+
requestId: string;
|
|
71
|
+
timestamp: Date;
|
|
72
|
+
method: string;
|
|
73
|
+
path: string;
|
|
74
|
+
sourceIp: string;
|
|
75
|
+
userAgent: string;
|
|
76
|
+
user: UserContext | null; // Populated for authenticated requests
|
|
77
|
+
tenant: TenantContext | null; // Populated when @venturekit/tenancy is enabled
|
|
78
|
+
locale: string;
|
|
79
|
+
queryParams?: Record<string, string | undefined>;
|
|
80
|
+
tx?: unknown; // Database transaction (transactional handlers)
|
|
81
|
+
intentOutputs?: Record<string, unknown>; // Infrastructure intent outputs
|
|
82
|
+
rawEvent: APIGatewayProxyEventV2;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Errors
|
|
87
|
+
|
|
88
|
+
Throw structured errors in your handlers — they are automatically serialized to JSON responses:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { NotFoundError, BadRequestError, ForbiddenError } from '@venturekit/runtime';
|
|
92
|
+
|
|
93
|
+
throw new NotFoundError('Task', '123'); // 404
|
|
94
|
+
throw new BadRequestError('Invalid input'); // 400
|
|
95
|
+
throw new ForbiddenError(); // 403
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Available error classes: `BadRequestError`, `UnauthorizedError`, `ForbiddenError`, `NotFoundError`, `ConflictError`, `ValidationError`, `RateLimitError`, `InternalError`, `ServiceUnavailableError`.
|
|
99
|
+
|
|
100
|
+
## Middleware
|
|
101
|
+
|
|
102
|
+
Compose middleware for cross-cutting concerns:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { compose, loggingMiddleware, corsMiddleware, timeoutMiddleware } from '@venturekit/runtime';
|
|
106
|
+
|
|
107
|
+
export const main = handler(async (body, ctx, logger) => {
|
|
108
|
+
return { ok: true };
|
|
109
|
+
}, {
|
|
110
|
+
middleware: [
|
|
111
|
+
corsMiddleware({ allowOrigins: ['*'], allowMethods: ['GET'], allowHeaders: ['*'], allowCredentials: false, maxAge: 3600 }),
|
|
112
|
+
timeoutMiddleware(5000),
|
|
113
|
+
],
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## WebSocket
|
|
118
|
+
|
|
119
|
+
For real-time applications, use the `connectionStore`:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { connectionStore } from '@venturekit/runtime';
|
|
123
|
+
|
|
124
|
+
await connectionStore.save(connectionId);
|
|
125
|
+
await connectionStore.authenticate(connectionId, { userId, email, tenantId });
|
|
126
|
+
await connectionStore.sendToUser(domainName, stage, userId, data);
|
|
127
|
+
await connectionStore.broadcast(domainName, stage, data);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## API Reference
|
|
131
|
+
|
|
132
|
+
See the [API reference](https://venturekit.dev/api-reference/runtime) for full documentation.
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
Apache-2.0 — see [LICENSE](../../LICENSE) for details.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venturekit/runtime",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.20260310105525",
|
|
4
4
|
"description": "VentureKit runtime utilities - handlers, context, middleware, logging",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,11 +24,11 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@venturekit/core": "0.0.0-dev.
|
|
27
|
+
"@venturekit/core": "0.0.0-dev.20260310105525",
|
|
28
28
|
"pino": "^9.0.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@venturekit/data": "0.0.0-dev.
|
|
31
|
+
"@venturekit/data": "0.0.0-dev.20260310105525",
|
|
32
32
|
"@aws-sdk/client-dynamodb": "^3.500.0",
|
|
33
33
|
"@aws-sdk/lib-dynamodb": "^3.500.0",
|
|
34
34
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.500.0"
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@venturekit/data": "0.0.0-dev.
|
|
51
|
+
"@venturekit/data": "0.0.0-dev.20260310105525",
|
|
52
52
|
"@types/aws-lambda": "^8.10.131",
|
|
53
53
|
"@types/node": "^20.10.0",
|
|
54
54
|
"typescript": "^5.3.0"
|