本文整理汇总了TypeScript中cls-hooked.createNamespace函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createNamespace函数的具体用法?TypeScript createNamespace怎么用?TypeScript createNamespace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createNamespace函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should support child loggers with all context variables from parent', async done => {
expect.assertions(5);
const ns = cls.createNamespace('my-logging-namespace3');
const spy = jest.fn();
const logger = createLogger(ns, {}, new DummyStream(spy));
// Run in context
ns.run(() => {
ns.set('key1', 'value1');
ns.set('key2', 'value2');
const childLogger = logger.child({ isChild: true });
ns.set('key3', 'value3');
childLogger.info('my logtext');
// Assertions
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(expect.stringContaining('"level":30'));
expect(spy).toHaveBeenCalledWith(expect.stringContaining('"msg":"my logtext"'));
expect(spy).toHaveBeenCalledWith(
expect.stringContaining('"context":{"key1":"value1","key2":"value2","key3":"value3"}'),
);
expect(spy).toHaveBeenCalledWith(expect.stringContaining('"isChild":true'));
});
// Done
done();
});
示例2: it
it('should set requestId automatically and capture custom properties', async done => {
expect.assertions(3);
const app = new Koa();
const ns = cls.createNamespace('my-namespace');
// Add middlewares
app.use((ctx, next) => {
// Add mock data
ctx.request.requestId = ctx.request.headers['x-request-id'];
return next();
});
app.use(setTransactionContext(ns));
app.use((ctx, next) => {
ns.set('delay', ctx.request.headers['x-delay']);
return next();
});
app.use(async ctx => {
await new Promise(resolve => setTimeout(resolve, parseInt(ctx.request.headers['x-delay'], 10)));
ctx.body = {
requestId: ns.get('requestId'),
delay: ns.get('delay'),
};
});
const server = request(app.callback());
const responses = await Promise.all([
server
.get('/')
.set('x-request-id', '1')
.set('x-delay', '100'),
server
.get('/')
.set('x-request-id', '2')
.set('x-delay', '1'),
server
.get('/')
.set('x-request-id', '3')
.set('x-delay', '50'),
]);
// Assertions
expect(responses[0].body).toEqual({
requestId: '1',
delay: '100',
});
expect(responses[1].body).toEqual({
requestId: '2',
delay: '1',
});
expect(responses[2].body).toEqual({
requestId: '3',
delay: '50',
});
// Done
done();
});
示例3: use
use(req: IncomingMessage, res: Response, next: Function) {
if (!req['user'] && req.headers.authorization) {
const authToken = req.headers.authorization.substr('Bearer '.length);
req['user'] = this.jwtService.decode(authToken);
}
const requestContext = new RequestContext(req, res);
const namespace = getNamespace(RequestContext.nsid) || createNamespace(RequestContext.nsid);
namespace.run(() => {
namespace.set('RequestContext', requestContext);
next();
});
}
示例4: bindLater
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');
const appNamespace = cls.createNamespace('applicationNameSpace');
const context = appNamespace.createContext();
function bindWithMiddleware(middlewareFn: () => void) {
return session.bind(middlewareFn, context);
}
示例5: bindLater
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');