diff --git a/src/base.ts b/src/base.ts index f99e13f..d6139fb 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,8 +1,5 @@ import type { ExecutionContainer } from '@artus/injection'; -import { - BaseContext, BaseInput, BaseOutput, - ContextStorage, ParamsDictionary, Next -} from './types'; +import { BaseContext, BaseInput, BaseOutput, ContextStorage, ParamsDictionary, Next } from './types'; const ContextStorageSymbol = Symbol('ARTUS::ContextStorage'); @@ -14,7 +11,7 @@ export class Output implements BaseOutput { public data: ParamsDictionary = new Map(); } -export class Storage implements ContextStorage{ +export class Storage implements ContextStorage { private storageMap = new Map(); get(key?: string | symbol): any { @@ -29,8 +26,8 @@ export class Storage implements ContextStorage{ } export class Context implements BaseContext { - public input: BaseInput = new Input(); - public output: BaseOutput = new Output(); + public input: Input = new Input(); + public output: Output = new Output(); private _container!: ExecutionContainer; private storageMap = new Map>(); @@ -66,7 +63,7 @@ export class Context implements BaseContext { } } -export type Middleware = (context: Context, next: Next) => void; +export type Middleware = (context: T, next: Next) => void; export type Middlewares = Middleware[]; export type PipelineLike = { middlewares: Middlewares }; export type MiddlewareInput = Middleware | Middlewares | PipelineLike; diff --git a/src/pipeline.ts b/src/pipeline.ts index 4061b07..a6b95ad 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,5 +1,5 @@ - -import { Context, Middlewares, MiddlewareInput, PipelineLike } from "./base"; +import { Middlewares, MiddlewareInput, PipelineLike } from './base'; +import { BaseContext } from './types'; export class Pipeline implements PipelineLike { middlewares: Middlewares = []; @@ -26,7 +26,7 @@ export class Pipeline implements PipelineLike { throw new Error(`${middleware} isn't type MiddlewareInput`); } - dispatch(i: number, ctx: Context, execution = { index: -1 }): Promise { + dispatch(i: number, ctx: BaseContext, execution = { index: -1 }): Promise { if (i <= execution.index) return Promise.reject(new Error('next() called multiple times')); execution.index = i; let fn = this.middlewares[i]; @@ -38,7 +38,7 @@ export class Pipeline implements PipelineLike { } } - run(ctx: Context): Promise { + run(ctx: BaseContext): Promise { return this.dispatch(0, ctx); } } diff --git a/src/types.ts b/src/types.ts index f6e4fa9..ba6c91d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,22 +3,22 @@ export interface ParamsDictionary { } export interface BaseInput

{ - params: P -}; + params?: P; +} export interface BaseOutput

{ - data: P -}; + data?: P; +} export interface BaseContext extends ParamsDictionary { - input: BaseInput, - output: BaseOutput, - namespace: (namespace: string) => ContextStorage, - restore?: () => void, -}; + input: BaseInput; + output: BaseOutput; + namespace?: (namespace: string) => ContextStorage; + restore?: () => void; +} export type ContextStorage = { - set: (value: T, key?: string | symbol) => void, - get: (key?: string | symbol) => T + set: (value: T, key?: string | symbol) => void; + get: (key?: string | symbol) => T; }; export type Next = () => Promise; diff --git a/test/context_pool.test.ts b/test/context_pool.test.ts index 05c4a1a..cc9efef 100644 --- a/test/context_pool.test.ts +++ b/test/context_pool.test.ts @@ -1,26 +1,26 @@ import { ContextPool } from '../src/context_pool'; -describe("test/context_pool.test.ts", () => { - it('should get ok', () => { - const ctxPool = new ContextPool(1); - const container = new Map(); - const ctx = ctxPool.get(container as any); - expect(ctx.container).toBe(container); - expect(ctx.input.params).toBeInstanceOf(Map); - expect(ctx.output.data).toBeInstanceOf(Map); - }); +describe('test/context_pool.test.ts', () => { + it('should get ok', () => { + const ctxPool = new ContextPool(1); + const container = new Map(); + const ctx = ctxPool.get(container as any); + expect(ctx.container).toBe(container); + expect(ctx.input.params).toBeInstanceOf(Map); + expect(ctx.output.data).toBeInstanceOf(Map); + }); - it('should release ok', () => { - const ctxPool = new ContextPool(1); - const container = new Map(); - const ctx = ctxPool.get(container as any); - ctx.input.params.set('id', 12); - ctx.output.data.set('user', { id: 12 }); - ctxPool.release(ctx); - const container2 = new Map(); - const ctx2 = ctxPool.get(container2 as any); - expect(ctx2.container).toBe(container2); - expect(ctx.input.params.has('id')).toBe(false); - expect(ctx.output.data.has('user')).toBe(false); - }); -}); \ No newline at end of file + it('should release ok', () => { + const ctxPool = new ContextPool(1); + const container = new Map(); + const ctx = ctxPool.get(container as any); + ctx.input.params!.set('id', 12); + ctx.output.data!.set('user', { id: 12 }); + ctxPool.release(ctx); + const container2 = new Map(); + const ctx2 = ctxPool.get(container2 as any); + expect(ctx2.container).toBe(container2); + expect(ctx.input.params!.has('id')).toBe(false); + expect(ctx.output.data!.has('user')).toBe(false); + }); +});