@6thbridge/utils 1.8.2-3 → 1.8.3
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/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/pipelines/index.d.ts +3 -0
- package/lib/pipelines/index.js +20 -0
- package/lib/pipelines/index.js.map +1 -0
- package/lib/pipelines/parallel-pipeline.d.ts +7 -0
- package/lib/pipelines/parallel-pipeline.js +49 -0
- package/lib/pipelines/parallel-pipeline.js.map +1 -0
- package/lib/pipelines/pipeline-step.d.ts +5 -0
- package/lib/pipelines/pipeline-step.js +3 -0
- package/lib/pipelines/pipeline-step.js.map +1 -0
- package/lib/pipelines/pipeline.example.d.ts +2 -0
- package/lib/pipelines/pipeline.example.js +96 -0
- package/lib/pipelines/pipeline.example.js.map +1 -0
- package/lib/pipelines/pipelines.test.d.ts +1 -0
- package/lib/pipelines/pipelines.test.js +204 -0
- package/lib/pipelines/pipelines.test.js.map +1 -0
- package/lib/pipelines/pipelines.typecheck.d.ts +1 -0
- package/lib/pipelines/pipelines.typecheck.js +68 -0
- package/lib/pipelines/pipelines.typecheck.js.map +1 -0
- package/lib/pipelines/serial-pipeline.d.ts +7 -0
- package/lib/pipelines/serial-pipeline.js +39 -0
- package/lib/pipelines/serial-pipeline.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/readme.md +88 -0
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -7,6 +7,7 @@ A comprehensive TypeScript utility library for Node.js applications, providing r
|
|
|
7
7
|
- [Core Modules](#core-modules)
|
|
8
8
|
- [API Reference](#api-reference)
|
|
9
9
|
- [Examples](#examples)
|
|
10
|
+
- [Pipelines](#pipelines)
|
|
10
11
|
- [Testing](#testing)
|
|
11
12
|
- [Contributing](#contributing)
|
|
12
13
|
- [License](#license)
|
|
@@ -290,6 +291,93 @@ LOG_OUT=1
|
|
|
290
291
|
# Service URLs
|
|
291
292
|
USER_SERVICE_URL=http://user-service:3000
|
|
292
293
|
```
|
|
294
|
+
## Pipelines
|
|
295
|
+
|
|
296
|
+
`SerialPipeline` passes each executed step's result to the next step. `ParallelPipeline`
|
|
297
|
+
runs independent steps concurrently with the same initial input and returns named results.
|
|
298
|
+
Both accept the existing `ContextEntity` as the first argument on every invocation; the
|
|
299
|
+
pipeline never stores context.
|
|
300
|
+
|
|
301
|
+
Every step must provide `shouldRun(contextEntity, input): Promise<boolean>` and
|
|
302
|
+
`run(contextEntity, input): Promise<Result>`. Implementers choose the result type.
|
|
303
|
+
Register objects or class instances with `.pipe(name, step)`. Names must be unique.
|
|
304
|
+
Registration returns a new pipeline, so retain the returned instance or chain calls.
|
|
305
|
+
|
|
306
|
+
### Serial transformations and skipped steps
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { ContextEntity, PipelineStep, SerialPipeline } from '@6thbridge/utils';
|
|
310
|
+
|
|
311
|
+
class MeasureText implements PipelineStep<string, number> {
|
|
312
|
+
constructor(private minimumLength: number) {}
|
|
313
|
+
|
|
314
|
+
async shouldRun(contextEntity: ContextEntity, input: string): Promise<boolean> {
|
|
315
|
+
return input.length >= this.minimumLength;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async run(contextEntity: ContextEntity, input: string): Promise<number> {
|
|
319
|
+
return input.length;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const pipeline = new SerialPipeline<string>()
|
|
324
|
+
.pipe('measure', new MeasureText(3))
|
|
325
|
+
.pipe('describe', {
|
|
326
|
+
async shouldRun(contextEntity, input) {
|
|
327
|
+
return true;
|
|
328
|
+
},
|
|
329
|
+
async run(contextEntity, input) {
|
|
330
|
+
// input is string | number: skipped measurement forwards the original string.
|
|
331
|
+
return typeof input === 'number' ? `Length: ${input}` : `Unmeasured: ${input}`;
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const contextEntity = new ContextEntity();
|
|
336
|
+
await pipeline.run(contextEntity, 'hello'); // "Length: 5"
|
|
337
|
+
await pipeline.run(new ContextEntity(), 'hi'); // "Unmeasured: hi"
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
Serial execution awaits each predicate and result in registration order. A skipped step
|
|
341
|
+
forwards its input unchanged, so downstream input and the final result include the types
|
|
342
|
+
of values that may pass through skipped steps. Class instance methods retain their `this`.
|
|
343
|
+
|
|
344
|
+
### Named parallel results
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
import { ContextEntity, ParallelPipeline } from '@6thbridge/utils';
|
|
348
|
+
|
|
349
|
+
const pipeline = new ParallelPipeline<string>()
|
|
350
|
+
.pipe('length', {
|
|
351
|
+
async shouldRun(contextEntity, input) {
|
|
352
|
+
return true;
|
|
353
|
+
},
|
|
354
|
+
async run(contextEntity, input) {
|
|
355
|
+
return input.length;
|
|
356
|
+
},
|
|
357
|
+
})
|
|
358
|
+
.pipe('uppercase', {
|
|
359
|
+
async shouldRun(contextEntity, input) {
|
|
360
|
+
return input.length > 0;
|
|
361
|
+
},
|
|
362
|
+
async run(contextEntity, input) {
|
|
363
|
+
return input.toUpperCase();
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
const results = await pipeline.run(new ContextEntity(), 'hello');
|
|
368
|
+
// { length: 5, uppercase: 'HELLO' }
|
|
369
|
+
// Inferred properties: length?: number; uppercase?: string.
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Parallel predicates and executions run independently. Skipped steps are omitted from the
|
|
373
|
+
result object; executed steps returning `undefined` still have a result property. Result
|
|
374
|
+
keys follow registration order regardless of completion order.
|
|
375
|
+
|
|
376
|
+
Both pipelines reject errors from `shouldRun` or `run`. Serial execution stops before the
|
|
377
|
+
next step; already started parallel work continues. Empty serial pipelines return their
|
|
378
|
+
input, and empty parallel pipelines return `{}`. Pipelines can be reused concurrently
|
|
379
|
+
with different contexts and inputs; callers manage any mutable state inside their steps.
|
|
380
|
+
|
|
293
381
|
## Testing
|
|
294
382
|
The library includes comprehensive test suites using Jest:
|
|
295
383
|
``` bash
|