diff --git a/types/mali-compose/index.d.ts b/types/mali-compose/index.d.ts new file mode 100644 index 0000000000..87b23087d3 --- /dev/null +++ b/types/mali-compose/index.d.ts @@ -0,0 +1,15 @@ +// Type definitions for mali 1.1 +// Project: https://github.com/malijs/mali-compose +// Definitions by: Daniel Byrne +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import { Context } from "mali"; + +declare function compose( + middleware: ReadonlyArray< + (ctx: Context, next: () => Promise) => Promise + > +): (context: Context, next: () => Promise) => Promise; + +export = compose; diff --git a/types/mali-compose/mali-compose-tests.ts b/types/mali-compose/mali-compose-tests.ts new file mode 100644 index 0000000000..190ab46458 --- /dev/null +++ b/types/mali-compose/mali-compose-tests.ts @@ -0,0 +1,39 @@ +import compose = require('mali-compose'); +import { Context } from 'mali'; + +type middleware = (context: Context, next: () => Promise) => Promise; + +const arr: number[] = []; +const stack: middleware[] = []; + +function _sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +stack.push(async (ctx: Context, next: () => Promise) => { + arr.push(1); + await _sleep(1); + await next(); + await _sleep(1); + arr.push(6); + }); + +stack.push(async (ctx: Context, next: () => Promise) => { + arr.push(2); + await _sleep(1); + await next(); + await _sleep(1); + arr.push(5); +}); + +stack.push(async (ctx: Context, next: () => Promise) => { + arr.push(3); + await _sleep(1); + await next(); + await _sleep(1); + arr.push(4); +}); + +compose(stack); +compose([]); +compose([compose(stack)]); diff --git a/types/mali-compose/tsconfig.json b/types/mali-compose/tsconfig.json new file mode 100644 index 0000000000..e0afe50e85 --- /dev/null +++ b/types/mali-compose/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mali-compose-tests.ts" + ] +} diff --git a/types/mali-compose/tslint.json b/types/mali-compose/tslint.json new file mode 100644 index 0000000000..d88586e5bd --- /dev/null +++ b/types/mali-compose/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}