@nimbus-cqrs/hono 2.0.0-beta.0 → 2.0.2
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 +127 -1
- package/esm/lib/middleware/correlationId.d.ts +1 -1
- package/esm/lib/middleware/correlationId.js +1 -1
- package/esm/lib/middleware/logger.d.ts +1 -1
- package/esm/lib/middleware/logger.js +2 -2
- package/esm/lib/onError.d.ts +1 -1
- package/esm/lib/onError.js +2 -2
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -5,10 +5,136 @@
|
|
|
5
5
|
|
|
6
6
|
# Nimbus Hono
|
|
7
7
|
|
|
8
|
-
Adapters and
|
|
8
|
+
Adapters and middleware that bridge [Hono](https://hono.dev/) and the Nimbus framework. The package gives your Hono app a correlation ID per request, structured request/response logging with OpenTelemetry tracing, and a single error handler that turns Nimbus exceptions into clean HTTP responses.
|
|
9
9
|
|
|
10
10
|
Refer to the [Nimbus main repository](https://github.com/overlap-dev/Nimbus) or the [Nimbus documentation](https://nimbus.overlap.at) for more information about the Nimbus framework.
|
|
11
11
|
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Deno
|
|
16
|
+
deno add jsr:@nimbus-cqrs/hono
|
|
17
|
+
|
|
18
|
+
# NPM
|
|
19
|
+
npm install @nimbus-cqrs/hono
|
|
20
|
+
|
|
21
|
+
# Bun
|
|
22
|
+
bun add @nimbus-cqrs/hono
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`hono` itself is a peer dependency — install it (or use one of the runtimes that ship it via `npm:`/`jsr:` specifiers).
|
|
26
|
+
|
|
27
|
+
# Examples
|
|
28
|
+
|
|
29
|
+
For detailed documentation, please refer to the [Nimbus documentation](https://nimbus.overlap.at).
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
A typical setup wires up all three pieces together: the correlation ID middleware first (so the logger and downstream handlers can read it), the logger second, your routes, and the error handler last.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { Hono } from 'hono';
|
|
37
|
+
import { correlationId, handleError, logger } from '@nimbus-cqrs/hono';
|
|
38
|
+
|
|
39
|
+
const app = new Hono();
|
|
40
|
+
|
|
41
|
+
app.use(correlationId());
|
|
42
|
+
app.use(logger({ enableTracing: true }));
|
|
43
|
+
|
|
44
|
+
app.get('/hello', (c) => c.json({ hello: 'world' }));
|
|
45
|
+
|
|
46
|
+
app.onError(handleError);
|
|
47
|
+
|
|
48
|
+
export default app;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## correlationId
|
|
52
|
+
|
|
53
|
+
`correlationId()` is a Hono middleware that ensures every request carries a stable correlation ID. It reads one from the incoming headers (`x-correlation-id`, `x-request-id` or `request-id`, in that order), or generates a fresh [ULID](https://github.com/ulid/spec) if none is present. The ID is stored on the Hono context and — by default — echoed back in the response as `x-correlation-id`.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { Hono } from 'hono';
|
|
57
|
+
import { correlationId, getCorrelationId } from '@nimbus-cqrs/hono';
|
|
58
|
+
|
|
59
|
+
const app = new Hono();
|
|
60
|
+
|
|
61
|
+
app.use(correlationId());
|
|
62
|
+
|
|
63
|
+
app.get('/whoami', (c) => {
|
|
64
|
+
const cid = getCorrelationId(c);
|
|
65
|
+
return c.json({ correlationId: cid });
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Use `getCorrelationId(c)` anywhere you have a Hono context (route handlers, downstream middleware, …) to forward the ID into log entries, outgoing requests, or messages you publish through Nimbus.
|
|
70
|
+
|
|
71
|
+
You can opt out of the response header or rename it:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
app.use(correlationId({
|
|
75
|
+
addToResponseHeaders: true,
|
|
76
|
+
responseHeaderName: 'x-trace-id',
|
|
77
|
+
}));
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## logger
|
|
81
|
+
|
|
82
|
+
`logger()` writes one structured log line when a request comes in and one when it leaves (with the elapsed time), using the Nimbus logger so every entry is automatically tagged with the current correlation ID.
|
|
83
|
+
|
|
84
|
+
When `enableTracing` is on (default), it also:
|
|
85
|
+
|
|
86
|
+
- extracts W3C trace context (`traceparent` / `tracestate`) from incoming headers, so spans created in your handlers stitch into the upstream trace,
|
|
87
|
+
- creates a server span named `HTTP <METHOD> <PATH>` for each request,
|
|
88
|
+
- records `http.method`, `url.path`, `http.target`, `http.status_code` and the correlation ID as span attributes,
|
|
89
|
+
- marks the span as errored on `5xx`/`4xx` responses or thrown exceptions.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { Hono } from 'hono';
|
|
93
|
+
import { correlationId, logger } from '@nimbus-cqrs/hono';
|
|
94
|
+
|
|
95
|
+
const app = new Hono();
|
|
96
|
+
|
|
97
|
+
app.use(correlationId());
|
|
98
|
+
app.use(logger({
|
|
99
|
+
enableTracing: true,
|
|
100
|
+
tracerName: 'api',
|
|
101
|
+
}));
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Set `enableTracing: false` if you only want the request/response log lines and don't run an OpenTelemetry SDK.
|
|
105
|
+
|
|
106
|
+
## handleError
|
|
107
|
+
|
|
108
|
+
`handleError` is a drop-in handler for `app.onError(...)`. It maps any `Exception` thrown anywhere in the request pipeline (a Nimbus core exception or one of its subclasses such as `NotFoundException`, `InvalidInputException`, `UnauthorizedException`, `ForbiddenException`, …) to a JSON response using the exception's `statusCode`, `name`, `message` and optional `details`. Anything else falls back to a generic `500 INTERNAL_SERVER_ERROR` and is logged at `critical` level.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { Hono } from 'hono';
|
|
112
|
+
import { NotFoundException } from '@nimbus-cqrs/core';
|
|
113
|
+
import { handleError } from '@nimbus-cqrs/hono';
|
|
114
|
+
|
|
115
|
+
const app = new Hono();
|
|
116
|
+
|
|
117
|
+
app.get('/todos/:id', (c) => {
|
|
118
|
+
throw new NotFoundException('Todo not found', { id: c.req.param('id') });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
app.onError(handleError);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
A request to `/todos/42` then responds with HTTP `404` and body:
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
{
|
|
128
|
+
"error": "NOT_FOUND",
|
|
129
|
+
"message": "Todo not found",
|
|
130
|
+
"details": {
|
|
131
|
+
"id": "42"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
This means your domain code can stay framework-agnostic — throw Nimbus exceptions where they belong (in command/query handlers) and the HTTP layer converts them consistently for you.
|
|
137
|
+
|
|
12
138
|
# License
|
|
13
139
|
|
|
14
140
|
Copyright 2024-present Overlap GmbH & Co KG (https://overlap.at)
|
|
@@ -33,7 +33,7 @@ export type CorrelationIdOptions = {
|
|
|
33
33
|
* @example
|
|
34
34
|
* ```ts
|
|
35
35
|
* import { Hono } from 'hono';
|
|
36
|
-
* import { correlationId, getCorrelationId } from '@nimbus/hono';
|
|
36
|
+
* import { correlationId, getCorrelationId } from '@nimbus-cqrs/hono';
|
|
37
37
|
*
|
|
38
38
|
* const app = new Hono();
|
|
39
39
|
* app.use(correlationId());
|
|
@@ -27,7 +27,7 @@ export const CORRELATION_ID_KEY = 'correlationId';
|
|
|
27
27
|
* @example
|
|
28
28
|
* ```ts
|
|
29
29
|
* import { Hono } from 'hono';
|
|
30
|
-
* import { correlationId, getCorrelationId } from '@nimbus/hono';
|
|
30
|
+
* import { correlationId, getCorrelationId } from '@nimbus-cqrs/hono';
|
|
31
31
|
*
|
|
32
32
|
* const app = new Hono();
|
|
33
33
|
* app.use(correlationId());
|
|
@@ -27,7 +27,7 @@ export type LoggerOptions = {
|
|
|
27
27
|
* @example
|
|
28
28
|
* ```ts
|
|
29
29
|
* import { Hono } from 'hono';
|
|
30
|
-
* import { logger } from '@nimbus/hono';
|
|
30
|
+
* import { logger } from '@nimbus-cqrs/hono';
|
|
31
31
|
*
|
|
32
32
|
* const app = new Hono();
|
|
33
33
|
* app.use(logger({ enableTracing: true }));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getLogger } from '
|
|
1
|
+
import { getLogger } from '@nimbus-cqrs/core';
|
|
2
2
|
import { context, propagation, SpanKind, SpanStatusCode, trace, } from '@opentelemetry/api';
|
|
3
3
|
import { getCorrelationId } from './correlationId.js';
|
|
4
4
|
const humanize = (times) => {
|
|
@@ -24,7 +24,7 @@ const time = (start) => {
|
|
|
24
24
|
* @example
|
|
25
25
|
* ```ts
|
|
26
26
|
* import { Hono } from 'hono';
|
|
27
|
-
* import { logger } from '@nimbus/hono';
|
|
27
|
+
* import { logger } from '@nimbus-cqrs/hono';
|
|
28
28
|
*
|
|
29
29
|
* const app = new Hono();
|
|
30
30
|
* app.use(logger({ enableTracing: true }));
|
package/esm/lib/onError.d.ts
CHANGED
package/esm/lib/onError.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Exception, getLogger } from '
|
|
1
|
+
import { Exception, getLogger } from '@nimbus-cqrs/core';
|
|
2
2
|
/**
|
|
3
3
|
* An error handler for Hono applications that maps
|
|
4
4
|
* Nimbus exceptions to HTTP responses and handles
|
|
@@ -9,7 +9,7 @@ import { Exception, getLogger } from '../deps/jsr.io/@nimbus/core/2.0.0/src/inde
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```ts
|
|
12
|
-
* import { handleError } from '@nimbus/hono';
|
|
12
|
+
* import { handleError } from '@nimbus-cqrs/hono';
|
|
13
13
|
*
|
|
14
14
|
* const app = new Hono();
|
|
15
15
|
* app.onError(handleError);
|
package/package.json
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimbus-cqrs/hono",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "Hono integration for the Nimbus
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "Simplify Event-Driven Applications - Hono integration for the Nimbus framework.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nimbus",
|
|
7
|
+
"cqrs",
|
|
8
|
+
"event-sourcing",
|
|
9
|
+
"event-driven",
|
|
10
|
+
"typescript",
|
|
11
|
+
"hono",
|
|
12
|
+
"http",
|
|
13
|
+
"middleware",
|
|
14
|
+
"api"
|
|
15
|
+
],
|
|
5
16
|
"author": "Daniel Gördes <d.goerdes@overlap.at> (https://overlap.at)",
|
|
6
17
|
"homepage": "https://nimbus.overlap.at",
|
|
7
18
|
"repository": {
|
|
@@ -26,7 +37,7 @@
|
|
|
26
37
|
"@opentelemetry/api": "^1.9.1",
|
|
27
38
|
"hono": "^4.12.14",
|
|
28
39
|
"zod": "^4.3.6",
|
|
29
|
-
"@nimbus-cqrs/core": "^2.0.
|
|
40
|
+
"@nimbus-cqrs/core": "^2.0.2"
|
|
30
41
|
},
|
|
31
42
|
"devDependencies": {
|
|
32
43
|
"@types/node": "^22.0.0"
|