mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-15 12:09:40 +08:00
Add typings for cls-hooked (#20154)
* Add typings for cld-hooks * Rename NameSpace to Namespace
This commit is contained in:
committed by
Ryan Cavanaugh
parent
1fcbed38d7
commit
66e7e2e2da
23
types/cls-hooked/cls-hooked-tests.ts
Normal file
23
types/cls-hooked/cls-hooked-tests.ts
Normal 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
25
types/cls-hooked/index.d.ts
vendored
Normal 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;
|
||||
22
types/cls-hooked/tsconfig.json
Normal file
22
types/cls-hooked/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/cls-hooked/tslint.json
Normal file
1
types/cls-hooked/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user