@flow-conductor/core 1.1.0 → 1.1.1
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 +126 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,8 @@ This package provides the foundational types and classes for building request ad
|
|
|
18
18
|
|
|
19
19
|
- **`RequestAdapter`** - Base abstract class for all request adapters
|
|
20
20
|
- **`RequestChain`** - Main class for chaining requests together
|
|
21
|
-
- **`
|
|
21
|
+
- **`RequestBatch`** - Class for executing multiple requests in parallel
|
|
22
|
+
- **`RequestManager`** - Base class for request management (extended by `RequestChain` and `RequestBatch`)
|
|
22
23
|
|
|
23
24
|
### Types
|
|
24
25
|
|
|
@@ -144,6 +145,128 @@ chain.addAll([
|
|
|
144
145
|
const results = await chain.executeAll();
|
|
145
146
|
```
|
|
146
147
|
|
|
148
|
+
### Using RequestBatch
|
|
149
|
+
|
|
150
|
+
`RequestBatch` executes multiple requests in parallel (or with a concurrency limit). It supports both homogeneous batches (all requests return the same type) and heterogeneous batches (each request can return a different type).
|
|
151
|
+
|
|
152
|
+
#### Homogeneous Batch
|
|
153
|
+
|
|
154
|
+
All requests return the same type:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { batch } from "@flow-conductor/core";
|
|
158
|
+
import { MyAdapter } from "./MyAdapter";
|
|
159
|
+
|
|
160
|
+
const adapter = new MyAdapter();
|
|
161
|
+
|
|
162
|
+
// Execute multiple requests in parallel
|
|
163
|
+
const batchInstance = batch(
|
|
164
|
+
[
|
|
165
|
+
{ config: { url: "https://api.example.com/users/1", method: "GET" } },
|
|
166
|
+
{ config: { url: "https://api.example.com/users/2", method: "GET" } },
|
|
167
|
+
{ config: { url: "https://api.example.com/users/3", method: "GET" } }
|
|
168
|
+
],
|
|
169
|
+
adapter
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const results = await batchInstance.execute(); // Returns User[]
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### Heterogeneous Batch
|
|
176
|
+
|
|
177
|
+
Each request can return a different type (using tuple types for type safety):
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { batch } from "@flow-conductor/core";
|
|
181
|
+
import { MyAdapter } from "./MyAdapter";
|
|
182
|
+
|
|
183
|
+
const adapter = new MyAdapter();
|
|
184
|
+
|
|
185
|
+
// Each request returns a different type
|
|
186
|
+
const batchInstance = batch(
|
|
187
|
+
[
|
|
188
|
+
{
|
|
189
|
+
config: { url: "https://api.example.com/users/1", method: "GET" },
|
|
190
|
+
mapper: (r) => r.json() as Promise<User>
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
config: { url: "https://api.example.com/products/1", method: "GET" },
|
|
194
|
+
mapper: (r) => r.json() as Promise<Product>
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
config: { url: "https://api.example.com/orders/1", method: "GET" },
|
|
198
|
+
mapper: (r) => r.json() as Promise<Order>
|
|
199
|
+
}
|
|
200
|
+
],
|
|
201
|
+
adapter
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const results = await batchInstance.execute(); // Returns [User, Product, Order]
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### Concurrency Control
|
|
208
|
+
|
|
209
|
+
Limit the number of concurrent requests:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { batch } from "@flow-conductor/core";
|
|
213
|
+
|
|
214
|
+
const batchInstance = batch([...requests], adapter)
|
|
215
|
+
.withConcurrency(5); // Execute max 5 requests at a time
|
|
216
|
+
|
|
217
|
+
const results = await batchInstance.execute();
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
#### Using Handlers
|
|
221
|
+
|
|
222
|
+
`RequestBatch` supports the same handlers as `RequestManager`:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import { batch } from "@flow-conductor/core";
|
|
226
|
+
|
|
227
|
+
const batchInstance = batch([...requests], adapter)
|
|
228
|
+
.withResultHandler((results) => {
|
|
229
|
+
console.log("Batch completed:", results);
|
|
230
|
+
})
|
|
231
|
+
.withErrorHandler((error) => {
|
|
232
|
+
console.error("Batch error:", error);
|
|
233
|
+
})
|
|
234
|
+
.withFinishHandler(() => {
|
|
235
|
+
console.log("Batch finished");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const results = await batchInstance.execute();
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
#### Nested in RequestChain
|
|
242
|
+
|
|
243
|
+
`RequestBatch` can be nested within a `RequestChain`:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { begin, batch } from "@flow-conductor/core";
|
|
247
|
+
|
|
248
|
+
const adapter = new MyAdapter();
|
|
249
|
+
|
|
250
|
+
const result = await begin(
|
|
251
|
+
{ config: { url: "https://api.example.com/users", method: "GET" } },
|
|
252
|
+
adapter
|
|
253
|
+
)
|
|
254
|
+
.next({
|
|
255
|
+
request: batch(
|
|
256
|
+
[
|
|
257
|
+
{ config: { url: "https://api.example.com/posts", method: "GET" } },
|
|
258
|
+
{ config: { url: "https://api.example.com/comments", method: "GET" } }
|
|
259
|
+
],
|
|
260
|
+
adapter
|
|
261
|
+
),
|
|
262
|
+
mapper: (batchResults) => {
|
|
263
|
+
// Process the batch results
|
|
264
|
+
return batchResults;
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
.execute();
|
|
268
|
+
```
|
|
269
|
+
|
|
147
270
|
## Type Definitions
|
|
148
271
|
|
|
149
272
|
### IRequestConfig
|
|
@@ -187,7 +310,9 @@ abstract class RequestAdapter<ExecutionResult, RequestConfig extends IRequestCon
|
|
|
187
310
|
- `RequestAdapter` - Base adapter class
|
|
188
311
|
- `RequestManager` - Base manager class
|
|
189
312
|
- `RequestChain` - Main chain class
|
|
313
|
+
- `RequestBatch` - Batch request class for parallel execution
|
|
190
314
|
- `begin` - Function alternative to `RequestChain.begin`
|
|
315
|
+
- `batch` - Function alternative to `RequestBatch.batch`
|
|
191
316
|
|
|
192
317
|
### Type Exports
|
|
193
318
|
|