本文整理汇总了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();
});
开发者ID:komapijs,项目名称:komapi,代码行数:27,代码来源:createLogger.ts
示例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();
});
开发者ID:komapijs,项目名称:komapi,代码行数:57,代码来源:setTransactionContext.ts
示例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();
});
}
开发者ID:OysteinAmundsen,项目名称:gymsystems,代码行数:14,代码来源:request-context.middleware.ts
示例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);
}
开发者ID:CNBoland,项目名称:DefinitelyTyped,代码行数:30,代码来源:cls-hooked-tests.ts
示例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');
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:23,代码来源:cls-hooked-tests.ts
注:本文中的cls-hooked.createNamespace函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论