@cardboardrobots/pipeline
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -6,3 +6,3 @@ import { Directive } from './directive'; | ||
*/ | ||
export declare class Pipeline<CONTEXT = Record<string, unknown>, VALUE = undefined, RESULT = void> { | ||
export declare class Pipeline<CONTEXT = {}, VALUE = void, NEXTCONTEXT = CONTEXT, RESULT = VALUE> { | ||
middlewares: Middleware<any, any, any>[]; | ||
@@ -14,4 +14,4 @@ run(context: CONTEXT, value: VALUE): Promise<Directive<RESULT>>; | ||
*/ | ||
use<NEWCONTEXT, NEWRESULT = RESULT>(middleware: Middleware<CONTEXT & NEWCONTEXT, RESULT, NEWRESULT>): Pipeline<CONTEXT & NEWCONTEXT, VALUE, NEWRESULT>; | ||
use<MIDDLEWARE extends Middleware<any, any, any>>(middleware: MIDDLEWARE): Pipeline<CONTEXT & MiddlewareContext<MIDDLEWARE>, VALUE, MiddlewareReturn<MIDDLEWARE>>; | ||
use<MIDDLEWARECONTEXT, MIDDLEWARERETURN = RESULT>(middleware: Middleware<NEXTCONTEXT & Partial<MIDDLEWARECONTEXT>, RESULT, MIDDLEWARERETURN>): Pipeline<CONTEXT, VALUE, NEXTCONTEXT & MIDDLEWARECONTEXT, MIDDLEWARERETURN>; | ||
use<MIDDLEWARE extends Middleware<any, any, any>>(middleware: MIDDLEWARE): Pipeline<CONTEXT, VALUE, NEXTCONTEXT & MiddlewareContext<MIDDLEWARE>, MiddlewareReturn<MIDDLEWARE>>; | ||
/** | ||
@@ -18,0 +18,0 @@ * Removes a `Middleware` function from the `Pipeline` |
@@ -8,2 +8,3 @@ "use strict"; | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
class Pipeline { | ||
@@ -10,0 +11,0 @@ constructor() { |
@@ -45,2 +45,47 @@ "use strict"; | ||
}); | ||
it('should support default types', function () { | ||
const pipeline = new Pipeline_1.Pipeline().use(async () => ''); | ||
pipeline.run({}); | ||
}); | ||
it('should support chaining', async function () { | ||
const pipeline = new Pipeline_1.Pipeline() | ||
.use(async (_, value) => `${value}b`) | ||
.use(async (_, value) => `${value}c`); | ||
const { value } = await pipeline.run({}, 'a'); | ||
expect(value).toBe('abc'); | ||
}); | ||
it('should support chaining with return changes', async function () { | ||
const pipeline = new Pipeline_1.Pipeline().use(async (_, value) => parseFloat(value)); | ||
const { value } = await pipeline.run({}, '1'); | ||
expect(value).toBe(1); | ||
}); | ||
it('should support context expansion', async function () { | ||
const pipeline = new Pipeline_1.Pipeline() | ||
.use(async (context) => { | ||
context.data = 'test'; | ||
}) | ||
.use(async (context) => { | ||
context.value = 1; | ||
}) | ||
.use(async ({ data, value }) => { | ||
return data + value; | ||
}); | ||
const { value } = await pipeline.run({}); | ||
expect(value).toBe('test1'); | ||
}); | ||
it('should support middleware merging', async function () { | ||
const pipeline = new Pipeline_1.Pipeline() | ||
.use(async (context) => { | ||
context.data = 'test'; | ||
return ''; | ||
}) | ||
.use(async (context) => { | ||
context.value = 1; | ||
}) | ||
.use(async ({ data, value }) => { | ||
return data + value; | ||
}); | ||
const { value } = await pipeline.run({}); | ||
expect(value).toBe('test1'); | ||
}); | ||
}); | ||
@@ -47,0 +92,0 @@ describe('remove', function () { |
{ | ||
"name": "@cardboardrobots/pipeline", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Pipeline middleware runner", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -29,3 +29,3 @@ import { exit, ExitDirective } from './directive'; | ||
it('should throw exceptions', async function () { | ||
const pipeline = new Pipeline<any, string, string>() | ||
const pipeline = new Pipeline<any, string>() | ||
.use(async () => { | ||
@@ -50,2 +50,52 @@ // eslint-disable-next-line no-throw-literal | ||
}); | ||
it('should support default types', function () { | ||
const pipeline = new Pipeline().use(async () => ''); | ||
pipeline.run({}); | ||
}); | ||
it('should support chaining', async function () { | ||
const pipeline = new Pipeline<unknown, string>() | ||
.use(async (_, value) => `${value}b`) | ||
.use(async (_, value) => `${value}c`); | ||
const { value } = await pipeline.run({}, 'a'); | ||
expect(value).toBe('abc'); | ||
}); | ||
it('should support chaining with return changes', async function () { | ||
const pipeline = new Pipeline<unknown, string>().use(async (_, value) => parseFloat(value)); | ||
const { value } = await pipeline.run({}, '1'); | ||
expect(value).toBe(1); | ||
}); | ||
it('should support context expansion', async function () { | ||
const pipeline = new Pipeline() | ||
.use<{ data: string }>(async (context) => { | ||
context.data = 'test'; | ||
}) | ||
.use<{ value: number }>(async (context) => { | ||
context.value = 1; | ||
}) | ||
.use(async ({ data, value }) => { | ||
return data + value; | ||
}); | ||
const { value } = await pipeline.run({}); | ||
expect(value).toBe('test1'); | ||
}); | ||
it('should support middleware merging', async function () { | ||
const pipeline = new Pipeline() | ||
.use(async (context: { data: string }) => { | ||
context.data = 'test'; | ||
return ''; | ||
}) | ||
.use(async (context: { value: number }) => { | ||
context.value = 1; | ||
}) | ||
.use(async ({ data, value }) => { | ||
return data + value; | ||
}); | ||
const { value } = await pipeline.run({}); | ||
expect(value).toBe('test1'); | ||
}); | ||
}); | ||
@@ -52,0 +102,0 @@ |
@@ -7,3 +7,4 @@ import { Directive, end } from './directive'; | ||
*/ | ||
export class Pipeline<CONTEXT = Record<string, unknown>, VALUE = undefined, RESULT = void> { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
export class Pipeline<CONTEXT = {}, VALUE = void, NEXTCONTEXT = CONTEXT, RESULT = VALUE> { | ||
middlewares: Middleware<any, any, any>[] = []; | ||
@@ -29,9 +30,9 @@ | ||
*/ | ||
use<NEWCONTEXT, NEWRESULT = RESULT>( | ||
middleware: Middleware<CONTEXT & NEWCONTEXT, RESULT, NEWRESULT> | ||
): Pipeline<CONTEXT & NEWCONTEXT, VALUE, NEWRESULT>; | ||
use<MIDDLEWARECONTEXT, MIDDLEWARERETURN = RESULT>( | ||
middleware: Middleware<NEXTCONTEXT & Partial<MIDDLEWARECONTEXT>, RESULT, MIDDLEWARERETURN> | ||
): Pipeline<CONTEXT, VALUE, NEXTCONTEXT & MIDDLEWARECONTEXT, MIDDLEWARERETURN>; | ||
use<MIDDLEWARE extends Middleware<any, any, any>>( | ||
middleware: MIDDLEWARE | ||
): Pipeline<CONTEXT & MiddlewareContext<MIDDLEWARE>, VALUE, MiddlewareReturn<MIDDLEWARE>>; | ||
): Pipeline<CONTEXT, VALUE, NEXTCONTEXT & MiddlewareContext<MIDDLEWARE>, MiddlewareReturn<MIDDLEWARE>>; | ||
@@ -38,0 +39,0 @@ use(middleware: any): any { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35153
576