Add typings for cls-hooked (#20154)

* Add typings for cld-hooks

* Rename NameSpace to Namespace
This commit is contained in:
Leo Liang
2017-10-04 02:06:35 +08:00
committed by Ryan Cavanaugh
parent 1fcbed38d7
commit 66e7e2e2da
4 changed files with 71 additions and 0 deletions

View File

@@ -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');

25
types/cls-hooked/index.d.ts vendored Normal file
View File

@@ -0,0 +1,25 @@
// Type definitions for cls-hooked 4.2
// Project: https://github.com/jeff-lewis/cls-hooked
// Definitions by: Leo Liang <https://github.com/aleung>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { EventEmitter } from 'events';
export interface Namespace {
active: any;
set<T>(key: string, value: T): T;
get(key: string): any;
run(fn: (...args: any[]) => void): void;
runAndReturn<T>(fn: (...args: any[]) => T): T;
bind<F extends Function>(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;

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }