From 66e7e2e2daf1fc4e30b6428ff8d361d0daf1c74a Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Wed, 4 Oct 2017 02:06:35 +0800 Subject: [PATCH] Add typings for cls-hooked (#20154) * Add typings for cld-hooks * Rename NameSpace to Namespace --- types/cls-hooked/cls-hooked-tests.ts | 23 +++++++++++++++++++++++ types/cls-hooked/index.d.ts | 25 +++++++++++++++++++++++++ types/cls-hooked/tsconfig.json | 22 ++++++++++++++++++++++ types/cls-hooked/tslint.json | 1 + 4 files changed, 71 insertions(+) create mode 100644 types/cls-hooked/cls-hooked-tests.ts create mode 100644 types/cls-hooked/index.d.ts create mode 100644 types/cls-hooked/tsconfig.json create mode 100644 types/cls-hooked/tslint.json diff --git a/types/cls-hooked/cls-hooked-tests.ts b/types/cls-hooked/cls-hooked-tests.ts new file mode 100644 index 0000000000..affaa6c54a --- /dev/null +++ b/types/cls-hooked/cls-hooked-tests.ts @@ -0,0 +1,23 @@ +import * as http from 'http'; +import * as cls from 'cls-hooked'; + +const session = cls.createNamespace('my session'); +const user = { id: 'foo' }; +session.set('user', user); +session.run((value: number) => { + session.set('value', value); +}); +http.createServer((req, res) => { + session.bindEmitter(req); + session.bindEmitter(res); +}); +function bindLater(callback: (x: number) => number) { + return session.bind(callback, session.createContext()); +} + +bindLater((x: number) => { + return x; +})(123); // passing argument 'abc' should get compile error + +const session2 = cls.getNamespace('my session'); +session2.get('user'); diff --git a/types/cls-hooked/index.d.ts b/types/cls-hooked/index.d.ts new file mode 100644 index 0000000000..6d0a68aa6e --- /dev/null +++ b/types/cls-hooked/index.d.ts @@ -0,0 +1,25 @@ +// Type definitions for cls-hooked 4.2 +// Project: https://github.com/jeff-lewis/cls-hooked +// Definitions by: Leo Liang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { EventEmitter } from 'events'; + +export interface Namespace { + active: any; + + set(key: string, value: T): T; + get(key: string): any; + run(fn: (...args: any[]) => void): void; + runAndReturn(fn: (...args: any[]) => T): T; + bind(fn: F, context?: any): F; // tslint:disable-line: ban-types + bindEmitter(emitter: EventEmitter): void; + createContext(): any; +} + +export function createNamespace(name: string): Namespace; +export function getNamespace(name: string): Namespace; +export function destroyNamespace(name: string): void; +export function reset(): void; diff --git a/types/cls-hooked/tsconfig.json b/types/cls-hooked/tsconfig.json new file mode 100644 index 0000000000..d5fe6249f9 --- /dev/null +++ b/types/cls-hooked/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cls-hooked-tests.ts" + ] +} diff --git a/types/cls-hooked/tslint.json b/types/cls-hooked/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/cls-hooked/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }