@amqp-contract/worker 0.24.0 → 1.0.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 +18 -18
- package/dist/index.cjs +227 -159
- package/dist/index.d.cts +269 -88
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +269 -88
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +221 -153
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +499 -466
- package/package.json +18 -15
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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 AsyncResult/Result error handling.**
|
|
4
4
|
|
|
5
|
-
[](https://github.com/btravstack/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
8
|
[](https://www.typescriptlang.org/)
|
|
9
9
|
[](https://opensource.org/licenses/MIT)
|
|
10
10
|
|
|
11
|
-
📖 **[Full documentation →](https://
|
|
11
|
+
📖 **[Full documentation →](https://btravstack.github.io/amqp-contract/api/worker)**
|
|
12
12
|
|
|
13
13
|
## Installation
|
|
14
14
|
|
|
@@ -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 { fromPromise, type AsyncResult } from "unthrown";
|
|
35
35
|
import { contract } from "./contract";
|
|
36
36
|
|
|
37
37
|
// Optional: Create a logger implementation
|
|
@@ -51,7 +51,7 @@ const worker = (
|
|
|
51
51
|
console.log("Processing order:", payload.orderId);
|
|
52
52
|
|
|
53
53
|
// Your business logic here
|
|
54
|
-
return
|
|
54
|
+
return fromPromise(
|
|
55
55
|
Promise.all([processPayment(payload), updateInventory(payload)]),
|
|
56
56
|
(error) => new RetryableError("Order processing failed", error),
|
|
57
57
|
).map(() => undefined);
|
|
@@ -60,7 +60,7 @@ const worker = (
|
|
|
60
60
|
urls: ["amqp://localhost"],
|
|
61
61
|
logger, // Optional: logs message consumption and errors
|
|
62
62
|
})
|
|
63
|
-
).
|
|
63
|
+
).unwrap();
|
|
64
64
|
|
|
65
65
|
// Worker is already consuming messages
|
|
66
66
|
|
|
@@ -70,7 +70,7 @@ const worker = (
|
|
|
70
70
|
|
|
71
71
|
### Advanced Features
|
|
72
72
|
|
|
73
|
-
For advanced features like prefetch configuration and **automatic retry**, see the [Worker Usage Guide](https://
|
|
73
|
+
For advanced features like prefetch configuration and **automatic retry**, see the [Worker Usage Guide](https://btravstack.github.io/amqp-contract/guide/worker-usage).
|
|
74
74
|
|
|
75
75
|
#### Retry configuration
|
|
76
76
|
|
|
@@ -99,7 +99,7 @@ Then use `RetryableError` in your handlers:
|
|
|
99
99
|
|
|
100
100
|
```typescript
|
|
101
101
|
import { TypedAmqpWorker, RetryableError } from "@amqp-contract/worker";
|
|
102
|
-
import {
|
|
102
|
+
import { fromPromise, type AsyncResult } from "unthrown";
|
|
103
103
|
|
|
104
104
|
const worker = (
|
|
105
105
|
await TypedAmqpWorker.create({
|
|
@@ -107,39 +107,39 @@ const worker = (
|
|
|
107
107
|
handlers: {
|
|
108
108
|
processOrder: ({ payload }) =>
|
|
109
109
|
// If this fails with RetryableError, message is automatically retried
|
|
110
|
-
|
|
110
|
+
fromPromise(
|
|
111
111
|
processPayment(payload),
|
|
112
112
|
(error) => new RetryableError("Payment failed", error),
|
|
113
113
|
).map(() => undefined),
|
|
114
114
|
},
|
|
115
115
|
urls: ["amqp://localhost"],
|
|
116
116
|
})
|
|
117
|
-
).
|
|
117
|
+
).unwrap();
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
-
See the [Error Handling and Retry](https://
|
|
120
|
+
See the [Error Handling and Retry](https://btravstack.github.io/amqp-contract/guide/worker-usage#error-handling-and-retry) section in the guide for complete details.
|
|
121
121
|
|
|
122
122
|
## Defining Handlers Externally
|
|
123
123
|
|
|
124
|
-
You can define handlers outside of the worker creation using `defineHandler` and `defineHandlers` for better code organization. See the [Worker API documentation](https://
|
|
124
|
+
You can define handlers outside of the worker creation using `defineHandler` and `defineHandlers` for better code organization. See the [Worker API documentation](https://btravstack.github.io/amqp-contract/api/worker) for details.
|
|
125
125
|
|
|
126
126
|
## Error Handling
|
|
127
127
|
|
|
128
|
-
Worker handlers return `
|
|
128
|
+
Worker handlers return `AsyncResult<void, HandlerError>` for explicit error handling:
|
|
129
129
|
|
|
130
130
|
```typescript
|
|
131
131
|
import { RetryableError, NonRetryableError } from "@amqp-contract/worker";
|
|
132
|
-
import {
|
|
132
|
+
import { err, fromPromise, type AsyncResult } from "unthrown";
|
|
133
133
|
|
|
134
134
|
handlers: {
|
|
135
135
|
processOrder: ({ payload }) => {
|
|
136
136
|
// Validation errors - non-retryable
|
|
137
137
|
if (payload.amount <= 0) {
|
|
138
|
-
return
|
|
138
|
+
return err(new NonRetryableError("Invalid amount")).toAsync();
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
// Transient errors - retryable
|
|
142
|
-
return
|
|
142
|
+
return fromPromise(process(payload), (error) => new RetryableError("Processing failed", error))
|
|
143
143
|
.map(() => undefined);
|
|
144
144
|
},
|
|
145
145
|
}
|
|
@@ -156,11 +156,11 @@ Worker defines error classes:
|
|
|
156
156
|
|
|
157
157
|
## API
|
|
158
158
|
|
|
159
|
-
For complete API documentation, see the [Worker API Reference](https://
|
|
159
|
+
For complete API documentation, see the [Worker API Reference](https://btravstack.github.io/amqp-contract/api/worker).
|
|
160
160
|
|
|
161
161
|
## Documentation
|
|
162
162
|
|
|
163
|
-
📖 **[Read the full documentation →](https://
|
|
163
|
+
📖 **[Read the full documentation →](https://btravstack.github.io/amqp-contract)**
|
|
164
164
|
|
|
165
165
|
## License
|
|
166
166
|
|