@amqp-contract/worker 0.23.1 → 0.25.0
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 +41 -36
- package/dist/index.cjs +260 -188
- package/dist/index.d.cts +144 -105
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +144 -105
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +261 -190
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +618 -205
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# @amqp-contract/worker
|
|
2
2
|
|
|
3
|
-
**Type-safe AMQP worker for consuming messages using amqp-contract with
|
|
3
|
+
**Type-safe AMQP worker for consuming messages using amqp-contract with ResultAsync/Result error handling.**
|
|
4
4
|
|
|
5
5
|
[](https://github.com/btravers/amqp-contract/actions/workflows/ci.yml)
|
|
6
6
|
[](https://www.npmjs.com/package/@amqp-contract/worker)
|
|
7
7
|
[](https://www.npmjs.com/package/@amqp-contract/worker)
|
|
8
|
-
[](https://www.typescriptlang.org/)
|
|
9
9
|
[](https://opensource.org/licenses/MIT)
|
|
10
10
|
|
|
11
11
|
📖 **[Full documentation →](https://btravers.github.io/amqp-contract/api/worker)**
|
|
@@ -31,7 +31,7 @@ pnpm add @amqp-contract/worker
|
|
|
31
31
|
```typescript
|
|
32
32
|
import { TypedAmqpWorker, RetryableError } from "@amqp-contract/worker";
|
|
33
33
|
import type { Logger } from "@amqp-contract/core";
|
|
34
|
-
import {
|
|
34
|
+
import { ResultAsync } from "neverthrow";
|
|
35
35
|
import { contract } from "./contract";
|
|
36
36
|
|
|
37
37
|
// Optional: Create a logger implementation
|
|
@@ -43,21 +43,24 @@ const logger: Logger = {
|
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
// Create worker from contract with handlers (automatically connects and starts consuming)
|
|
46
|
-
const worker =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
.
|
|
55
|
-
|
|
46
|
+
const worker = (
|
|
47
|
+
await TypedAmqpWorker.create({
|
|
48
|
+
contract,
|
|
49
|
+
handlers: {
|
|
50
|
+
processOrder: ({ payload }) => {
|
|
51
|
+
console.log("Processing order:", payload.orderId);
|
|
52
|
+
|
|
53
|
+
// Your business logic here
|
|
54
|
+
return ResultAsync.fromPromise(
|
|
55
|
+
Promise.all([processPayment(payload), updateInventory(payload)]),
|
|
56
|
+
(error) => new RetryableError("Order processing failed", error),
|
|
57
|
+
).map(() => undefined);
|
|
58
|
+
},
|
|
56
59
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
urls: ["amqp://localhost"],
|
|
61
|
+
logger, // Optional: logs message consumption and errors
|
|
62
|
+
})
|
|
63
|
+
)._unsafeUnwrap();
|
|
61
64
|
|
|
62
65
|
// Worker is already consuming messages
|
|
63
66
|
|
|
@@ -96,19 +99,22 @@ Then use `RetryableError` in your handlers:
|
|
|
96
99
|
|
|
97
100
|
```typescript
|
|
98
101
|
import { TypedAmqpWorker, RetryableError } from "@amqp-contract/worker";
|
|
99
|
-
import {
|
|
100
|
-
|
|
101
|
-
const worker =
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
102
|
+
import { ResultAsync } from "neverthrow";
|
|
103
|
+
|
|
104
|
+
const worker = (
|
|
105
|
+
await TypedAmqpWorker.create({
|
|
106
|
+
contract,
|
|
107
|
+
handlers: {
|
|
108
|
+
processOrder: ({ payload }) =>
|
|
109
|
+
// If this fails with RetryableError, message is automatically retried
|
|
110
|
+
ResultAsync.fromPromise(
|
|
111
|
+
processPayment(payload),
|
|
112
|
+
(error) => new RetryableError("Payment failed", error),
|
|
113
|
+
).map(() => undefined),
|
|
114
|
+
},
|
|
115
|
+
urls: ["amqp://localhost"],
|
|
116
|
+
})
|
|
117
|
+
)._unsafeUnwrap();
|
|
112
118
|
```
|
|
113
119
|
|
|
114
120
|
See the [Error Handling and Retry](https://btravers.github.io/amqp-contract/guide/worker-usage#error-handling-and-retry) section in the guide for complete details.
|
|
@@ -119,23 +125,22 @@ You can define handlers outside of the worker creation using `defineHandler` and
|
|
|
119
125
|
|
|
120
126
|
## Error Handling
|
|
121
127
|
|
|
122
|
-
Worker handlers return `
|
|
128
|
+
Worker handlers return `ResultAsync<void, HandlerError>` for explicit error handling:
|
|
123
129
|
|
|
124
130
|
```typescript
|
|
125
131
|
import { RetryableError, NonRetryableError } from "@amqp-contract/worker";
|
|
126
|
-
import {
|
|
132
|
+
import { errAsync, ResultAsync } from "neverthrow";
|
|
127
133
|
|
|
128
134
|
handlers: {
|
|
129
135
|
processOrder: ({ payload }) => {
|
|
130
136
|
// Validation errors - non-retryable
|
|
131
137
|
if (payload.amount <= 0) {
|
|
132
|
-
return
|
|
138
|
+
return errAsync(new NonRetryableError("Invalid amount"));
|
|
133
139
|
}
|
|
134
140
|
|
|
135
141
|
// Transient errors - retryable
|
|
136
|
-
return
|
|
137
|
-
.
|
|
138
|
-
.mapError((error) => new RetryableError("Processing failed", error));
|
|
142
|
+
return ResultAsync.fromPromise(process(payload), (error) => new RetryableError("Processing failed", error))
|
|
143
|
+
.map(() => undefined);
|
|
139
144
|
},
|
|
140
145
|
}
|
|
141
146
|
```
|